@@ -2839,17 +2839,24 @@ func TestUpdate(t *testing.T) {
2839
2839
}
2840
2840
2841
2841
func TestExists (t * testing.T ) {
2842
- model , session , server := initSimulator (t )
2842
+ withPoweredOffVMS := func () simulatorModelOption {
2843
+ return func (m * simulator.Model ) {
2844
+ m .Autostart = false
2845
+ }
2846
+ }
2847
+
2848
+ model , server := initSimulatorCustom (t , withPoweredOffVMS ())
2849
+ session := getSimulatorSession (t , server )
2843
2850
defer model .Remove ()
2844
2851
defer server .Close ()
2845
2852
credentialsSecretUsername := fmt .Sprintf ("%s.username" , server .URL .Host )
2846
2853
credentialsSecretPassword := fmt .Sprintf ("%s.password" , server .URL .Host )
2847
2854
2848
2855
password , _ := server .URL .User .Password ()
2849
2856
namespace := "test"
2850
- vm := simulator .Map .Any ("VirtualMachine" ).( * simulator. VirtualMachine )
2851
- instanceUUID := "a5764857-ae35-34dc-8f25-a9c9e73aa898"
2852
- vm . Config . InstanceUuid = instanceUUID
2857
+ VMs := simulator .Map .All ("VirtualMachine" )
2858
+ poweredOffVM := VMs [ 0 ].( * simulator. VirtualMachine )
2859
+ poweredOnVM := VMs [ 1 ].( * simulator. VirtualMachine )
2853
2860
2854
2861
credentialsSecret := corev1.Secret {
2855
2862
ObjectMeta : metav1.ObjectMeta {
@@ -2862,7 +2869,7 @@ func TestExists(t *testing.T) {
2862
2869
},
2863
2870
}
2864
2871
2865
- vmObj := object .NewVirtualMachine (session .Client .Client , vm .Reference ())
2872
+ vmObj := object .NewVirtualMachine (session .Client .Client , poweredOnVM .Reference ())
2866
2873
task , err := vmObj .PowerOn (context .TODO ())
2867
2874
if err != nil {
2868
2875
t .Fatal (err )
@@ -2874,11 +2881,12 @@ func TestExists(t *testing.T) {
2874
2881
instanceState string
2875
2882
exists bool
2876
2883
vmExists bool
2884
+ vm * simulator.VirtualMachine
2877
2885
}{
2878
2886
{
2879
2887
name : "VM doesn't exist" ,
2880
2888
machinePhase : "Provisioning" ,
2881
- instanceState : string ( types . VirtualMachinePowerStatePoweredOn ) ,
2889
+ instanceState : "" ,
2882
2890
exists : false ,
2883
2891
vmExists : false ,
2884
2892
},
@@ -2888,54 +2896,67 @@ func TestExists(t *testing.T) {
2888
2896
instanceState : string (types .VirtualMachinePowerStatePoweredOn ),
2889
2897
exists : true ,
2890
2898
vmExists : true ,
2899
+ vm : poweredOnVM ,
2891
2900
},
2892
2901
{
2893
2902
name : "VM exists but didnt powered on after clone" ,
2894
2903
machinePhase : "Provisioning" ,
2895
2904
instanceState : string (types .VirtualMachinePowerStatePoweredOff ),
2896
2905
exists : false ,
2897
2906
vmExists : true ,
2907
+ vm : poweredOffVM ,
2898
2908
},
2899
2909
{
2900
2910
name : "VM exists, but powered off" ,
2901
2911
machinePhase : "Provisioned" ,
2902
2912
instanceState : string (types .VirtualMachinePowerStatePoweredOff ),
2903
2913
exists : true ,
2904
2914
vmExists : true ,
2915
+ vm : poweredOffVM ,
2905
2916
},
2906
2917
}
2907
2918
2908
2919
for _ , tc := range cases {
2909
2920
t .Run (tc .name , func (t * testing.T ) {
2910
- machineScope := machineScope {
2911
- Context : context .TODO (),
2912
- machine : & machinev1.Machine {
2913
- ObjectMeta : metav1.ObjectMeta {
2914
- Name : "test" ,
2915
- Namespace : "test" ,
2916
- Labels : map [string ]string {
2917
- machinev1 .MachineClusterIDLabel : "CLUSTERID" ,
2918
- },
2919
- },
2920
- Status : machinev1.MachineStatus {
2921
- Phase : & tc .machinePhase ,
2921
+
2922
+ var name , uuid string
2923
+ if tc .vm != nil {
2924
+ name = tc .vm .Name
2925
+ uuid = tc .vm .Config .InstanceUuid
2926
+ }
2927
+
2928
+ machineObj := & machinev1.Machine {
2929
+ ObjectMeta : metav1.ObjectMeta {
2930
+ Name : "test" ,
2931
+ Namespace : "test" ,
2932
+ Labels : map [string ]string {
2933
+ machinev1 .MachineClusterIDLabel : "CLUSTERID" ,
2922
2934
},
2923
2935
},
2936
+ Status : machinev1.MachineStatus {
2937
+ Phase : & tc .machinePhase ,
2938
+ },
2939
+ }
2940
+
2941
+ machineScope := machineScope {
2942
+ Context : context .TODO (),
2943
+ machine : machineObj ,
2944
+ machineToBePatched : runtimeclient .MergeFrom (machineObj .DeepCopy ()),
2924
2945
providerSpec : & machinev1.VSphereMachineProviderSpec {
2925
- Template : vm . Name ,
2946
+ Template : name ,
2926
2947
},
2927
2948
session : session ,
2928
2949
providerStatus : & machinev1.VSphereMachineProviderStatus {
2929
2950
TaskRef : task .Reference ().Value ,
2930
2951
InstanceState : & tc .instanceState ,
2931
2952
},
2932
- client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithRuntimeObjects (& credentialsSecret ).Build (),
2953
+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithRuntimeObjects (& credentialsSecret , machineObj ). WithStatusSubresource ( machineObj ).Build (),
2933
2954
}
2934
2955
2935
2956
reconciler := newReconciler (& machineScope )
2936
2957
2937
2958
if tc .vmExists {
2938
- reconciler .machine .UID = apimachinerytypes .UID (instanceUUID )
2959
+ reconciler .machine .UID = apimachinerytypes .UID (uuid )
2939
2960
}
2940
2961
2941
2962
exists , err := reconciler .exists ()
0 commit comments