@@ -38,6 +38,7 @@ import (
38
38
ctrl "sigs.k8s.io/controller-runtime"
39
39
"sigs.k8s.io/controller-runtime/pkg/client"
40
40
"sigs.k8s.io/controller-runtime/pkg/client/fake"
41
+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
41
42
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
42
43
43
44
machinemocks "sigs.k8s.io/cluster-api-provider-kubevirt/pkg/kubevirt/mock"
@@ -318,7 +319,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
318
319
319
320
})
320
321
321
- setupClient := func (machineFactory kubevirt.MachineFactory , objects []client.Object ) {
322
+ setupClientWithInterceptors := func (machineFactory kubevirt.MachineFactory , objects []client.Object , interceptorFuncs interceptor. Funcs ) {
322
323
machineContext = & context.MachineContext {
323
324
Context : gocontext .Background (),
324
325
Cluster : cluster ,
@@ -328,15 +329,21 @@ var _ = Describe("reconcile a kubevirt machine", func() {
328
329
Logger : testLogger ,
329
330
}
330
331
331
- fakeClient = fake .NewClientBuilder ().WithScheme (testing .SetupScheme ()).WithObjects (objects ... ).WithStatusSubresource (objects ... ).Build ()
332
+ fakeClient = fake .NewClientBuilder ().WithScheme (testing .SetupScheme ()).WithObjects (objects ... ).WithStatusSubresource (objects ... ).WithInterceptorFuncs ( interceptorFuncs ). Build ()
332
333
kubevirtMachineReconciler = KubevirtMachineReconciler {
333
334
Client : fakeClient ,
334
335
WorkloadCluster : workloadClusterMock ,
335
336
InfraCluster : infraClusterMock ,
336
337
MachineFactory : machineFactory ,
337
338
}
339
+ }
340
+
341
+ setupClient := func (machineFactory kubevirt.MachineFactory , objects []client.Object ) {
342
+
343
+ setupClientWithInterceptors (machineFactory , objects , interceptor.Funcs {})
338
344
339
345
}
346
+
340
347
AfterEach (func () {})
341
348
342
349
It ("should create KubeVirt VM" , func () {
@@ -879,6 +886,43 @@ var _ = Describe("reconcile a kubevirt machine", func() {
879
886
Expect (conditions [0 ].Type ).To (Equal (infrav1 .VMProvisionedCondition ))
880
887
Expect (conditions [0 ].Reason ).To (Equal (infrav1 .WaitingForBootstrapDataReason ))
881
888
})
889
+
890
+ It ("adds a failed VMProvisionedCondition with reason VMCreateFailed when failng to create VM" , func () {
891
+ objects := []client.Object {
892
+ cluster ,
893
+ kubevirtCluster ,
894
+ machine ,
895
+ kubevirtMachine ,
896
+ sshKeySecret ,
897
+ bootstrapSecret ,
898
+ }
899
+
900
+ injectErr := interceptor.Funcs {
901
+ Create : func (ctx gocontext.Context , client client.WithWatch , obj client.Object , opts ... client.CreateOption ) error {
902
+
903
+ _ , ok := obj .(* kubevirtv1.VirtualMachine )
904
+ if ok {
905
+ return errors .New ("vm create error" )
906
+ }
907
+ return nil
908
+ },
909
+ }
910
+
911
+ setupClientWithInterceptors (kubevirt.DefaultMachineFactory {}, objects , injectErr )
912
+
913
+ infraClusterMock .EXPECT ().GenerateInfraClusterClient (kubevirtMachine .Spec .InfraClusterSecretRef , kubevirtMachine .Namespace , machineContext .Context ).Return (fakeClient , kubevirtMachine .Namespace , nil )
914
+
915
+ _ , err := kubevirtMachineReconciler .reconcileNormal (machineContext )
916
+
917
+ Expect (err ).Should (HaveOccurred ())
918
+
919
+ // should expect condition
920
+ conditions := machineContext .KubevirtMachine .GetConditions ()
921
+ Expect (conditions [0 ].Type ).To (Equal (infrav1 .VMProvisionedCondition ))
922
+ Expect (conditions [0 ].Status ).To (Equal (corev1 .ConditionFalse ))
923
+ Expect (conditions [0 ].Reason ).To (Equal (infrav1 .VMCreateFailedReason ))
924
+ })
925
+
882
926
It ("adds a succeeded VMProvisionedCondition" , func () {
883
927
vmiReadyCondition := kubevirtv1.VirtualMachineInstanceCondition {
884
928
Type : kubevirtv1 .VirtualMachineInstanceReady ,
0 commit comments