|
| 1 | +/* |
| 2 | +Copyright 2025 The KusionStack 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 client |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/stretchr/testify/suite" |
| 21 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 23 | +) |
| 24 | + |
| 25 | +type mapsTestSuite struct { |
| 26 | + suite.Suite |
| 27 | +} |
| 28 | + |
| 29 | +func (s *mapsTestSuite) TestMutateLabelsAndAnnotations() { |
| 30 | + type testCase struct { |
| 31 | + name string |
| 32 | + setupFunc func() client.Object |
| 33 | + mutateLabels func(labels map[string]string) |
| 34 | + expectedLabels map[string]string |
| 35 | + mutateAnnotations func(annotations map[string]string) |
| 36 | + expectedAnnotations map[string]string |
| 37 | + } |
| 38 | + |
| 39 | + testCases := []testCase{ |
| 40 | + { |
| 41 | + name: "mutate pod labels and annotations", |
| 42 | + setupFunc: func() client.Object { |
| 43 | + return newTestRandomPod() |
| 44 | + }, |
| 45 | + mutateLabels: func(labels map[string]string) { |
| 46 | + labels["mutate-labels"] = "test" |
| 47 | + }, |
| 48 | + expectedLabels: map[string]string{ |
| 49 | + "version": "v1", |
| 50 | + "mutate-labels": "test", |
| 51 | + }, |
| 52 | + mutateAnnotations: func(annotations map[string]string) { |
| 53 | + annotations["mutate-annotations"] = "test" |
| 54 | + }, |
| 55 | + expectedAnnotations: map[string]string{ |
| 56 | + "mutate-annotations": "test", |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "mutate unstructured labels and annotations", |
| 61 | + setupFunc: func() client.Object { |
| 62 | + return &unstructured.Unstructured{Object: map[string]any{}} |
| 63 | + }, |
| 64 | + mutateLabels: func(labels map[string]string) { |
| 65 | + labels["mutate-labels"] = "test" |
| 66 | + }, |
| 67 | + expectedLabels: map[string]string{ |
| 68 | + "mutate-labels": "test", |
| 69 | + }, |
| 70 | + mutateAnnotations: func(annotations map[string]string) { |
| 71 | + annotations["mutate-annotations"] = "test" |
| 72 | + }, |
| 73 | + expectedAnnotations: map[string]string{ |
| 74 | + "mutate-annotations": "test", |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "keep nil annotation", |
| 79 | + setupFunc: func() client.Object { |
| 80 | + return newTestRandomPod() |
| 81 | + }, |
| 82 | + mutateAnnotations: func(annotations map[string]string) { |
| 83 | + }, |
| 84 | + expectedAnnotations: nil, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "keep empty labels", |
| 88 | + setupFunc: func() client.Object { |
| 89 | + return newTestRandomPod() |
| 90 | + }, |
| 91 | + mutateLabels: func(labels map[string]string) { |
| 92 | + delete(labels, "version") |
| 93 | + }, |
| 94 | + expectedLabels: map[string]string{}, |
| 95 | + }, |
| 96 | + } |
| 97 | + for i := range testCases { |
| 98 | + tc := testCases[i] |
| 99 | + s.Run(tc.name, func() { |
| 100 | + obj := tc.setupFunc() |
| 101 | + |
| 102 | + if tc.mutateLabels != nil { |
| 103 | + MutateLabels(obj, tc.mutateLabels) |
| 104 | + s.Equal(tc.expectedLabels, obj.GetLabels()) |
| 105 | + } |
| 106 | + |
| 107 | + if tc.mutateAnnotations != nil { |
| 108 | + MutateAnnotations(obj, tc.mutateAnnotations) |
| 109 | + s.Equal(tc.expectedAnnotations, obj.GetAnnotations()) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (s *mapsTestSuite) TestGetMapValueByDefault() { |
| 116 | + type testCase struct { |
| 117 | + name string |
| 118 | + m map[string]string |
| 119 | + key string |
| 120 | + defValue string |
| 121 | + expected string |
| 122 | + } |
| 123 | + |
| 124 | + testCases := []testCase{ |
| 125 | + { |
| 126 | + name: "get value by key", |
| 127 | + m: map[string]string{"key": "value"}, |
| 128 | + key: "key", |
| 129 | + expected: "value", |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "get value by default", |
| 133 | + m: map[string]string{}, |
| 134 | + key: "key", |
| 135 | + defValue: "default", |
| 136 | + expected: "default", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "get value from nil map", |
| 140 | + m: nil, |
| 141 | + key: "key", |
| 142 | + defValue: "default", |
| 143 | + expected: "default", |
| 144 | + }, |
| 145 | + } |
| 146 | + for i := range testCases { |
| 147 | + tc := testCases[i] |
| 148 | + s.Run(tc.name, func() { |
| 149 | + s.Equal(tc.expected, GetMapValueByDefault(tc.m, tc.key, tc.defValue)) |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments