def test_power_management(self):
        # arrange
        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)

        uuid = pv_service.find_vm_by_name(si, 'QualiSB/Raz', '2').config.uuid
        power_manager = VirtualMachinePowerManagementCommand(pv_service,
                                                             SynchronousTaskWaiter())

        power_manager.power_on(si, uuid)
        power_manager.power_off(si, uuid)
Exemplo n.º 2
0
    def test_power_management(self):
        # arrange
        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)

        uuid = pv_service.find_vm_by_name(si, 'QualiSB/Raz', '2').config.uuid
        power_manager = VirtualMachinePowerManagementCommand(
            pv_service, SynchronousTaskWaiter())

        power_manager.power_on(si, uuid)
        power_manager.power_off(si, uuid)
    def test_power_off_already(self):
        vm_uuid = 'uuid'
        si = Mock(spec=vim.ServiceInstance)
        vm = Mock(spec=vim.VirtualMachine)
        vm.summary = Mock()
        vm.summary.runtime = Mock()
        vm.summary.runtime.powerState = 'poweredOff'
        session = Mock()
        pv_service = Mock()
        pv_service.find_by_uuid = Mock(return_value=vm)

        power_manager = VirtualMachinePowerManagementCommand(
            pv_service, Mock())

        # act
        res = power_manager.power_off(si=si,
                                      logger=Mock(),
                                      session=session,
                                      vcenter_data_model=Mock(),
                                      vm_uuid=vm_uuid,
                                      resource_fullname=None)

        # assert
        self.assertTrue(res, 'already powered off')
        self.assertFalse(vm.PowerOn.called)
    def test_power_off_soft(self):
        # arrange
        vcenter_name = 'vcenter name'
        vm_uuid = 'uuid'
        session = Mock()
        si = Mock(spec=vim.ServiceInstance)
        vm = Mock(spec=vim.VirtualMachine)
        task = Mock()
        pv_service = Mock()
        pv_service.find_by_uuid = Mock(return_value=vm)

        vm.PowerOff = Mock(return_value=task)

        synchronous_task_waiter = Mock()
        synchronous_task_waiter.wait_for_task = Mock(return_value=True)

        power_manager = VirtualMachinePowerManagementCommand(
            pv_service, synchronous_task_waiter)
        power_manager._connect_to_vcenter = Mock(return_value=si)
        power_manager._get_vm = Mock(return_value=vm)

        vcenter = Mock()
        vcenter.shutdown_method = 'soft'
        # act
        res = power_manager.power_off(si=si,
                                      logger=Mock(),
                                      session=session,
                                      vcenter_data_model=vcenter,
                                      vm_uuid=vm_uuid,
                                      resource_fullname=None)

        # assert
        self.assertTrue(res)
        self.assertTrue(vm.ShutdownGuest.called)
        self.assertTrue(
            power_manager._connect_to_vcenter.called_with(vcenter_name))
        self.assertTrue(power_manager._get_vm.called_with(si, vm_uuid))
        self.assertTrue(
            synchronous_task_waiter.wait_for_task.called_with(task))
    def test_power_off_hard(self):
        # arrange
        vcenter_name = 'vcenter name'
        vm_uuid = 'uuid'
        session = Mock()
        si = Mock(spec=vim.ServiceInstance)
        vm = Mock(spec=vim.VirtualMachine)
        task = Mock()
        pv_service = Mock()
        pv_service.find_by_uuid = Mock(return_value=vm)

        vm.PowerOff = Mock(return_value=task)

        synchronous_task_waiter = Mock()
        synchronous_task_waiter.wait_for_task = Mock(return_value=True)

        power_manager = VirtualMachinePowerManagementCommand(pv_service, synchronous_task_waiter)
        power_manager._connect_to_vcenter = Mock(return_value=si)
        power_manager._get_vm = Mock(return_value=vm)

        vcenter = Mock()
        vcenter.shutdown_method = 'hard'
        # act
        res = power_manager.power_off(si=si,
                                      logger=Mock(),
                                      session=session,
                                      vcenter_data_model=vcenter,
                                      vm_uuid=vm_uuid,
                                      resource_fullname=None)

        # assert
        self.assertTrue(res)
        self.assertTrue(vm.ShutdownGuest.called)
        self.assertTrue(power_manager._connect_to_vcenter.called_with(vcenter_name))
        self.assertTrue(power_manager._get_vm.called_with(si, vm_uuid))
        self.assertTrue(synchronous_task_waiter.wait_for_task.called_with(task))
    def test_power_off_already(self):
        vm_uuid = 'uuid'
        si = Mock(spec=vim.ServiceInstance)
        vm = Mock(spec=vim.VirtualMachine)
        vm.summary = Mock()
        vm.summary.runtime = Mock()
        vm.summary.runtime.powerState = 'poweredOff'
        session = Mock()
        pv_service = Mock()
        pv_service.find_by_uuid = Mock(return_value=vm)

        power_manager = VirtualMachinePowerManagementCommand(pv_service, Mock())

        # act
        res = power_manager.power_off(si=si,
                                      logger=Mock(),
                                      session=session,
                                      vcenter_data_model=Mock(),
                                      vm_uuid=vm_uuid,
                                      resource_fullname=None)

        # assert
        self.assertTrue(res, 'already powered off')
        self.assertFalse(vm.PowerOn.called)