-
Notifications
You must be signed in to change notification settings - Fork 4
/
suite_test.go
420 lines (368 loc) · 12.1 KB
/
suite_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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
Copyright 2021 Skyscanner Limited.
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 controllers
import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/fluxcd/pkg/apis/meta"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
syncv1alpha1 "github.com/Skyscanner/applicationset-progressive-sync/api/v1alpha1"
"github.com/Skyscanner/applicationset-progressive-sync/internal/consts"
"github.com/Skyscanner/applicationset-progressive-sync/mocks"
applicationset "github.com/argoproj-labs/applicationset/api/v1alpha1"
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/gitops-engine/pkg/health"
//+kubebuilder:scaffold:imports
)
var (
cancel context.CancelFunc
ctx context.Context
k8sClient client.Client
reconciler *ProgressiveSyncReconciler
testEnv *envtest.Environment
mockedClient mocks.MockArgoCDAppClientCalledWith
)
const Revision = "asdfghjkl"
func init() {
rand.Seed(time.Now().UnixNano())
SetDefaultEventuallyTimeout(5 * time.Second)
SetDefaultEventuallyPollingInterval(1 * time.Second)
utilruntime.Must(syncv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(applicationset.AddToScheme(scheme.Scheme))
utilruntime.Must(argov1alpha1.AddToScheme(scheme.Scheme))
}
func TestMain(m *testing.M) {
var err error
ctx, cancel = context.WithCancel(context.TODO())
log.SetLogger(zap.New(zap.WriteTo(os.Stderr), zap.UseDevMode(true)))
testEnv = &envtest.Environment{
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: []string{
filepath.Join("..", "config", "crd", "bases"),
filepath.Join("..", "hack"),
},
}
cfg, err := testEnv.Start()
if err != nil {
panic(fmt.Sprintf("unabled to start envtest: %v", err))
}
// Uncached client
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
if err != nil {
panic(fmt.Sprintf("unabled to create k8sClient: %v", err))
}
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
})
if err != nil {
panic(fmt.Sprintf("unabled to create k8sManager: %v", err))
}
reconciler = &ProgressiveSyncReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
ArgoCDAppClient: &mockedClient,
}
err = reconciler.SetupWithManager(k8sManager)
if err != nil {
panic(fmt.Sprintf("unabled to create reconciler: %v", err))
}
// Start the manager
go func() {
fmt.Println("starting the manager")
if err := k8sManager.Start(ctx); err != nil {
panic(fmt.Sprintf("unabled to start k8sManager: %v", err))
}
}()
<-k8sManager.Elected()
// Run the tests
code := m.Run()
// Stop the manager and the test environment
cancel()
if err := testEnv.Stop(); err != nil {
panic(fmt.Sprintf("unable to stop the test environment: %v", err))
}
os.Exit(code)
}
var numbers = []rune("1234567890")
// randStringNumber returns n random numbers
func randStringNumber(n int) string {
s := make([]rune, n)
for i := range s {
s[i] = numbers[rand.Intn(len(numbers))]
}
return string(s)
}
// createNamespace creates the target namespace
func createNamespace(name string) (corev1.Namespace, error) {
ns := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: name},
}
return ns, k8sClient.Create(ctx, &ns)
}
// deleteNamespace deletes the target namespace
func deleteNamespace(ns corev1.Namespace) error {
return k8sClient.Delete(ctx, &ns)
}
// newProgressiveSync builds the target ProgressiveSync
func newProgressiveSync(name, namespace, appSet string) syncv1alpha1.ProgressiveSync {
return syncv1alpha1.ProgressiveSync{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: syncv1alpha1.ProgressiveSyncSpec{
AppSetRef: meta.LocalObjectReference{
Name: appSet,
},
},
}
}
// newStage builds the target Stage
func newStage(name string, maxTargets, maxParallel int64, selector metav1.LabelSelector) syncv1alpha1.Stage {
return syncv1alpha1.Stage{
Name: name,
MaxParallel: maxParallel,
MaxTargets: maxTargets,
Targets: syncv1alpha1.Targets{
Clusters: syncv1alpha1.Clusters{
Selector: selector,
},
},
}
}
// createApplicationSet creates the target ApplicationSet
func createApplicationSet(name, namespace string) (applicationset.ApplicationSet, error) {
appSet := applicationset.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: applicationset.ApplicationSetSpec{
Generators: []applicationset.ApplicationSetGenerator{},
},
}
return appSet, k8sClient.Create(ctx, &appSet)
}
// createApplication creates an Application targeting a cluster.
// The name MUST be in the format app_name-account_name-az_name-number.
func createApplication(name, namespace, appSet string) (argov1alpha1.Application, error) {
cluster := strings.Join(strings.Split(name, "-")[1:], "-")
app := argov1alpha1.Application{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
OwnerReferences: []metav1.OwnerReference{{
APIVersion: consts.AppSetAPIVersion,
Kind: consts.AppSetKind,
Name: appSet,
UID: uuid.NewUUID(),
}},
},
Spec: argov1alpha1.ApplicationSpec{
Destination: argov1alpha1.ApplicationDestination{
Server: fmt.Sprintf("https://%s.kubernetes.io", cluster),
Namespace: namespace,
Name: cluster,
}},
Status: argov1alpha1.ApplicationStatus{
Sync: argov1alpha1.SyncStatus{
Status: argov1alpha1.SyncStatusCodeOutOfSync,
Revision: Revision,
},
Health: argov1alpha1.HealthStatus{
Status: health.HealthStatusHealthy,
},
},
}
return app, k8sClient.Create(ctx, &app)
}
// createApplications creates multiple Applications created by the same ApplicationSet
func createApplications(names []string, namespace, appSet string) ([]argov1alpha1.Application, error) {
var apps []argov1alpha1.Application
for _, name := range names {
app, err := createApplication(name, namespace, appSet)
if err != nil {
return nil, err
}
apps = append(apps, app)
}
return apps, nil
}
// setApplicationSyncStatus sets the desired SyncStatusCode in the target Application
func setApplicationSyncStatus(name, namespace string, status argov1alpha1.SyncStatusCode) error {
var app argov1alpha1.Application
if err := k8sClient.Get(ctx,
types.NamespacedName{
Name: name,
Namespace: namespace,
},
&app,
); err != nil {
return err
}
app.Status.Sync.Status = status
if err := k8sClient.Update(ctx, &app); err != nil {
return err
}
return nil
}
// setApplicationHealthStatus sets the desired HealthStatusCode in the target Application
func setApplicationHealthStatus(name, namespace string, health health.HealthStatusCode) error {
var app argov1alpha1.Application
if err := k8sClient.Get(ctx,
types.NamespacedName{
Name: name,
Namespace: namespace,
},
&app,
); err != nil {
return err
}
app.Status.Health.Status = health
if err := k8sClient.Update(ctx, &app); err != nil {
return err
}
return nil
}
// setApplicationRevision sets the desired Revision in the target Application
func setApplicationRevision(name, namespace string, revision string) error {
var app argov1alpha1.Application
if err := k8sClient.Get(ctx,
types.NamespacedName{
Name: name,
Namespace: namespace,
},
&app,
); err != nil {
return err
}
app.Status.Sync.Revision = revision
if err := k8sClient.Update(ctx, &app); err != nil {
return err
}
return nil
}
// createSecret creates a secret with labels.
// The name MUST be in the format account_name-az_name-number,
// for example account1-eu-west-1a-1
func createSecret(name, namespace string) (corev1.Secret, error) {
az := strings.Join(strings.Split(name, "-")[1:len(strings.Split(name, "-"))-1], "-")
region := az[:len(az)-1]
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
consts.ArgoCDSecretTypeLabel: consts.ArgoCDSecretTypeCluster,
"region": region,
"az": az,
"cluster": name,
}},
Data: map[string][]byte{
"server": []byte(fmt.Sprintf("https://%s.kubernetes.io", name)),
},
}
return secret, k8sClient.Create(ctx, &secret)
}
// createSecrets creates multiple secrets
func createSecrets(names []string, namespace string) ([]corev1.Secret, error) {
var secrets []corev1.Secret
for _, name := range names {
secret, err := createSecret(name, namespace)
if err != nil {
return nil, err
}
secrets = append(secrets, secret)
}
return secrets, nil
}
// assertHaveLastSyncedStage ensures the target ProgressiveSync has the desired Stage as LastSyncedStage
func assertHaveLastSyncedStage(g *WithT, ps syncv1alpha1.ProgressiveSync, stage string) {
g.EventuallyWithOffset(1, func() string {
var resultPs syncv1alpha1.ProgressiveSync
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(&ps), &resultPs); err != nil {
return err.Error()
}
return resultPs.Status.LastSyncedStage
}).Should(Equal(stage))
}
// assertHaveLastSyncedStageStatus ensures the target ProgressiveSync has the desired StageStatus as LastSyncedStageStatus
func assertHaveLastSyncedStageStatus(g *WithT, ps syncv1alpha1.ProgressiveSync, status syncv1alpha1.StageStatus) {
g.EventuallyWithOffset(1, func() syncv1alpha1.StageStatus {
var resultPs syncv1alpha1.ProgressiveSync
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(&ps), &resultPs); err != nil {
return ""
}
return resultPs.Status.LastSyncedStageStatus
}).Should(Equal(status))
}
// assertHaveCondition ensures the target ProgressiveSync has the desired condition as true
func assertHaveCondition(g *WithT, ps syncv1alpha1.ProgressiveSync, condition string) {
g.EventuallyWithOffset(1, func() bool {
var resultPs syncv1alpha1.ProgressiveSync
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(&ps), &resultPs); err != nil {
return false
}
return apimeta.IsStatusConditionTrue(*resultPs.GetStatusConditions(), condition)
}).Should(BeTrue(), "assertion failed because the condition "+condition+" is false")
}
// assertHaveSyncedApp ensures the controller synced the target Application
func assertHaveSyncedApp(g *WithT, name string) {
g.EventuallyWithOffset(1, func() bool {
for _, app := range mockedClient.GetSyncedApps() {
if name == app {
return true
}
}
return false
}).Should(BeTrue(), "assertion failed because the controller didn't sync the app "+name)
}
// assertHaveSyncedApp ensures the controller didn't sync the target Application
func assertHaveNotSyncedApp(g *WithT, name string) {
g.EventuallyWithOffset(1, func() bool {
for _, app := range mockedClient.GetSyncedApps() {
if name == app {
return true
}
}
return false
}).ShouldNot(BeTrue(), "assertion failed because the controller synced the app "+name)
}
// assertHaveSyncedAtStage ensures the target Application synced at the desired Stage
func assertHaveSyncedAtStage(g *WithT, ps syncv1alpha1.ProgressiveSync, app, stage string) {
g.EventuallyWithOffset(1, func() string {
stateMap, err := reconciler.ReadStateMap(ctx, ps)
if err != nil {
return err.Error()
}
return stateMap.Apps[app].SyncedAtStage
}).Should(Equal(stage), "assertion failed because the app "+app+" didn't sync at stage "+stage)
}