2018-09-14 18:04:56 -06:00
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mgr
import (
"testing"
2018-12-03 15:08:29 -07:00
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
2018-09-14 18:04:56 -06:00
"github.com/rook/rook/pkg/clusterd"
2020-07-01 22:46:39 -06:00
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
2019-03-08 13:15:43 -07:00
"github.com/rook/rook/pkg/operator/ceph/config"
2021-02-18 13:10:11 +00:00
"github.com/rook/rook/pkg/operator/ceph/test"
2018-09-14 18:04:56 -06:00
optest "github.com/rook/rook/pkg/operator/test"
2019-03-08 13:15:43 -07:00
"github.com/stretchr/testify/assert"
2019-03-05 12:13:44 +01:00
v1 "k8s.io/api/core/v1"
2018-09-14 18:04:56 -06:00
"k8s.io/apimachinery/pkg/api/resource"
)
func TestPodSpec ( t * testing . T ) {
2020-01-07 18:51:19 +05:30
clientset := optest . New ( t , 1 )
2021-02-18 13:10:11 +00:00
ownerInfo := cephclient . NewMinimumOwnerInfoWithOwnerRef ()
clusterInfo := & cephclient . ClusterInfo { Namespace : "ns" , FSID : "myfsid" , OwnerInfo : ownerInfo }
2020-09-16 18:37:30 +05:30
clusterInfo . SetName ( "test" )
2020-07-10 16:40:16 -06:00
clusterSpec := cephv1 . ClusterSpec {
2021-07-22 10:33:30 +02:00
CephVersion : cephv1 . CephVersionSpec { Image : "quay.io/ceph/ceph:myceph" },
2020-07-10 16:40:16 -06:00
Dashboard : cephv1 . DashboardSpec { Port : 1234 },
2021-09-22 16:39:09 -06:00
PriorityClassNames : map [ cephv1 . KeyType ] string { cephv1 . KeyMgr : "my-priority-class" },
2020-07-10 16:40:16 -06:00
DataDirHostPath : "/var/lib/rook/" ,
2021-05-14 18:03:38 -06:00
Resources : cephv1 . ResourceSpec { string ( cephv1 . KeyMgr ): v1 . ResourceRequirements {
2018-09-14 18:04:56 -06:00
Limits : v1 . ResourceList {
2019-03-05 15:51:20 +01:00
v1 . ResourceCPU : * resource . NewQuantity ( 200.0 , resource . BinarySI ),
v1 . ResourceMemory : * resource . NewQuantity ( 500.0 , resource . BinarySI ),
2018-09-14 18:04:56 -06:00
},
Requests : v1 . ResourceList {
2019-03-05 12:13:44 +01:00
v1 . ResourceCPU : * resource . NewQuantity ( 100.0 , resource . BinarySI ),
2019-03-05 15:51:20 +01:00
v1 . ResourceMemory : * resource . NewQuantity ( 250.0 , resource . BinarySI ),
2018-09-14 18:04:56 -06:00
},
},
2020-07-10 16:40:16 -06:00
},
}
c := New ( & clusterd . Context { Clientset : clientset }, clusterInfo , clusterSpec , "rook/rook:myversion" )
2018-09-14 18:04:56 -06:00
mgrTestConfig := mgrConfig {
2019-10-18 17:26:09 -06:00
DaemonID : "a" ,
ResourceName : "rook-ceph-mgr-a" ,
DataPathMap : config . NewStatelessDaemonDataPathMap ( config . MgrType , "a" , "rook-ceph" , "/var/lib/rook/" ),
2018-09-14 18:04:56 -06:00
}
2021-07-06 23:27:15 +02:00
t . Run ( "traditional deployment" , func ( t * testing . T ) {
d , err := c . makeDeployment ( & mgrTestConfig )
assert . NoError ( t , err )
2018-09-14 18:04:56 -06:00
2021-07-06 23:27:15 +02:00
// Deployment should have Ceph labels
test . AssertLabelsContainCephRequirements ( t , d . ObjectMeta . Labels ,
2021-12-06 12:52:11 +05:30
config . MgrType , "a" , AppName , "ns" , "test" , "cephclusters.ceph.rook.io" , "ceph-mgr" )
2018-09-14 18:04:56 -06:00
2021-07-06 23:27:15 +02:00
podTemplate := test . NewPodTemplateSpecTester ( t , & d . Spec . Template )
podTemplate . Spec (). Containers (). RequireAdditionalEnvVars (
2023-11-22 17:49:31 +01:00
"CEPH_ARGS" ,
2021-07-06 23:27:15 +02:00
"ROOK_OPERATOR_NAMESPACE" , "ROOK_CEPH_CLUSTER_CRD_VERSION" ,
"ROOK_CEPH_CLUSTER_CRD_NAME" )
2021-07-22 10:33:30 +02:00
podTemplate . RunFullSuite ( config . MgrType , "a" , AppName , "ns" , "quay.io/ceph/ceph:myceph" ,
2021-07-06 23:27:15 +02:00
"200" , "100" , "500" , "250" , /* resources */
2021-12-06 12:52:11 +05:30
"my-priority-class" , "test" , "cephclusters.ceph.rook.io" , "ceph-mgr" )
2021-07-06 23:27:15 +02:00
assert . Equal ( t , 2 , len ( d . Spec . Template . Annotations ))
assert . Equal ( t , 1 , len ( d . Spec . Template . Spec . Containers ))
2022-11-16 17:57:23 +05:30
assert . Equal ( t , 6 , len ( d . Spec . Template . Spec . Containers [ 0 ]. VolumeMounts ))
2021-07-06 23:27:15 +02:00
})
t . Run ( "deployment with multus with new sidecar proxy command container" , func ( t * testing . T ) {
c . spec . Network . Provider = "multus"
d , err := c . makeDeployment ( & mgrTestConfig )
assert . NoError ( t , err )
assert . Equal ( t , 3 , len ( d . Spec . Template . Annotations )) // Multus annotations
assert . Equal ( t , 2 , len ( d . Spec . Template . Spec . Containers )) // mgr pod + sidecar
2022-04-24 17:29:47 +02:00
assert . Equal ( t , cephclient . CommandProxyInitContainerName , d . Spec . Template . Spec . Containers [ 1 ]. Name ) // sidecar pod
2022-11-16 17:57:23 +05:30
assert . Equal ( t , 7 , len ( d . Spec . Template . Spec . Containers [ 1 ]. VolumeMounts )) // + admin keyring
2021-07-06 23:27:15 +02:00
assert . Equal ( t , "CEPH_ARGS" , d . Spec . Template . Spec . Containers [ 1 ]. Env [ len ( d . Spec . Template . Spec . Containers [ 1 ]. Env ) - 1 ]. Name ) // connection info to the cluster
assert . Equal ( t , "-m $(ROOK_CEPH_MON_HOST) -k /etc/ceph/admin-keyring-store/keyring" , d . Spec . Template . Spec . Containers [ 1 ]. Env [ len ( d . Spec . Template . Spec . Containers [ 1 ]. Env ) - 1 ]. Value ) // connection info to the cluster
})
2022-02-03 20:26:33 +05:30
t . Run (( "check mgr ConfigureProbe" ), func ( t * testing . T ) {
c . spec . HealthCheck . StartupProbe = make ( map [ cephv1 . KeyType ] * cephv1 . ProbeSpec )
c . spec . HealthCheck . StartupProbe [ cephv1 . KeyMgr ] = & cephv1 . ProbeSpec { Disabled : false , Probe : & v1 . Probe { InitialDelaySeconds : 1000 }}
c . spec . HealthCheck . LivenessProbe = make ( map [ cephv1 . KeyType ] * cephv1 . ProbeSpec )
c . spec . HealthCheck . LivenessProbe [ cephv1 . KeyMgr ] = & cephv1 . ProbeSpec { Disabled : false , Probe : & v1 . Probe { InitialDelaySeconds : 900 }}
container := c . makeMgrDaemonContainer ( & mgrTestConfig )
assert . NotNil ( t , container . LivenessProbe )
assert . NotNil ( t , container . StartupProbe )
assert . Equal ( t , int32 ( 900 ), container . LivenessProbe . InitialDelaySeconds )
assert . Equal ( t , int32 ( 1000 ), container . StartupProbe . InitialDelaySeconds )
})
2018-09-14 18:04:56 -06:00
}
func TestServiceSpec ( t * testing . T ) {
2020-01-07 18:51:19 +05:30
clientset := optest . New ( t , 1 )
2021-02-18 13:10:11 +00:00
ownerInfo := cephclient . NewMinimumOwnerInfoWithOwnerRef ()
clusterInfo := & cephclient . ClusterInfo { Namespace : "ns" , FSID : "myfsid" , OwnerInfo : ownerInfo }
2020-07-10 16:40:16 -06:00
clusterSpec := cephv1 . ClusterSpec {}
c := New ( & clusterd . Context { Clientset : clientset }, clusterInfo , clusterSpec , "myversion" )
2018-09-14 18:04:56 -06:00
2023-03-03 18:17:46 +01:00
s , err := c . MakeMetricsService ( "rook-mgr" , serviceMetricName )
2021-02-18 13:10:11 +00:00
assert . NoError ( t , err )
2018-09-14 18:04:56 -06:00
assert . NotNil ( t , s )
assert . Equal ( t , "rook-mgr" , s . Name )
assert . Equal ( t , 1 , len ( s . Spec . Ports ))
2023-03-03 18:17:46 +01:00
assert . Equal ( t , 2 , len ( s . Labels ))
// Check service selector
2023-03-03 10:39:51 +01:00
assert . Equal ( t , 3 , len ( s . Spec . Selector ))
2023-03-03 18:17:46 +01:00
assert . Equal ( t , s . Spec . Selector [ "app" ], "rook-ceph-mgr" )
assert . Equal ( t , s . Spec . Selector [ "mgr_role" ], "active" )
assert . Equal ( t , s . Spec . Selector [ "rook_cluster" ], "ns" )
2018-09-14 18:04:56 -06:00
}
func TestHostNetwork ( t * testing . T ) {
2020-01-07 18:51:19 +05:30
clientset := optest . New ( t , 1 )
2021-02-18 13:10:11 +00:00
ownerInfo := cephclient . NewMinimumOwnerInfoWithOwnerRef ()
clusterInfo := & cephclient . ClusterInfo { Namespace : "ns" , FSID : "myfsid" , OwnerInfo : ownerInfo }
2020-09-16 18:37:30 +05:30
clusterInfo . SetName ( "test" )
2020-07-10 16:40:16 -06:00
clusterSpec := cephv1 . ClusterSpec {
Network : cephv1 . NetworkSpec { HostNetwork : true },
Dashboard : cephv1 . DashboardSpec { Port : 1234 },
DataDirHostPath : "/var/lib/rook/" ,
}
c := New ( & clusterd . Context { Clientset : clientset }, clusterInfo , clusterSpec , "myversion" )
2018-09-14 18:04:56 -06:00
mgrTestConfig := mgrConfig {
2019-10-18 17:26:09 -06:00
DaemonID : "a" ,
ResourceName : "mgr-a" ,
DataPathMap : config . NewStatelessDaemonDataPathMap ( config . MgrType , "a" , "rook-ceph" , "/var/lib/rook/" ),
2018-09-14 18:04:56 -06:00
}
2020-07-24 00:26:37 +05:30
d , err := c . makeDeployment ( & mgrTestConfig )
assert . NoError ( t , err )
2018-09-14 18:04:56 -06:00
assert . NotNil ( t , d )
2020-07-10 16:40:16 -06:00
assert . Equal ( t , true , c . spec . Network . IsHost ())
2018-09-14 18:04:56 -06:00
assert . Equal ( t , v1 . DNSClusterFirstWithHostNet , d . Spec . Template . Spec . DNSPolicy )
}
2019-04-30 10:57:37 -07:00
2019-10-10 17:25:57 +02:00
func TestApplyPrometheusAnnotations ( t * testing . T ) {
2020-01-07 18:51:19 +05:30
clientset := optest . New ( t , 1 )
2020-07-10 16:40:16 -06:00
clusterSpec := cephv1 . ClusterSpec {
DataDirHostPath : "/var/lib/rook/" ,
}
2021-02-18 13:10:11 +00:00
ownerInfo := cephclient . NewMinimumOwnerInfoWithOwnerRef ()
clusterInfo := & cephclient . ClusterInfo { Namespace : "ns" , FSID : "myfsid" , OwnerInfo : ownerInfo }
2020-09-16 18:37:30 +05:30
clusterInfo . SetName ( "test" )
2020-07-10 16:40:16 -06:00
c := New ( & clusterd . Context { Clientset : clientset }, clusterInfo , clusterSpec , "myversion" )
2019-10-10 17:25:57 +02:00
mgrTestConfig := mgrConfig {
2019-10-18 17:26:09 -06:00
DaemonID : "a" ,
ResourceName : "rook-ceph-mgr-a" ,
DataPathMap : config . NewStatelessDaemonDataPathMap ( config . MgrType , "a" , "rook-ceph" , "/var/lib/rook/" ),
2019-10-10 17:25:57 +02:00
}
2020-07-24 00:26:37 +05:30
d , err := c . makeDeployment ( & mgrTestConfig )
assert . NoError ( t , err )
2019-10-10 17:25:57 +02:00
// Test without annotations
c . applyPrometheusAnnotations ( & d . ObjectMeta )
assert . Equal ( t , 2 , len ( d . ObjectMeta . Annotations ))
// Test with existing annotations
// applyPrometheusAnnotations() shouldn't do anything
// re-initialize "d"
2020-07-24 00:26:37 +05:30
d , err = c . makeDeployment ( & mgrTestConfig )
assert . NoError ( t , err )
2019-10-10 17:25:57 +02:00
2021-09-22 16:39:09 -06:00
fakeAnnotations := cephv1 . Annotations {
2019-10-10 17:25:57 +02:00
"foo.io/bar" : "foobar" ,
}
2021-09-22 16:39:09 -06:00
c . spec . Annotations = map [ cephv1 . KeyType ] cephv1 . Annotations { cephv1 . KeyMgr : fakeAnnotations }
2019-10-10 17:25:57 +02:00
c . applyPrometheusAnnotations ( & d . ObjectMeta )
2020-07-10 16:40:16 -06:00
assert . Equal ( t , 1 , len ( c . spec . Annotations ))
2019-10-10 17:25:57 +02:00
assert . Equal ( t , 0 , len ( d . ObjectMeta . Annotations ))
}