-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathapply_online_test.go
More file actions
276 lines (252 loc) · 7.93 KB
/
apply_online_test.go
File metadata and controls
276 lines (252 loc) · 7.93 KB
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
package apply
import (
"context"
"time"
"go.uber.org/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher/fleet/integrationtests/cli"
"github.com/rancher/fleet/internal/cmd/cli/apply"
"github.com/rancher/fleet/internal/mocks"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
var _ = Describe("Fleet apply online", Label("online"), func() {
var (
ctrl *gomock.Controller
clientMock *mocks.MockK8sClient
name string
dirs []string
options apply.Options
oldBundle *fleet.Bundle
newBundle *fleet.Bundle
listedBundles []fleet.Bundle
)
JustBeforeEach(func() {
// Setting up all the needed mocked interfaces for the test
ctrl = gomock.NewController(GinkgoT())
clientMock = mocks.NewMockK8sClient(ctrl)
clientMock.EXPECT().Get(
gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&fleet.Bundle{}),
).DoAndReturn(
func(_ context.Context, ns types.NamespacedName, bundle *fleet.Bundle, _ ...interface{}) error {
bundle.ObjectMeta = oldBundle.ObjectMeta
bundle.Name = ns.Name
bundle.Namespace = ns.Namespace
bundle.Spec = oldBundle.Spec
return nil
},
).AnyTimes()
// Set listedBundles to a non-empty list to test bundle pruning behaviour (pruneBundlesNotFoundInRepo)
clientMock.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(
func(_ context.Context, l *fleet.BundleList, _ ...interface{}) error {
l.Items = listedBundles
return nil
},
).AnyTimes()
clientMock.EXPECT().Delete(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
// so it does not try to use OCI storage
clientMock.EXPECT().Get(
gomock.Any(), gomock.Any(), gomock.AssignableToTypeOf(&corev1.Secret{}),
).Return(errors.NewNotFound(schema.GroupResource{}, "")).AnyTimes()
})
When("We want to delete a label in the bundle from the cluster", func() {
BeforeEach(func() {
name = "labels_update"
dirs = []string{cli.AssetsPath + "labels_update"}
// bundle in the cluster
oldBundle = &fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"new": "fleet-label2",
"new_changed": "fleet_label_changed",
},
Namespace: "foo",
Name: "test_labels",
},
}
// bundle in the cm.yaml file; some values are autofilled in the implementation (this is why there are not only the labels)
newBundle = &fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"new": "fleet-label2",
},
Namespace: "foo",
Name: "test_labels",
},
Spec: fleet.BundleSpec{
Resources: []fleet.BundleResource{
{
Name: "cm.yaml",
Content: `apiVersion: v1
kind: ConfigMap
metadata:
name: cm3
data:
test: "value23"
`,
},
},
Targets: []fleet.BundleTarget{
{
Name: "default",
ClusterGroup: "default",
Source: "gitrepo",
},
},
},
}
})
It("deletes labels present on the bundle but not in fleet.yaml", func() {
// Update is called with the actual output of the `apply` command here, hence we validate that its argument is what we expect.
clientMock.EXPECT().Update(gomock.Any(), gomock.Any()).DoAndReturn(
func(_ context.Context, bundle *fleet.Bundle, _ ...interface{}) error {
Expect(bundle.Spec).To(Equal(newBundle.Spec))
Expect(bundle.Labels).To(Equal(newBundle.Labels))
return nil
},
)
err := fleetApplyOnline(clientMock, name, dirs, options)
Expect(err).NotTo(HaveOccurred())
})
})
When("A HelmOps bundle already exists in the same namespace", func() {
BeforeEach(func() {
name = "labels_update"
dirs = []string{cli.AssetsPath + "labels_update"}
// bundle in the cluster
oldBundle = &fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "labels_update",
},
Spec: fleet.BundleSpec{
HelmOpOptions: &fleet.BundleHelmOptions{
// values themselves do not matter, as long as Helm options are non-null and the bundle is therefore detected as
// a HelmOps bundle.
SecretName: "foo",
},
},
}
})
It("detects the existing bundle and fails to create the new bundle", func() {
// No update expected here, as checks for existence of an existing bundle should reveal that a
// HelmOps bundle with the same name already exists.
err := fleetApplyOnline(clientMock, name, dirs, options)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("already exists"))
})
})
When("a bundle to be created already exists and is marked for deletion", func() {
BeforeEach(func() {
ts := metav1.NewTime(time.Now())
name = "labels_update"
dirs = []string{cli.AssetsPath + "labels_update"}
// bundle in the cluster
oldBundle = &fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "test_labels",
DeletionTimestamp: &ts, // as long as it's non-nil
},
}
// bundle in the cm.yaml file; some values are autofilled in the implementation (this is why there are not only the labels)
newBundle = &fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"new": "fleet-label2",
},
Namespace: "foo",
Name: "test_labels",
},
Spec: fleet.BundleSpec{
Resources: []fleet.BundleResource{
{
Name: "cm.yaml",
Content: `apiVersion: v1
kind: ConfigMap
metadata:
name: cm3
data:
test: "value23"
`,
},
},
Targets: []fleet.BundleTarget{
{
Name: "default",
ClusterGroup: "default",
},
},
},
}
})
It("does not update the bundle", func() {
// no expected call to update nor updateStatus, as the existing bundle is being deleted
err := fleetApplyOnline(clientMock, name, dirs, options)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("is being deleted"))
})
It("does not update an OCI bundle", func() {
// no expected call to update nor updateStatus, as the existing bundle is being deleted
bkp := options.OCIRegistry.Reference
options.OCIRegistry.Reference = "oci://foo" // non-empty
defer func() { options.OCIRegistry.Reference = bkp }()
err := fleetApplyOnline(clientMock, name, dirs, options)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("is being deleted"))
})
})
When("A bundle with a different name and overlapping resources already exists in the same namespace", func() {
BeforeEach(func() {
name = "labels_update"
dirs = []string{cli.AssetsPath + "labels_update"}
// bundle in the cluster
oldBundle = &fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "labels_update",
},
Spec: fleet.BundleSpec{
Resources: []fleet.BundleResource{
{
Name: "cm.yaml",
Content: `apiVersion: v1
kind: ConfigMap
metadata:
name: cm3
data:
test: "value34"`, // same name, same kind, contents should not matter
},
{
Name: "chart/values.yaml",
Content: "name: ignore-missing-resource",
},
},
},
}
listedBundles = []fleet.Bundle{*oldBundle}
})
It("detects the existing bundle and populates Overwrites on the new bundle", func() {
expectedOverwrites := []fleet.OverwrittenResource{
{
Kind: "ConfigMap",
Name: "cm3",
},
}
clientMock.EXPECT().Update(gomock.Any(), gomock.AssignableToTypeOf(&fleet.Bundle{}), gomock.Any()).DoAndReturn(
func(_ context.Context, b *fleet.Bundle, _ ...interface{}) error {
Expect(b).NotTo(BeNil())
Expect(b.Spec.Overwrites).To(Equal(expectedOverwrites))
return nil
},
)
err := fleetApplyOnline(clientMock, name, dirs, options)
Expect(err).NotTo(HaveOccurred())
})
})
})