forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller_test.go
More file actions
213 lines (179 loc) · 7.14 KB
/
controller_test.go
File metadata and controls
213 lines (179 loc) · 7.14 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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package worker_test
import (
"context"
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/testing"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
. "github.com/gardener/gardener/extensions/pkg/controller/worker"
"github.com/gardener/gardener/pkg/client/kubernetes"
)
var _ = Describe("Controller", func() {
Describe("#MachineConditionChangedPredicate", func() {
var (
ctx context.Context
c client.Client
machineDeployment *machinev1alpha1.MachineDeployment
oldMachine *machinev1alpha1.Machine
machine *machinev1alpha1.Machine
p predicate.Predicate
)
BeforeEach(func() {
ctx = context.Background()
// Starting with controller-runtime v0.22.0, the default object tracker does not work with resources which include
// structs directly as pointer, e.g. *MachineConfiguration in Machine resource. Hence, use the old one instead.
objectTracker := testing.NewObjectTracker(kubernetes.SeedScheme, scheme.Codecs.UniversalDecoder())
c = fakeclient.NewClientBuilder().WithScheme(kubernetes.SeedScheme).WithObjectTracker(objectTracker).Build()
machineDeployment = &machinev1alpha1.MachineDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: "machineDeployment",
Namespace: "namespace",
},
Spec: machinev1alpha1.MachineDeploymentSpec{
Strategy: machinev1alpha1.MachineDeploymentStrategy{
Type: machinev1alpha1.InPlaceUpdateMachineDeploymentStrategyType,
InPlaceUpdate: &machinev1alpha1.InPlaceUpdateMachineDeployment{
OrchestrationType: machinev1alpha1.OrchestrationTypeManual,
},
},
},
Status: machinev1alpha1.MachineDeploymentStatus{
Replicas: 2,
UpdatedReplicas: 2,
},
}
machine = &machinev1alpha1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "machine",
Namespace: "namespace",
Labels: map[string]string{
"name": machineDeployment.Name,
},
},
Status: machinev1alpha1.MachineStatus{
Conditions: []corev1.NodeCondition{
{
Type: machinev1alpha1.NodeInPlaceUpdate,
Status: corev1.ConditionTrue,
Reason: machinev1alpha1.CandidateForUpdate,
},
},
},
}
Expect(c.Create(ctx, machineDeployment)).To(Succeed())
DeferCleanup(func() {
Expect(client.IgnoreNotFound(c.Delete(ctx, machineDeployment))).To(Succeed())
})
oldMachine = machine.DeepCopy()
p = MachineConditionChangedPredicate(ctx, logr.Discard(), c)
})
Describe("#Create", func() {
It("should return false when object is not machine", func() {
Expect(p.Create(event.CreateEvent{Object: &corev1.Secret{}})).To(BeFalse())
})
It("should return false when machine does not have a machineDeployment label", func() {
delete(machine.Labels, "name")
Expect(p.Create(event.CreateEvent{Object: machine})).To(BeFalse())
})
It("should return false when machineDeployment is not found", func() {
Expect(c.Delete(ctx, machineDeployment)).To(Succeed())
Expect(p.Create(event.CreateEvent{Object: machine})).To(BeFalse())
})
It("should return false when machineDeployment strategy type is not InPlace", func() {
machineDeployment.Spec.Strategy.Type = machinev1alpha1.RollingUpdateMachineDeploymentStrategyType
Expect(c.Update(ctx, machineDeployment)).To(Succeed())
Expect(p.Create(event.CreateEvent{Object: machine})).To(BeFalse())
})
It("should return false when machineDeployment orchestration type is not manual", func() {
machineDeployment.Spec.Strategy.InPlaceUpdate.OrchestrationType = machinev1alpha1.OrchestrationTypeAuto
Expect(c.Update(ctx, machineDeployment)).To(Succeed())
Expect(p.Create(event.CreateEvent{Object: machine})).To(BeFalse())
})
It("should return true when machineDeployment strategy type is InPlace and orchestration type is Manual", func() {
Expect(p.Create(event.CreateEvent{Object: machine})).To(BeTrue())
})
})
Describe("#Update", func() {
It("should return false because new object is not machine", func() {
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: &corev1.Secret{},
})).To(BeFalse())
})
It("should return false because old object is not machine", func() {
Expect(p.Update(event.UpdateEvent{
ObjectOld: &corev1.Secret{},
ObjectNew: machine,
})).To(BeFalse())
})
It("should return false because the machine strategy type is not InPlace", func() {
machineDeployment.Spec.Strategy.Type = machinev1alpha1.RollingUpdateMachineDeploymentStrategyType
Expect(c.Update(ctx, machineDeployment)).To(Succeed())
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: machine,
})).To(BeFalse())
})
It("should return false because the machineDeployment orchestration type is not manual", func() {
machineDeployment.Spec.Strategy.InPlaceUpdate.OrchestrationType = machinev1alpha1.OrchestrationTypeAuto
Expect(c.Update(ctx, machineDeployment)).To(Succeed())
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: machine,
})).To(BeFalse())
})
It("should return false if oldMachine InPlaceUpdate condition is nil or newMachine InPlaceUpdate condition is nil", func() {
oldMachine.Status.Conditions = nil
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: machine,
})).To(BeFalse())
machine.Status.Conditions = nil
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: machine,
})).To(BeFalse())
})
It("should return false if oldMachine InPlaceUpdate condition reason is not UpdateCandidate or newMachine InPlaceUpdate condition reason is not SelectedForUpdate", func() {
oldMachine.Status.Conditions[0].Reason = "notUpdateCandidate"
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: machine,
})).To(BeFalse())
machine.Status.Conditions[0].Reason = "notSelectedForUpdate"
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: machine,
})).To(BeFalse())
})
It("should return true if oldMachine InPlaceUpdate condition reason is UpdateCandidate and newMachine InPlaceUpdate condition reason is SelectedForUpdate", func() {
machine.Status.Conditions[0].Reason = machinev1alpha1.SelectedForUpdate
Expect(p.Update(event.UpdateEvent{
ObjectOld: oldMachine,
ObjectNew: machine,
})).To(BeTrue())
})
})
Describe("#Delete", func() {
It("should return false", func() {
Expect(p.Delete(event.DeleteEvent{})).To(BeFalse())
})
})
Describe("#Generic", func() {
It("should return false", func() {
Expect(p.Generic(event.GenericEvent{})).To(BeFalse())
})
})
})
})