|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes 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 mutators |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "testing" |
| 23 | + |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | +) |
| 28 | + |
| 29 | +func TestApplyMutators(t *testing.T) { |
| 30 | + ctx := context.Background() |
| 31 | + |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + resources []runtime.RawExtension |
| 35 | + mutators []ResourcesMutator |
| 36 | + expected []*unstructured.Unstructured |
| 37 | + expectedErr error |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "no mutators", |
| 41 | + resources: []runtime.RawExtension{ |
| 42 | + {Raw: []byte(`{"apiVersion": "v1", "kind": "SomeObject"}`)}, |
| 43 | + }, |
| 44 | + expected: []*unstructured.Unstructured{ |
| 45 | + {Object: map[string]interface{}{"apiVersion": "v1", "kind": "SomeObject"}}, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "mutators apply in order", |
| 50 | + resources: []runtime.RawExtension{ |
| 51 | + {Raw: []byte(`{"apiVersion": "v1", "kind": "SomeObject"}`)}, |
| 52 | + }, |
| 53 | + mutators: []ResourcesMutator{ |
| 54 | + func(_ context.Context, us []*unstructured.Unstructured) error { |
| 55 | + us[0].Object["f1"] = "3" |
| 56 | + us[0].Object["f2"] = "3" |
| 57 | + us[0].Object["f3"] = "3" |
| 58 | + return nil |
| 59 | + }, |
| 60 | + func(_ context.Context, us []*unstructured.Unstructured) error { |
| 61 | + us[0].Object["f1"] = "2" |
| 62 | + us[0].Object["f2"] = "2" |
| 63 | + return nil |
| 64 | + }, |
| 65 | + func(_ context.Context, us []*unstructured.Unstructured) error { |
| 66 | + us[0].Object["f1"] = "1" |
| 67 | + return nil |
| 68 | + }, |
| 69 | + }, |
| 70 | + expected: []*unstructured.Unstructured{ |
| 71 | + { |
| 72 | + Object: map[string]interface{}{ |
| 73 | + "apiVersion": "v1", |
| 74 | + "kind": "SomeObject", |
| 75 | + "f1": "1", |
| 76 | + "f2": "2", |
| 77 | + "f3": "3", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "error", |
| 84 | + resources: []runtime.RawExtension{}, |
| 85 | + mutators: []ResourcesMutator{ |
| 86 | + func(_ context.Context, us []*unstructured.Unstructured) error { |
| 87 | + return errors.New("mutator err") |
| 88 | + }, |
| 89 | + }, |
| 90 | + expectedErr: errors.New("mutator err"), |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + for _, test := range tests { |
| 95 | + t.Run(test.name, func(t *testing.T) { |
| 96 | + g := NewGomegaWithT(t) |
| 97 | + |
| 98 | + actual, err := ApplyMutators(ctx, test.resources, test.mutators...) |
| 99 | + if test.expectedErr != nil { |
| 100 | + g.Expect(err).To(MatchError(test.expectedErr)) |
| 101 | + } else { |
| 102 | + g.Expect(err).NotTo(HaveOccurred()) |
| 103 | + } |
| 104 | + g.Expect(actual).To(Equal(test.expected)) |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments