-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontroller_suite_test.go
131 lines (106 loc) · 4.12 KB
/
controller_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
/*
SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"context"
"testing"
"time"
"github.com/gardener/gardener/pkg/api/indexer"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
dashboardv1alpha1 "github.com/gardener/terminal-controller-manager/api/v1alpha1"
"github.com/gardener/terminal-controller-manager/internal/gardenclient"
"github.com/gardener/terminal-controller-manager/test"
"github.com/gardener/terminal-controller-manager/webhooks"
)
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
const (
randomLength = 5
charset = "abcdefghijklmnopqrstuvwxyz0123456789"
)
var (
e test.Environment
ctx context.Context
cancel context.CancelFunc
cmConfig *dashboardv1alpha1.ControllerManagerConfiguration
mutator *webhooks.TerminalMutator
validator *webhooks.TerminalValidator
terminalReconciler *TerminalReconciler
terminalHeartbeatReconciler *TerminalHeartbeatReconciler
serviceAccountReconciler *ServiceAccountReconciler
)
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
SetDefaultEventuallyTimeout(30 * time.Second)
RunSpecs(t, "Controller Suite")
}
var _ = BeforeSuite(func() {
ctx, cancel = context.WithCancel(context.TODO())
cmConfig = test.DefaultConfiguration()
mutator = &webhooks.TerminalMutator{
Log: ctrl.Log.WithName("webhooks").WithName("TerminalMutation"),
}
validator = &webhooks.TerminalValidator{
Log: ctrl.Log.WithName("webhooks").WithName("TerminalValidation"),
Config: cmConfig,
}
e = test.New(mutator, validator)
mutator.Decoder = admission.NewDecoder(e.GardenEnv.Scheme)
validator.Decoder = admission.NewDecoder(e.GardenEnv.Scheme)
validator.Client = e.K8sClient
kube, err := kubernetes.NewForConfig(e.Config)
Expect(err).ToNot(HaveOccurred())
recorder := CreateRecorder(kube, e.K8sManager.GetScheme())
terminalReconciler = &TerminalReconciler{
ClientSet: gardenclient.NewClientSet(e.Config, e.K8sManager.GetClient(), kube),
Scheme: e.K8sManager.GetScheme(),
Recorder: recorder,
Config: cmConfig,
ReconcilerCountPerNamespace: map[string]int{},
}
err = terminalReconciler.SetupWithManager(e.K8sManager, cmConfig.Controllers.Terminal)
Expect(err).ToNot(HaveOccurred())
terminalHeartbeatReconciler = &TerminalHeartbeatReconciler{
Client: e.K8sManager.GetClient(),
Recorder: recorder,
Config: cmConfig,
}
err = terminalHeartbeatReconciler.SetupWithManager(e.K8sManager, cmConfig.Controllers.TerminalHeartbeat)
Expect(err).ToNot(HaveOccurred())
serviceAccountReconciler = &ServiceAccountReconciler{
Client: e.K8sManager.GetClient(),
Recorder: recorder,
Config: cmConfig,
Log: ctrl.Log.WithName("controllers").WithName("ServiceAccount"),
}
err = serviceAccountReconciler.SetupWithManager(e.K8sManager, cmConfig.Controllers.ServiceAccount)
Expect(err).ToNot(HaveOccurred())
err = indexer.AddProjectNamespace(ctx, e.K8sManager.GetFieldIndexer())
Expect(err).ToNot(HaveOccurred())
e.Start(ctx)
})
// TODO reuse from main
func CreateRecorder(kubeClient kubernetes.Interface, scheme *runtime.Scheme) record.EventRecorder {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&v1.EventSinkImpl{Interface: v1.New(kubeClient.CoreV1().RESTClient()).Events("")})
return eventBroadcaster.NewRecorder(scheme, corev1.EventSource{Component: dashboardv1alpha1.TerminalComponent})
}
var _ = AfterSuite(func() {
cancel()
By("tearing down the test environment")
if e.GardenEnv == nil {
return
}
err := e.GardenEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})