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