-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
status_test.go
304 lines (270 loc) · 17.6 KB
/
status_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
package status
import (
"context"
"fmt"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
)
var _ = Describe("Status", func() {
var ctrl *gomock.Controller
var kvInterface *kubecli.MockKubeVirtInterface
var virtClient *kubecli.MockKubevirtClient
var vmInterface *kubecli.MockVirtualMachineInterface
var vmirsInterface *kubecli.MockReplicaSetInterface
var migrationInterface *kubecli.MockVirtualMachineInstanceMigrationInterface
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
virtClient = kubecli.NewMockKubevirtClient(ctrl)
kvInterface = kubecli.NewMockKubeVirtInterface(ctrl)
vmInterface = kubecli.NewMockVirtualMachineInterface(ctrl)
vmirsInterface = kubecli.NewMockReplicaSetInterface(ctrl)
migrationInterface = kubecli.NewMockVirtualMachineInstanceMigrationInterface(ctrl)
virtClient.EXPECT().KubeVirt(gomock.Any()).Return(kvInterface).AnyTimes()
virtClient.EXPECT().VirtualMachineInstanceMigration(gomock.Any()).Return(migrationInterface).AnyTimes()
virtClient.EXPECT().ReplicaSet(gomock.Any()).Return(vmirsInterface).AnyTimes()
virtClient.EXPECT().VirtualMachine(gomock.Any()).Return(vmInterface).AnyTimes()
})
Context("starting with the assumption of /status being present", func() {
Context("for PUT operations", func() {
It("should continuously use the /status subresource if no errors occur", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, nil).Times(2)
Expect(updater.UpdateStatus(kv)).To(Succeed())
Expect(updater.UpdateStatus(kv)).To(Succeed())
})
It("should fall back on a 404 error on the /status subresource to an ordinary update", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
kvInterface.EXPECT().Update(kv).Return(kv, nil).Times(2)
Expect(updater.UpdateStatus(kv)).To(Succeed())
Expect(updater.UpdateStatus(kv)).To(Succeed())
})
It("should fall back on a 404 error on the /status subresource to an ordinary update but keep in mind that objects may have disappeared", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
kvInterface.EXPECT().Update(kv).Return(kv, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
Expect(updater.UpdateStatus(kv)).ToNot(Succeed())
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, nil).Times(1)
Expect(updater.UpdateStatus(kv)).To(Succeed())
})
It("should fall back on a 404 error on the /status subresource to an ordinary update but keep in mind that the subresource may get enabled directly afterwards", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
newKV := kv.DeepCopy()
newKV.Status.Phase = v1.KubeVirtPhaseDeleted
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
kvInterface.EXPECT().Update(kv).Return(newKV, nil).Times(1)
Expect(updater.UpdateStatus(kv)).ToNot(Succeed())
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, nil).Times(1)
Expect(updater.UpdateStatus(kv)).To(Succeed())
})
It("should stick with /status if an arbitrary error occurs", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, fmt.Errorf("I am not 404")).Times(1)
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, nil).Times(1)
Expect(updater.UpdateStatus(kv)).ToNot(Succeed())
Expect(updater.UpdateStatus(kv)).To(Succeed())
})
})
Context("for PATCH operations", func() {
It("should continuously use the /status subresource if no errors occur", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})
It("should fall back on a 404 error on the /status subresource to an ordinary update", func() {
updater := NewVMStatusUpdater(virtClient)
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})
It("should fall back on a 404 error on the /status subresource to an ordinary update but keep in mind that objects may have disappeared", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})
It("should fall back on a 404 error on the /status subresource to an ordinary update but keep in mind that the subresource may get enabled directly afterwards", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})
It("should stick with /status if an arbitrary error occurs", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, fmt.Errorf("I am not a 404 error")).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})
})
})
Context("starting with the assumption that /status is not present", func() {
Context("for PUT operations", func() {
It("should stick with a normal update if the status did change", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
updater.updater.subresource = false
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().Update(kv).Return(kv, nil).Times(2)
Expect(updater.UpdateStatus(kv)).To(Succeed())
Expect(updater.UpdateStatus(kv)).To(Succeed())
})
It("should stick with a normal update if we get a 404 error", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
updater.updater.subresource = false
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().Update(kv).Return(kv, errors.NewNotFound(schema.GroupResource{}, "something")).Times(2)
Expect(updater.UpdateStatus(kv)).ToNot(Succeed())
Expect(updater.UpdateStatus(kv)).ToNot(Succeed())
})
It("should stick with a normal update if we get an arbitrary error", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
updater.updater.subresource = false
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().Update(kv).Return(kv, fmt.Errorf("I am not 404")).Times(2)
Expect(updater.UpdateStatus(kv)).ToNot(Succeed())
Expect(updater.UpdateStatus(kv)).ToNot(Succeed())
})
It("should fall back to /status if the status did not change and stick to it", func() {
updater := NewKubeVirtStatusUpdater(virtClient)
updater.updater.subresource = false
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
oldKV := kv.DeepCopy()
oldKV.Status.Phase = v1.KubeVirtPhaseDeploying
kvInterface.EXPECT().Update(kv).Return(oldKV, nil).Times(1)
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, nil).Times(1)
Expect(updater.UpdateStatus(kv)).To(Succeed())
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, nil).Times(1)
Expect(updater.UpdateStatus(kv)).To(Succeed())
})
})
Context("for PATCH operations", func() {
It("should stick with a normal update if the resource version did change", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})
It("should stick with a normal update if we get a 404 error", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
})
It("should stick with a normal update if we get an arbitrary error", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, fmt.Errorf("I am an arbitrary error")).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
})
It("should fall back to /status if the status did not change and stick to it", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(1)
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})
})
})
Context("the generic updater", func() {
It("should work for /status based updates for all types needed", func() {
By("checking the KubeVirt resource")
kvUpdater := NewKubeVirtStatusUpdater(virtClient)
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().UpdateStatus(kv).Return(kv, nil).Times(2)
Expect(kvUpdater.UpdateStatus(kv)).To(Succeed())
Expect(kvUpdater.UpdateStatus(kv)).To(Succeed())
By("checking the VirtualMachine resource")
vmUpdater := NewVMStatusUpdater(virtClient)
vm := &v1.VirtualMachine{Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().UpdateStatus(context.Background(), vm, metav1.UpdateOptions{}).Return(vm, nil).Times(1)
Expect(vmUpdater.UpdateStatus(vm)).To(Succeed())
By("checking the VirtualMachineInstanceReplicaSet resource")
vmirsUpdater := NewVMIRSStatusUpdater(virtClient)
vmirs := &v1.VirtualMachineInstanceReplicaSet{Status: v1.VirtualMachineInstanceReplicaSetStatus{Replicas: 2}}
vmirsInterface.EXPECT().UpdateStatus(vmirs).Return(vmirs, nil).Times(1)
Expect(vmirsUpdater.UpdateStatus(vmirs)).To(Succeed())
By("checking the VirtualMachineInstanceMigration resource")
migrationUpdater := NewMigrationStatusUpdater(virtClient)
migration := &v1.VirtualMachineInstanceMigration{Status: v1.VirtualMachineInstanceMigrationStatus{Phase: v1.MigrationPhaseUnset}}
migrationInterface.EXPECT().UpdateStatus(context.Background(), migration, metav1.UpdateOptions{}).Return(migration, nil).Times(1)
Expect(migrationUpdater.UpdateStatus(migration)).To(Succeed())
})
It("should work for resource where /status is not enabled", func() {
By("checking the KubeVirt resource")
kvUpdater := NewKubeVirtStatusUpdater(virtClient)
kvUpdater.updater.subresource = false
kv := &v1.KubeVirt{Status: v1.KubeVirtStatus{Phase: v1.KubeVirtPhaseDeployed}}
kvInterface.EXPECT().Update(kv).Return(kv, nil).Times(2)
Expect(kvUpdater.UpdateStatus(kv)).To(Succeed())
Expect(kvUpdater.UpdateStatus(kv)).To(Succeed())
By("checking the VirtualMachine resource")
vmUpdater := NewVMStatusUpdater(virtClient)
vmUpdater.updater.subresource = false
vm := &v1.VirtualMachine{Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().Update(context.Background(), vm, metav1.UpdateOptions{}).Return(vm, nil).Times(1)
Expect(vmUpdater.UpdateStatus(vm)).To(Succeed())
By("checking the VirtualMachineInstanceReplicaSet resource")
vmirsUpdater := NewVMIRSStatusUpdater(virtClient)
vmirsUpdater.updater.subresource = false
vmirs := &v1.VirtualMachineInstanceReplicaSet{Status: v1.VirtualMachineInstanceReplicaSetStatus{Replicas: 2}}
vmirsInterface.EXPECT().Update(vmirs).Return(vmirs, nil).Times(1)
Expect(vmirsUpdater.UpdateStatus(vmirs)).To(Succeed())
By("checking the VirtualMachineInstanceMigration resource")
migrationUpdater := NewMigrationStatusUpdater(virtClient)
migrationUpdater.updater.subresource = false
migration := &v1.VirtualMachineInstanceMigration{Status: v1.VirtualMachineInstanceMigrationStatus{Phase: v1.MigrationPhaseUnset}}
migrationInterface.EXPECT().Update(context.Background(), migration, metav1.UpdateOptions{}).Return(migration, nil).Times(1)
Expect(migrationUpdater.UpdateStatus(migration)).To(Succeed())
})
})
})