From 55f24c9a44dd4005c3d9b430cb7af1678955b7f2 Mon Sep 17 00:00:00 2001 From: Andre Date: Fri, 8 Dec 2023 13:03:46 +0000 Subject: [PATCH] Add more functional tests --- tests/functional/barbican_controller_test.go | 174 +++++++++++++------ tests/functional/base_test.go | 14 +- tests/functional/suite_test.go | 14 +- 3 files changed, 144 insertions(+), 58 deletions(-) diff --git a/tests/functional/barbican_controller_test.go b/tests/functional/barbican_controller_test.go index ec57a07..72a5788 100644 --- a/tests/functional/barbican_controller_test.go +++ b/tests/functional/barbican_controller_test.go @@ -6,17 +6,18 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" . "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" //"k8s.io/utils/ptr" - condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" ) var _ = Describe("Barbican controller", func() { var barbicanName types.NamespacedName - //var dbSyncJobName types.NamespacedName + var barbicanTransportURL types.NamespacedName + var dbSyncJobName types.NamespacedName //var bootstrapJobName types.NamespacedName //var deploymentName types.NamespacedName @@ -26,11 +27,15 @@ var _ = Describe("Barbican controller", func() { Name: "barbican", Namespace: namespace, } + barbicanTransportURL = types.NamespacedName{ + Name: "barbican-barbican-transport", + Namespace: namespace, + } + dbSyncJobName = types.NamespacedName{ + Name: "barbican-db-sync", + Namespace: namespace, + } /* - dbSyncJobName = types.NamespacedName{ - Name: "keystone-db-sync", - Namespace: namespace, - } bootstrapJobName = types.NamespacedName{ Name: "keystone-bootstrap", Namespace: namespace, @@ -50,65 +55,134 @@ var _ = Describe("Barbican controller", func() { DeferCleanup(th.DeleteInstance, CreateBarbican(barbicanName, GetDefaultBarbicanSpec())) }) - /* - It("should have the Spec fields defaulted", func() { - Barbican := GetBarbican(barbicanName) - Expect(Barbican.Spec.ServiceUser).Should(Equal("barbican")) - Expect(Barbican.Spec.DatabaseInstance).Should(Equal("openstack")) - Expect(Barbican.Spec.DatabaseUser).Should(Equal("barbican")) - }) + It("should have the Spec fields defaulted", func() { + Barbican := GetBarbican(barbicanName) + Expect(Barbican.Spec.ServiceUser).Should(Equal("barbican")) + Expect(Barbican.Spec.DatabaseInstance).Should(Equal("openstack")) + Expect(Barbican.Spec.DatabaseUser).Should(Equal("barbican")) + }) - It("should have the Status fields initialized", func() { - Barbican := GetBarbican(barbicanName) - Expect(Barbican.Status.Hash).To(BeEmpty()) - Expect(Barbican.Status.BarbicanAPIReadyCount).To(Equal(int32(0))) - Expect(Barbican.Status.BarbicanWorkerReadyCount).To(Equal(int32(0))) - Expect(Barbican.Status.BarbicanKeystoneListenerReadyCount).To(Equal(int32(0))) - Expect(Barbican.Status.TransportURLSecret).To(Equal("")) - Expect(Barbican.Status.DatabaseHostname).To(Equal("")) - }) - */ + It("should have the Status fields initialized", func() { + Barbican := GetBarbican(barbicanName) + Expect(Barbican.Status.Hash).To(BeEmpty()) + Expect(Barbican.Status.BarbicanAPIReadyCount).To(Equal(int32(0))) + Expect(Barbican.Status.BarbicanWorkerReadyCount).To(Equal(int32(0))) + Expect(Barbican.Status.BarbicanKeystoneListenerReadyCount).To(Equal(int32(0))) + Expect(Barbican.Status.TransportURLSecret).To(Equal("")) + Expect(Barbican.Status.DatabaseHostname).To(Equal("")) + }) It("should have input not ready and unknown Conditions initialized", func() { - /* + th.ExpectCondition( + barbicanName, + ConditionGetterFunc(BarbicanConditionGetter), + condition.ReadyCondition, + corev1.ConditionFalse, + ) + + th.ExpectCondition( + barbicanName, + ConditionGetterFunc(BarbicanConditionGetter), + condition.InputReadyCondition, + corev1.ConditionUnknown, + ) + + for _, cond := range []condition.Type{ + condition.ServiceConfigReadyCondition, + condition.DBReadyCondition, + condition.DBSyncReadyCondition, + //condition.BarbicanAPIReadyCondition, + //condition.BarbicanWorkerReadyCondition, + //condition.BarbicanKeystoneListenerReadyCondition, + //condition., + //condition.ExposeServiceReadyCondition, + //condition.BootstrapReadyCondition, + //condition.DeploymentReadyCondition, + condition.NetworkAttachmentsReadyCondition, + //condition.CronJobReadyCondition, + } { th.ExpectCondition( barbicanName, ConditionGetterFunc(BarbicanConditionGetter), - condition.ReadyCondition, - corev1.ConditionFalse, + cond, + corev1.ConditionUnknown, ) - */ + } + }) + It("should have a finalizer", func() { + // the reconciler loop adds the finalizer so we have to wait for + // it to run + Eventually(func() []string { + return GetBarbican(barbicanName).Finalizers + }, timeout, interval).Should(ContainElement("Barbican")) + }) + }) + When("Barbican DB is created", func() { + BeforeEach(func() { + DeferCleanup(k8sClient.Delete, ctx, CreateBarbicanMessageBusSecret(barbicanName.Namespace, "rabbitmq-secret")) + DeferCleanup(th.DeleteInstance, CreateBarbican(barbicanName, GetDefaultBarbicanSpec())) + DeferCleanup( + mariadb.DeleteDBService, + mariadb.CreateDBService( + namespace, + GetBarbican(barbicanName).Spec.DatabaseInstance, + corev1.ServiceSpec{ + Ports: []corev1.ServicePort{{Port: 3306}}, + }, + ), + ) + infra.SimulateTransportURLReady(barbicanTransportURL) + //DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, "memcached", memcachedSpec)) + /* + infra.SimulateMemcachedReady(cinderTest.CinderMemcached) + DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(namespace)) + */ + }) + It("Should set DBReady Condition and set DatabaseHostname Status when DB is Created", func() { + mariadb.SimulateMariaDBDatabaseCompleted(barbicanName) + th.SimulateJobSuccess(dbSyncJobName) + Barbican := GetBarbican(barbicanName) + Expect(Barbican.Status.DatabaseHostname).To(Equal("hostname-for-openstack")) th.ExpectCondition( barbicanName, ConditionGetterFunc(BarbicanConditionGetter), condition.InputReadyCondition, + corev1.ConditionTrue, + ) + th.ExpectCondition( + barbicanName, + ConditionGetterFunc(BarbicanConditionGetter), + condition.DBSyncReadyCondition, corev1.ConditionFalse, ) - /* - - for _, cond := range []condition.Type{ - condition.ServiceConfigReadyCondition, + }) + /* + It("Should fail if db-sync job fails when DB is Created", func() { + mariadb.SimulateMariaDBDatabaseCompleted(cinderTest.Instance) + th.SimulateJobFailure(cinderTest.CinderDBSync) + th.ExpectCondition( + cinderTest.Instance, + ConditionGetterFunc(CinderConditionGetter), condition.DBReadyCondition, + corev1.ConditionTrue, + ) + th.ExpectCondition( + cinderTest.Instance, + ConditionGetterFunc(CinderConditionGetter), condition.DBSyncReadyCondition, - //condition.BarbicanAPIReadyCondition, - //condition.BarbicanWorkerReadyCondition, - //condition.BarbicanKeystoneListenerReadyCondition, - //condition., - //condition.ExposeServiceReadyCondition, - //condition.BootstrapReadyCondition, - //condition.DeploymentReadyCondition, - condition.NetworkAttachmentsReadyCondition, - //condition.CronJobReadyCondition, - } { - th.ExpectCondition( - barbicanName, - ConditionGetterFunc(BarbicanConditionGetter), - cond, - corev1.ConditionUnknown, - ) - } - */ - }) + corev1.ConditionFalse, + ) + }) + It("Does not create CinderAPI", func() { + CinderAPINotExists(cinderTest.Instance) + }) + It("Does not create CinderScheduler", func() { + CinderSchedulerNotExists(cinderTest.Instance) + }) + It("Does not create CinderVolume", func() { + CinderVolumeNotExists(cinderTest.Instance) + }) + */ }) }) diff --git a/tests/functional/base_test.go b/tests/functional/base_test.go index d89c39b..327fde8 100644 --- a/tests/functional/base_test.go +++ b/tests/functional/base_test.go @@ -17,10 +17,11 @@ limitations under the License. package functional_test import ( + "fmt" . "github.com/onsi/gomega" //batchv1 "k8s.io/api/batch/v1" - //corev1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -58,6 +59,17 @@ func GetBarbican(name types.NamespacedName) *barbicanv1.Barbican { return instance } +func CreateBarbicanMessageBusSecret(namespace string, name string) *corev1.Secret { + s := th.CreateSecret( + types.NamespacedName{Namespace: namespace, Name: name}, + map[string][]byte{ + "transport_url": []byte(fmt.Sprintf("rabbit://%s/fake", name)), + }, + ) + logger.Info("Secret created", "name", name) + return s +} + /* func CreateKeystoneAPISecret(namespace string, name string) *corev1.Secret { return th.CreateSecret( diff --git a/tests/functional/suite_test.go b/tests/functional/suite_test.go index bb74303..d8f4e8d 100644 --- a/tests/functional/suite_test.go +++ b/tests/functional/suite_test.go @@ -24,7 +24,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1" + //memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1" //keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1" networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" barbicanv1 "github.com/openstack-k8s-operators/barbican-operator/api/v1beta1" @@ -100,9 +100,9 @@ var _ = BeforeSuite(func() { mariaDBCRDs, err := test.GetCRDDirFromModule( "github.com/openstack-k8s-operators/mariadb-operator/api", "../../go.mod", "bases") Expect(err).ShouldNot(HaveOccurred()) - memcachedCRDs, err := test.GetCRDDirFromModule( - "github.com/openstack-k8s-operators/infra-operator/apis", "../../go.mod", "bases") - Expect(err).ShouldNot(HaveOccurred()) + //memcachedCRDs, err := test.GetCRDDirFromModule( + // "github.com/openstack-k8s-operators/infra-operator/apis", "../../go.mod", "bases") + //Expect(err).ShouldNot(HaveOccurred()) By("bootstrapping test environment") testEnv = &envtest.Environment{ @@ -111,7 +111,7 @@ var _ = BeforeSuite(func() { keystoneCRDs, rabbitmqCRDs, mariaDBCRDs, - memcachedCRDs, + //memcachedCRDs, }, CRDInstallOptions: envtest.CRDInstallOptions{ Paths: []string{ @@ -140,8 +140,8 @@ var _ = BeforeSuite(func() { Expect(err).NotTo(HaveOccurred()) err = mariadbv1.AddToScheme(scheme.Scheme) Expect(err).NotTo(HaveOccurred()) - err = memcachedv1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) + //err = memcachedv1.AddToScheme(scheme.Scheme) + //Expect(err).NotTo(HaveOccurred()) err = rabbitmqv1.AddToScheme(scheme.Scheme) Expect(err).NotTo(HaveOccurred()) err = networkv1.AddToScheme(scheme.Scheme)