@@ -15,10 +15,12 @@ import (
1515 mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
1616 watcherv1beta1 "github.com/openstack-k8s-operators/watcher-operator/api/v1beta1"
1717 corev1 "k8s.io/api/core/v1"
18+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1819 "k8s.io/apimachinery/pkg/fields"
1920 "k8s.io/apimachinery/pkg/types"
2021 "k8s.io/utils/ptr"
2122 "sigs.k8s.io/controller-runtime/pkg/client"
23+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2224)
2325
2426var (
@@ -42,11 +44,11 @@ var _ = Describe("Watcher controller with minimal spec values", func() {
4244
4345 It ("should have the Spec fields defaulted" , func () {
4446 Watcher := GetWatcher (watcherTest .Instance )
45- Expect (Watcher .Spec .DatabaseInstance ).Should (Equal ("openstack" ))
47+ Expect (* ( Watcher .Spec .DatabaseInstance ) ).Should (Equal ("openstack" ))
4648 Expect (Watcher .Spec .DatabaseAccount ).Should (Equal ("watcher" ))
4749 Expect (Watcher .Spec .Secret ).Should (Equal ("osp-secret" ))
4850 Expect (Watcher .Spec .PasswordSelectors ).Should (Equal (watcherv1beta1.PasswordSelector {Service : "WatcherPassword" }))
49- Expect (Watcher .Spec .RabbitMqClusterName ).Should (Equal ("rabbitmq" ))
51+ Expect (* ( Watcher .Spec .RabbitMqClusterName ) ).Should (Equal ("rabbitmq" ))
5052 Expect (Watcher .Spec .ServiceUser ).Should (Equal ("watcher" ))
5153 Expect (Watcher .Spec .PreserveJobs ).Should (BeFalse ())
5254 })
@@ -84,11 +86,11 @@ var _ = Describe("Watcher controller", func() {
8486
8587 It ("should have the Spec fields defaulted" , func () {
8688 Watcher := GetWatcher (watcherTest .Instance )
87- Expect (Watcher .Spec .DatabaseInstance ).Should (Equal ("openstack" ))
89+ Expect (* ( Watcher .Spec .DatabaseInstance ) ).Should (Equal ("openstack" ))
8890 Expect (Watcher .Spec .DatabaseAccount ).Should (Equal ("watcher" ))
8991 Expect (Watcher .Spec .ServiceUser ).Should (Equal ("watcher" ))
9092 Expect (Watcher .Spec .Secret ).Should (Equal ("test-osp-secret" ))
91- Expect (Watcher .Spec .RabbitMqClusterName ).Should (Equal ("rabbitmq" ))
93+ Expect (* ( Watcher .Spec .RabbitMqClusterName ) ).Should (Equal ("rabbitmq" ))
9294 Expect (Watcher .Spec .PreserveJobs ).Should (BeFalse ())
9395 })
9496
@@ -197,7 +199,7 @@ var _ = Describe("Watcher controller", func() {
197199 mariadb .DeleteDBService ,
198200 mariadb .CreateDBService (
199201 watcherTest .Instance .Namespace ,
200- GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
202+ * GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
201203 corev1.ServiceSpec {
202204 Ports : []corev1.ServicePort {{Port : 3306 }},
203205 },
@@ -459,7 +461,7 @@ var _ = Describe("Watcher controller", func() {
459461 mariadb .DeleteDBService ,
460462 mariadb .CreateDBService (
461463 watcherTest .Instance .Namespace ,
462- GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
464+ * GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
463465 corev1.ServiceSpec {
464466 Ports : []corev1.ServicePort {{Port : 3306 }},
465467 },
@@ -531,6 +533,61 @@ var _ = Describe("Watcher controller", func() {
531533 Expect (Watcher .Spec .ApplierContainerImageURL ).To (Equal ("watcher-applier-custom-image-env" ))
532534 })
533535 })
536+
537+ When ("Watcher rejects when empty databaseinstance is used" , func () {
538+ It ("should raise an error for empty databaseInstance" , func () {
539+ spec := GetDefaultWatcherAPISpec ()
540+ spec ["databaseInstance" ] = ""
541+
542+ raw := map [string ]interface {}{
543+ "apiVersion" : "watcher.openstack.org/v1beta1" ,
544+ "kind" : "watcher" ,
545+ "metadata" : map [string ]interface {}{
546+ "name" : watcherName .Name ,
547+ "namespace" : watcherName .Namespace ,
548+ },
549+ "spec" : spec ,
550+ }
551+
552+ unstructuredObj := & unstructured.Unstructured {Object : raw }
553+ _ , err := controllerutil .CreateOrPatch (
554+ th .Ctx , th .K8sClient , unstructuredObj , func () error { return nil })
555+ Expect (err ).To (HaveOccurred ())
556+ Expect (err .Error ()).To (
557+ ContainSubstring (
558+ "admission webhook \" vwatcher.kb.io\" denied the request: " +
559+ "databaseInstance field should not be empty" ),
560+ )
561+
562+ })
563+ })
564+
565+ When ("Watcher is created with empty RabbitMqClusterName" , func () {
566+ It ("should raise an error for empty RabbitMqClusterName" , func () {
567+ spec := GetDefaultWatcherAPISpec ()
568+ spec ["rabbitMqClusterName" ] = ""
569+
570+ raw := map [string ]interface {}{
571+ "apiVersion" : "watcher.openstack.org/v1beta1" ,
572+ "kind" : "watcher" ,
573+ "metadata" : map [string ]interface {}{
574+ "name" : watcherName .Name ,
575+ "namespace" : watcherName .Namespace ,
576+ },
577+ "spec" : spec ,
578+ }
579+
580+ unstructuredObj := & unstructured.Unstructured {Object : raw }
581+ _ , err := controllerutil .CreateOrPatch (
582+ th .Ctx , th .K8sClient , unstructuredObj , func () error { return nil })
583+ Expect (err ).To (HaveOccurred ())
584+ Expect (err .Error ()).To (
585+ ContainSubstring (
586+ "admission webhook \" vwatcher.kb.io\" denied the request: " +
587+ "rabbitMqClusterName field should not be empty" ),
588+ )
589+ })
590+ })
534591 When ("Watcher with non-default values are created" , func () {
535592 BeforeEach (func () {
536593 DeferCleanup (th .DeleteInstance , CreateWatcher (watcherTest .Instance , GetNonDefaultWatcherSpec ()))
@@ -546,7 +603,7 @@ var _ = Describe("Watcher controller", func() {
546603 mariadb .DeleteDBService ,
547604 mariadb .CreateDBService (
548605 watcherTest .Instance .Namespace ,
549- GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
606+ * GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
550607 corev1.ServiceSpec {
551608 Ports : []corev1.ServicePort {{Port : 3306 }},
552609 },
@@ -557,12 +614,12 @@ var _ = Describe("Watcher controller", func() {
557614
558615 It ("should have the Spec fields with the expected values" , func () {
559616 Watcher := GetWatcher (watcherTest .Instance )
560- Expect (Watcher .Spec .DatabaseInstance ).Should (Equal ("fakeopenstack" ))
617+ Expect (* ( Watcher .Spec .DatabaseInstance ) ).Should (Equal ("fakeopenstack" ))
561618 Expect (Watcher .Spec .DatabaseAccount ).Should (Equal ("watcher" ))
562619 Expect (Watcher .Spec .ServiceUser ).Should (Equal ("fakeuser" ))
563620 Expect (Watcher .Spec .Secret ).Should (Equal ("test-osp-secret" ))
564621 Expect (Watcher .Spec .PreserveJobs ).Should (BeTrue ())
565- Expect (Watcher .Spec .RabbitMqClusterName ).Should (Equal ("rabbitmq" ))
622+ Expect (* ( Watcher .Spec .RabbitMqClusterName ) ).Should (Equal ("rabbitmq" ))
566623 })
567624
568625 It ("Should create watcher service with custom values" , func () {
0 commit comments