forked from fluxcd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_apply_test.go
289 lines (244 loc) · 8 KB
/
manager_apply_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright 2021 Stefan Prodan
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ssa
import (
"context"
"encoding/base64"
"fmt"
"sort"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func TestApply(t *testing.T) {
timeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
id := generateName("apply")
objects, err := readManifest("testdata/test1.yaml", id)
if err != nil {
t.Fatal(err)
}
manager.SetOwnerLabels(objects, "app1", "default")
configMapName, configMap := getFirstObject(objects, "ConfigMap", id)
secretName, secret := getFirstObject(objects, "Secret", id)
crbName, crb := getFirstObject(objects, "ClusterRoleBinding", id)
t.Run("creates objects in order", func(t *testing.T) {
// create objects
changeSet, err := manager.ApplyAllStaged(ctx, objects, false, timeout)
if err != nil {
t.Fatal(err)
}
// expected created order
sort.Sort(SortableUnstructureds(objects))
var expected []string
for _, object := range objects {
expected = append(expected, FmtUnstructured(object))
}
// verify the change set contains only created actions
var output []string
for _, entry := range changeSet.Entries {
if diff := cmp.Diff(entry.Action, string(CreatedAction)); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
output = append(output, entry.Subject)
}
// verify the change set contains all objects in the right order
if diff := cmp.Diff(expected, output); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
})
t.Run("does not apply unchanged objects", func(t *testing.T) {
// no-op apply
changeSet, err := manager.ApplyAllStaged(ctx, objects, false, timeout)
if err != nil {
t.Fatal(err)
}
// verify the change set contains only unchanged actions
var output []string
for _, entry := range changeSet.Entries {
if diff := cmp.Diff(string(UnchangedAction), entry.Action); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s\n%v", diff, changeSet)
}
output = append(output, entry.Subject)
}
})
t.Run("applies only changed objects", func(t *testing.T) {
// update a value in the configmap
err = unstructured.SetNestedField(configMap.Object, "val", "data", "key")
if err != nil {
t.Fatal(err)
}
// apply changes
changeSet, err := manager.ApplyAllStaged(ctx, objects, false, timeout)
if err != nil {
t.Fatal(err)
}
// verify the change set contains the configured action only for the configmap
for _, entry := range changeSet.Entries {
if entry.Subject == configMapName {
if diff := cmp.Diff(string(ConfiguredAction), entry.Action); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
} else {
if diff := cmp.Diff(string(UnchangedAction), entry.Action); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
}
}
// get the configmap from cluster
configMapClone := configMap.DeepCopy()
err = manager.client.Get(ctx, client.ObjectKeyFromObject(configMapClone), configMapClone)
if err != nil {
t.Fatal(err)
}
// get data value from the in-cluster configmap
val, _, err := unstructured.NestedFieldCopy(configMapClone.Object, "data", "key")
if err != nil {
t.Fatal(err)
}
// verify the configmap was updated in cluster with the right data value
if diff := cmp.Diff(val, "val"); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
})
t.Run("fails to apply immutable secret", func(t *testing.T) {
// update a value in the secret
err = unstructured.SetNestedField(secret.Object, "val-secret", "stringData", "key")
if err != nil {
t.Fatal(err)
}
// apply and expect to fail
_, err := manager.ApplyAllStaged(ctx, objects, false, timeout)
if err == nil {
t.Fatal("Expected error got none")
}
// verify that the error message does not contain sensitive information
expectedErr := fmt.Sprintf("%s invalid, error: secret is immutable", FmtUnstructured(secret))
if diff := cmp.Diff(expectedErr, err.Error()); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
})
t.Run("force applies immutable secret", func(t *testing.T) {
// force apply
changeSet, err := manager.ApplyAllStaged(ctx, objects, true, timeout)
if err != nil {
t.Fatal(err)
}
// verify the secret was recreated
for _, entry := range changeSet.Entries {
if entry.Subject == secretName {
if diff := cmp.Diff(string(CreatedAction), entry.Action); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
} else {
if diff := cmp.Diff(string(UnchangedAction), entry.Action); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
}
}
// get the secret from cluster
secretClone := secret.DeepCopy()
err = manager.client.Get(ctx, client.ObjectKeyFromObject(secretClone), secretClone)
if err != nil {
t.Fatal(err)
}
// get data value from the in-cluster secret
val, _, err := unstructured.NestedFieldCopy(secretClone.Object, "data", "key")
if err != nil {
t.Fatal(err)
}
// verify the secret was updated in cluster with the right data value
if diff := cmp.Diff(val, base64.StdEncoding.EncodeToString([]byte("val-secret"))); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
})
t.Run("recreates immutable RBAC", func(t *testing.T) {
// update roleRef
err = unstructured.SetNestedField(crb.Object, "test", "roleRef", "name")
if err != nil {
t.Fatal(err)
}
// force apply
changeSet, err := manager.ApplyAllStaged(ctx, objects, true, timeout)
if err != nil {
t.Fatal(err)
}
// verify the binding was recreated
for _, entry := range changeSet.Entries {
if entry.Subject == crbName {
if diff := cmp.Diff(string(CreatedAction), entry.Action); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
}
break
}
}
})
}
func TestApply_SetNativeKindsDefaults(t *testing.T) {
timeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
id := generateName("fix")
objects, err := readManifest("testdata/test2.yaml", id)
if err != nil {
t.Fatal(err)
}
manager.SetOwnerLabels(objects, "app1", "default")
if err := SetNativeKindsDefaults(objects); err != nil {
t.Fatal(err)
}
t.Run("creates objects", func(t *testing.T) {
// create objects
_, err := manager.ApplyAllStaged(ctx, objects, false, timeout)
if err != nil {
t.Fatal(err)
}
})
}
func TestApply_NoOp(t *testing.T) {
timeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
id := generateName("fix")
objects, err := readManifest("testdata/test3.yaml", id)
if err != nil {
t.Fatal(err)
}
manager.SetOwnerLabels(objects, "app1", "default")
if err := SetNativeKindsDefaults(objects); err != nil {
t.Fatal(err)
}
t.Run("creates objects", func(t *testing.T) {
// create objects
_, err := manager.ApplyAllStaged(ctx, objects, false, timeout)
if err != nil {
t.Fatal(err)
}
})
t.Run("skips apply", func(t *testing.T) {
// apply changes
changeSet, err := manager.ApplyAll(ctx, objects, false)
if err != nil {
t.Fatal(err)
}
for _, entry := range changeSet.Entries {
if entry.Action != string(UnchangedAction) {
t.Errorf("Diff found for %s", entry.String())
}
}
})
}