|
| 1 | +/* |
| 2 | +Copyright 2022 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ctrlutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 30 | + |
| 31 | + workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1" |
| 32 | + workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" |
| 33 | + "github.com/karmada-io/karmada/pkg/util" |
| 34 | +) |
| 35 | + |
| 36 | +func TestCreateOrUpdateWork(t *testing.T) { |
| 37 | + scheme := runtime.NewScheme() |
| 38 | + assert.NoError(t, workv1alpha1.Install(scheme)) |
| 39 | + assert.NoError(t, workv1alpha2.Install(scheme)) |
| 40 | + |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + existingWork *workv1alpha1.Work |
| 44 | + workMeta metav1.ObjectMeta |
| 45 | + resource *unstructured.Unstructured |
| 46 | + wantErr bool |
| 47 | + verify func(*testing.T, client.Client) |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "create new work", |
| 51 | + workMeta: metav1.ObjectMeta{ |
| 52 | + Namespace: "default", |
| 53 | + Name: "test-work", |
| 54 | + }, |
| 55 | + resource: &unstructured.Unstructured{ |
| 56 | + Object: map[string]interface{}{ |
| 57 | + "apiVersion": "apps/v1", |
| 58 | + "kind": "Deployment", |
| 59 | + "metadata": map[string]interface{}{ |
| 60 | + "name": "test-deployment", |
| 61 | + "uid": "test-uid", |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + verify: func(t *testing.T, c client.Client) { |
| 66 | + work := &workv1alpha1.Work{} |
| 67 | + err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work) |
| 68 | + assert.NoError(t, err) |
| 69 | + assert.Equal(t, "test-work", work.Name) |
| 70 | + assert.Equal(t, 1, len(work.Spec.Workload.Manifests)) |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "create work with PropagationInstruction", |
| 75 | + workMeta: metav1.ObjectMeta{ |
| 76 | + Namespace: "default", |
| 77 | + Name: "test-work", |
| 78 | + Labels: map[string]string{ |
| 79 | + util.PropagationInstruction: "some-value", |
| 80 | + }, |
| 81 | + Annotations: map[string]string{ |
| 82 | + workv1alpha2.ResourceConflictResolutionAnnotation: "overwrite", |
| 83 | + }, |
| 84 | + }, |
| 85 | + resource: &unstructured.Unstructured{ |
| 86 | + Object: map[string]interface{}{ |
| 87 | + "apiVersion": "apps/v1", |
| 88 | + "kind": "Deployment", |
| 89 | + "metadata": map[string]interface{}{ |
| 90 | + "name": "test-deployment", |
| 91 | + "uid": "test-uid", |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + verify: func(t *testing.T, c client.Client) { |
| 96 | + work := &workv1alpha1.Work{} |
| 97 | + err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work) |
| 98 | + assert.NoError(t, err) |
| 99 | + |
| 100 | + // Get the resource from manifests |
| 101 | + manifest := &unstructured.Unstructured{} |
| 102 | + err = manifest.UnmarshalJSON(work.Spec.Workload.Manifests[0].Raw) |
| 103 | + assert.NoError(t, err) |
| 104 | + |
| 105 | + // Verify labels and annotations were set |
| 106 | + labels := manifest.GetLabels() |
| 107 | + assert.Equal(t, util.ManagedByKarmadaLabelValue, labels[util.ManagedByKarmadaLabel]) |
| 108 | + |
| 109 | + annotations := manifest.GetAnnotations() |
| 110 | + assert.Equal(t, "test-uid", annotations[workv1alpha2.ResourceTemplateUIDAnnotation]) |
| 111 | + assert.Equal(t, "test-work", annotations[workv1alpha2.WorkNameAnnotation]) |
| 112 | + assert.Equal(t, "default", annotations[workv1alpha2.WorkNamespaceAnnotation]) |
| 113 | + assert.Equal(t, "overwrite", annotations[workv1alpha2.ResourceConflictResolutionAnnotation]) |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "create work with PropagationInstructionSuppressed", |
| 118 | + workMeta: metav1.ObjectMeta{ |
| 119 | + Namespace: "default", |
| 120 | + Name: "test-work", |
| 121 | + Labels: map[string]string{ |
| 122 | + util.PropagationInstruction: util.PropagationInstructionSuppressed, |
| 123 | + }, |
| 124 | + }, |
| 125 | + resource: &unstructured.Unstructured{ |
| 126 | + Object: map[string]interface{}{ |
| 127 | + "apiVersion": "apps/v1", |
| 128 | + "kind": "Deployment", |
| 129 | + "metadata": map[string]interface{}{ |
| 130 | + "name": "test-deployment", |
| 131 | + "uid": "test-uid", |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + verify: func(t *testing.T, c client.Client) { |
| 136 | + work := &workv1alpha1.Work{} |
| 137 | + err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work) |
| 138 | + assert.NoError(t, err) |
| 139 | + |
| 140 | + // Get the resource from manifests |
| 141 | + manifest := &unstructured.Unstructured{} |
| 142 | + err = manifest.UnmarshalJSON(work.Spec.Workload.Manifests[0].Raw) |
| 143 | + assert.NoError(t, err) |
| 144 | + |
| 145 | + // Verify labels and annotations were NOT set |
| 146 | + labels := manifest.GetLabels() |
| 147 | + assert.Empty(t, labels[util.ManagedByKarmadaLabel]) |
| 148 | + |
| 149 | + annotations := manifest.GetAnnotations() |
| 150 | + assert.Empty(t, annotations[workv1alpha2.ResourceTemplateUIDAnnotation]) |
| 151 | + }, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "update existing work", |
| 155 | + existingWork: &workv1alpha1.Work{ |
| 156 | + ObjectMeta: metav1.ObjectMeta{ |
| 157 | + Namespace: "default", |
| 158 | + Name: "test-work", |
| 159 | + }, |
| 160 | + }, |
| 161 | + workMeta: metav1.ObjectMeta{ |
| 162 | + Namespace: "default", |
| 163 | + Name: "test-work", |
| 164 | + }, |
| 165 | + resource: &unstructured.Unstructured{ |
| 166 | + Object: map[string]interface{}{ |
| 167 | + "apiVersion": "apps/v1", |
| 168 | + "kind": "Deployment", |
| 169 | + "metadata": map[string]interface{}{ |
| 170 | + "name": "test-deployment", |
| 171 | + "uid": "test-uid", |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + verify: func(t *testing.T, c client.Client) { |
| 176 | + work := &workv1alpha1.Work{} |
| 177 | + err := c.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-work"}, work) |
| 178 | + assert.NoError(t, err) |
| 179 | + assert.Equal(t, 1, len(work.Spec.Workload.Manifests)) |
| 180 | + }, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "error when work is being deleted", |
| 184 | + existingWork: &workv1alpha1.Work{ |
| 185 | + ObjectMeta: metav1.ObjectMeta{ |
| 186 | + Namespace: "default", |
| 187 | + Name: "test-work", |
| 188 | + DeletionTimestamp: &metav1.Time{Time: time.Now()}, |
| 189 | + Finalizers: []string{"test.finalizer.io"}, // Finalizer to satisfy fake client requirement |
| 190 | + }, |
| 191 | + Spec: workv1alpha1.WorkSpec{ |
| 192 | + Workload: workv1alpha1.WorkloadTemplate{ |
| 193 | + Manifests: []workv1alpha1.Manifest{ |
| 194 | + { |
| 195 | + RawExtension: runtime.RawExtension{ |
| 196 | + Raw: []byte(`{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"test-deployment"}}`), |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + workMeta: metav1.ObjectMeta{ |
| 204 | + Namespace: "default", |
| 205 | + Name: "test-work", |
| 206 | + }, |
| 207 | + resource: &unstructured.Unstructured{ |
| 208 | + Object: map[string]interface{}{ |
| 209 | + "apiVersion": "apps/v1", |
| 210 | + "kind": "Deployment", |
| 211 | + "metadata": map[string]interface{}{ |
| 212 | + "name": "test-deployment", |
| 213 | + }, |
| 214 | + }, |
| 215 | + }, |
| 216 | + wantErr: true, |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + for _, tt := range tests { |
| 221 | + t.Run(tt.name, func(t *testing.T) { |
| 222 | + c := fake.NewClientBuilder().WithScheme(scheme) |
| 223 | + if tt.existingWork != nil { |
| 224 | + c = c.WithObjects(tt.existingWork) |
| 225 | + } |
| 226 | + client := c.Build() |
| 227 | + |
| 228 | + err := CreateOrUpdateWork(context.TODO(), client, tt.workMeta, tt.resource) |
| 229 | + |
| 230 | + if tt.wantErr { |
| 231 | + assert.Error(t, err) |
| 232 | + return |
| 233 | + } |
| 234 | + assert.NoError(t, err) |
| 235 | + if tt.verify != nil { |
| 236 | + tt.verify(t, client) |
| 237 | + } |
| 238 | + }) |
| 239 | + } |
| 240 | +} |
0 commit comments