@@ -2839,17 +2839,24 @@ func TestUpdate(t *testing.T) {
28392839}
28402840
28412841func 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 )
28432850 defer model .Remove ()
28442851 defer server .Close ()
28452852 credentialsSecretUsername := fmt .Sprintf ("%s.username" , server .URL .Host )
28462853 credentialsSecretPassword := fmt .Sprintf ("%s.password" , server .URL .Host )
28472854
28482855 password , _ := server .URL .User .Password ()
28492856 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 )
28532860
28542861 credentialsSecret := corev1.Secret {
28552862 ObjectMeta : metav1.ObjectMeta {
@@ -2862,7 +2869,7 @@ func TestExists(t *testing.T) {
28622869 },
28632870 }
28642871
2865- vmObj := object .NewVirtualMachine (session .Client .Client , vm .Reference ())
2872+ vmObj := object .NewVirtualMachine (session .Client .Client , poweredOnVM .Reference ())
28662873 task , err := vmObj .PowerOn (context .TODO ())
28672874 if err != nil {
28682875 t .Fatal (err )
@@ -2874,11 +2881,12 @@ func TestExists(t *testing.T) {
28742881 instanceState string
28752882 exists bool
28762883 vmExists bool
2884+ vm * simulator.VirtualMachine
28772885 }{
28782886 {
28792887 name : "VM doesn't exist" ,
28802888 machinePhase : "Provisioning" ,
2881- instanceState : string ( types . VirtualMachinePowerStatePoweredOn ) ,
2889+ instanceState : "" ,
28822890 exists : false ,
28832891 vmExists : false ,
28842892 },
@@ -2888,54 +2896,67 @@ func TestExists(t *testing.T) {
28882896 instanceState : string (types .VirtualMachinePowerStatePoweredOn ),
28892897 exists : true ,
28902898 vmExists : true ,
2899+ vm : poweredOnVM ,
28912900 },
28922901 {
28932902 name : "VM exists but didnt powered on after clone" ,
28942903 machinePhase : "Provisioning" ,
28952904 instanceState : string (types .VirtualMachinePowerStatePoweredOff ),
28962905 exists : false ,
28972906 vmExists : true ,
2907+ vm : poweredOffVM ,
28982908 },
28992909 {
29002910 name : "VM exists, but powered off" ,
29012911 machinePhase : "Provisioned" ,
29022912 instanceState : string (types .VirtualMachinePowerStatePoweredOff ),
29032913 exists : true ,
29042914 vmExists : true ,
2915+ vm : poweredOffVM ,
29052916 },
29062917 }
29072918
29082919 for _ , tc := range cases {
29092920 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" ,
29222934 },
29232935 },
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 ()),
29242945 providerSpec : & machinev1.VSphereMachineProviderSpec {
2925- Template : vm . Name ,
2946+ Template : name ,
29262947 },
29272948 session : session ,
29282949 providerStatus : & machinev1.VSphereMachineProviderStatus {
29292950 TaskRef : task .Reference ().Value ,
29302951 InstanceState : & tc .instanceState ,
29312952 },
2932- client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithRuntimeObjects (& credentialsSecret ).Build (),
2953+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithRuntimeObjects (& credentialsSecret , machineObj ). WithStatusSubresource ( machineObj ).Build (),
29332954 }
29342955
29352956 reconciler := newReconciler (& machineScope )
29362957
29372958 if tc .vmExists {
2938- reconciler .machine .UID = apimachinerytypes .UID (instanceUUID )
2959+ reconciler .machine .UID = apimachinerytypes .UID (uuid )
29392960 }
29402961
29412962 exists , err := reconciler .exists ()
0 commit comments