github.com/verrazzano/verrazzano@v1.7.0/pkg/namespace/namespace_test.go (about) 1 // Copyright (c) 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package namespace 5 6 import ( 7 "github.com/stretchr/testify/assert" 8 "github.com/verrazzano/verrazzano/pkg/k8sutil" 9 "github.com/verrazzano/verrazzano/pkg/log/vzlog" 10 "github.com/verrazzano/verrazzano/platform-operator/constants" 11 v1 "k8s.io/api/core/v1" 12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 k8sfake "k8s.io/client-go/kubernetes/fake" 14 corev1Cli "k8s.io/client-go/kubernetes/typed/core/v1" 15 "testing" 16 ) 17 18 // TestCheckIfVerrazzanoManagedNamespaceExists tests the CheckIfVerrazzanoManagedNamespaceExists fn 19 // GIVEN a call to CheckIfVerrazzanoManagedNamespaceExists 20 // WHEN the ns exists and has the Verrzzano namespace label 21 // THEN true is retured without error 22 func TestCheckIfVerrazzanoManagedNamespaceExists(t *testing.T) { 23 const namespace = "somens" 24 k8sutil.GetCoreV1Func = func(_ ...vzlog.VerrazzanoLogger) (corev1Cli.CoreV1Interface, error) { 25 return k8sfake.NewSimpleClientset( 26 &v1.Namespace{ 27 ObjectMeta: metav1.ObjectMeta{ 28 Name: namespace, 29 Labels: map[string]string{ 30 constants.VerrazzanoManagedKey: namespace, 31 }, 32 }, 33 }, 34 ).CoreV1(), nil 35 } 36 defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }() 37 38 exists, err := CheckIfVerrazzanoManagedNamespaceExists(namespace) 39 assert.NoError(t, err) 40 assert.True(t, exists) 41 } 42 43 // TestCheckIfUnmanagedNamespaceExists tests the CheckIfVerrazzanoManagedNamespaceExists fn 44 // GIVEN a call to CheckIfVerrazzanoManagedNamespaceExists 45 // WHEN the ns exists and does NOT have the Verrzzano namespace label 46 // THEN false is retured without error 47 func TestCheckIfUnmanagedNamespaceExists(t *testing.T) { 48 const namespace = "somens" 49 k8sutil.GetCoreV1Func = func(_ ...vzlog.VerrazzanoLogger) (corev1Cli.CoreV1Interface, error) { 50 return k8sfake.NewSimpleClientset( 51 &v1.Namespace{ 52 ObjectMeta: metav1.ObjectMeta{ 53 Name: namespace, 54 }, 55 }, 56 ).CoreV1(), nil 57 } 58 defer func() { k8sutil.GetCoreV1Func = k8sutil.GetCoreV1Client }() 59 60 exists, err := CheckIfVerrazzanoManagedNamespaceExists(namespace) 61 assert.NoError(t, err) 62 assert.False(t, exists) 63 }