@Test(dependsOnMethods = "testStartAndValidateVirtualMachines")
  public void testStopVirtualMachines() {
    for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
      String instanceName = vmName + "_" + i;
      IMachine machine = manager.getVBox().findMachine(instanceName);

      try {
        ISession machineSession = manager.openMachineSession(machine);
        IProgress progress = machineSession.getConsole().powerDown();
        progress.waitForCompletion(-1);
        machineSession.unlockMachine();

        while (!machine.getSessionState().equals(SessionState.Unlocked)) {
          try {
            System.out.println(
                "waiting for unlocking session - session state: " + machine.getSessionState());
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        assertEquals(machine.getState(), MachineState.PoweredOff);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  @Test
  public void testCloneMachineFromAnotherMachine() throws Exception {
    VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi();
    ComputeServiceContext localHostContext =
        computeServiceForLocalhostAndGuest(
            hostId, "localhost", guestId, "localhost", new Credentials("toor", "password"));

    IMachine master = getMasterNode(manager, localHostContext);

    if (master.getCurrentSnapshot() != null) {
      ISession session = manager.openMachineSession(master);
      session.getConsole().deleteSnapshot(master.getCurrentSnapshot().getId());
      session.unlockMachine();
    }

    IMachine clone =
        new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(
                manager,
                localHostContext,
                settingsFile,
                osTypeId,
                vmId,
                forceOverwrite,
                cloneName,
                hostId,
                snapshotName,
                snapshotDesc,
                controllerIDE)
            .apply(master);
    assertEquals(clone.getNetworkAdapter(0L).getAttachmentType(), Bridged);
  }
 @Test
 public void testEnsureMachineisLaunchedAndSessionIsUnlocked() {
   cloneFromMaster();
   ISession cloneMachineSession = machineController.ensureMachineIsLaunched(instanceName);
   assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
   cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
   assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
 }
 @Test(dependsOnMethods = "testEnsureMachineisLaunchedAndSessionIsUnlocked")
 public void testEnsureMachineCanBePoweredOffMoreThanOneTimeAndSessionIsUnlocked() {
   ISession cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
   cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName);
   assertTrue(cloneMachineSession.getState() == SessionState.Unlocked);
 }
 private void launchVMProcess(IMachine machine, ISession session) {
   IProgress prog = machine.launchVMProcess(session, "gui", "");
   prog.waitForCompletion(-1);
   session.unlockMachine();
 }