Exemplo n.º 1
0
class VM_Server_225_145:
    def __init__(self):
        self.sdk = Sdk()
        self.server_details = Config().vsphere_server_details()
        self.vm_name__minio_test = 'dinis-test-via-ovf'

    def minio_test_setup_network(self):
        vm = self.sdk.find_by_name(self.vm_name__minio_test)
        vm_process = VM_Process(vm)
        #print(vm_process.exec('/bin/ip', 'a'))
        #print(vm_process.exec('/bin/ip', 'addr add 10.102.66.200/24 dev enp0s25'))
        #print(vm_process.exec('/bin/cat', '/etc/shadow'))
        #print(vm_process.exec('/bin/bash', '-c "sudo /bin/cat /etc/shadow"'))
        print(
            vm_process.exec('/bin/bash',
                            '-c "sudo ip addr add 91.109.26.22/27 dev eth0"')
        )  # commands from https://ubuntu.com/server/docs/network-configuration
        print(
            vm_process.exec('/bin/bash',
                            '-c "sudo ip route add default via 91.109.26.30"'))

        print(vm_process.exec('/bin/ip', 'a'))
        print(vm_process.exec('/bin/ip', 'route show'))

    def minio_test_get_request(self):
        resp = requests.get('http://91.109.26.22')
        return resp.text

    def minio_test_vm_power_off(self):
        vm = self.sdk.find_by_name(self.vm_name__minio_test)
        vm.task().power_off()
        return vm.info()
Exemplo n.º 2
0
    def test____enter____exit__(self):
        sdk = Sdk()
        vm_name = f"test____enter____exit__{random_string()}"
        with Temp_VM(vm_name) as vm:
            assert vm.name() == vm_name
            assert sdk.find_by_name(vm_name).name() == vm_name      # search VM and confirm it is there

        assert sdk.find_by_name(vm_name) == None                    # search outsite the `with` statement and confirm that is NOT there
Exemplo n.º 3
0
class test_VM_Process(TestCase):
    def setUp(self) -> None:
        self.sdk = Sdk()
        self.vm_name = 'dinis-test-via-ovf'
        self.vm = self.sdk.find_by_name(self.vm_name)
        if self.vm is None:
            pytest.skip(f"target server did not have vm {self.vm_name}")
        else:
            if self.vm.powered_off():
                pytest.skip(
                    f"target server exists but it not Powered On {self.vm_name}"
                )

        self.vm_process = VM_Process(vm=self.vm)

    def test_start_process_return_stdout(self):
        program_path = "/bin/ip"
        arguments = "a"
        result = self.vm_process.start_process_return_stdout(
            program_path, arguments)
        assert 'eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>' in result
Exemplo n.º 4
0
class test_Sdk(TestCase_VM):
    vm_name = f"tests__unit__" + __name__

    def setUp(self):
        self.sdk = Sdk()

    def test_about(self):
        content = self.sdk.about()
        assert content.vendor == "VMware, Inc."
        assert content.version == "6.7.0"
        assert content.licenseProductName == "VMware ESX Server"
        assert content.licenseProductName == "VMware ESX Server"
        assert content.licenseProductVersion == "6.0"

    def test_file_info(self):
        datastore_path = 'an data store'
        vmx_file = self.sdk.file_info(datastore_path)
        assert vmx_file.vmPathName == datastore_path

    # def test_find_iso(self):
    #     vm = self.sdk.find_by_host_name('haproxy-icap')
    #     #pprint(vm.info())
    #     #print(self.sdk.json_dump("VirtualMachine","42"))
    #     #vim.vm.device.VirtualCdrom
    #     print(vm.config().hardware)

    def test_find_by_host_name(self):
        for vm in self.sdk.vms():
            host_name = vm.host_name()
            if host_name:
                assert self.sdk.find_by_host_name(
                    host_name).host_name() == host_name
                return
        print(
            "Warning test ESX server had no VMs with host_names (dnsNames) setup"
        )

    def test_find_by_name(self):
        assert self.sdk.find_by_name(self.vm_name).name() == self.vm_name
        assert self.sdk.find_by_name("AAA_BBB_CCC") is None

    def test_find_by_ip(self):
        for vm in self.sdk.vms():
            ip = vm.ip()
            if ip:
                assert self.sdk.find_by_ip(ip).ip() == ip
                return
        print("Warning test ESX server had no VMs with IPs")

    def test_find_by_uuid(self):
        uuid = self.vm.uuid()
        assert self.sdk.find_by_uuid(uuid).uuid() == uuid

    def test_get_object(self):
        name = self.vm.name()
        vm = self.sdk.get_object(pyVmomi.vim.VirtualMachine, name)
        assert vm.name == name

    def test_get_objects_Datastore(self):
        datastores = self.sdk.get_objects_Datastore()
        assert len(datastores) > 0

    def test_get_object_virtual_machine(self):
        name = self.vm.name()
        vm = VM(self.sdk.get_object_virtual_machine(name))
        assert vm.name() == name

    def test_get_objects(self):
        objects = self.sdk.get_objects()
        assert len(objects) > 0

    def test_folders(self):
        folders = self.sdk.folders()
        assert str(folders) == "['vim.Folder:ha-folder-vm']"

    def test_vms(self):
        vms = self.sdk.vms()
        assert len(vms) > 0

    def test_names(self):
        names = self.sdk.vms_names()
        assert len(names) > 0

    def test_service_instance(self):
        service_instance = self.sdk.service_instance()
        assert service_instance.content.about.apiVersion == '6.7.3'
        assert service_instance.content.about.licenseProductName == 'VMware ESX Server'
        assert service_instance.content.about.osType == 'vmnix-x86'

    def test_dump_json(self):

        obj_type = "VirtualMachine"
        moid = self.vm.moid()

        json_dump = self.sdk.json_dump(obj_type, moid)
        json_data = json.loads(json_dump)
        assert json_data['_vimid'] == moid
        assert json_data['_vimtype'] == "vim.VirtualMachine"

    def test_tasks(
        self
    ):  # todo: find better way to do this since when running multiple tests the self.sdk.tasks_recent() can take multiple seconds to execute
        self.sdk.find_by_ip('aaaaaa')
        recent_tasks = self.sdk.tasks_recent()
        most_recent_one = recent_tasks.pop()

        assert most_recent_one['DescriptionId'] == 'SearchIndex.findByIp'
        assert most_recent_one[
            'Key'] == f"haTask--vim.SearchIndex.findByIp-{most_recent_one['EventChainId']}"
        assert most_recent_one['State'] == 'success'
        assert most_recent_one['Entity'] == 'None'
Exemplo n.º 5
0
class VM_Server_113_4:

    def __init__(self):
        self.sdk            = Sdk()
        self.network        = Network()
        self.server_details = Config().vsphere_server_details()

        # settings
        self.vswitch_name    = 'vSwitch0'
        self.port_group_name = 'VM'

    def settings__check(self):
        switches = self.network.virtual_switches()
        assert len(switches)    == 1                            # there should only be one virtual switch
        assert switches[0].name == self.vswitch_name            # confirm expected name

    def setup_network(self):
        if self.port_group_name not in self.network.networks_names():
            self.network.port_group_create(self.port_group_name, self.vswitch_name)

    def setup_network__check(self):
        assert self.port_group_name in self.network.networks_names()

    def get_network(self):
        return self.network.network(self.port_group_name)



    # Ubuntu VM tests
    def create_ubuntu_vm(self):
        vm_name    = "test_ubuntu"
        iso_ubuntu = "[datastore1] isos/ubuntu-20.04.1-live-server-amd64.iso"

        vm = self.sdk.find_by_name(vm_name)
        if(vm):
            vm.task().delete()

        vm_create = VM_Create(vm_name=vm_name)
        vm_create.add_device__nic(self.get_network())
        vm = vm_create.create()

        vm_device = VM_Device(vm)
        vm_device.cdrom_iso_add_to_vm(iso_ubuntu)
        vm_device.disk_ide_add_to_vm(10)

        vm.task().power_on()

    def ubuntu_vm_run_commands(self):
        vm_name = "test_ubuntu"
        vm = self.sdk.find_by_name(vm_name)
        vm_process = VM_Process(vm)
        vm_process.set_vm_account_from_env("VM_UBUNTU")
        print(vm_process.vm_account)
        #print(vm_process.ls("/"))

    # misc vm tests

    def minio_test_setup_network(self):
        vm = self.sdk.find_by_name("test-vm-from-ovf")
        vm_process = VM_Process(vm)
        #print(vm_process.exec('/bin/ip', 'a'))
        #print(vm_process.exec('/bin/ip', 'addr add 10.102.66.200/24 dev enp0s25'))
        #print(vm_process.exec('/bin/cat', '/etc/shadow'))
        #print(vm_process.exec('/bin/bash', '-c "sudo /bin/cat /etc/shadow"'))
        print(vm_process.exec('/bin/bash', '-c "sudo ip addr add 78.159.113.32/26 dev eth0"')) # commands from https://ubuntu.com/server/docs/network-configuration
        print(vm_process.exec('/bin/bash', '-c "sudo ip route add default via 78.159.113.62"'))

        print(vm_process.exec('/bin/ip', 'a'))
        print(vm_process.exec('/bin/ip', 'route show'))
Exemplo n.º 6
0
class test_Sdk(TestCase_VM):
    vm_name = f"tests__unit__" + __name__

    def setUp(self):
        self.sdk = Sdk()

    def test_about(self):
        content = self.sdk.about()
        assert content.vendor == "VMware, Inc."
        assert content.version == "6.7.0"
        assert content.licenseProductName == "VMware ESX Server"
        assert content.licenseProductName == "VMware ESX Server"
        assert content.licenseProductVersion == "6.0"

    def test_file_info(self):
        datastore_path = 'an data store'
        vmx_file = self.sdk.file_info(datastore_path)
        assert vmx_file.vmPathName == datastore_path

    # def test_find_iso(self):
    #     vm = self.sdk.find_by_host_name('haproxy-icap')
    #     #pprint(vm.info())
    #     #print(self.sdk.json_dump("VirtualMachine","42"))
    #     #vim.vm.device.VirtualCdrom
    #     print(vm.config().hardware)

    def test_find_by_host_name(self):
        for vm in self.sdk.vms():
            host_name = vm.host_name()
            if host_name:
                assert self.sdk.find_by_host_name(
                    host_name).host_name() == host_name
                return
        print(
            "Warning test ESX server had no VMs with host_names (dnsNames) setup"
        )

    def test_find_by_name(self):
        assert self.sdk.find_by_name(self.vm_name).name() == self.vm_name
        assert self.sdk.find_by_name("AAA_BBB_CCC") is None

    def test_find_by_ip(self):
        for vm in self.sdk.vms():
            ip = vm.ip()
            if ip:
                assert self.sdk.find_by_ip(ip).ip() == ip
                return
        print("Warning test ESX server had no VMs with IPs")

    def test_find_by_uuid(self):
        uuid = self.vm.uuid()
        assert self.sdk.find_by_uuid(uuid).uuid() == uuid

    def test_get_object(self):
        with View_Soap_Calls():
            name = self.vm.name()
            vm = self.sdk.get_object(pyVmomi.vim.VirtualMachine, name)
            pprint(vm)
            #assert vm.name == name

    def test_get_objects_Datastore(self):
        datastores = self.sdk.get_objects_Datastore()
        assert len(datastores) > 0

    def test_get_object_virtual_machine(self):
        name = self.vm.name()
        vm = VM(self.sdk.get_object_virtual_machine(name))
        assert vm.name() == name

    def test_get_objects(self):
        objects = self.sdk.get_objects()
        assert len(objects) > 0

    def test_get_objects_properties(self):

        target_objects = [self.vm.vm
                          ]  # use a temp vm to make sure we always have one
        object_type = type(self.vm.vm)
        vm_id = self.vm.id()

        properties_names = self.sdk.object_properties_names(object_type)
        results = self.sdk.get_objects_properties(object_type, target_objects,
                                                  properties_names)

        assert vm_id == f'vim.VirtualMachine:{self.vm.moid()}'
        assert vm_id in results
        assert len(results) == 1
        object_properties = results[vm_id]
        properties_names.remove(
            'alarmActionsEnabled'
        )  # these properties are not returned from the server
        properties_names.remove('parentVApp')
        properties_names.remove('snapshot')
        assert sorted(list(set(object_properties))) == sorted(properties_names)
        assert object_properties['name'] == self.vm.name()

        # to get properties from all current VMs use:
        #   target_objects = self.sdk.get_objects_Virtual_Machines()

    def test_folders(self):
        folders = self.sdk.folders()
        assert str(folders) == "['vim.Folder:ha-folder-vm']"

    def test_object_filter_spec(self):
        # see test_get_objects_properties
        pass

    def test_object_methods_names(self):
        object_type = pyVmomi.vim.VirtualMachine
        method_names = self.sdk.object_methods_names(object_type)
        assert method_names == [
            'SetCustomValue',
            'Destroy',
            'Reload',
            'Rename',  # these ones where not in list(object._methodInfo.keys())
            'AcquireMksTicket',
            'AcquireTicket',
            'Answer',
            'ApplyEvcMode',
            'AttachDisk',
            'CheckCustomizationSpec',
            'Clone',
            'ConsolidateDisks',
            'CreateScreenshot',
            'CreateSecondaryEx',
            'CreateSecondary',
            'CreateSnapshotEx',
            'CreateSnapshot',
            'CryptoUnlock',
            'Customize',
            'DefragmentAllDisks',
            'DetachDisk',
            'DisableSecondary',
            'DropConnections',
            'EnableSecondary',
            'EstimateStorageRequirementForConsolidate',
            'ExportVm',
            'ExtractOvfEnvironment',
            'InstantClone',
            'MakePrimary',
            'MarkAsTemplate',
            'MarkAsVirtualMachine',
            'Migrate',
            'MountToolsInstaller',
            'PowerOff',
            'PowerOn',
            'PromoteDisks',
            'PutUsbScanCodes',
            'QueryChangedDiskAreas',
            'QueryConnections',
            'QueryFaultToleranceCompatibility',
            'QueryFaultToleranceCompatibilityEx',
            'QueryUnownedFiles',
            'RebootGuest',
            'Reconfigure',
            'RefreshStorageInfo',
            'Relocate',
            'RemoveAllSnapshots',
            'ResetGuestInformation',
            'Reset',
            'RevertToCurrentSnapshot',
            'SendNMI',
            'SetDisplayTopology',
            'SetScreenResolution',
            'ShutdownGuest',
            'StandbyGuest',
            'StartRecording',
            'StartReplaying',
            'StopRecording',
            'StopReplaying',
            'Suspend',
            'TerminateFaultTolerantVM',
            'Terminate',
            'TurnOffFaultTolerance',
            'UnmountToolsInstaller',
            'Unregister',
            'UpgradeTools',
            'UpgradeVirtualHardware',
            'ReloadFromPath'
        ]

        datastore_methods = self.sdk.object_methods_names(
            pyVmomi.vim.Datastore)

        assert datastore_methods == [
            'SetCustomValue', 'Destroy', 'Reload', 'Rename',
            'EnterMaintenanceMode', 'ExitMaintenanceMode', 'DestroyDatastore',
            'Refresh', 'RefreshStorageInfo', 'RenameDatastore',
            'UpdateVVolVirtualMachineFiles', 'UpdateVirtualMachineFiles'
        ]

    def test_object_properties_names(self):
        object_type = pyVmomi.vim.VirtualMachine

        properties_names = self.sdk.object_properties_names(object_type)
        assert properties_names == [
            'value', 'availableField', 'parent', 'customValue',
            'overallStatus', 'configStatus', 'configIssue', 'effectiveRole',
            'permission', 'name', 'disabledMethod', 'recentTask',
            'declaredAlarmState', 'triggeredAlarmState', 'alarmActionsEnabled',
            'tag', 'capability', 'config', 'layout', 'layoutEx', 'storage',
            'environmentBrowser', 'resourcePool', 'parentVApp',
            'resourceConfig', 'runtime', 'guest', 'summary', 'datastore',
            'network', 'snapshot', 'rootSnapshot', 'guestHeartbeatStatus'
        ]

        datastore_properties = self.sdk.object_properties_names(
            pyVmomi.vim.Datastore)
        assert datastore_properties == [
            'value', 'availableField', 'parent', 'customValue',
            'overallStatus', 'configStatus', 'configIssue', 'effectiveRole',
            'permission', 'name', 'disabledMethod', 'recentTask',
            'declaredAlarmState', 'triggeredAlarmState', 'alarmActionsEnabled',
            'tag', 'info', 'summary', 'host', 'vm', 'browser', 'capability',
            'iormConfiguration'
        ]

    def test_vms(self):
        vms = self.sdk.vms()
        assert len(vms) > 0

    def test_names(self):
        names = self.sdk.vms_names()
        assert len(names) > 0

    def test_service_instance(self):
        service_instance = self.sdk.service_instance()
        assert service_instance.content.about.apiVersion == '6.7.3'
        assert service_instance.content.about.licenseProductName == 'VMware ESX Server'
        assert service_instance.content.about.osType == 'vmnix-x86'

    # def test_dump_json(self):
    #
    #     obj_type = "VirtualMachine"
    #     moid     = self.vm.moid()
    #
    #     json_dump = self.sdk.json_dump(obj_type, moid)
    #     json_data = json.loads(json_dump)
    #     assert json_data['_vimid'  ] == moid
    #     assert json_data['_vimtype'] == "vim.VirtualMachine"

    # todo: fix experiment (see tasks_recent_experiment)
    def test_tasks(
        self
    ):  # todo: find better way to do this since when running multiple tests the self.sdk.tasks_recent() can take multiple seconds to execute
        #with View_Soap_Calls():
        # with Catch():
        #     #pprint(self.sdk.get_objects())
        #     self.sdk.find_by_ip('aaaaaa')
        #     recent_tasks    = self.sdk.tasks_recent(self.vm.vm)
        #     pprint(recent_tasks)
        #     return
        self.sdk.find_by_ip('aaaaaa')
        recent_tasks = self.sdk.tasks_recent()
        most_recent_one = recent_tasks.pop()

        assert most_recent_one['DescriptionId'] == 'SearchIndex.findByIp'
        assert most_recent_one[
            'Key'] == f"haTask--vim.SearchIndex.findByIp-{most_recent_one['EventChainId']}"
        assert most_recent_one['State'] == 'success'
        assert most_recent_one['Entity'] == 'None'