@@ -14,9 +14,11 @@ import (
1414 mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
1515 watcherv1beta1 "github.com/openstack-k8s-operators/watcher-operator/api/v1beta1"
1616 corev1 "k8s.io/api/core/v1"
17+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1718 "k8s.io/apimachinery/pkg/fields"
1819 "k8s.io/apimachinery/pkg/types"
1920 "sigs.k8s.io/controller-runtime/pkg/client"
21+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2022)
2123
2224var (
@@ -40,11 +42,11 @@ var _ = Describe("Watcher controller with minimal spec values", func() {
4042
4143 It ("should have the Spec fields defaulted" , func () {
4244 Watcher := GetWatcher (watcherTest .Instance )
43- Expect (Watcher .Spec .DatabaseInstance ).Should (Equal ("openstack" ))
45+ Expect (* ( Watcher .Spec .DatabaseInstance ) ).Should (Equal ("openstack" ))
4446 Expect (Watcher .Spec .DatabaseAccount ).Should (Equal ("watcher" ))
4547 Expect (Watcher .Spec .Secret ).Should (Equal ("osp-secret" ))
4648 Expect (Watcher .Spec .PasswordSelectors ).Should (Equal (watcherv1beta1.PasswordSelector {Service : "WatcherPassword" }))
47- Expect (Watcher .Spec .RabbitMqClusterName ).Should (Equal ("rabbitmq" ))
49+ Expect (* ( Watcher .Spec .RabbitMqClusterName ) ).Should (Equal ("rabbitmq" ))
4850 Expect (Watcher .Spec .ServiceUser ).Should (Equal ("watcher" ))
4951 Expect (Watcher .Spec .PreserveJobs ).Should (BeFalse ())
5052 })
@@ -82,11 +84,11 @@ var _ = Describe("Watcher controller", func() {
8284
8385 It ("should have the Spec fields defaulted" , func () {
8486 Watcher := GetWatcher (watcherTest .Instance )
85- Expect (Watcher .Spec .DatabaseInstance ).Should (Equal ("openstack" ))
87+ Expect (* ( Watcher .Spec .DatabaseInstance ) ).Should (Equal ("openstack" ))
8688 Expect (Watcher .Spec .DatabaseAccount ).Should (Equal ("watcher" ))
8789 Expect (Watcher .Spec .ServiceUser ).Should (Equal ("watcher" ))
8890 Expect (Watcher .Spec .Secret ).Should (Equal ("test-osp-secret" ))
89- Expect (Watcher .Spec .RabbitMqClusterName ).Should (Equal ("rabbitmq" ))
91+ Expect (* ( Watcher .Spec .RabbitMqClusterName ) ).Should (Equal ("rabbitmq" ))
9092 Expect (Watcher .Spec .PreserveJobs ).Should (BeFalse ())
9193 })
9294
@@ -188,7 +190,7 @@ var _ = Describe("Watcher controller", func() {
188190 mariadb .DeleteDBService ,
189191 mariadb .CreateDBService (
190192 watcherTest .Instance .Namespace ,
191- GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
193+ * GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
192194 corev1.ServiceSpec {
193195 Ports : []corev1.ServicePort {{Port : 3306 }},
194196 },
@@ -422,7 +424,7 @@ var _ = Describe("Watcher controller", func() {
422424 mariadb .DeleteDBService ,
423425 mariadb .CreateDBService (
424426 watcherTest .Instance .Namespace ,
425- GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
427+ * GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
426428 corev1.ServiceSpec {
427429 Ports : []corev1.ServicePort {{Port : 3306 }},
428430 },
@@ -494,6 +496,61 @@ var _ = Describe("Watcher controller", func() {
494496 Expect (Watcher .Spec .ApplierContainerImageURL ).To (Equal ("watcher-applier-custom-image-env" ))
495497 })
496498 })
499+
500+ When ("Watcher rejects when empty databaseinstance is used" , func () {
501+ It ("should raise an error for empty databaseInstance" , func () {
502+ spec := GetDefaultWatcherAPISpec ()
503+ spec ["databaseInstance" ] = ""
504+
505+ raw := map [string ]interface {}{
506+ "apiVersion" : "watcher.openstack.org/v1beta1" ,
507+ "kind" : "watcher" ,
508+ "metadata" : map [string ]interface {}{
509+ "name" : watcherName .Name ,
510+ "namespace" : watcherName .Namespace ,
511+ },
512+ "spec" : spec ,
513+ }
514+
515+ unstructuredObj := & unstructured.Unstructured {Object : raw }
516+ _ , err := controllerutil .CreateOrPatch (
517+ th .Ctx , th .K8sClient , unstructuredObj , func () error { return nil })
518+ Expect (err ).To (HaveOccurred ())
519+ Expect (err .Error ()).To (
520+ ContainSubstring (
521+ "admission webhook \" vwatcher.kb.io\" denied the request: " +
522+ "databaseInstance field should not be empty" ),
523+ )
524+
525+ })
526+ })
527+
528+ When ("Watcher is created with empty RabbitMqClusterName" , func () {
529+ It ("should raise an error for empty RabbitMqClusterName" , func () {
530+ spec := GetDefaultWatcherAPISpec ()
531+ spec ["rabbitMqClusterName" ] = ""
532+
533+ raw := map [string ]interface {}{
534+ "apiVersion" : "watcher.openstack.org/v1beta1" ,
535+ "kind" : "watcher" ,
536+ "metadata" : map [string ]interface {}{
537+ "name" : watcherName .Name ,
538+ "namespace" : watcherName .Namespace ,
539+ },
540+ "spec" : spec ,
541+ }
542+
543+ unstructuredObj := & unstructured.Unstructured {Object : raw }
544+ _ , err := controllerutil .CreateOrPatch (
545+ th .Ctx , th .K8sClient , unstructuredObj , func () error { return nil })
546+ Expect (err ).To (HaveOccurred ())
547+ Expect (err .Error ()).To (
548+ ContainSubstring (
549+ "admission webhook \" vwatcher.kb.io\" denied the request: " +
550+ "rabbitMqClusterName field should not be empty" ),
551+ )
552+ })
553+ })
497554 When ("Watcher with non-default values are created" , func () {
498555 BeforeEach (func () {
499556 DeferCleanup (th .DeleteInstance , CreateWatcher (watcherTest .Instance , GetNonDefaultWatcherSpec ()))
@@ -502,7 +559,7 @@ var _ = Describe("Watcher controller", func() {
502559 mariadb .DeleteDBService ,
503560 mariadb .CreateDBService (
504561 watcherTest .Instance .Namespace ,
505- GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
562+ * GetWatcher (watcherTest .Instance ).Spec .DatabaseInstance ,
506563 corev1.ServiceSpec {
507564 Ports : []corev1.ServicePort {{Port : 3306 }},
508565 },
@@ -512,12 +569,12 @@ var _ = Describe("Watcher controller", func() {
512569
513570 It ("should have the Spec fields with the expected values" , func () {
514571 Watcher := GetWatcher (watcherTest .Instance )
515- Expect (Watcher .Spec .DatabaseInstance ).Should (Equal ("fakeopenstack" ))
572+ Expect (* ( Watcher .Spec .DatabaseInstance ) ).Should (Equal ("fakeopenstack" ))
516573 Expect (Watcher .Spec .DatabaseAccount ).Should (Equal ("watcher" ))
517574 Expect (Watcher .Spec .ServiceUser ).Should (Equal ("fakeuser" ))
518575 Expect (Watcher .Spec .Secret ).Should (Equal ("test-osp-secret" ))
519576 Expect (Watcher .Spec .PreserveJobs ).Should (BeTrue ())
520- Expect (Watcher .Spec .RabbitMqClusterName ).Should (Equal ("rabbitmq" ))
577+ Expect (* ( Watcher .Spec .RabbitMqClusterName ) ).Should (Equal ("rabbitmq" ))
521578 })
522579
523580 It ("Should create watcher service with custom values" , func () {
0 commit comments