Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rewant Soni <[email protected]>
  • Loading branch information
rewantsoni committed Sep 27, 2024
1 parent 17c1252 commit c7cd50e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 129 deletions.
4 changes: 3 additions & 1 deletion controllers/storagecluster/cephblockpools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func TestInjectingPeerTokenToCephBlockPool(t *testing.T) {
},
}

obj := &ocsCephBlockPools{}

for _, c := range cases {
cr := getInitData(c.spec)
request := reconcile.Request{
Expand All @@ -92,7 +94,7 @@ func TestInjectingPeerTokenToCephBlockPool(t *testing.T) {
},
}
reconciler := createReconcilerFromCustomResources(t, cr)
_, err := reconciler.Reconcile(context.TODO(), request)
_, err := obj.ensureCreated(&reconciler, cr)
assert.NoError(t, err)
if c.label == "test-injecting-peer-token-to-cephblockpool" {
assertCephBlockPools(t, reconciler, cr, request, true, true)
Expand Down
4 changes: 3 additions & 1 deletion controllers/storagecluster/cephrbdmirrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func TestCephRbdMirror(t *testing.T) {
},
}

obj := &ocsCephRbdMirrors{}

for _, c := range cases {
cr := getInitData(c.spec)
request := reconcile.Request{
Expand All @@ -50,7 +52,7 @@ func TestCephRbdMirror(t *testing.T) {
},
}
reconciler := createReconcilerFromCustomResources(t, cr)
_, err := reconciler.Reconcile(context.TODO(), request)
_, err := obj.ensureCreated(&reconciler, cr)
assert.NoError(t, err)
switch c.label {
case "create-ceph-rbd-mirror":
Expand Down
82 changes: 42 additions & 40 deletions controllers/storagecluster/initialization_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,50 +177,12 @@ func initStorageClusterResourceCreateUpdateTestProviderMode(t *testing.T, runtim
}

if remoteConsumers {
node := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"node-role.kubernetes.io/worker": "",
},
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Type: v1.NodeInternalIP,
Address: "0:0:0:0",
},
},
},
}

os.Setenv(providerAPIServerImage, "fake-image")
os.Setenv(util.WatchNamespaceEnvVar, "")
os.Setenv(onboardingValidationKeysGeneratorImage, "fake-image")

deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
}
deployment.Status.AvailableReplicas = 1

service := &v1.Service{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
}
service.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{
{
Hostname: "fake",
},
}

secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
}

clientConfigMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: ocsClientConfigMapName},
}

addedRuntimeObjects := []runtime.Object{node, service, deployment, secret, clientConfigMap}
rtObjsToCreateReconciler = append(rtObjsToCreateReconciler, addedRuntimeObjects...)
rtObjsToCreateReconciler = append(rtObjsToCreateReconciler, clientConfigMap)

util.AddAnnotation(cr, "ocs.openshift.io/deployment-mode", "provider")
}
Expand Down Expand Up @@ -358,6 +320,46 @@ func createFakeInitializationStorageClusterReconciler(t *testing.T, obj ...runti
},
}

workerNode := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "workerNode",
Labels: map[string]string{
"node-role.kubernetes.io/worker": "",
},
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Type: v1.NodeInternalIP,
Address: "0:0:0:0",
},
},
},
}

os.Setenv(providerAPIServerImage, "fake-image")
os.Setenv(onboardingValidationKeysGeneratorImage, "fake-image")

ocsProviderServiceDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
Status: appsv1.DeploymentStatus{
AvailableReplicas: 1,
},
}

ocsProviderService := &v1.Service{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
Status: v1.ServiceStatus{
LoadBalancer: v1.LoadBalancerStatus{
Ingress: []v1.LoadBalancerIngress{{Hostname: "fake"}},
},
},
}

ocsProviderServiceSecret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
}

operatorNamespace := os.Getenv("OPERATOR_NAMESPACE")
verOcs, err := semver.Make(ocsversion.Version)
if err != nil {
Expand Down Expand Up @@ -393,7 +395,7 @@ func createFakeInitializationStorageClusterReconciler(t *testing.T, obj ...runti
}
}

runtimeObjects = append(runtimeObjects, mockNodeList.DeepCopy(), cbp, cfs, cnfs, cnfsbp, cnfssvc, infrastructure, networkConfig, rookCephMonSecret, csv)
runtimeObjects = append(runtimeObjects, mockNodeList.DeepCopy(), cbp, cfs, cnfs, cnfsbp, cnfssvc, infrastructure, networkConfig, rookCephMonSecret, csv, workerNode, ocsProviderServiceSecret, ocsProviderServiceDeployment, ocsProviderService)
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(runtimeObjects...).WithStatusSubresource(statusSubresourceObjs...).Build()

return StorageClusterReconciler{
Expand Down
100 changes: 17 additions & 83 deletions controllers/storagecluster/provider_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (

func TestOcsProviderServerEnsureCreated(t *testing.T) {

t.Run("Ensure that Deployment,Service,Secret is created when AllowRemoteStorageConsumers is enabled", func(t *testing.T) {
t.Run("Ensure that Deployment,Service is created when storageCluster is created", func(t *testing.T) {

r, instance := createSetupForOcsProviderTest(t, true, "")
r, instance := createSetupForOcsProviderTest(t, "")

obj := &ocsProviderServer{}
res, err := obj.ensureCreated(r, instance)
Expand Down Expand Up @@ -81,22 +81,11 @@ func TestOcsProviderServerEnsureCreated(t *testing.T) {
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(service), service))
expectedService := GetProviderAPIServerServiceForTest(instance)
assert.Equal(t, expectedService.Spec, service.Spec)

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
}
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(secret), secret))

cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: ocsClientConfigMapName},
}
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(cm), cm))
assert.Equal(t, "true", cm.Data[deployCSIKey])
})

t.Run("Ensure that Deployment,Service,Secret is created when AllowRemoteStorageConsumers and ProviderAPIServerServiceType set to loadBalancer", func(t *testing.T) {
t.Run("Ensure that Deployment,Service is created when ProviderAPIServerServiceType set to loadBalancer", func(t *testing.T) {

r, instance := createSetupForOcsProviderTest(t, true, corev1.ServiceTypeLoadBalancer)
r, instance := createSetupForOcsProviderTest(t, corev1.ServiceTypeLoadBalancer)

obj := &ocsProviderServer{}
res, err := obj.ensureCreated(r, instance)
Expand Down Expand Up @@ -150,22 +139,11 @@ func TestOcsProviderServerEnsureCreated(t *testing.T) {
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(service), service))
expectedService := GetLoadBalancerProviderAPIServerServiceForTest(instance)
assert.Equal(t, expectedService.Spec, service.Spec)

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
}
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(secret), secret))

cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: ocsClientConfigMapName},
}
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(cm), cm))
assert.Equal(t, "true", cm.Data[deployCSIKey])
})

t.Run("Ensure that Deployment,Service,Secret is created when AllowRemoteStorageConsumers and ProviderAPIServerServiceType set to ClusterIP", func(t *testing.T) {
t.Run("Ensure that Deployment,Service is created when ProviderAPIServerServiceType set to ClusterIP", func(t *testing.T) {

r, instance := createSetupForOcsProviderTest(t, true, corev1.ServiceTypeClusterIP)
r, instance := createSetupForOcsProviderTest(t, corev1.ServiceTypeClusterIP)

obj := &ocsProviderServer{}
res, err := obj.ensureCreated(r, instance)
Expand Down Expand Up @@ -214,22 +192,11 @@ func TestOcsProviderServerEnsureCreated(t *testing.T) {
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(service), service))
expectedService := GetClusterIPProviderAPIServerServiceForTest(instance)
assert.Equal(t, expectedService.Spec, service.Spec)

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: ocsProviderServerName},
}
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(secret), secret))

cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Name: ocsClientConfigMapName},
}
assert.NoError(t, r.Client.Get(context.TODO(), client.ObjectKeyFromObject(cm), cm))
assert.Equal(t, "true", cm.Data[deployCSIKey])
})

t.Run("Ensure that Service is not created when AllowRemoteStorageConsumers is enabled and ProviderAPIServerServiceType is set to any other value than NodePort, ClusterIP or LoadBalancer", func(t *testing.T) {
t.Run("Ensure that Service is not created when ProviderAPIServerServiceType is set to any other value than NodePort, ClusterIP or LoadBalancer", func(t *testing.T) {

r, instance := createSetupForOcsProviderTest(t, true, corev1.ServiceTypeExternalName)
r, instance := createSetupForOcsProviderTest(t, corev1.ServiceTypeExternalName)

obj := &ocsProviderServer{}
_, err := obj.ensureCreated(r, instance)
Expand All @@ -239,40 +206,13 @@ func TestOcsProviderServerEnsureCreated(t *testing.T) {
}
assert.True(t, errors.IsNotFound(r.Client.Get(context.TODO(), client.ObjectKeyFromObject(service), service)))
})

t.Run("Ensure that Deployment,Service,Secret is not created when AllowRemoteStorageConsumers is disabled", func(t *testing.T) {

r, instance := createSetupForOcsProviderTest(t, false, "")

obj := &ocsProviderServer{}
_, err := obj.ensureCreated(r, instance)

assert.NoError(t, err)

assertNotFoundProviderResources(t, r.Client)
})
}

func TestOcsProviderServerEnsureDeleted(t *testing.T) {

t.Run("Ensure that Deployment,Service,Secret is deleted when AllowRemoteStorageConsumers is disabled", func(t *testing.T) {

r, instance := createSetupForOcsProviderTest(t, true, "")
obj := &ocsProviderServer{}
// create resources and ignore error as it should be tested via TestOcsProviderServerEnsureCreated
_, _ = obj.ensureCreated(r, instance)

instance.Spec.AllowRemoteStorageConsumers = false
// the resources will be deleted through the ensureCreated func as we are not in the deletion phase
_, err := obj.ensureCreated(r, instance)
assert.NoError(t, err)

assertNotFoundProviderResources(t, r.Client)
})
t.Run("Ensure that Deployment,Service is deleted while uninstalling", func(t *testing.T) {

t.Run("Ensure that Deployment,Service,Secret is deleted while uninstalling", func(t *testing.T) {

r, instance := createSetupForOcsProviderTest(t, true, "")
r, instance := createSetupForOcsProviderTest(t, "")
obj := &ocsProviderServer{}
// create resources and ignore error as it should be tested via TestOcsProviderServerEnsureCreated
_, _ = obj.ensureCreated(r, instance)
Expand Down Expand Up @@ -303,7 +243,7 @@ func assertNotFoundProviderResources(t *testing.T, cli client.Client) {

}

func createSetupForOcsProviderTest(t *testing.T, allowRemoteStorageConsumers bool, providerAPIServerServiceType corev1.ServiceType) (*StorageClusterReconciler, *ocsv1.StorageCluster) {
func createSetupForOcsProviderTest(t *testing.T, providerAPIServerServiceType corev1.ServiceType) (*StorageClusterReconciler, *ocsv1.StorageCluster) {

node := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -339,14 +279,12 @@ func createSetupForOcsProviderTest(t *testing.T, allowRemoteStorageConsumers boo

instance := &ocsv1.StorageCluster{
Spec: ocsv1.StorageClusterSpec{
AllowRemoteStorageConsumers: allowRemoteStorageConsumers,
ProviderAPIServerServiceType: providerAPIServerServiceType,
},
}

os.Setenv(providerAPIServerImage, "fake-image")
os.Setenv(onboardingValidationKeysGeneratorImage, "fake-image")
os.Setenv(util.WatchNamespaceEnvVar, "openshift-storage")

return r, instance
}
Expand Down Expand Up @@ -379,16 +317,12 @@ func GetProviderAPIServerDeploymentForTest(instance *ocsv1.StorageCluster) *apps
Command: []string{"/usr/local/bin/provider-api"},
Env: []corev1.EnvVar{
{
Name: util.WatchNamespaceEnvVar,
Value: os.Getenv(util.WatchNamespaceEnvVar),
},
{
Name: "STORAGE_CLUSTER_NAME",
Value: instance.Name,
},
{
Name: "STORAGE_CLUSTER_UID",
Value: string(instance.UID),
Name: util.PodNamespaceEnvVar,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
},
Ports: []corev1.ContainerPort{
Expand Down
Loading

0 comments on commit c7cd50e

Please sign in to comment.