diff --git a/src/Tools/vmOpsTool/IVMWare.java b/src/Tools/vmOpsTool/IVMWare.java index 2574be6..f720658 100644 --- a/src/Tools/vmOpsTool/IVMWare.java +++ b/src/Tools/vmOpsTool/IVMWare.java @@ -59,11 +59,12 @@ public interface IVMWare { /** * @param vmName name of the virtual machine + * @param defaultValue this value is returned when not able to determine vm power on status * @param connData connection information for vCenter * @return true if powered on, otherwise false * @throws Exception */ - boolean isVMPoweredOn(String vmName, ConnectionData connData) throws Exception; + boolean isVMPoweredOn(String vmName, boolean defaultValue, ConnectionData connData) throws Exception; /** * @param templateName name of the virtual machine template to be cloned diff --git a/src/Tools/vmOpsTool/VMWareImpl.java b/src/Tools/vmOpsTool/VMWareImpl.java index 4d0ce54..dd54c63 100644 --- a/src/Tools/vmOpsTool/VMWareImpl.java +++ b/src/Tools/vmOpsTool/VMWareImpl.java @@ -21,7 +21,8 @@ public class VMWareImpl implements IVMWare { private final String SNAPSHOT = "snapshot"; private final String CONFIG = "config"; private final String NAME = "name"; - private final String GUEST_TOOLS_STATUS = "guest.toolsStatus"; + private final String GUEST_TOOLS_RUNNING_STATUS = "guest.toolsRunningStatus"; + private final String GUEST_TOOLS_VERSION_STATUS = "guest.toolsVersionStatus"; private final String GUEST_HEART_BEAT_STATUS = "guestHeartbeatStatus"; private final String INFO_STATE = "info.state"; @@ -133,7 +134,7 @@ public class VMWareImpl implements IVMWare { public void powerOnVM(String vmName, ConnectionData connData) throws Exception { connect(connData); - if (!isVMPoweredOn(vmName, connData)) { + if (!isVMPoweredOn(vmName, false, connData)) { ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false); ManagedObjectReference task = vimPort.powerOnVMTask(vmMor, null); @@ -142,8 +143,8 @@ public class VMWareImpl implements IVMWare { String.format("Failed to power on virtual machine [ %s ].", vmName)); } System.out.println(String.format("Waiting for virtual machine [ %s ] to start.", vmName)); - waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_STATUS}, new String[]{GUEST_TOOLS_STATUS}, - new Object[][]{new Object[]{VirtualMachineToolsStatus.TOOLS_OK, VirtualMachineToolsStatus.TOOLS_OLD}}, + waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_RUNNING_STATUS}, new String[]{GUEST_TOOLS_RUNNING_STATUS}, + new Object[][]{new Object[]{VirtualMachineToolsRunningStatus.GUEST_TOOLS_RUNNING.value()}}, Constants.START_STOP_MAX_WAIT_IN_MINUTES); waitOnMorProperties(vmMor, new String[]{GUEST_HEART_BEAT_STATUS}, new String[]{GUEST_HEART_BEAT_STATUS}, new Object[][]{new Object[]{ManagedEntityStatus.GREEN}}, Constants.START_STOP_MAX_WAIT_IN_MINUTES); @@ -155,12 +156,13 @@ public class VMWareImpl implements IVMWare { public void shutdownVM(String vmName, ConnectionData connData) throws Exception { connect(connData); - if (isVMPoweredOn(vmName, connData)) { + if (isVMPoweredOn(vmName, true, connData)) { ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false); vimPort.shutdownGuest(vmMor); System.out.println(String.format("Waiting for virtual machine [ %s ] to shutdown.", vmName)); - waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_STATUS}, new String[]{GUEST_TOOLS_STATUS}, - new Object[][]{new Object[]{VirtualMachineToolsStatus.TOOLS_NOT_RUNNING}}, Constants.START_STOP_MAX_WAIT_IN_MINUTES); + waitOnMorProperties(vmMor, new String[]{GUEST_TOOLS_RUNNING_STATUS}, new String[]{GUEST_TOOLS_RUNNING_STATUS}, + new Object[][]{new Object[]{VirtualMachineToolsRunningStatus.GUEST_TOOLS_NOT_RUNNING.value()}}, + Constants.START_STOP_MAX_WAIT_IN_MINUTES); waitOnMorProperties(vmMor, new String[]{GUEST_HEART_BEAT_STATUS}, new String[]{GUEST_HEART_BEAT_STATUS}, new Object[][]{new Object[]{ManagedEntityStatus.GRAY}}, Constants.START_STOP_MAX_WAIT_IN_MINUTES); System.out.println(String.format("Successfully shutdowned the virtual machine [ %s ].", vmName)); @@ -170,7 +172,7 @@ public class VMWareImpl implements IVMWare { public void powerOffVM(String vmName, ConnectionData connData) throws Exception { connect(connData); - if (isVMPoweredOn(vmName, connData)) { + if (isVMPoweredOn(vmName, true, connData)) { ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false); ManagedObjectReference powerOffTask = vimPort.powerOffVMTask(vmMor); System.out.println(String.format("Waiting for virtual machine [ %s ] to power off.", vmName)); @@ -179,6 +181,7 @@ public class VMWareImpl implements IVMWare { String.format("Failed to power off virtual machine [ %s ].", vmName)); } System.out.println(String.format("Successfully powered off the virtual machine [ %s ].", vmName)); + return; } System.out.println(String.format("Virtual machine [ %s ] is already powered off.", vmName)); } @@ -228,12 +231,22 @@ public class VMWareImpl implements IVMWare { return true; } - public boolean isVMPoweredOn(String vmName, ConnectionData connData) throws Exception { + public boolean isVMPoweredOn(String vmName, boolean defaultValue, ConnectionData connData) throws Exception { connect(connData); System.out.println("Checking virtual machine [ " + vmName + " ] power status."); ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false); - VirtualMachineToolsStatus vmToolsStatus = (VirtualMachineToolsStatus) getMorProperties(vmMor, new String[]{GUEST_TOOLS_STATUS}).get(GUEST_TOOLS_STATUS); - return (vmToolsStatus != null) && (vmToolsStatus == VirtualMachineToolsStatus.TOOLS_OK); + + VirtualMachineToolsVersionStatus toolsVersionStatus = VirtualMachineToolsVersionStatus.fromValue( + (String) getMorProperties(vmMor, new String[]{GUEST_TOOLS_VERSION_STATUS}).get(GUEST_TOOLS_VERSION_STATUS)); + + if (toolsVersionStatus.equals(VirtualMachineToolsVersionStatus.GUEST_TOOLS_NOT_INSTALLED)) { + System.out.println("VMware tools are not installed, proceeding without checking poweron status"); + return defaultValue; + } + + VirtualMachineToolsRunningStatus toolsRunningStatus = VirtualMachineToolsRunningStatus.fromValue( + (String) getMorProperties(vmMor, new String[]{GUEST_TOOLS_RUNNING_STATUS}).get(GUEST_TOOLS_RUNNING_STATUS)); + return !(toolsRunningStatus.equals(VirtualMachineToolsRunningStatus.GUEST_TOOLS_NOT_RUNNING)); } private ManagedObjectReference getMorByName(ManagedObjectReference rootContainer, String mobName, String morefType, diff --git a/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java b/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java index ff8d832..b98dd08 100644 --- a/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java +++ b/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java @@ -155,7 +155,7 @@ public class InMemoryVMWareImpl implements IVMWare { return vmSnapshotInfo.containsKey(vmName); } - public boolean isVMPoweredOn(String vmName, ConnectionData connData) throws Exception { + public boolean isVMPoweredOn(String vmName, boolean defaultValue, ConnectionData connData) throws Exception { vmName = vmName.toLowerCase(); if (vmStateInformation.containsKey(vmName)) { return vmStateInformation.get(vmName).equals(started); diff --git a/tests/Tools/vmOpsTool/VMWarePlatformTests.java b/tests/Tools/vmOpsTool/VMWarePlatformTests.java index 58e5773..8338abb 100644 --- a/tests/Tools/vmOpsTool/VMWarePlatformTests.java +++ b/tests/Tools/vmOpsTool/VMWarePlatformTests.java @@ -258,9 +258,9 @@ public abstract class VMWarePlatformTests { connData.setTargetDC(targetDC); vmWareImpl.powerOnVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true); + assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true); vmWareImpl.shutdownVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false); } @Test @@ -270,14 +270,14 @@ public abstract class VMWarePlatformTests { connData.setTargetDC(targetDC); vmWareImpl.powerOnVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true); + assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true); vmWareImpl.powerOnVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true); + assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true); vmWareImpl.shutdownVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false); vmWareImpl.shutdownVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false); } @Test @@ -287,10 +287,10 @@ public abstract class VMWarePlatformTests { connData.setTargetDC(targetDC); vmWareImpl.powerOnVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true); + assertThat(vmWareImpl.isVMPoweredOn(vmName, true, connData)).isEqualTo(true); vmWareImpl.powerOffVM(vmName, connData); - assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn(vmName, false, connData)).isEqualTo(false); } // Common for restore/delete snapshot operations diff --git a/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java b/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java index 87161d2..a28e1e5 100644 --- a/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java +++ b/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java @@ -68,20 +68,20 @@ public class VmOpsToolUnitTests { vmOpsTool.executeActionOnVmsInParallel(cmdArgs); - assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(true); - assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(true); + assertThat(vmWareImpl.isVMPoweredOn("vm1", true, connData)).isEqualTo(true); + assertThat(vmWareImpl.isVMPoweredOn("vm2", true, connData)).isEqualTo(true); cmdArgs = getCmdArgs("vm1", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION); vmOpsTool.executeActionOnVmsInParallel(cmdArgs); - assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn("vm1", false, connData)).isEqualTo(false); cmdArgs = getCmdArgs("vm2", Constants.POWER_OPS, Constants.POWER_OFF_VM_ACTION); vmOpsTool.executeActionOnVmsInParallel(cmdArgs); - assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn("vm2", false, connData)).isEqualTo(false); } @Test @@ -97,7 +97,7 @@ public class VmOpsToolUnitTests { } assertThat(exp).isNotNull(); - assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(true); + assertThat(vmWareImpl.isVMPoweredOn("vm1", true, connData)).isEqualTo(true); cmdArgs = getCmdArgs("vm1, VmThatFailsInShutdown", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION); @@ -109,7 +109,7 @@ public class VmOpsToolUnitTests { } assertThat(exp).isNotNull(); - assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn("vm1", false, connData)).isEqualTo(false); cmdArgs = getCmdArgs("vm2, VmThatFailsInPowerOff", Constants.POWER_OPS, Constants.POWER_OFF_VM_ACTION); @@ -121,7 +121,7 @@ public class VmOpsToolUnitTests { } assertThat(exp).isNotNull(); - assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(false); + assertThat(vmWareImpl.isVMPoweredOn("vm2", false, connData)).isEqualTo(false); } @Test