def test_deploy_image(self):
        # arrange

        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)
        service = OvfImageDeployerService('C:\\Program Files\\VMware\\VMware OVF Tool\\ovftool.exe', Mock())
        deployer = VirtualMachineDeployer(pv_service, generate_unique_name, service)

        # vm = pv_service.find_vm_by_name(si, 'QualiSB/Raz', '2')

        params = DeployDataHolder(
            {
                "image_url": "http://192.168.65.88/ovf/Debian%2064%20-%20Yoav.ovf",
                "cluster_name": "QualiSB Cluster",
                "resource_pool": "LiverPool",
                "datastore_name": "datastore1",
                "datacenter_name": "QualiSB",
                "power_on": True,
                "app_name": "appName 1",
                "vm_folder": "Raz 2"
            })

        deploy_command = DeployCommand(deployer)
        # act
        result = deploy_command.execute_deploy_from_image(si, params, cred)

        # assert
        self.assertIsNotNone(result)
    def test_deploy_template(self):
        # arrange

        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)
        deployer = VirtualMachineDeployer(pv_service, generate_unique_name)

        # vm = pv_service.find_vm_by_name(si, 'QualiSB/Raz', '2')

        deploy_params = DeployDataHolder({
            "template_model": {
                "vCenter_resource_name": "QualiSB",
                "vm_folder": "QualiSB/Raz",
                "template_name": "2"
            },
            "vm_cluster_model": {
                "cluster_name": "QualiSB Cluster",
                "resource_pool": "IT"
            },
            "datastore_name": "eric ds cluster",
            "power_on": False
        })

        deploy_command = DeployCommand(deployer)

        # act
        result = deploy_command.execute_deploy_from_template(si, deploy_params)

        # assert
        self.assertIsNotNone(result)
Beispiel #3
0
    def test_vnic_reconfig_task(self):
        vm = Mock()
        vm.ReconfigVM_Task = lambda x: isinstance(x, vim.vm.ConfigSpec)

        api_wrapper = pyVmomiService(Mock, Mock(), Mock())
        res = api_wrapper.vm_reconfig_task(vm, [])
        self.assertTrue(res)
    def test_get_vm_by_uuid_vm_with_path(self):
        """
        Checks whether the function can grab object by uuid
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        dc = Mock(spec=vim.Datacenter)
        pv_service.find_item_in_path_by_type = Mock(return_value=dc)

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())

        si.content.searchIndex = Mock()
        si.content.searchIndex.FindByUuid = Mock(
            return_value='b8e4da4e-a2ff-11e5-bf7f-feff819cdc9f')

        '#act'
        result = pv_service.find_by_uuid(
            si, 'b8e4da4e-a2ff-11e5-bf7f-feff819cdc9f', True, 'path/path/path')

        '#assert'
        self.assertTrue(result)
        self.assertTrue(
            pv_service.find_item_in_path_by_type.called_with(
                si, 'path/path/path', vim.Datacenter))
Beispiel #5
0
    def integrationtest(self):
        resource_connection_details_retriever = Mock()
        credentials = TestCredentials()
        resource_connection_details_retriever.connection_details = Mock(
            return_value=VCenterConnectionDetails(
                credentials.host, credentials.username, credentials.password))
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        synchronous_task_waiter = SynchronousTaskWaiter()
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service,
                                                   synchronous_task_waiter)

        pg_name_generator = DvPortGroupNameGenerator
        vnic_to_network_mapper = VnicToNetworkMapper(pg_name_generator)
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(
            py_vmomi_service, synchronous_task_waiter, vnic_to_network_mapper,
            VNicService())

        virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)

        si = py_vmomi_service.connect(credentials.host, credentials.username,
                                      credentials.password, credentials.port)

        virtual_machine_path = 'Boris'
        virtual_machine_name = 'boris1'
        vm = py_vmomi_service.get_obj(si.content, [vim.VirtualMachine],
                                      virtual_machine_name)
        vm_uuid = vm.config.uuid
        #vm_uuid = self.get_vm_uuid(py_vmomi_service, si, virtual_machine_name)
        dv_switch_path = 'QualiSB'
        dv_switch_name = 'dvSwitch'
        dv_port_name = 'boris_group59'
    def test_deploy_template(self):
        # arrange

        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)
        deployer = VirtualMachineDeployer(pv_service, generate_unique_name)

        # vm = pv_service.find_vm_by_name(si, 'QualiSB/Raz', '2')

        deploy_params = DeployDataHolder({
            "template_model": {
                "vCenter_resource_name": "QualiSB",
                "vm_folder": "QualiSB/Raz",
                "template_name": "2"
            },
            "vm_cluster_model": {
                "cluster_name": "QualiSB Cluster",
                "resource_pool": "IT"
            },
            "datastore_name": "eric ds cluster",
            "power_on": False
        })

        deploy_command = DeployCommand(deployer)

        # act
        result = deploy_command.execute_deploy_from_template(si, deploy_params)

        # assert
        self.assertIsNotNone(result)
    def integration_test_connect_B(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port)
        vm_uuid = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Boris', 'Boris2-win7').config.uuid

        mapping = VmNetworkMapping()
        mapping.vlan_id = '114'
        # mapping.dv_port_name = 65
        mapping.dv_switch_name = 'dvSwitch'
        mapping.dv_switch_path = 'QualiSB'
        mapping.port_group_path = 'QualiSB'
        mapping.vlan_spec = 'Trunk'

        vlan_spec = VlanSpecFactory()
        range_fac = VLanIdRangeParser()
        synchronous_task_waiter = SynchronousTaskWaiter()
        name_gen = DvPortGroupNameGenerator()
        mapper = VnicToNetworkMapper(name_gen)
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter)
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(py_vmomi_service,
                                                                                  synchronous_task_waiter,
                                                                                  mapper,
                                                                                  VNicService())
        connector = VirtualSwitchToMachineConnector(dv_port_group_creator, virtual_machine_port_group_configurer)

        command = VirtualSwitchConnectCommand(py_vmomi_service, connector, name_gen, vlan_spec, range_fac, Mock())

        command.connect_to_networks(si, vm_uuid, [mapping], 'QualiSB/anetwork', [])
    def integration_test_connect_B(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password,
                                      cred.port)
        vm_uuid = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Boris',
                                                   'Boris2-win7').config.uuid

        mapping = VmNetworkMapping()
        mapping.vlan_id = '114'
        # mapping.dv_port_name = 65
        mapping.dv_switch_name = 'dvSwitch'
        mapping.dv_switch_path = 'QualiSB'
        mapping.vlan_spec = 'Trunk'

        vlan_spec = VlanSpecFactory()
        range_fac = VLanIdRangeParser()
        synchronous_task_waiter = SynchronousTaskWaiter()
        name_gen = DvPortGroupNameGenerator()
        mapper = VnicToNetworkMapper(name_gen)
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service,
                                                   synchronous_task_waiter)
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(
            py_vmomi_service, synchronous_task_waiter, mapper, VNicService())
        connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)

        command = VirtualSwitchConnectCommand(py_vmomi_service, connector,
                                              name_gen, vlan_spec, range_fac,
                                              Mock())

        command.connect_to_networks(si, vm_uuid, [mapping], 'QualiSB/anetwork',
                                    [])
    def integration_test_connect_A(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port)
        synchronous_task_waiter = SynchronousTaskWaiter()
        mapper = VnicToNetworkMapper(DvPortGroupNameGenerator())
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter)
        port_group_name_generator = DvPortGroupNameGenerator()
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(py_vmomi_service,
                                                                                  synchronous_task_waiter,
                                                                                  mapper,
                                                                                  VNicService(),
                                                                                  port_group_name_generator)

        mapping = VmNetworkMapping()
        mapping.vlan_id = [vim.NumericRange(start=65, end=65)]
        mapping.dv_port_name = DvPortGroupNameGenerator().generate_port_group_name(65, 'Trunk')
        mapping.dv_switch_name = 'dvSwitch'
        mapping.dv_switch_path = 'QualiSB'
        mapping.port_group_path = 'QualiSB'
        mapping.vlan_spec = vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec()
        connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator,
            virtual_machine_port_group_configurer)

        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Raz', '2')

        # Act
        connector.connect_by_mapping(si, vm, [mapping], None, [])

        pass
    def __init__(self, resource_config, cs_api, vcenter_name, logger):
        """

        :param resource_config:
        :param cs_api:
        :param vcenter_name:
        :param logger:
        """
        self._resource_config = resource_config
        self._cs_api = cs_api
        self._logger = logger
        self._vcenter_service = pyVmomiService(SmartConnect,
                                               Disconnect,
                                               task_waiter=None)

        self._vcenter_si = self._get_vcenter_si(vcenter_service=self._vcenter_service,
                                                cs_api=cs_api,
                                                vcenter_name=vcenter_name)

        vm_uid = self._get_vm_uid(resource_config=resource_config,
                                  cs_api=cs_api,
                                  logger=logger)

        self._vm = self._get_vm(vcenter_service=self._vcenter_service,
                                vm_uid=vm_uid)

        self._vm_creds = self._get_vm_creds(resource_config=resource_config, cs_api=cs_api)
    def test_vnic_reconfig_task(self):
        vm = Mock()
        vm.ReconfigVM_Task = lambda x: isinstance(x,  vim.vm.ConfigSpec)

        api_wrapper = pyVmomiService(Mock, Mock(), Mock())
        res = api_wrapper.vm_reconfig_task(vm, [])
        self.assertTrue(res)
    def test_vm_get_network_by_name_1(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        network = Mock()
        network.name = 'main_network'

        backing = Mock()
        backing.network = network

        virtual_card = create_autospec(vim.vm.device.VirtualEthernetCard)
        virtual_card.macAddress = 'AA-BB'
        virtual_card.backing = backing

        hardware = Mock()
        hardware.device = [virtual_card]

        config = Mock()
        config.hardware = hardware

        vm = Mock()
        vm.config = config

        # Act
        actual_network = pv_service.get_network_by_mac_address(vm, 'BB-CC')

        # Assert
        self.assertIsNone(actual_network)
    def integrationtest(self):
        resource_connection_details_retriever = Mock()
        credentials = TestCredentials()
        resource_connection_details_retriever.connection_details = Mock(
                return_value=VCenterConnectionDetails(credentials.host, credentials.username, credentials.password))
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        synchronous_task_waiter = SynchronousTaskWaiter()
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter)

        pg_name_generator = DvPortGroupNameGenerator
        vnic_to_network_mapper = VnicToNetworkMapper(pg_name_generator)
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(py_vmomi_service,
                                                                                  synchronous_task_waiter,
                                                                                  vnic_to_network_mapper,
                                                                                  VNicService())

        virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector(dv_port_group_creator,
                                                                              virtual_machine_port_group_configurer)

        si = py_vmomi_service.connect(credentials.host, credentials.username,
                                      credentials.password,
                                      credentials.port)



        virtual_machine_path = 'Boris'
        virtual_machine_name = 'boris1'
        vm = py_vmomi_service.get_obj(si.content, [vim.VirtualMachine], virtual_machine_name)
        vm_uuid = vm.config.uuid
        #vm_uuid = self.get_vm_uuid(py_vmomi_service, si, virtual_machine_name)
        dv_switch_path = 'QualiSB'
        dv_switch_name = 'dvSwitch'
        dv_port_name = 'boris_group59'
    def integration_clone_vm_destory(self):
        """
        Checks whether clone_vm and destroy methods works
        """
        '#arrange'
        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)

        params = pv_service.CloneVmParameters(si=si,
                                              template_name='DC0_C0_RP0_VM20',
                                              vm_name='my_clone',
                                              vm_folder='DC0')
        '#act'
        now = datetime.now()
        res = pv_service.clone_vm(clone_params=params,
                                  logger=Mock())

        '#assert'
        self.assertTrue(type(res.vm), vim.VirtualMachine)

        '#teardown'
        now = datetime.now()
        if res.error is None and res.vm is not None:
            destroyed = pv_service.destroy_vm(vm=res.vm, logger=Mock())

        print 'destroy took: {0}'.format(str(datetime.now() - now))

        self.assertIsNone(destroyed)
Beispiel #15
0
    def test_integrationtest_refresh_ip(self):
        resource_context = Mock()
        resource_context.attributes = {
            "vCenter Template": "vCenter/Boris/Boris2-win7"
        }
        qualipy_helpers = Mock()
        qualipy_helpers.get_resource_context_details = Mock(
            return_value=resource_context)

        credentials = TestCredentials()
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        si = py_vmomi_service.connect(credentials.host, credentials.username,
                                      credentials.password)
        cloudshell_data_retriever_service = Mock()
        cloudshell_data_retriever_service.getVCenterConnectionDetails = Mock(
            return_value=VCenterConnectionDetails(
                credentials.host, credentials.username, credentials.password))

        vm_resource = Mock()
        vm_resource.default_network = 'default'

        resource_model_parser = Mock()
        resource_model_parser.convert_to_resource_model = Mock(
            return_value=vm_resource)

        refresh_ip_command = RefreshIpCommand(
            py_vmomi_service, cloudshell_data_retriever_service,
            qualipy_helpers, resource_model_parser)

        # uuid = helpers.get_uuid('VM Deployment_afee8038')
        ip = refresh_ip_command.refresh_ip(
            si, '4222f613-e714-6204-8095-e282796985fb',
            'VM Deployment_afee8038')
        print ip
        pass
 def __init__(self):
     # self.cs_helper = CloudshellDriverHelper()
     self.model_parser = ResourceModelParser()
     self.ip_manager = VMIPManager()
     self.task_waiter = SynchronousTaskWaiter()
     self.pv_service = pyVmomiService(SmartConnect, Disconnect,
                                      self.task_waiter)
    def integration_test_connect_A(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password,
                                      cred.port)
        synchronous_task_waiter = SynchronousTaskWaiter()
        mapper = VnicToNetworkMapper(DvPortGroupNameGenerator())
        dv_port_group_creator = DvPortGroupCreator(py_vmomi_service,
                                                   synchronous_task_waiter)
        port_group_name_generator = DvPortGroupNameGenerator()
        virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer(
            py_vmomi_service, synchronous_task_waiter, mapper, VNicService(),
            port_group_name_generator)

        mapping = VmNetworkMapping()
        mapping.vlan_id = [vim.NumericRange(start=65, end=65)]
        mapping.dv_port_name = DvPortGroupNameGenerator(
        ).generate_port_group_name(65, 'Trunk')
        mapping.dv_switch_name = 'dvSwitch'
        mapping.dv_switch_path = 'QualiSB'
        mapping.vlan_spec = vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec(
        )
        connector = VirtualSwitchToMachineConnector(
            dv_port_group_creator, virtual_machine_port_group_configurer)

        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Raz', '2')

        # Act
        connector.connect_by_mapping(si, vm, [mapping], None, [])

        pass
    def test_deploy_image(self):
        # arrange

        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)
        service = OvfImageDeployerService(
            'C:\\Program Files\\VMware\\VMware OVF Tool\\ovftool.exe', Mock())
        deployer = VirtualMachineDeployer(pv_service, generate_unique_name,
                                          service)

        # vm = pv_service.find_vm_by_name(si, 'QualiSB/Raz', '2')

        params = DeployDataHolder({
            "image_url": "http://192.168.65.88/ovf/Debian%2064%20-%20Yoav.ovf",
            "cluster_name": "QualiSB Cluster",
            "resource_pool": "LiverPool",
            "datastore_name": "datastore1",
            "datacenter_name": "QualiSB",
            "power_on": True,
            "app_name": "appName 1",
            "vm_folder": "Raz 2"
        })

        deploy_command = DeployCommand(deployer)
        # act
        result = deploy_command.execute_deploy_from_image(si, params, cred)

        # assert
        self.assertIsNotNone(result)
    def test_destroy_vm_by_uuid(self):
        """
        Checks whether the vm found and call to be destroy
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())
        si.content.searchIndex = Mock()

        pv_service.find_by_uuid = Mock(return_value=Mock(name='vm'))
        pv_service.destroy_vm = Mock(return_value=True)

        '#act'
        result = pv_service.destroy_vm_by_uuid(
            si=si,
            vm_uuid='thisuni-vers-ally-uniq-ueidentifier',
            vm_path='fake/path',
            logger=Mock())

        '#assert'
        self.assertTrue(result)
        self.assertTrue(pv_service.find_by_uuid.called)
        self.assertTrue(pv_service.destroy_vm.called)
    def test_get_object_by_path_checks_childEntity(self):
        """
        Checks whether the function can grab from child entities
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        def search_child(*args, **keys):
            if args[0].name == pv_service.ChildEntity:
                return True
            return False

        class FolderMock:
            childEntity = None

        folder = Mock(spec=FolderMock())
        folder.name = pv_service.ChildEntity
        get_folder = MagicMock(return_value=folder)
        pv_service.get_folder = get_folder

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())
        si.content.searchIndex = Mock()
        si.content.searchIndex.FindChild = MagicMock(side_effect=search_child)

        '#act'
        result = pv_service.find_obj_by_path(si, '', '', '')

        '#assert'
        self.assertTrue(result)
    def test_find_item_in_path_by_type_complex_path(self):
        """
        Checks whether the function can grab object by uuid
        """
        '#arrange'

        class counter:
            i = 0

        def side_eff(*args, **kwargs):
            if counter.i != 1:
                counter.i = counter.i + 1
                return 'not yet'
            else:
                counter.i = counter.i + 1
                return Mock(spec=vim.Datacenter)

        pv_service = pyVmomiService(None, None, Mock())

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())

        si.content.searchIndex = Mock()
        si.content.rootFolder = Mock()
        si.content.searchIndex.FindChild = Mock(side_effect=side_eff)

        '#act'
        result = pv_service.find_item_in_path_by_type(si, 'test//dc/asd',
                                                      vim.Datacenter)

        '#assert'
        self.assertTrue(isinstance(result, vim.Datacenter))
        self.assertTrue(counter.i, 1)
    def test_vm_get_network_by_name_1(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        network = Mock()
        network.name = 'main_network'

        backing = Mock()
        backing.network = network

        virtual_card = create_autospec(vim.vm.device.VirtualEthernetCard)
        virtual_card.macAddress = 'AA-BB'
        virtual_card.backing = backing

        hardware = Mock()
        hardware.device = [virtual_card]

        config = Mock()
        config.hardware = hardware

        vm = Mock()
        vm.config = config

        # Act
        actual_network = pv_service.get_network_by_mac_address(vm, 'BB-CC')

        # Assert
        self.assertIsNone(actual_network)
    def integration_clone_vm_destory(self):
        """
        Checks whether clone_vm and destroy methods works
        """
        '#arrange'
        cred = TestCredentials()
        pv_service = pyVmomiService(SmartConnect, Disconnect)
        si = pv_service.connect(cred.host, cred.username, cred.password)

        params = pv_service.CloneVmParameters(si=si,
                                              template_name='DC0_C0_RP0_VM20',
                                              vm_name='my_clone',
                                              vm_folder='DC0')
        '#act'
        now = datetime.now()
        res = pv_service.clone_vm(clone_params=params, logger=Mock())

        '#assert'
        self.assertTrue(type(res.vm), vim.VirtualMachine)

        '#teardown'
        now = datetime.now()
        if res.error is None and res.vm is not None:
            destroyed = pv_service.destroy_vm(vm=res.vm, logger=Mock())

        print 'destroy took: {0}'.format(str(datetime.now() - now))

        self.assertIsNone(destroyed)
    def test_get_object_by_path_checks_childEntity(self):
        """
        Checks whether the function can grab from child entities
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        def search_child(*args, **keys):
            if args[0].name == pv_service.ChildEntity:
                return True
            return False

        class FolderMock:
            childEntity = None

        folder = Mock(spec=FolderMock())
        folder.name = pv_service.ChildEntity
        get_folder = MagicMock(return_value=folder)
        pv_service.get_folder = get_folder

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())
        si.content.searchIndex = Mock()
        si.content.searchIndex.FindChild = MagicMock(side_effect=search_child)

        '#act'
        result = pv_service.find_obj_by_path(si, '', '', '')

        '#assert'
        self.assertTrue(result)
 def __init__(self):
     self.cs_helper = CloudshellDriverHelper()
     self.model_parser = ResourceModelParser()
     self.ip_manager = VMIPManager()
     self.task_waiter = SynchronousTaskWaiter()
     self.logger = get_qs_logger('VM AutoLoad')
     self.pv_service = pyVmomiService(SmartConnect, Disconnect, self.task_waiter)
Beispiel #26
0
    def integrationtest_refresh_ip(self):
        resource_context = Mock()
        resource_context.attributes = {"vCenter Template": "vCenter/Alex/test"}
        qualipy_helpers = Mock()
        qualipy_helpers.get_resource_context_details = Mock(
            return_value=resource_context)

        credentials = TestCredentials()
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cloudshell_data_retriever_service = Mock()
        cloudshell_data_retriever_service.getVCenterConnectionDetails = Mock(
            return_value=VCenterConnectionDetails(
                credentials.host, credentials.username, credentials.password))

        vm_resource = Mock()
        vm_resource.default_network = 'default'

        resource_model_parser = Mock()
        resource_model_parser.convert_to_resource_model = Mock(
            return_value=vm_resource)

        refresh_ip_command = RefreshIpCommand(
            py_vmomi_service, cloudshell_data_retriever_service,
            qualipy_helpers, resource_model_parser)

        uuid = helpers.get_uuid('vCenter-Server-Appliance-yaniv')
        refresh_ip_command.refresh_ip(uuid, '')

        pass
Beispiel #27
0
 def __init__(self, method_ame='runTest'):
     super(SearchObjectsPerfTest, self).__init__(method_ame)
     cred = TestCredentials()
     self.pv_service = pyVmomiService(SmartConnect, Disconnect)
     self.si = self.pv_service.connect(cred.host, cred.username,
                                       cred.password)
     self.logger = get_qs_logger()
    def test_find_item_in_path_by_type_complex_path(self):
        """
        Checks whether the function can grab object by uuid
        """
        '#arrange'

        class counter:
            i = 0

        def side_eff(*args, **kwargs):
            if counter.i != 1:
                counter.i = counter.i + 1;
                return 'not yet'
            else:
                counter.i = counter.i + 1;
                return Mock(spec=vim.Datacenter)

        pv_service = pyVmomiService(None, None, Mock())

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())

        si.content.searchIndex = Mock()
        si.content.rootFolder = Mock()
        si.content.searchIndex.FindChild = Mock(side_effect=side_eff)

        '#act'
        result = pv_service.find_item_in_path_by_type(si, 'test//dc/asd', vim.Datacenter)

        '#assert'
        self.assertTrue(isinstance(result, vim.Datacenter))
        self.assertTrue(counter.i, 1)
Beispiel #29
0
def get_uuid(virtual_machine_name):
    credentials = TestCredentials()
    py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
    si = py_vmomi_service.connect(credentials.host, credentials.username,
                                  credentials.password, credentials.port)
    vm_uuid = get_vm_uuid(py_vmomi_service, si, virtual_machine_name)
    return vm_uuid
    def test_get_folder_deep_and_complex_path(self):
        """
        Checks when path is deep and complex, goes through all the folder types
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        def find_child_mock(*args):
            root = args[0]
            if hasattr(root, pv_service.ChildEntity):
                for folder in root.childEntity:
                    if folder.name == args[1]:
                        return folder
            else:
                for folder in root:
                    if folder.name == args[1]:
                        return folder
            return None

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())
        si.content.searchIndex = Mock()
        si.content.searchIndex.FindChild = MagicMock(
            side_effect=find_child_mock)

        first_folder = Mock(spec=[], name='first')
        first_folder.name = 'first'

        second_folder = Mock(spec=[], name='second')
        second_folder.name = 'second'

        third_folder = Mock(spec=[], name='third')
        third_folder.name = 'third'

        fourth_folder = Mock(spec=[], name='fourth')
        fourth_folder.name = 'fourth'

        fifth_folder = Mock(spec=[], name='fifth')
        fifth_folder.name = 'fifth'

        sixth_folder = Mock(spec=[], name='sixth')
        sixth_folder.name = 'sixth'

        si.content.rootFolder = Mock()
        si.content.rootFolder.name = 'rootFolder'
        si.content.rootFolder.childEntity = [first_folder, second_folder]
        first_folder.vmFolder = [second_folder, sixth_folder]
        second_folder.networkFolder = [fourth_folder, third_folder]
        third_folder.hostFolder = [third_folder, fourth_folder]
        fourth_folder.datacenterFolder = [fifth_folder]
        fifth_folder.datastoreFolder = [sixth_folder]

        '#act'
        result = pv_service.get_folder(
            si, 'first/second/third/fourth/fifth/sixth')

        '#assert'
        self.assertEqual(result, sixth_folder)
Beispiel #31
0
def get_uuid(virtual_machine_name):
    credentials = TestCredentials()
    py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
    si = py_vmomi_service.connect(credentials.host, credentials.username,
                                  credentials.password,
                                  credentials.port)
    vm_uuid = get_vm_uuid(py_vmomi_service, si, virtual_machine_name)
    return vm_uuid
Beispiel #32
0
    def test_vm_ware_tools(self):
        credentials = TestCredentials()
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        si = py_vmomi_service.connect(credentials.host, credentials.username,
                                      credentials.password)
        vm = py_vmomi_service.get_vm_by_uuid(
            si, '4222f613-e714-6204-8095-e282796985fb')

        self.assertIsNotNone(vm.guest)
Beispiel #33
0
 def __init__(self):
     super(PaloAltoStaticShellDriver, self).__init__()
     self._cli = None
     self.cs_helper = CloudshellDriverHelper()
     self.model_parser = ResourceModelParser()
     self.ip_manager = VMIPManager()
     self.task_waiter = SynchronousTaskWaiter()
     self.pv_service = pyVmomiService(SmartConnect, Disconnect,
                                      self.task_waiter)
    def test_get_folder_deep_and_complex_path(self):
        """
        Checks when path is deep and complex, goes through all the folder types
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        def find_child_mock(*args):
            root = args[0]
            if hasattr(root, pv_service.ChildEntity):
                for folder in root.childEntity:
                    if folder.name == args[1]:
                        return folder
            else:
                for folder in root:
                    if folder.name == args[1]:
                        return folder
            return None

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())
        si.content.searchIndex = Mock()
        si.content.searchIndex.FindChild = MagicMock(side_effect=find_child_mock)

        first_folder = Mock(spec=[], name='first')
        first_folder.name = 'first'

        second_folder = Mock(spec=[], name='second')
        second_folder.name = 'second'

        third_folder = Mock(spec=[], name='third')
        third_folder.name = 'third'

        fourth_folder = Mock(spec=[], name='fourth')
        fourth_folder.name = 'fourth'

        fifth_folder = Mock(spec=[], name='fifth')
        fifth_folder.name = 'fifth'

        sixth_folder = Mock(spec=[], name='sixth')
        sixth_folder.name = 'sixth'

        si.content.rootFolder = Mock()
        si.content.rootFolder.name = 'rootFolder'
        si.content.rootFolder.childEntity = [first_folder, second_folder]
        first_folder.vmFolder = [second_folder, sixth_folder]
        second_folder.networkFolder = [fourth_folder, third_folder]
        third_folder.hostFolder = [third_folder, fourth_folder]
        fourth_folder.datacenterFolder = [fifth_folder]
        fifth_folder.datastoreFolder = [sixth_folder]

        '#act'
        result = pv_service.get_folder(si, 'first/second/third/fourth/fifth/sixth')

        '#assert'
        self.assertEqual(result, sixth_folder)
    def test_get_snapshot_no_snapshot_param(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        # Act
        actual_network = pv_service._get_snapshot(Mock(spec=[]), Mock)

        # Assert
        self.assertIsNone(actual_network)
    def test_get_snapshot_no_snapshot_param(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        # Act
        actual_network = pv_service._get_snapshot(Mock(spec=[]), Mock)

        # Assert
        self.assertIsNone(actual_network)
    def test_test(self):

        dev_helpers.attach_to_cloudshell_as("admin", "admin", "Global", "8d36098c-6dd0-4d47-8ad8-b159191e3f63")
        details = helpers.get_reservation_context_details()
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port)
        vm = py_vmomi_service.find_by_uuid(si, '4222941e-a02d-dc78-80f6-44b88e0cb24f')
        network = py_vmomi_service.get_network_by_mac_address(vm, '00:50:56:a2:06:87')

        print network.name
    def test_disconnect(self):
        # arrange
        pv_service = pyVmomiService(None, None, Mock())
        si = create_autospec(spec=vim.ServiceInstance)
        pv_service.pyvmomi_disconnect = Mock()

        # act
        pv_service.disconnect(si)

        # assert
        self.assertTrue(pv_service.pyvmomi_disconnect.called)
    def test_disconnect(self):
        # arrange
        pv_service = pyVmomiService(None, None, Mock())
        si = create_autospec(spec=vim.ServiceInstance)
        pv_service.pyvmomi_disconnect = Mock()

        # act
        pv_service.disconnect(si)

        # assert
        self.assertTrue(pv_service.pyvmomi_disconnect.called)
Beispiel #40
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_get_network_by_full_name(self):
        # arrange
        pv_service = pyVmomiService(None, None, Mock())
        si = create_autospec(spec=vim.ServiceInstance)
        default_network_full_name = 'Root/Folder/Folder2/Name'
        pv_service.find_network_by_name = Mock()

        # act
        pv_service.get_network_by_full_name(si, default_network_full_name)

        # assert
        self.assertTrue(pv_service.find_network_by_name.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_get_network_by_full_name(self):
        # arrange
        pv_service = pyVmomiService(None, None, Mock())
        si = create_autospec(spec=vim.ServiceInstance)
        default_network_full_name = 'Root/Folder/Folder2/Name'
        pv_service.find_network_by_name = Mock()

        # act
        pv_service.get_network_by_full_name(si, default_network_full_name)

        # assert
        self.assertTrue(pv_service.find_network_by_name.called)
Beispiel #44
0
    def test_dicconnect_bulk(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port)
        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Alex', 'test_25bf07ee')
        mac_address = '00:50:56:a2:5f:43'
        vnics = [device.backing.network for device in vm.config.hardware.device
                 if isinstance(device, vim.vm.device.VirtualEthernetCard)
                 and hasattr(device.backing, 'network')
                 and hasattr(device, 'macAddress')
                 and device.macAddress == mac_address]

        pass
    def test_dicconnect_bulk(self):
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port)
        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Alex', 'test_25bf07ee')
        mac_address = '00:50:56:a2:5f:43'
        vnics = [device.backing.network for device in vm.config.hardware.device
                 if isinstance(device, vim.vm.device.VirtualEthernetCard)
                 and hasattr(device.backing, 'network')
                 and hasattr(device, 'macAddress')
                 and device.macAddress == mac_address]

        pass
    def test_connect(self):
        # arrange
        pv_service = pyVmomiService(SmartConnect, Disconnect, Mock())
        address = Mock()
        user = Mock()
        password = Mock()
        pv_service.pyvmomi_connect = Mock()

        # act
        pv_service.connect(address, user, password)

        # assert
        self.assertTrue(pv_service.pyvmomi_connect.called)
    def test_connect(self):
        # arrange
        pv_service = pyVmomiService(SmartConnect, Disconnect, Mock())
        address = Mock()
        user = Mock()
        password = Mock()
        pv_service.pyvmomi_connect = Mock()

        # act
        pv_service.connect(address, user, password)

        # assert
        self.assertTrue(pv_service.pyvmomi_connect.called)
    def test_get_snapshot_snapshot_has_no_root(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        params = Mock()
        params.snapshot = 'aa/bb/ee'

        template = Mock(vim.VirtualMachine)
        template.snapshot = Mock()
        template.snapshot.rootSnapshotList = None

        # Act
        self.assertRaises(ValueError, pv_service._get_snapshot, params, template)
    def integration_test(self):

        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port)
        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Alex', 'Ubuntu_a287f573')

        os.environ['VM_UUID'] = vm.config.uuid
        os.environ['VLAN_ID'] = '25'
        os.environ['VLAN_SPEC_TYPE'] = 'Access'

        bootstrapper = Bootstrapper()
        executer_service = bootstrapper.get_command_executer_service()
        executer_service.connect()
Beispiel #50
0
    def test_test(self):

        dev_helpers.attach_to_cloudshell_as(
            "admin", "admin", "Global", "8d36098c-6dd0-4d47-8ad8-b159191e3f63")
        details = helpers.get_reservation_context_details()
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password,
                                      cred.port)
        vm = py_vmomi_service.find_by_uuid(
            si, '4222941e-a02d-dc78-80f6-44b88e0cb24f')
        network = py_vmomi_service.get_network_by_mac_address(
            vm, '00:50:56:a2:06:87')

        print network.name
    def test_get_snapshot_snapshot_has_no_root(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        params = Mock()
        params.snapshot = 'aa/bb/ee'

        template = Mock(vim.VirtualMachine)
        template.snapshot = Mock()
        template.snapshot.rootSnapshotList = None

        # Act
        self.assertRaises(ValueError, pv_service._get_snapshot, params,
                          template)
Beispiel #52
0
    def integration_test(self):

        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password,
                                      cred.port)
        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Alex',
                                              'Ubuntu_a287f573')

        os.environ['VM_UUID'] = vm.config.uuid
        os.environ['VLAN_ID'] = '25'
        os.environ['VLAN_SPEC_TYPE'] = 'Access'

        bootstrapper = Bootstrapper()
        executer_service = bootstrapper.get_command_executer_service()
        executer_service.connect()
    def test_clone_vm_si_is_none(self):
        """
        Checks clone_vm
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())
        params = pv_service.CloneVmParameters(si=None,
                                              template_name=None,
                                              vm_name=None,
                                              vm_folder=None)

        '#act'
        res = pv_service.clone_vm(clone_params=params, logger=Mock())

        '#assert'
        self.assertTrue(res.error is not None)
    def test_get_object_by_path_no_nested_objs(self):
        """
        Checks whether the function returns 'None' if it doesn't find an object
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        get_folder = MagicMock(return_value=Mock(spec=[]))
        pv_service.get_folder = get_folder

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())

        '#act'
        self.assertRaises(ValueError, pv_service.find_obj_by_path, si, '', '', '')
    def test_get_vm_by_uuid_vm_without_uuid(self):
        """
        Checks whether the function can grab object by uuid
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())

        '#act'
        result = pv_service.find_by_uuid(si, None)

        '#assert'
        self.assertIsNone(result)
    def test_destroy_vm(self):
        # arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()
        vm = Mock()
        vm.runtime = Mock()
        vm.runtime.powerState = 'poweredOn'
        vm.PowerOffVM_Task = Mock()
        vm.Destroy_Task = Mock()

        # act
        pv_service.destroy_vm(vm=vm, logger=Mock())

        # assert
        self.assertTrue(vm.PowerOffVM_Task.called)
        self.assertTrue(vm.Destroy_Task.called)
    def test_get_snapshot_snapshot(self):
        # Arrange
        pv_service = pyVmomiService(None, None, Mock())
        pv_service.wait_for_task = Mock()

        params = Mock()
        params.snapshot = 'aa/bb/ee'

        template = Mock(vim.VirtualMachine)
        template.snapshot = Mock()

        aa = Mock(spec=[])
        bb1 = Mock(spec=[])
        bb2 = Mock(spec=[])
        cc = Mock(spec=[])
        dd = Mock(spec=[])
        ee = Mock(spec=[])
        aa.name = 'aa'
        aa.createTime = 1

        bb1.name = 'bb'
        bb1.createTime = 1

        bb2.name = 'bb'
        bb2.createTime = 2

        cc.name = 'cc'
        cc.createTime = 1

        dd.name = 'dd'
        dd.createTime = 1

        ee.name = 'ee'
        ee.createTime = 1

        ee.snapshot = Mock()

        aa.childSnapshotList = [dd, bb1, bb2]
        dd.childSnapshotList = [cc]
        bb2.childSnapshotList = [dd, cc, ee]

        template.snapshot.rootSnapshotList = [aa]

        # Act
        res = pv_service._get_snapshot(params, template)

        self.assertEqual(res, ee.snapshot)
    def integration_test_update_vnics(self):


        dev_helpers.attach_to_cloudshell_as("admin", "admin", "Global", "1205e711-edf7-4b12-8a5e-e0ff53768ce7")
        details = helpers.get_reservation_context_details()
        py_vmomi_service = pyVmomiService(SmartConnect, Disconnect)
        cred = TestCredentials()
        si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port)
        vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Alex', 'Ubuntu_a287f573')

        nics = [x for x in vm.config.hardware.device
                if isinstance(x, vim.vm.device.VirtualEthernetCard)]

        for nic in nics:
            network_name = nic.backing.network.name
            mac_address = nic.macAddress

            print network_name + mac_address
    def test_clone_vm_template_name_is_none(self):
        """
        Checks clone_vm
        """
        '#arrange'
        si = create_autospec(spec=vim.ServiceInstance)

        pv_service = pyVmomiService(None, None, Mock())
        params = pv_service.CloneVmParameters(si=si,
                                              template_name=None,
                                              vm_name=None,
                                              vm_folder=None)

        '#act'
        res = pv_service.clone_vm(clone_params=params, logger=Mock())

        '#assert'
        self.assertTrue(res.error is not None)
    def test_find_item_in_path_by_type_path_None(self):
        """
        Checks whether the function can grab object by uuid
        """
        '#arrange'
        pv_service = pyVmomiService(None, None, Mock())

        si = create_autospec(spec=vim.ServiceInstance)
        si.RetrieveContent = Mock()
        si.content = create_autospec(spec=vim.ServiceInstanceContent())

        si.content.searchIndex = Mock()
        si.content.rootFolder = Mock

        '#act'
        result = pv_service.find_item_in_path_by_type(si, None, 'not none')

        '#assert'
        self.assertEqual(result, si.content.rootFolder)