forked from rabbitmq/cluster-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite_test.go
136 lines (108 loc) · 4.18 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
/*
RabbitMQ Cluster Operator
Copyright 2020 VMware, Inc. All Rights Reserved.
This product is licensed to you under the Mozilla Public license, Version 2.0 (the "License"). You may not use this product except in compliance with the Mozilla Public License.
This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
*/
package controllers_test
import (
"context"
"path/filepath"
"testing"
"k8s.io/client-go/util/retry"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/controllers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
const (
controllerName = "rabbitmqcluster-controller"
defaultRabbitmqImage = "default-rabbit-image:stable"
defaultUserUpdaterImage = "default-UU-image:unstable"
defaultImagePullSecrets = "image-secret-1,image-secret-2,image-secret-3"
)
var (
testEnv *envtest.Environment
client runtimeClient.Client
clientSet *kubernetes.Clientset
fakeExecutor *fakePodExecutor
ctx context.Context
cancel context.CancelFunc
updateWithRetry = func(cr *rabbitmqv1beta1.RabbitmqCluster, mutateFn func(r *rabbitmqv1beta1.RabbitmqCluster)) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
if err := client.Get(ctx, runtimeClient.ObjectKeyFromObject(cr), cr); err != nil {
return err
}
mutateFn(cr)
return client.Update(ctx, cr)
})
}
)
func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Controller Suite")
}
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
ctx, cancel = context.WithCancel(ctrl.SetupSignalHandler())
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
}
cfg, err := testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())
Expect(scheme.AddToScheme(scheme.Scheme)).To(Succeed())
Expect(rabbitmqv1beta1.AddToScheme(scheme.Scheme)).To(Succeed())
clientSet, err = kubernetes.NewForConfig(cfg)
Expect(err).NotTo(HaveOccurred())
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
MetricsBindAddress: "0",
})
Expect(err).ToNot(HaveOccurred())
fakeExecutor = &fakePodExecutor{}
err = (&controllers.RabbitmqClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor(controllerName),
Namespace: "rabbitmq-system",
Clientset: clientSet,
PodExecutor: fakeExecutor,
DefaultRabbitmqImage: defaultRabbitmqImage,
DefaultUserUpdaterImage: defaultUserUpdaterImage,
DefaultImagePullSecrets: defaultImagePullSecrets,
}).SetupWithManager(mgr)
Expect(err).ToNot(HaveOccurred())
go func() {
err = mgr.Start(ctx)
Expect(err).ToNot(HaveOccurred())
}()
client = mgr.GetClient()
Expect(client).ToNot(BeNil())
})
var _ = AfterSuite(func() {
cancel()
By("tearing down the test environment")
Expect(testEnv.Stop()).To(Succeed())
})
type fakePodExecutor struct {
executedCommands []command
}
type command []string
func (f *fakePodExecutor) Exec(clientset *kubernetes.Clientset, clusterConfig *rest.Config, namespace, podName, containerName string, command ...string) (string, string, error) {
f.executedCommands = append(f.executedCommands, command)
return "", "", nil
}
func (f *fakePodExecutor) ExecutedCommands() []command { return f.executedCommands }
func (f *fakePodExecutor) ResetExecutedCommands() { f.executedCommands = []command{} }
var _ = AfterEach(func() { fakeExecutor.ResetExecutedCommands() })