github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/servicedescriptor_controller_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package apps
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    31  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    32  )
    33  
    34  var _ = Describe("test clusterVersion controller", func() {
    35  
    36  	var (
    37  		randomStr = testCtx.GetRandomStr()
    38  		namespace = "default"
    39  	)
    40  
    41  	cleanEnv := func() {
    42  		// must wait till resources deleted and no longer existed before the testcases start,
    43  		// otherwise if later it needs to create some new resource objects with the same name,
    44  		// in race conditions, it will find the existence of old objects, resulting failure to
    45  		// create the new objects.
    46  		By("clean resources")
    47  
    48  		// delete cluster(and all dependent sub-resources), clusterversion and clusterdef
    49  		testapps.ClearClusterResources(&testCtx)
    50  	}
    51  	BeforeEach(cleanEnv)
    52  
    53  	AfterEach(cleanEnv)
    54  
    55  	Context("test ServiceDescriptor controller", func() {
    56  		It("test ServiceDescriptor controller", func() {
    57  			By("create a ServiceDescriptor obj")
    58  
    59  			endpoint := appsv1alpha1.CredentialVar{
    60  				Value: "mock-endpoint",
    61  			}
    62  			port := appsv1alpha1.CredentialVar{
    63  				Value: "mock-port",
    64  			}
    65  			auth := appsv1alpha1.ConnectionCredentialAuth{
    66  				Username: &appsv1alpha1.CredentialVar{
    67  					Value: "mock-username",
    68  				},
    69  				Password: &appsv1alpha1.CredentialVar{
    70  					Value: "mock-password",
    71  				},
    72  			}
    73  			validSDName := "service-descriptor-valid-" + randomStr
    74  			validServiceDescriptor := testapps.NewServiceDescriptorFactory(namespace, validSDName).
    75  				SetServiceKind("mock-kind").
    76  				SetServiceVersion("mock-version").
    77  				SetEndpoint(endpoint).
    78  				SetPort(port).
    79  				SetAuth(auth).
    80  				Create(&testCtx).GetObject()
    81  
    82  			By("wait for ServiceDescriptor phase is available when serviceDescriptor is valid")
    83  			Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(validServiceDescriptor),
    84  				func(g Gomega, tmpSCC *appsv1alpha1.ServiceDescriptor) {
    85  					g.Expect(tmpSCC.Status.Phase).Should(Equal(appsv1alpha1.AvailablePhase))
    86  				})).Should(Succeed())
    87  
    88  			invalidSDName := "service-descriptor-invalid-" + randomStr
    89  			mockSecretRefName := "mock-secret-for-scc" + randomStr
    90  			authValueFrom := appsv1alpha1.ConnectionCredentialAuth{
    91  				Username: &appsv1alpha1.CredentialVar{
    92  					ValueFrom: &corev1.EnvVarSource{
    93  						SecretKeyRef: &corev1.SecretKeySelector{
    94  							LocalObjectReference: corev1.LocalObjectReference{
    95  								Name: mockSecretRefName,
    96  							},
    97  							Key: "mock-secret-username-key",
    98  						},
    99  					},
   100  				},
   101  				Password: &appsv1alpha1.CredentialVar{
   102  					ValueFrom: &corev1.EnvVarSource{
   103  						SecretKeyRef: &corev1.SecretKeySelector{
   104  							LocalObjectReference: corev1.LocalObjectReference{
   105  								Name: mockSecretRefName,
   106  							},
   107  							Key: "mock-secret-password-key",
   108  						},
   109  					},
   110  				},
   111  			}
   112  			invalidServiceDescriptor := testapps.NewServiceDescriptorFactory(testCtx.DefaultNamespace, invalidSDName).
   113  				SetServiceKind("mock-kind").
   114  				SetServiceVersion("mock-version").
   115  				SetEndpoint(endpoint).
   116  				SetPort(port).
   117  				SetAuth(authValueFrom).
   118  				Create(&testCtx).GetObject()
   119  			By("wait for ServiceDescriptor phase is Unavailable because serviceDescriptor secretRef not found")
   120  			Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(invalidServiceDescriptor),
   121  				func(g Gomega, tmpSCC *appsv1alpha1.ServiceDescriptor) {
   122  					g.Expect(tmpSCC.Status.Phase).Should(Equal(appsv1alpha1.UnavailablePhase))
   123  				})).Should(Succeed())
   124  
   125  			secret := &corev1.Secret{
   126  				ObjectMeta: metav1.ObjectMeta{
   127  					Name:      mockSecretRefName,
   128  					Namespace: namespace,
   129  				},
   130  				Data: map[string][]byte{
   131  					"mock-secret-username-key": []byte("mock-username"),
   132  					"mock-secret-password-key": []byte("mock-password"),
   133  				},
   134  			}
   135  			Expect(testCtx.CheckedCreateObj(ctx, secret)).Should(Succeed())
   136  			By("wait for ServiceDescriptor phase is available because serviceDescriptor secretRef found")
   137  			Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(invalidServiceDescriptor),
   138  				func(g Gomega, tmpSCC *appsv1alpha1.ServiceDescriptor) {
   139  					g.Expect(tmpSCC.Status.Phase).Should(Equal(appsv1alpha1.AvailablePhase))
   140  				})).Should(Succeed())
   141  		})
   142  	})
   143  })