|
| 1 | +package crypto |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/client-go/kubernetes/fake" |
| 11 | + ktesting "k8s.io/client-go/testing" |
| 12 | + |
| 13 | + "github.com/kubean-io/kubean-api/constants" |
| 14 | + "github.com/kubean-io/kubean/pkg/util" |
| 15 | +) |
| 16 | + |
| 17 | +func TestInitConfiguration(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + existingCM *corev1.ConfigMap |
| 21 | + expectError bool |
| 22 | + expectCreate bool |
| 23 | + expectUpdate bool |
| 24 | + getError error |
| 25 | + updateError error |
| 26 | + createError error |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "private key already exists", |
| 30 | + existingCM: &corev1.ConfigMap{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Name: constants.KubeanConfigMapName, |
| 33 | + Namespace: util.GetCurrentNSOrDefault(), |
| 34 | + }, |
| 35 | + Data: map[string]string{ |
| 36 | + PrivateKey: "existing-private-key", |
| 37 | + }, |
| 38 | + }, |
| 39 | + expectError: false, |
| 40 | + expectCreate: false, |
| 41 | + expectUpdate: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "private key is empty string", |
| 45 | + existingCM: &corev1.ConfigMap{ |
| 46 | + ObjectMeta: metav1.ObjectMeta{ |
| 47 | + Name: constants.KubeanConfigMapName, |
| 48 | + Namespace: util.GetCurrentNSOrDefault(), |
| 49 | + }, |
| 50 | + Data: map[string]string{ |
| 51 | + PrivateKey: "", |
| 52 | + }, |
| 53 | + }, |
| 54 | + expectError: false, |
| 55 | + expectCreate: true, |
| 56 | + expectUpdate: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "no private key field", |
| 60 | + existingCM: &corev1.ConfigMap{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{ |
| 62 | + Name: constants.KubeanConfigMapName, |
| 63 | + Namespace: util.GetCurrentNSOrDefault(), |
| 64 | + }, |
| 65 | + Data: map[string]string{}, |
| 66 | + }, |
| 67 | + expectError: false, |
| 68 | + expectCreate: true, |
| 69 | + expectUpdate: true, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "configmap not found", |
| 73 | + existingCM: nil, |
| 74 | + expectError: true, |
| 75 | + getError: errors.New("configmap not found"), |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "update fails", |
| 79 | + existingCM: &corev1.ConfigMap{ |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: constants.KubeanConfigMapName, |
| 82 | + Namespace: util.GetCurrentNSOrDefault(), |
| 83 | + }, |
| 84 | + Data: map[string]string{}, |
| 85 | + }, |
| 86 | + expectError: true, |
| 87 | + updateError: errors.New("update failed"), |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "create pubkey configmap fails", |
| 91 | + existingCM: &corev1.ConfigMap{ |
| 92 | + ObjectMeta: metav1.ObjectMeta{ |
| 93 | + Name: constants.KubeanConfigMapName, |
| 94 | + Namespace: util.GetCurrentNSOrDefault(), |
| 95 | + }, |
| 96 | + Data: map[string]string{}, |
| 97 | + }, |
| 98 | + expectError: true, |
| 99 | + createError: errors.New("create failed"), |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + // Create fake clientset |
| 106 | + var objects []runtime.Object |
| 107 | + if tt.existingCM != nil { |
| 108 | + objects = append(objects, tt.existingCM) |
| 109 | + } |
| 110 | + clientset := fake.NewSimpleClientset(objects...) |
| 111 | + |
| 112 | + // Mock errors if specified |
| 113 | + if tt.getError != nil { |
| 114 | + clientset.PrependReactor("get", "configmaps", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) { |
| 115 | + return true, nil, tt.getError |
| 116 | + }) |
| 117 | + } |
| 118 | + if tt.updateError != nil { |
| 119 | + clientset.PrependReactor("update", "configmaps", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) { |
| 120 | + return true, nil, tt.updateError |
| 121 | + }) |
| 122 | + } |
| 123 | + if tt.createError != nil { |
| 124 | + clientset.PrependReactor("create", "configmaps", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) { |
| 125 | + return true, nil, tt.createError |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + // Execute test |
| 130 | + err := InitConfiguration(clientset) |
| 131 | + |
| 132 | + // Verify results |
| 133 | + if tt.expectError && err == nil { |
| 134 | + t.Errorf("expected error but got none") |
| 135 | + } |
| 136 | + if !tt.expectError && err != nil { |
| 137 | + t.Errorf("unexpected error: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + // Verify actions |
| 141 | + actions := clientset.Actions() |
| 142 | + |
| 143 | + // Should always have a get action |
| 144 | + if len(actions) < 1 || actions[0].GetVerb() != "get" { |
| 145 | + t.Errorf("expected get action") |
| 146 | + } |
| 147 | + |
| 148 | + if tt.expectUpdate { |
| 149 | + found := false |
| 150 | + for _, action := range actions { |
| 151 | + if action.GetVerb() == "update" { |
| 152 | + found = true |
| 153 | + break |
| 154 | + } |
| 155 | + } |
| 156 | + if !found { |
| 157 | + t.Errorf("expected update action") |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if tt.expectCreate { |
| 162 | + found := false |
| 163 | + for _, action := range actions { |
| 164 | + if action.GetVerb() == "create" { |
| 165 | + found = true |
| 166 | + break |
| 167 | + } |
| 168 | + } |
| 169 | + if !found && tt.createError == nil { |
| 170 | + t.Errorf("expected create action") |
| 171 | + } |
| 172 | + } |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
0 commit comments