diff --git a/modules/common/deployment/deployment.go b/modules/common/deployment/deployment.go index a2ada126..566eafa6 100644 --- a/modules/common/deployment/deployment.go +++ b/modules/common/deployment/deployment.go @@ -127,8 +127,3 @@ func GetDeploymentWithName( return depl, nil } - -// UpdateInternalDeployment - Allows to internally update the state of Deployment -func (d *Deployment) UpdateInternalDeployment(deployment appsv1.Deployment) { - d.deployment = &deployment -} diff --git a/modules/common/go.mod b/modules/common/go.mod index e7183771..fd89ca02 100644 --- a/modules/common/go.mod +++ b/modules/common/go.mod @@ -10,6 +10,7 @@ require ( github.com/onsi/gomega v1.28.0 github.com/openshift/api v3.9.0+incompatible github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.8.1 go.uber.org/zap v1.26.0 k8s.io/api v0.26.9 k8s.io/apimachinery v0.26.9 @@ -76,6 +77,7 @@ require ( require ( github.com/kr/pretty v0.3.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect ) diff --git a/modules/common/tls/tls.go b/modules/common/tls/tls.go index 50b8fc2f..aee5831d 100644 --- a/modules/common/tls/tls.go +++ b/modules/common/tls/tls.go @@ -142,8 +142,11 @@ func (t *TLS) CreateVolumes() []corev1.Volume { // UpdateDeploymentWithTLS updates a given deployment with the necessary volumes and volume mounts to support TLS configurations. func (t *TLS) UpdateDeploymentWithTLS(ctx context.Context, d *deployment.Deployment, h *helper.Helper) error { // Debug - fmt.Println("Service SecretName:", t.Service.SecretName) - fmt.Println("CA SecretName:", t.Ca.CaSecretName) + if t.Service != nil { + fmt.Println("Service SecretName:", t.Service.SecretName) + } else { + fmt.Println("Service is nil") + } tlsVolumes := t.CreateVolumes() fmt.Println("Generated TLS Volumes:", tlsVolumes) // Debug @@ -152,14 +155,11 @@ func (t *TLS) UpdateDeploymentWithTLS(ctx context.Context, d *deployment.Deploym fmt.Println("Generated TLS VolumeMounts:", tlsVolumeMounts) // Debug currentDeployment := d.GetDeployment() - currentDeployment.Spec.Template.Spec.Volumes = append(currentDeployment.Spec.Template.Spec.Volumes, tlsVolumes...) for idx := range currentDeployment.Spec.Template.Spec.Containers { currentDeployment.Spec.Template.Spec.Containers[idx].VolumeMounts = append(currentDeployment.Spec.Template.Spec.Containers[idx].VolumeMounts, tlsVolumeMounts...) } - d.UpdateInternalDeployment(currentDeployment) - _, err := d.CreateOrPatch(ctx, h) return err } diff --git a/modules/common/tls/tls_test.go b/modules/common/tls/tls_test.go index dda5d72a..252d980b 100644 --- a/modules/common/tls/tls_test.go +++ b/modules/common/tls/tls_test.go @@ -18,38 +18,44 @@ package tls import ( "context" + "os" "testing" + "time" "github.com/openstack-k8s-operators/lib-common/modules/common/deployment" "github.com/openstack-k8s-operators/lib-common/modules/common/helper" + "github.com/stretchr/testify/assert" appsv1 "k8s.io/api/apps/v1" - ctrl "sigs.k8s.io/controller-runtime" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/controller-runtime/pkg/log" ) -type mockDeployment struct { - *deployment.Deployment -} +var ( + k8sClient client.Client +) -func (m *mockDeployment) GetDeployment() appsv1.Deployment { - return m.Deployment.GetDeployment() -} +func TestMain(m *testing.M) { + t := &envtest.Environment{} -func (m *mockDeployment) UpdateInternalDeployment(deployment appsv1.Deployment) { - m.Deployment = &deployment -} + cfg, err := t.Start() + if err != nil { + panic(err) + } -func (m *mockDeployment) CreateOrPatch(ctx context.Context, h *helper.Helper) (ctrl.Result, error) { - return ctrl.Result{}, nil -} + k8sClient, err = client.New(cfg, client.Options{}) + if err != nil { + panic(err) + } -type mockHelper struct { - *helper.Helper -} + code := m.Run() + + t.Stop() -func (m *mockHelper) GetClient() client.Client { - return m.Helper.GetClient() + os.Exit(code) } + func TestCreateVolumeMounts(t *testing.T) { tests := []struct { name string @@ -139,27 +145,40 @@ func TestCreateVolumes(t *testing.T) { } func TestUpdateDeploymentWithTLS(t *testing.T) { - // Mock objects - d := &mockDeployment{ - Deployment: &deployment.Deployment{}, - } + assert := assert.New(t) - h := &mockHelper{ - Helper: &helper.Helper{}, + dep := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-deployment", + Namespace: "default", + }, } - tls := &TLS{ - Service: &Service{SecretName: "test-service-secret"}, - Ca: &Ca{CaSecretName: "test-ca-secret"}, + customDeployment := deployment.NewDeployment(dep, time.Second*30) + + tlsObj := &TLS{ + Service: &Service{ + SecretName: "tls-secret-name", + }, + Ca: &Ca{ + CaSecretName: "ca-secret-name", + }, } - err := tls.UpdateDeploymentWithTLS(context.TODO(), d, h) + logger := log.Log.WithName("test-logger") + + helperObj, err := helper.NewHelper(dep, k8sClient, nil, k8sClient.Scheme(), logger) if err != nil { - t.Fatalf("Expected no error, but got: %v", err) + t.Fatalf("failed to create helper: %v", err) } - // Add assertions as needed, for example: - if len(d.deployment.Spec.Template.Spec.Volumes) == 0 { - t.Fatalf("Expected Volumes to be updated, but got none.") - } + err = tlsObj.UpdateDeploymentWithTLS(context.Background(), customDeployment, helperObj) + assert.Nil(err, "failed to update deployment with TLS") + + updatedDep := &appsv1.Deployment{} + err = k8sClient.Get(context.Background(), client.ObjectKey{Name: "test-deployment", Namespace: "default"}, updatedDep) + assert.Nil(err, "failed to get updated deployment") + + assert.NotZero(len(updatedDep.Spec.Template.Spec.Volumes), "expected TLS volumes to be added but found none") + }