github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/utils/kubernetes_test.go (about) 1 package utils 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 corev1 "k8s.io/api/core/v1" 9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 10 "sigs.k8s.io/controller-runtime/pkg/client/fake" 11 12 argoprojiov1alpha1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 13 ) 14 15 func TestGetSecretRef(t *testing.T) { 16 secret := &corev1.Secret{ 17 ObjectMeta: metav1.ObjectMeta{Name: "test-secret", Namespace: "test"}, 18 Data: map[string][]byte{ 19 "my-token": []byte("secret"), 20 }, 21 } 22 client := fake.NewClientBuilder().WithObjects(secret).Build() 23 ctx := t.Context() 24 25 cases := []struct { 26 name, namespace, token string 27 ref *argoprojiov1alpha1.SecretRef 28 hasError bool 29 }{ 30 { 31 name: "valid ref", 32 ref: &argoprojiov1alpha1.SecretRef{SecretName: "test-secret", Key: "my-token"}, 33 namespace: "test", 34 token: "secret", 35 hasError: false, 36 }, 37 { 38 name: "nil ref", 39 ref: nil, 40 namespace: "test", 41 token: "", 42 hasError: false, 43 }, 44 { 45 name: "wrong name", 46 ref: &argoprojiov1alpha1.SecretRef{SecretName: "other", Key: "my-token"}, 47 namespace: "test", 48 token: "", 49 hasError: true, 50 }, 51 { 52 name: "wrong key", 53 ref: &argoprojiov1alpha1.SecretRef{SecretName: "test-secret", Key: "other-token"}, 54 namespace: "test", 55 token: "", 56 hasError: true, 57 }, 58 { 59 name: "wrong namespace", 60 ref: &argoprojiov1alpha1.SecretRef{SecretName: "test-secret", Key: "my-token"}, 61 namespace: "other", 62 token: "", 63 hasError: true, 64 }, 65 } 66 67 for _, c := range cases { 68 t.Run(c.name, func(t *testing.T) { 69 token, err := GetSecretRef(ctx, client, c.ref, c.namespace, false) 70 if c.hasError { 71 require.Error(t, err) 72 } else { 73 require.NoError(t, err) 74 } 75 assert.Equal(t, c.token, token) 76 }) 77 } 78 } 79 80 func TestGetConfigMapData(t *testing.T) { 81 configMap := &corev1.ConfigMap{ 82 ObjectMeta: metav1.ObjectMeta{Name: "test-configmap", Namespace: "test"}, 83 Data: map[string]string{ 84 "my-data": "configmap-data", 85 }, 86 } 87 client := fake.NewClientBuilder().WithObjects(configMap).Build() 88 ctx := t.Context() 89 90 cases := []struct { 91 name, namespace, data string 92 ref *argoprojiov1alpha1.ConfigMapKeyRef 93 hasError bool 94 }{ 95 { 96 name: "valid ref", 97 ref: &argoprojiov1alpha1.ConfigMapKeyRef{ConfigMapName: "test-configmap", Key: "my-data"}, 98 namespace: "test", 99 data: "configmap-data", 100 hasError: false, 101 }, 102 { 103 name: "nil ref", 104 ref: nil, 105 namespace: "test", 106 data: "", 107 hasError: false, 108 }, 109 { 110 name: "wrong name", 111 ref: &argoprojiov1alpha1.ConfigMapKeyRef{ConfigMapName: "other", Key: "my-data"}, 112 namespace: "test", 113 data: "", 114 hasError: true, 115 }, 116 { 117 name: "wrong key", 118 ref: &argoprojiov1alpha1.ConfigMapKeyRef{ConfigMapName: "test-configmap", Key: "other-data"}, 119 namespace: "test", 120 data: "", 121 hasError: true, 122 }, 123 { 124 name: "wrong namespace", 125 ref: &argoprojiov1alpha1.ConfigMapKeyRef{ConfigMapName: "test-configmap", Key: "my-data"}, 126 namespace: "other", 127 data: "", 128 hasError: true, 129 }, 130 } 131 132 for _, c := range cases { 133 t.Run(c.name, func(t *testing.T) { 134 data, err := GetConfigMapData(ctx, client, c.ref, c.namespace) 135 if c.hasError { 136 require.Error(t, err) 137 } else { 138 require.NoError(t, err) 139 } 140 if !c.hasError { 141 assert.Equal(t, c.data, string(data)) 142 } 143 }) 144 } 145 }