-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcontrollers_suite_test.go
472 lines (397 loc) · 17.1 KB
/
controllers_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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
Copyright 2022 The Kubernetes 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 controllers_test
import (
"context"
"flag"
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/cluster-api-provider-cloudstack/test/fakes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/go-logr/logr"
"github.com/golang/mock/gomock"
ginkgo "github.com/onsi/ginkgo/v2"
gomega "github.com/onsi/gomega"
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
csReconcilers "sigs.k8s.io/cluster-api-provider-cloudstack/controllers"
csCtrlrUtils "sigs.k8s.io/cluster-api-provider-cloudstack/controllers/utils"
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/mocks"
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
//+kubebuilder:scaffold:imports
)
func TestAPIs(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Controller Suite")
}
var (
clusterAPIVersionRegex = regexp.MustCompile(`^(\W)sigs.k8s.io/cluster-api v(.+)`)
)
const (
timeout = 10 * time.Second
pollInterval = 1 * time.Second
fakeEventBufferSize = 10
)
func envOr(envKey, defaultValue string) string {
if value, ok := os.LookupEnv(envKey); ok {
return value
}
return defaultValue
}
// Have to get the path to the installed CAPI to inject CAPI CRDs.
func getFilePathToCAPICRDs(root string) string {
modBits, err := os.ReadFile(filepath.Join(root, "go.mod"))
if err != nil {
return ""
}
var clusterAPIVersion string
for _, line := range strings.Split(string(modBits), "\n") {
matches := clusterAPIVersionRegex.FindStringSubmatch(line)
if len(matches) == 3 {
clusterAPIVersion = matches[2]
}
}
if clusterAPIVersion == "" {
return ""
}
gopath := envOr("GOPATH", build.Default.GOPATH)
return filepath.Join(gopath, "pkg", "mod", "sigs.k8s.io",
fmt.Sprintf("cluster-api@v%s", clusterAPIVersion), "config", "crd", "bases")
}
var (
// TestEnv vars...
testEnv *envtest.Environment
k8sClient client.Client
ctx context.Context
cancel context.CancelFunc
k8sManager manager.Manager
cfg *rest.Config
logger logr.Logger
fakeCtrlClient client.Client
fakeRecorder *record.FakeRecorder
// Mock Vars.
mockCtrl *gomock.Controller
mockCloudClient *mocks.MockClient
mockCSAPIClient *cloudstack.CloudStackClient
// Reconcilers
MachineReconciler *csReconcilers.CloudStackMachineReconciler
ClusterReconciler *csReconcilers.CloudStackClusterReconciler
FailureDomainReconciler *csReconcilers.CloudStackFailureDomainReconciler
IsoNetReconciler *csReconcilers.CloudStackIsoNetReconciler
AffinityGReconciler *csReconcilers.CloudStackAffinityGroupReconciler
// CKS Reconcilers
CksClusterReconciler *csReconcilers.CksClusterReconciler
CksMachineReconciler *csReconcilers.CksMachineReconciler
)
var _ = ginkgo.BeforeSuite(func() {
repoRoot := os.Getenv("REPO_ROOT")
// Add ginkgo recover statements to controllers.
cmd := exec.Command(repoRoot+"/hack/testing_ginkgo_recover_statements.sh", "--contains")
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Println(errors.Wrapf(err, "refusing to run test suite without ginkgo recover statements present"))
os.Exit(1)
}
ginkgo.By("bootstrapping test environment")
gomega.Ω(infrav1.AddToScheme(scheme.Scheme)).Should(gomega.Succeed())
gomega.Ω(clusterv1.AddToScheme(scheme.Scheme)).Should(gomega.Succeed())
gomega.Ω(fakes.AddToScheme(scheme.Scheme)).Should(gomega.Succeed())
// Increase log verbosity.
klog.InitFlags(nil)
gomega.Ω(flag.Lookup("v").Value.Set("1")).Should(gomega.Succeed())
flag.Parse()
logger = klog.Background()
})
// A mock fo the CloudClient interface used in controller utils.
type MockCtrlrCloudClientImplementation struct {
*csCtrlrUtils.ReconciliationRunner
csCtrlrUtils.CloudClientExtension
}
// AsFailureDomainUser is a method used in the reconciliation runner to set up the CloudStack client. Using this here
// just sets the CSClient to a mock client.
func (m *MockCtrlrCloudClientImplementation) AsFailureDomainUser(
*infrav1.CloudStackFailureDomainSpec) csCtrlrUtils.CloudStackReconcilerMethod {
return func() (ctrl.Result, error) {
m.CSUser = mockCloudClient
return ctrl.Result{}, nil
}
}
func (m *MockCtrlrCloudClientImplementation) RegisterExtension(r *csCtrlrUtils.ReconciliationRunner) csCtrlrUtils.CloudClientExtension {
return &MockCtrlrCloudClientImplementation{ReconciliationRunner: r}
}
func SetupTestEnvironment() {
repoRoot := os.Getenv("REPO_ROOT")
crdPaths := []string{filepath.Join(repoRoot, "config", "crd", "bases"), filepath.Join(repoRoot, "test", "fakes")}
// Append CAPI CRDs path
if capiPath := getFilePathToCAPICRDs(repoRoot); capiPath != "" {
crdPaths = append(crdPaths, capiPath)
}
testEnv = &envtest.Environment{
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: crdPaths,
}
var err error
done := make(chan interface{})
go func() {
defer ginkgo.GinkgoRecover()
cfg, err = testEnv.Start()
close(done)
}()
gomega.Eventually(done).WithTimeout(time.Minute).Should(gomega.BeClosed())
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
gomega.Ω(cfg).ShouldNot(gomega.BeNil())
//+kubebuilder:scaffold:scheme
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
gomega.Ω(k8sClient).ShouldNot(gomega.BeNil())
k8sManager, _ = ctrl.NewManager(cfg, ctrl.Options{Scheme: scheme.Scheme})
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
// Base reconciler shared across reconcilers.
base := csCtrlrUtils.ReconcilerBase{
K8sClient: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
CSClient: mockCloudClient,
BaseLogger: logger,
CloudClientExtension: &MockCtrlrCloudClientImplementation{},
}
// Setup each specific reconciler.
ClusterReconciler = &csReconcilers.CloudStackClusterReconciler{ReconcilerBase: base}
MachineReconciler = &csReconcilers.CloudStackMachineReconciler{ReconcilerBase: base}
FailureDomainReconciler = &csReconcilers.CloudStackFailureDomainReconciler{ReconcilerBase: base}
IsoNetReconciler = &csReconcilers.CloudStackIsoNetReconciler{ReconcilerBase: base}
AffinityGReconciler = &csReconcilers.CloudStackAffinityGroupReconciler{ReconcilerBase: base}
CksClusterReconciler = &csReconcilers.CksClusterReconciler{ReconcilerBase: base}
CksMachineReconciler = &csReconcilers.CksMachineReconciler{ReconcilerBase: base}
ctx, cancel = context.WithCancel(context.TODO())
// Setup mock clients.
mockCSAPIClient = cloudstack.NewMockClient(mockCtrl)
mockCloudClient = mocks.NewMockClient(mockCtrl)
// Set on reconcilers. The mock client wasn't available at suite startup, so set it now.
ClusterReconciler.CSClient = mockCloudClient
IsoNetReconciler.CSClient = mockCloudClient
MachineReconciler.CSClient = mockCloudClient
AffinityGReconciler.CSClient = mockCloudClient
FailureDomainReconciler.CSClient = mockCloudClient
CksClusterReconciler.CSClient = mockCloudClient
CksMachineReconciler.CSClient = mockCloudClient
setupClusterCRDs()
// See reconciliation results. Left commented as it's noisy otherwise.
// TODO: find a way to see controller output without the additional setup output.
ctrl.SetLogger(logger)
ginkgo.DeferCleanup(func() {
// Cancelling the context shuts down any outstanding requests and the test environment.
cancel()
gomega.Ω(testEnv.Stop()).Should(gomega.Succeed())
k8sManager = nil
})
}
// Sets up a fake k8s controller runtime client with CAPI and CloudStack clusters.
func setupFakeTestClient() {
dummies.SetDummyVars()
// Make a fake k8s client with CloudStack and CAPI cluster.
fakeCtrlClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).
WithObjects(dummies.CSCluster, dummies.CAPICluster).
WithStatusSubresource(dummies.CSCluster, dummies.CSMachine1).Build()
fakeRecorder = record.NewFakeRecorder(fakeEventBufferSize)
// Setup mock clients.
mockCSAPIClient = cloudstack.NewMockClient(mockCtrl)
mockCloudClient = mocks.NewMockClient(mockCtrl)
// Base reconciler shared across reconcilers.
base := csCtrlrUtils.ReconcilerBase{
K8sClient: fakeCtrlClient,
Scheme: scheme.Scheme,
CSClient: mockCloudClient,
BaseLogger: logger,
Recorder: fakeRecorder,
CloudClientExtension: &MockCtrlrCloudClientImplementation{},
}
ctx, cancel = context.WithCancel(context.TODO())
// Setup each specific reconciler.
ClusterReconciler = &csReconcilers.CloudStackClusterReconciler{ReconcilerBase: base}
MachineReconciler = &csReconcilers.CloudStackMachineReconciler{ReconcilerBase: base}
FailureDomainReconciler = &csReconcilers.CloudStackFailureDomainReconciler{ReconcilerBase: base}
IsoNetReconciler = &csReconcilers.CloudStackIsoNetReconciler{ReconcilerBase: base}
AffinityGReconciler = &csReconcilers.CloudStackAffinityGroupReconciler{ReconcilerBase: base}
// Set on reconcilers. The mock client wasn't available at suite startup, so set it now.
ClusterReconciler.CSClient = mockCloudClient
IsoNetReconciler.CSClient = mockCloudClient
MachineReconciler.CSClient = mockCloudClient
FailureDomainReconciler.CSClient = mockCloudClient
AffinityGReconciler.CSClient = mockCloudClient
ginkgo.DeferCleanup(func() {
cancel()
})
}
// Setup and teardown on a per test basis.
var _ = ginkgo.BeforeEach(func() {
dummies.SetDummyVars()
mockCtrl = gomock.NewController(ginkgo.GinkgoT())
})
var _ = ginkgo.JustBeforeEach(func() {
if k8sManager != nil { // Allow skipping a test environment for tests that don't need it.
// Launch the k8s manager.
// Needs to be in JustBeforeEach() so individual contexts can register controllers first.
go func() {
defer ginkgo.GinkgoRecover()
gomega.Ω(k8sManager.Start(ctx)).Should(gomega.Succeed(), "failed to run manager")
}()
}
})
var _ = ginkgo.AfterEach(func() {
// Finishing mockCtrl checks expected calls on mock objects matched.
mockCtrl.Finish()
})
var _ = ginkgo.AfterSuite(func() {})
// setClusterReady patches the cluster with ready status true.
func setClusterReady(client client.Client) {
gomega.Eventually(func() error {
ph, err := patch.NewHelper(dummies.CSCluster, client)
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
dummies.CSCluster.Status.Ready = true
return ph.Patch(ctx, dummies.CSCluster, patch.WithStatusObservedGeneration{})
}, timeout).Should(gomega.Succeed())
}
// setupClusterCRDs creates a CAPI and CloudStack cluster with an appropriate ownership ref between them.
func setupClusterCRDs() {
// Create them.
gomega.Ω(k8sClient.Create(ctx, dummies.CAPICluster)).Should(gomega.Succeed())
gomega.Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(gomega.Succeed())
// Fetch the CS Cluster that was created.
key := client.ObjectKey{Namespace: dummies.CSCluster.Namespace, Name: dummies.CSCluster.Name}
gomega.Eventually(func() error {
return k8sClient.Get(ctx, key, dummies.CSCluster)
}, timeout).Should(gomega.BeNil())
// Set owner ref from CAPI cluster to CS Cluster and patch back the CS Cluster.
gomega.Eventually(func() error {
ph, err := patch.NewHelper(dummies.CSCluster, k8sClient)
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
dummies.CSCluster.OwnerReferences = append(dummies.CSCluster.OwnerReferences, metav1.OwnerReference{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
Name: dummies.CAPICluster.Name,
UID: "uniqueness",
})
return ph.Patch(ctx, dummies.CSCluster, patch.WithStatusObservedGeneration{})
}, timeout).Should(gomega.Succeed())
}
// setupMachineCRDs creates a CAPI and CloudStack machine with an appropriate ownership ref between them.
func setupMachineCRDs() {
// Create them.
gomega.Ω(k8sClient.Create(ctx, dummies.CAPIMachine)).Should(gomega.Succeed())
gomega.Ω(k8sClient.Create(ctx, dummies.CSMachine1)).Should(gomega.Succeed())
// Fetch the CS Machine that was created.
key := client.ObjectKey{Namespace: dummies.CSCluster.Namespace, Name: dummies.CSMachine1.Name}
gomega.Eventually(func() error {
return k8sClient.Get(ctx, key, dummies.CSMachine1)
}, timeout).Should(gomega.BeNil())
// Set owner ref from CAPI machine to CS machine and patch back the CS machine.
gomega.Eventually(func() error {
ph, err := patch.NewHelper(dummies.CSMachine1, k8sClient)
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
dummies.CSMachine1.OwnerReferences = append(dummies.CSMachine1.OwnerReferences, metav1.OwnerReference{
Kind: "Machine",
APIVersion: clusterv1.GroupVersion.String(),
Name: dummies.CAPIMachine.Name,
UID: "uniqueness",
})
return ph.Patch(ctx, dummies.CSMachine1, patch.WithStatusObservedGeneration{})
}, timeout).Should(gomega.Succeed())
}
func setCSMachineOwnerCRD(owner *fakes.CloudStackMachineOwner, specReplicas, statusReplicas, statusReadyReplicas *int32, statusReady *bool) {
owner.Spec.Replicas = specReplicas
gomega.Ω(k8sClient.Create(ctx, owner)).Should(gomega.Succeed())
key := client.ObjectKey{Namespace: owner.Namespace, Name: owner.Name}
gomega.Eventually(func() error {
return k8sClient.Get(ctx, key, owner)
}, timeout).Should(gomega.BeNil())
gomega.Eventually(func() error {
owner.Status.Ready = statusReady
owner.Status.Replicas = statusReplicas
owner.Status.ReadyReplicas = statusReadyReplicas
return k8sClient.Status().Update(ctx, owner)
}, timeout).Should(gomega.BeNil())
}
// setCAPIMachineAndCSMachineCRDs creates a CAPI and CloudStack machine with an appropriate ownership ref between them.
func setCAPIMachineAndCSMachineCRDs(CSMachine *infrav1.CloudStackMachine, CAPIMachine *clusterv1.Machine) {
// Create them.
gomega.Ω(k8sClient.Create(ctx, CAPIMachine)).Should(gomega.Succeed())
gomega.Ω(k8sClient.Create(ctx, CSMachine)).Should(gomega.Succeed())
// Fetch the CS Machine that was created.
key := client.ObjectKey{Namespace: dummies.CSCluster.Namespace, Name: CSMachine.Name}
gomega.Eventually(func() error {
return k8sClient.Get(ctx, key, CSMachine)
}, timeout).Should(gomega.BeNil())
// Fetch the CAPI Machine that was created.
key = client.ObjectKey{Namespace: dummies.ClusterNameSpace, Name: CAPIMachine.Name}
gomega.Eventually(func() error {
return k8sClient.Get(ctx, key, CAPIMachine)
}, timeout).Should(gomega.BeNil())
// Set ownerReference to CAPI machine in CS machine and patch back the CS machine.
gomega.Eventually(func() error {
ph, err := patch.NewHelper(dummies.CSMachine1, k8sClient)
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
dummies.CSMachine1.OwnerReferences = append(dummies.CSMachine1.OwnerReferences, metav1.OwnerReference{
Kind: "Machine",
APIVersion: clusterv1.GroupVersion.String(),
Name: CAPIMachine.Name,
UID: "uniqueness",
})
return ph.Patch(ctx, CSMachine, patch.WithStatusObservedGeneration{})
}, timeout).Should(gomega.Succeed())
}
func setMachineOwnerReference(CSMachine *infrav1.CloudStackMachine, ownerRef metav1.OwnerReference) {
key := client.ObjectKey{Namespace: dummies.CSCluster.Namespace, Name: CSMachine.Name}
gomega.Eventually(func() error {
return k8sClient.Get(ctx, key, CSMachine)
}, timeout).Should(gomega.BeNil())
// Set ownerReference to CAPI machine in CS machine and patch back the CS machine.
gomega.Eventually(func() error {
ph, err := patch.NewHelper(CSMachine, k8sClient)
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
CSMachine.OwnerReferences = append(CSMachine.OwnerReferences, ownerRef)
return ph.Patch(ctx, CSMachine, patch.WithStatusObservedGeneration{})
}, timeout).Should(gomega.Succeed())
}
// labelMachineFailuredomain add cloudstackfailuredomain info in the labels.
func labelMachineFailuredomain(CSMachine *infrav1.CloudStackMachine, CSFailureDomain1 *infrav1.CloudStackFailureDomain) {
key := client.ObjectKey{Namespace: dummies.CSCluster.Namespace, Name: CSMachine.Name}
gomega.Eventually(func() error {
return k8sClient.Get(ctx, key, CSMachine)
}, timeout).Should(gomega.BeNil())
// set cloudstack failuredomain in machine labels.
gomega.Eventually(func() error {
ph, err := patch.NewHelper(CSMachine, k8sClient)
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
CSMachine.Labels["cloudstackfailuredomain.infrastructure.cluster.x-k8s.io/name"] = CSFailureDomain1.Name
return ph.Patch(ctx, CSMachine, patch.WithStatusObservedGeneration{})
}, timeout).Should(gomega.Succeed())
}