github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/builder/builder_service_descriptor_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 builder
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	corev1 "k8s.io/api/core/v1"
    27  
    28  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  	"github.com/1aal/kubeblocks/pkg/constant"
    30  )
    31  
    32  var _ = Describe("service descriptor builder", func() {
    33  	It("should work well", func() {
    34  		const (
    35  			name           = "foo"
    36  			ns             = "default"
    37  			serviceKind    = "mock-kind"
    38  			serviceVersion = "mock-version"
    39  			endpointName   = "mock-endpoint"
    40  			secretRefName  = "foo"
    41  		)
    42  		endpoint := appsv1alpha1.CredentialVar{
    43  			Value: endpointName,
    44  		}
    45  		username := &appsv1alpha1.CredentialVar{
    46  			ValueFrom: &corev1.EnvVarSource{
    47  				SecretKeyRef: &corev1.SecretKeySelector{
    48  					LocalObjectReference: corev1.LocalObjectReference{Name: secretRefName},
    49  					Key:                  constant.ServiceDescriptorUsernameKey,
    50  				},
    51  			},
    52  		}
    53  		password := &appsv1alpha1.CredentialVar{
    54  			ValueFrom: &corev1.EnvVarSource{
    55  				SecretKeyRef: &corev1.SecretKeySelector{
    56  					LocalObjectReference: corev1.LocalObjectReference{Name: secretRefName},
    57  					Key:                  constant.ServiceDescriptorPasswordKey,
    58  				},
    59  			},
    60  		}
    61  		port := appsv1alpha1.CredentialVar{
    62  			ValueFrom: &corev1.EnvVarSource{
    63  				SecretKeyRef: &corev1.SecretKeySelector{
    64  					LocalObjectReference: corev1.LocalObjectReference{Name: secretRefName},
    65  					Key:                  constant.ServiceDescriptorPortKey,
    66  				},
    67  			},
    68  		}
    69  		auth := appsv1alpha1.ConnectionCredentialAuth{
    70  			Username: username,
    71  			Password: password,
    72  		}
    73  		sd := NewServiceDescriptorBuilder(ns, name).
    74  			SetServiceKind(serviceKind).
    75  			SetServiceVersion(serviceVersion).
    76  			SetEndpoint(endpoint).
    77  			SetPort(port).
    78  			SetAuth(auth).
    79  			GetObject()
    80  
    81  		Expect(sd.Name).Should(Equal(name))
    82  		Expect(sd.Namespace).Should(Equal(ns))
    83  		Expect(sd.Spec.ServiceKind).Should(Equal(serviceKind))
    84  		Expect(sd.Spec.ServiceVersion).Should(Equal(serviceVersion))
    85  		Expect(sd.Spec.Endpoint.Value).Should(Equal(endpointName))
    86  		Expect(sd.Spec.Endpoint.ValueFrom).Should(BeNil())
    87  		Expect(sd.Spec.Auth.Username.Value).Should(BeEmpty())
    88  		Expect(sd.Spec.Auth.Username.ValueFrom.SecretKeyRef.Key).Should(Equal(constant.ServiceDescriptorUsernameKey))
    89  		Expect(sd.Spec.Auth.Username.ValueFrom.SecretKeyRef.Name).Should(Equal(secretRefName))
    90  		Expect(sd.Spec.Auth.Password.Value).Should(BeEmpty())
    91  		Expect(sd.Spec.Auth.Password.ValueFrom.SecretKeyRef.Key).Should(Equal(constant.ServiceDescriptorPasswordKey))
    92  		Expect(sd.Spec.Auth.Password.ValueFrom.SecretKeyRef.Name).Should(Equal(secretRefName))
    93  		Expect(sd.Spec.Port.Value).Should(BeEmpty())
    94  		Expect(sd.Spec.Port.ValueFrom.SecretKeyRef.Key).Should(Equal(constant.ServiceDescriptorPortKey))
    95  		Expect(sd.Spec.Port.ValueFrom.SecretKeyRef.Name).Should(Equal(secretRefName))
    96  	})
    97  })