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_on(self):
        # arrange
        vm_uuid = 'uuid'
        si = Mock(spec=vim.ServiceInstance)
        vm = Mock(spec=vim.VirtualMachine)
        session = Mock()
        pv_service = Mock()
        pv_service.find_by_uuid = Mock(return_value=vm)

        task = Mock()

        vm.PowerOn = 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)

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

        # assert
        self.assertTrue(res)
        self.assertTrue(
            synchronous_task_waiter.wait_for_task.called_with(task))
        self.assertTrue(vm.PowerOn.called)
    def test_power_on(self):
        # arrange
        vm_uuid = 'uuid'
        si = Mock(spec=vim.ServiceInstance)
        vm = Mock(spec=vim.VirtualMachine)
        session = Mock()
        pv_service = Mock()
        pv_service.find_by_uuid = Mock(return_value=vm)

        task = Mock()

        vm.PowerOn = 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)

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

        # assert
        self.assertTrue(res)
        self.assertTrue(synchronous_task_waiter.wait_for_task.called_with(task))
        self.assertTrue(vm.PowerOn.called)
    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_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_on_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 = 'poweredOn'
        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_on(si=si,
                                     logger=Mock(),
                                     session=session,
                                     vm_uuid=vm_uuid,
                                     resource_fullname=None)

        # assert
        self.assertTrue(res, 'already powered on')
        self.assertFalse(vm.PowerOn.called)
    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))
Beispiel #8
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 __init__(self):
        """
        Initialize the driver session, this function is called everytime a new instance of the driver is created
        in here the driver is going to be bootstrapped

        :param context: models.QualiDriverModels.InitCommandContext
        """
        self.cs_helper = CloudshellDriverHelper()
        synchronous_task_waiter = SynchronousTaskWaiter()
        pv_service = pyVmomiService(connect=SmartConnect,
                                    disconnect=Disconnect,
                                    task_waiter=synchronous_task_waiter)
        self.resource_model_parser = ResourceModelParser()
        port_group_name_generator = DvPortGroupNameGenerator()

        vnic_to_network_mapper = VnicToNetworkMapper(
            quali_name_generator=port_group_name_generator)
        resource_remover = CloudshellResourceRemover()
        ovf_service = OvfImageDeployerService(self.resource_model_parser)

        self.vm_loader = VMLoader(pv_service)

        vm_deployer = VirtualMachineDeployer(
            pv_service=pv_service,
            name_generator=generate_unique_name,
            ovf_service=ovf_service,
            cs_helper=self.cs_helper,
            resource_model_parser=ResourceModelParser())

        dv_port_group_creator = DvPortGroupCreator(
            pyvmomi_service=pv_service,
            synchronous_task_waiter=synchronous_task_waiter)
        virtual_machine_port_group_configurer = \
            VirtualMachinePortGroupConfigurer(pyvmomi_service=pv_service,
                                              synchronous_task_waiter=synchronous_task_waiter,
                                              vnic_to_network_mapper=vnic_to_network_mapper,
                                              vnic_service=VNicService(),
                                              name_gen=port_group_name_generator)
        virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)
        # Command Wrapper
        self.command_wrapper = CommandWrapper(
            pv_service=pv_service,
            cloud_shell_helper=self.cs_helper,
            resource_model_parser=self.resource_model_parser,
            context_based_logger_factory=ContextBasedLoggerFactory())
        # Deploy Command
        self.deploy_command = DeployCommand(deployer=vm_deployer)

        # Virtual Switch Revoke
        self.virtual_switch_disconnect_command = \
            VirtualSwitchToMachineDisconnectCommand(
                pyvmomi_service=pv_service,
                port_group_configurer=virtual_machine_port_group_configurer,
                resource_model_parser=self.resource_model_parser)

        # Virtual Switch Connect
        virtual_switch_connect_command = \
            VirtualSwitchConnectCommand(
                pv_service=pv_service,
                virtual_switch_to_machine_connector=virtual_switch_to_machine_connector,
                dv_port_group_name_generator=DvPortGroupNameGenerator(),
                vlan_spec_factory=VlanSpecFactory(),
                vlan_id_range_parser=VLanIdRangeParser())
        self.connection_orchestrator = ConnectionCommandOrchestrator(
            connector=virtual_switch_connect_command,
            disconnector=self.virtual_switch_disconnect_command,
            resource_model_parser=self.resource_model_parser)

        # Destroy VM Command
        self.destroy_virtual_machine_command = \
            DestroyVirtualMachineCommand(pv_service=pv_service,
                                         resource_remover=resource_remover,
                                         disconnector=self.virtual_switch_disconnect_command)
        # Power Command
        self.vm_power_management_command = \
            VirtualMachinePowerManagementCommand(pyvmomi_service=pv_service,
                                                 synchronous_task_waiter=synchronous_task_waiter)

        ip_manager = VMIPManager()

        # Refresh IP command
        self.refresh_ip_command = RefreshIpCommand(
            pyvmomi_service=pv_service,
            resource_model_parser=ResourceModelParser(),
            ip_manager=ip_manager)

        # Save Snapshot
        self.snapshot_saver = SaveSnapshotCommand(
            pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter)

        # Snapshot Restorer
        self.snapshot_restorer = SnapshotRestoreCommand(
            pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter)

        self.snapshots_retriever = RetrieveSnapshotsCommand(
            pyvmomi_service=pv_service)