def test_01_stop_vm(self):
        """Test Stop Virtual Machine
        """

        # Validate the following
        # 1. Should Not be able to login to the VM.
        # 2. listVM command should return
        #    this VM.State of this VM should be ""Stopped"".

        self.debug("Stopping VM - ID: %s" % self.virtual_machine.id)
        self.small_virtual_machine.stop(self.apiclient)

        list_vm_response = list_virtual_machines(
                                            self.apiclient,
                                            id=self.small_virtual_machine.id
                                            )

        self.assertEqual(
                            isinstance(list_vm_response, list),
                            True,
                            "Check list response returns a valid list"
                        )
        self.assertNotEqual(
                            len(list_vm_response),
                            0,
                            "Check VM available in List Virtual Machines"
                        )

        self.assertEqual(
                            list_vm_response[0].state,
                            "Stopped",
                            "Check virtual machine is in stopped state"
                        )
        return
 def check_vm_is_moved_in_account_domainid(self, account):
     list_vm_response = list_virtual_machines(self.api_client,
                                              id=self.virtual_machine.id,
                                              account=account.name,
                                              domainid=account.domainid)
     self.debug('VM=%s moved to account=%s and domainid=%s' % (list_vm_response, account.name, account.domainid))
     self.assertNotEqual(len(list_vm_response), 0, 'Unable to move VM to account=%s domainid=%s' % (account.name, account.domainid))
    def tearDown(self):
        try:
            if self.egressruleid:
                self.debug('remove egress rule id=%s' % self.egressruleid)
                self.deleteEgressRule()
            self.debug("Cleaning up the resources")
            self.virtual_machine.delete(self.apiclient)
            wait_for_cleanup(self.apiclient, ["expunge.interval", "expunge.delay"])

            retriesCount = 5
            while True:
                vms = list_virtual_machines(self.apiclient, id=self.virtual_machine.id)
                if vms is None:
                    break
                elif retriesCount == 0:
                    self.fail("Failed to delete/expunge VM")

                time.sleep(10)
                retriesCount -= 1

            self.network.delete(self.apiclient)
            self.debug("Sleep for Network cleanup to complete.")
            wait_for_cleanup(self.apiclient, ["network.gc.wait", "network.gc.interval"])
            cleanup_resources(self.apiclient, reversed(self.cleanup))
            self.debug("Cleanup complete!")
        except Exception as e:
            self.fail("Warning! Cleanup failed: %s" % e)            
    def test_02_start_vm(self):
        """Test Start Virtual Machine
        """
        # Validate the following
        # 1. listVM command should return this VM.State
        #    of this VM should be Running".

        self.debug("Starting VM - ID: %s" % self.virtual_machine.id)
        self.small_virtual_machine.start(self.apiclient)

        list_vm_response = list_virtual_machines(
            self.apiclient, id=self.small_virtual_machine.id)
        self.assertEqual(isinstance(list_vm_response, list), True,
                         "Check list response returns a valid list")

        self.assertNotEqual(len(list_vm_response), 0,
                            "Check VM avaliable in List Virtual Machines")

        self.debug(
                "Verify listVirtualMachines response for virtual machine: %s" \
                % self.small_virtual_machine.id
                )
        self.assertEqual(list_vm_response[0].state, "Running",
                         "Check virtual machine is in running state")
        return
    def test_03_reboot_vm(self):
        """Test Reboot Virtual Machine
        """

        # Validate the following
        # 1. Should be able to login to the VM.
        # 2. listVM command should return the deployed VM.
        #    State of this VM should be "Running"

        self.debug("Rebooting VM - ID: %s" % self.virtual_machine.id)
        self.small_virtual_machine.reboot(self.apiclient)

        list_vm_response = list_virtual_machines(
                                            self.apiclient,
                                            id=self.small_virtual_machine.id
                                            )
        self.assertEqual(
                            isinstance(list_vm_response, list),
                            True,
                            "Check list response returns a valid list"
                        )

        self.assertNotEqual(
                            len(list_vm_response),
                            0,
                            "Check VM avaliable in List Virtual Machines"
                        )

        self.assertEqual(
                            list_vm_response[0].state,
                            "Running",
                            "Check virtual machine is in running state"
                        )
        return
    def test_07_restore_vm(self):
        """Test recover Virtual Machine
        """

        # Validate the following
        # 1. listVM command should return this VM.
        #    State of this VM should be "Stopped".
        # 2. We should be able to Start this VM successfully.

        self.debug("Recovering VM - ID: %s" % self.small_virtual_machine.id)

        cmd = recoverVirtualMachine.recoverVirtualMachineCmd()
        cmd.id = self.small_virtual_machine.id
        self.apiclient.recoverVirtualMachine(cmd)

        list_vm_response = list_virtual_machines(
            self.apiclient, id=self.small_virtual_machine.id)
        self.assertEqual(isinstance(list_vm_response, list), True,
                         "Check list response returns a valid list")

        self.assertNotEqual(len(list_vm_response), 0,
                            "Check VM avaliable in List Virtual Machines")

        self.assertEqual(list_vm_response[0].state, "Stopped",
                         "Check virtual machine is in Stopped state")

        return
    def tearDown(self):
        try:
            if self.egressruleid:
                self.debug('remove egress rule id=%s' % self.egressruleid)
                self.deleteEgressRule()
            self.debug("Cleaning up the resources")
            self.virtual_machine.delete(self.apiclient)
            wait_for_cleanup(self.apiclient,
                             ["expunge.interval", "expunge.delay"])

            retriesCount = 5
            while True:
                vms = list_virtual_machines(self.apiclient,
                                            id=self.virtual_machine.id)
                if vms is None:
                    break
                elif retriesCount == 0:
                    self.fail("Failed to delete/expunge VM")

                time.sleep(10)
                retriesCount -= 1

            self.network.delete(self.apiclient)
            self.debug("Sleep for Network cleanup to complete.")
            wait_for_cleanup(self.apiclient,
                             ["network.gc.wait", "network.gc.interval"])
            cleanup_resources(self.apiclient, reversed(self.cleanup))
            self.debug("Cleanup complete!")
        except Exception as e:
            self.fail("Warning! Cleanup failed: %s" % e)
    def test_06_destroy_vm(self):
        """Test destroy Virtual Machine
        """

        # Validate the following
        # 1. Should not be able to login to the VM.
        # 2. listVM command should return this VM.State
        #    of this VM should be "Destroyed".

        self.debug("Destroy VM - ID: %s" % self.small_virtual_machine.id)
        self.small_virtual_machine.delete(self.apiclient)

        list_vm_response = list_virtual_machines(
                                            self.apiclient,
                                            id=self.small_virtual_machine.id
                                            )
        self.assertEqual(
                            isinstance(list_vm_response, list),
                            True,
                            "Check list response returns a valid list"
                        )

        self.assertNotEqual(
                            len(list_vm_response),
                            0,
                            "Check VM avaliable in List Virtual Machines"
                        )

        self.assertEqual(
                            list_vm_response[0].state,
                            "Destroyed",
                            "Check virtual machine is in destroyed state"
                        )
        return
    def test_deploy_vm(self):
        """Test Deploy Virtual Machine
        """
        # Validate the following:
        # 1. Virtual Machine is accessible via SSH
        # 2. listVirtualMachines returns accurate information
        list_vm_response = list_virtual_machines(self.apiclient,
                                                 id=self.virtual_machine.id)

        self.debug(
                "Verify listVirtualMachines response for virtual machine: %s" \
                % self.virtual_machine.id
            )
        self.assertEqual(isinstance(list_vm_response, list), True,
                         "Check list response returns a valid list")
        self.assertNotEqual(len(list_vm_response), 0,
                            "Check VM available in List Virtual Machines")
        vm_response = list_vm_response[0]
        self.assertEqual(vm_response.id, self.virtual_machine.id,
                         "Check virtual machine id in listVirtualMachines")
        self.assertEqual(vm_response.name, self.virtual_machine.name,
                         "Check virtual machine name in listVirtualMachines")
        self.assertEqual(vm_response.state,
                         'Running',
                         msg="VM is not in Running state")
        return
Exemple #10
0
 def check_vm_is_moved_in_account_domainid(self, account):
     list_vm_response = list_virtual_machines(self.api_client,
                                              id=self.virtual_machine.id,
                                              account=account.name,
                                              domainid=account.domainid)
     self.debug('VM=%s moved to account=%s and domainid=%s' %
                (list_vm_response, account.name, account.domainid))
     self.assertNotEqual(
         len(list_vm_response), 0,
         'Unable to move VM to account=%s domainid=%s' %
         (account.name, account.domainid))
 def create_vm(self,
               account,
               domain,
               isRunning=False,
               project  =None,
               limit    =None,
               pfrule   =False,
               lbrule   =None,
               natrule  =None,
               volume   =None,
               snapshot =False):
     #TODO: Implemnt pfrule/lbrule/natrule
     self.debug("Deploying instance in the account: %s" % account.name)
     self.virtual_machine = VirtualMachine.create(self.apiclient,
                                                  self.services["virtual_machine"],
                                                  accountid=account.name,
                                                  domainid=domain.id,
                                                  serviceofferingid=self.service_offering.id,
                                                  mode=self.zone.networktype if pfrule else 'basic',
                                                  projectid=project.id if project else None)
     self.debug("Deployed instance in account: %s" % account.name)
     list_virtual_machines(self.apiclient,
                           id=self.virtual_machine.id)
     if snapshot:
        volumes = list_volumes(self.apiclient,
                               virtualmachineid=self.virtual_machine.id,
                               type='ROOT',
                               listall=True)
        self.snapshot = Snapshot.create(self.apiclient,
                                   volumes[0].id,
                                   account=account.name,
                                   domainid=account.domainid)
     if volume:
         self.virtual_machine.attach_volume(self.apiclient,
                                            volume)
     if not isRunning:
         self.virtual_machine.stop(self.apiclient)
     self.cleanup.append(self.virtual_machine)
Exemple #12
0
 def create_vm(self,
               account,
               domain,
               isRunning=False,
               project=None,
               limit=None,
               pfrule=False,
               lbrule=None,
               natrule=None,
               volume=None,
               snapshot=False):
     #TODO: Implemnt pfrule/lbrule/natrule
     self.debug("Deploying instance in the account: %s" % account.name)
     self.virtual_machine = VirtualMachine.create(
         self.apiclient,
         self.services["virtual_machine"],
         accountid=account.name,
         domainid=domain.id,
         serviceofferingid=self.service_offering.id,
         mode=self.zone.networktype if pfrule else 'basic',
         projectid=project.id if project else None)
     self.debug("Deployed instance in account: %s" % account.name)
     list_virtual_machines(self.apiclient, id=self.virtual_machine.id)
     if snapshot:
         volumes = list_volumes(self.apiclient,
                                virtualmachineid=self.virtual_machine.id,
                                type='ROOT',
                                listall=True)
         self.snapshot = Snapshot.create(self.apiclient,
                                         volumes[0].id,
                                         account=account.name,
                                         domainid=account.domainid)
     if volume:
         self.virtual_machine.attach_volume(self.apiclient, volume)
     if not isRunning:
         self.virtual_machine.stop(self.apiclient)
     self.cleanup.append(self.virtual_machine)
    def test_09_expunge_vm(self):
        """Test destroy(expunge) Virtual Machine
        """
        # Validate the following
        # 1. listVM command should NOT  return this VM any more.

        self.debug("Expunge VM-ID: %s" % self.small_virtual_machine.id)

        cmd = destroyVirtualMachine.destroyVirtualMachineCmd()
        cmd.id = self.small_virtual_machine.id
        self.apiclient.destroyVirtualMachine(cmd)

        config = list_configurations(
                                     self.apiclient,
                                     name='expunge.delay'
                                     )

        expunge_delay = int(config[0].value)
        time.sleep(expunge_delay * 2)

        #VM should be destroyed unless expunge thread hasn't run
        #Wait for two cycles of the expunge thread
        config = list_configurations(
                                     self.apiclient,
                                     name='expunge.interval'
                                     )
        expunge_cycle = int(config[0].value)
        wait_time = expunge_cycle * 2
        while wait_time >= 0:
            list_vm_response = list_virtual_machines(
                                                self.apiclient,
                                                id=self.small_virtual_machine.id
                                                )
            if list_vm_response:
                time.sleep(expunge_cycle)
                wait_time = wait_time - expunge_cycle
            else:
                break

        self.debug("listVirtualMachines response: %s" % list_vm_response)

        self.assertEqual(
                        list_vm_response,
                        None,
                        "Check Expunged virtual machine is in listVirtualMachines response"
                    )
        return
    def test_deploy_vm(self):
        """Test Deploy Virtual Machine
        """
        # Validate the following:
        # 1. Virtual Machine is accessible via SSH
        # 2. listVirtualMachines returns accurate information
        list_vm_response = list_virtual_machines(
                                                 self.apiclient,
                                                 id=self.virtual_machine.id
                                                 )

        self.debug(
                "Verify listVirtualMachines response for virtual machine: %s" \
                % self.virtual_machine.id
            )
        self.assertEqual(
                            isinstance(list_vm_response, list),
                            True,
                            "Check list response returns a valid list"
                        )
        self.assertNotEqual(
                            len(list_vm_response),
                            0,
                            "Check VM available in List Virtual Machines"
                        )
        vm_response = list_vm_response[0]
        self.assertEqual(

                            vm_response.id,
                            self.virtual_machine.id,
                            "Check virtual machine id in listVirtualMachines"
                        )
        self.assertEqual(
                    vm_response.name,
                    self.virtual_machine.name,
                    "Check virtual machine name in listVirtualMachines"
                    )
        self.assertEqual(
            vm_response.state,
            'Running',
             msg="VM is not in Running state"
        )
        return
Exemple #15
0
    def tearDown(self):
        try:
            if self.egressruleid:
                self.debug('remove egress rule id=%s' % self.egressruleid)
                self.deleteEgressRule()

            self.debug("Cleaning up the resources")

            #below components is not a part of cleanup because to mandate the order and to cleanup network
            try:
                for vm in self.cleanup_vms:
                    if str(vm.state).lower() != "error":
                        vm.delete(self.api_client)
            except Exception as e:
                self.fail("Warning: Exception during virtual machines cleanup : %s" % e)

            # Wait for VMs to expunge
            wait_for_cleanup(self.api_client, ["expunge.delay", "expunge.interval"])

            if len(self.cleanup_vms) > 0:
                retriesCount = 10
                while True:
                    vms = list_virtual_machines(self.api_client, id=self.virtual_machine.id)
                    if vms is None:
                        break
                    elif retriesCount == 0:
                        self.fail("Failed to expunge vm even after 10 minutes")
                    time.sleep(60)
                    retriesCount -= 1

            try:
                for network in self.cleanup_networks:
                    network.delete(self.api_client)
            except Exception as e:
                self.fail("Warning: Exception during networks cleanup : %s" % e)

            self.debug("Sleep for Network cleanup to complete.")
            wait_for_cleanup(self.apiclient, ["network.gc.wait", "network.gc.interval"])

            cleanup_resources(self.apiclient, reversed(self.cleanup))
            self.debug("Cleanup complete!")
        except Exception as e:
            self.fail("Warning! Cleanup failed: %s" % e)
    def test_07_restore_vm(self):
        #TODO: SIMENH: add another test the data on the restored VM.
        """Test recover Virtual Machine
        """

        # Validate the following
        # 1. listVM command should return this VM.
        #    State of this VM should be "Stopped".
        # 2. We should be able to Start this VM successfully.

        self.debug("Recovering VM - ID: %s" % self.small_virtual_machine.id)

        cmd = recoverVirtualMachine.recoverVirtualMachineCmd()
        cmd.id = self.small_virtual_machine.id
        self.apiclient.recoverVirtualMachine(cmd)

        list_vm_response = list_virtual_machines(
                                            self.apiclient,
                                            id=self.small_virtual_machine.id
                                            )
        self.assertEqual(
                            isinstance(list_vm_response, list),
                            True,
                            "Check list response returns a valid list"
                        )

        self.assertNotEqual(
                            len(list_vm_response),
                            0,
                            "Check VM avaliable in List Virtual Machines"
                        )

        self.assertEqual(
                            list_vm_response[0].state,
                            "Stopped",
                            "Check virtual machine is in Stopped state"
                        )

        return
 def test_deployvm_userdata(self):
     """Test userdata as GET, size > 2k
     """
     deployVmResponse = VirtualMachine.create(
         self.apiClient,
         services=self.services["virtual_machine"],
         accountid=self.account.name,
         domainid=self.account.domainid,
         serviceofferingid=self.service_offering.id,
         templateid=self.template.id,
         zoneid=self.zone.id
     )
     vms = list_virtual_machines(
         self.apiClient,
         account=self.account.name,
         domainid=self.account.domainid,
         id=deployVmResponse.id
     )
     self.assert_(len(vms) > 0, "There are no Vms deployed in the account %s" % self.account.name)
     vm = vms[0]
     self.assert_(vm.id == str(deployVmResponse.id), "Vm deployed is different from the test")
     self.assert_(vm.state == "Running", "VM is not in Running state")
 def test_deployvm_userdata(self):
     """Test userdata as GET, size > 2k
     """
     deployVmResponse = VirtualMachine.create(
         self.apiClient,
         services=self.services["virtual_machine"],
         accountid=self.account.name,
         domainid=self.account.domainid,
         serviceofferingid=self.service_offering.id,
         templateid=self.template.id,
         zoneid=self.zone.id)
     vms = list_virtual_machines(self.apiClient,
                                 account=self.account.name,
                                 domainid=self.account.domainid,
                                 id=deployVmResponse.id)
     self.assert_(
         len(vms) > 0,
         "There are no Vms deployed in the account %s" % self.account.name)
     vm = vms[0]
     self.assert_(vm.id == str(deployVmResponse.id),
                  "Vm deployed is different from the test")
     self.assert_(vm.state == "Running", "VM is not in Running state")
    def test_09_expunge_vm(self):
        """Test destroy(expunge) Virtual Machine
        """
        # Validate the following
        # 1. listVM command should NOT  return this VM any more.

        self.debug("Expunge VM-ID: %s" % self.small_virtual_machine.id)

        cmd = destroyVirtualMachine.destroyVirtualMachineCmd()
        cmd.id = self.small_virtual_machine.id
        self.apiclient.destroyVirtualMachine(cmd)

        config = list_configurations(self.apiclient, name='expunge.delay')

        expunge_delay = int(config[0].value)
        time.sleep(expunge_delay * 2)

        #VM should be destroyed unless expunge thread hasn't run
        #Wait for two cycles of the expunge thread
        config = list_configurations(self.apiclient, name='expunge.interval')
        expunge_cycle = int(config[0].value)
        wait_time = expunge_cycle * 2
        while wait_time >= 0:
            list_vm_response = list_virtual_machines(
                self.apiclient, id=self.small_virtual_machine.id)
            if list_vm_response:
                time.sleep(expunge_cycle)
                wait_time = wait_time - expunge_cycle
            else:
                break

        self.debug("listVirtualMachines response: %s" % list_vm_response)

        self.assertEqual(
            list_vm_response, None,
            "Check Expunged virtual machine is in listVirtualMachines response"
        )
        return
    def test_06_destroy_vm(self):
        """Test destroy Virtual Machine
        """

        # Validate the following
        # 1. Should not be able to login to the VM.
        # 2. listVM command should return this VM.State
        #    of this VM should be "Destroyed".

        self.debug("Destroy VM - ID: %s" % self.small_virtual_machine.id)
        self.small_virtual_machine.delete(self.apiclient)

        list_vm_response = list_virtual_machines(
            self.apiclient, id=self.small_virtual_machine.id)
        self.assertEqual(isinstance(list_vm_response, list), True,
                         "Check list response returns a valid list")

        self.assertNotEqual(len(list_vm_response), 0,
                            "Check VM avaliable in List Virtual Machines")

        self.assertEqual(list_vm_response[0].state, "Destroyed",
                         "Check virtual machine is in destroyed state")
        return
    def test_03_reboot_vm(self):
        """Test Reboot Virtual Machine
        """

        # Validate the following
        # 1. Should be able to login to the VM.
        # 2. listVM command should return the deployed VM.
        #    State of this VM should be "Running"

        self.debug("Rebooting VM - ID: %s" % self.virtual_machine.id)
        self.small_virtual_machine.reboot(self.apiclient)

        list_vm_response = list_virtual_machines(
            self.apiclient, id=self.small_virtual_machine.id)
        self.assertEqual(isinstance(list_vm_response, list), True,
                         "Check list response returns a valid list")

        self.assertNotEqual(len(list_vm_response), 0,
                            "Check VM avaliable in List Virtual Machines")

        self.assertEqual(list_vm_response[0].state, "Running",
                         "Check virtual machine is in running state")
        return
    def test_01_stop_vm(self):
        """Test Stop Virtual Machine
        """

        # Validate the following
        # 1. Should Not be able to login to the VM.
        # 2. listVM command should return
        #    this VM.State of this VM should be ""Stopped"".

        self.debug("Stopping VM - ID: %s" % self.virtual_machine.id)
        self.small_virtual_machine.stop(self.apiclient)

        list_vm_response = list_virtual_machines(
            self.apiclient, id=self.small_virtual_machine.id)

        self.assertEqual(isinstance(list_vm_response, list), True,
                         "Check list response returns a valid list")
        self.assertNotEqual(len(list_vm_response), 0,
                            "Check VM available in List Virtual Machines")

        self.assertEqual(list_vm_response[0].state, "Stopped",
                         "Check virtual machine is in stopped state")
        return
    def test_02_start_vm(self):
        """Test Start Virtual Machine
        """
        # Validate the following
        # 1. listVM command should return this VM.State
        #    of this VM should be Running".

        self.debug("Starting VM - ID: %s" % self.virtual_machine.id)
        self.small_virtual_machine.start(self.apiclient)

        list_vm_response = list_virtual_machines(
                                            self.apiclient,
                                            id=self.small_virtual_machine.id
                                            )
        self.assertEqual(
                            isinstance(list_vm_response, list),
                            True,
                            "Check list response returns a valid list"
                        )

        self.assertNotEqual(
                            len(list_vm_response),
                            0,
                            "Check VM avaliable in List Virtual Machines"
                        )

        self.debug(
                "Verify listVirtualMachines response for virtual machine: %s" \
                % self.small_virtual_machine.id
                )
        self.assertEqual(
                            list_vm_response[0].state,
                            "Running",
                            "Check virtual machine is in running state"
                        )
        return
    def test_08_migrate_vm(self):
        """Test migrate VM
        """
        # Validate the following
        # 1. Environment has enough hosts for migration
        # 2. DeployVM on suitable host (with another host in the cluster)
        # 3. Migrate the VM and assert migration successful

        suitable_hosts = None

        hosts = Host.list(
            self.apiclient,
            zoneid=self.zone.id,
            type='Routing'
        )
        self.assertEqual(validateList(hosts)[0], PASS, "hosts list validation failed")

        if len(hosts) < 2:
            self.skipTest("At least two hosts should be present in the zone for migration")

        hypervisor = str(get_hypervisor_type(self.apiclient)).lower()

        # For KVM, two hosts used for migration should  be present in same cluster
        # For XenServer and VMware, migration is possible between hosts belonging to different clusters
        # with the help of XenMotion and Vmotion respectively.

        if hypervisor == "kvm":
            #identify suitable host
            clusters = [h.clusterid for h in hosts]
            #find hosts withe same clusterid
            clusters = [cluster for index, cluster in enumerate(clusters) if clusters.count(cluster) > 1]

            if len(clusters) <= 1:
                self.skipTest("In KVM, Live Migration needs two hosts within same cluster")

            suitable_hosts = [host for host in hosts if host.clusterid == clusters[0]]
        else:
            suitable_hosts = hosts

        target_host = suitable_hosts[0]
        migrate_host = suitable_hosts[1]

        #deploy VM on target host
        self.vm_to_migrate = VirtualMachine.create(
            self.api_client,
            self.services["small"],
            accountid=self.account.name,
            domainid=self.account.domainid,
            serviceofferingid=self.small_offering.id,
            mode=self.services["mode"],
            hostid=target_host.id
        )
        self.debug("Migrating VM-ID: %s to Host: %s" % (
                                        self.vm_to_migrate.id,
                                        migrate_host.id
                                        ))

        self.vm_to_migrate.migrate(self.api_client, migrate_host.id)

        list_vm_response = list_virtual_machines(
                                            self.apiclient,
                                            id=self.vm_to_migrate.id
                                            )
        self.assertNotEqual(
                            list_vm_response,
                            None,
                            "Check virtual machine is listed"
                        )

        vm_response = list_vm_response[0]

        self.assertEqual(
                            vm_response.id,
                            self.vm_to_migrate.id,
                            "Check virtual machine ID of migrated VM"
                        )

        self.assertEqual(
                            vm_response.hostid,
                            migrate_host.id,
                            "Check destination hostID of migrated VM"
                        )
        return
Exemple #25
0
    def test_04_change_offering_small(self):
        """Test to change service to a small capacity
        """
        # Validate the following
        # 1. Log in to the Vm .We should see that the CPU and memory Info of
        #    this Vm matches the one specified for "Small" service offering.
        # 2. Using  listVM command verify that this Vm
        #    has Small service offering Id.

        self.debug("Stopping VM - ID: %s" % self.medium_virtual_machine.id)
        self.medium_virtual_machine.stop(self.apiclient)
        # Ensure that VM is in stopped state
        list_vm_response = list_virtual_machines(
            self.apiclient, id=self.medium_virtual_machine.id)
        if isinstance(list_vm_response, list):
            vm = list_vm_response[0]
            if vm.state == 'Stopped':
                self.debug("VM state: %s" % vm.state)
            else:
                raise Exception(
                    "Failed to stop VM (ID: %s) in change service offering" %
                    vm.id)

        self.debug("Change Service offering VM - ID: %s" %
                   self.medium_virtual_machine.id)

        cmd = changeServiceForVirtualMachine.changeServiceForVirtualMachineCmd(
        )
        cmd.id = self.medium_virtual_machine.id
        cmd.serviceofferingid = self.small_offering.id
        self.apiclient.changeServiceForVirtualMachine(cmd)

        self.debug("Starting VM - ID: %s" % self.medium_virtual_machine.id)
        self.medium_virtual_machine.start(self.apiclient)
        # Ensure that VM is in running state
        list_vm_response = list_virtual_machines(
            self.apiclient, id=self.medium_virtual_machine.id)

        if isinstance(list_vm_response, list):
            vm = list_vm_response[0]
            if vm.state == 'Running':
                self.debug("VM state: %s" % vm.state)
            else:
                raise Exception(
                    "Failed to start VM (ID: %s) after changing service offering"
                    % vm.id)

        try:
            ssh = self.medium_virtual_machine.get_ssh_client()
        except Exception as e:
            self.fail(
                "SSH Access failed for %s: %s" %\
                (self.medium_virtual_machine.ipaddress, e)
            )

        cpuinfo = ssh.execute("cat /proc/cpuinfo")
        cpu_cnt = len([i for i in cpuinfo if "processor" in i])
        #'cpu MHz\t\t: 2660.499'
        cpu_speed = [i for i in cpuinfo if "cpu MHz" in i][0].split()[3]
        meminfo = ssh.execute("cat /proc/meminfo")
        #MemTotal:        1017464 kB
        total_mem = [i for i in meminfo if "MemTotal" in i][0].split()[1]

        self.debug("CPU count: %s, CPU Speed: %s, Mem Info: %s" %
                   (cpu_cnt, cpu_speed, total_mem))
        self.assertAlmostEqual(int(cpu_cnt), self.small_offering.cpunumber,
                               "Check CPU Count for small offering")
        self.assertAlmostEqual(list_vm_response[0].cpuspeed,
                               self.small_offering.cpuspeed,
                               "Check CPU Speed for small offering")
        self.assertTrue(
            isAlmostEqual(int(int(total_mem) / 1024),
                          int(self.small_offering.memory),
                          range=20), "Check Memory(kb) for small offering")
        return
    def test_08_migrate_vm(self):
        """Test migrate VM
        """
        # Validate the following
        # 1. Environment has enough hosts for migration
        # 2. DeployVM on suitable host (with another host in the cluster)
        # 3. Migrate the VM and assert migration successful

        suitable_hosts = None

        hosts = Host.list(self.apiclient, zoneid=self.zone.id, type='Routing')
        self.assertEqual(
            validateList(hosts)[0], PASS, "hosts list validation failed")

        if len(hosts) < 2:
            self.skipTest(
                "At least two hosts should be present in the zone for migration"
            )

        hypervisor = str(get_hypervisor_type(self.apiclient)).lower()

        # For KVM, two hosts used for migration should  be present in same cluster
        # For XenServer and VMware, migration is possible between hosts belonging to different clusters
        # with the help of XenMotion and Vmotion respectively.

        if hypervisor == "kvm":
            #identify suitable host
            clusters = [h.clusterid for h in hosts]
            #find hosts withe same clusterid
            clusters = [
                cluster for index, cluster in enumerate(clusters)
                if clusters.count(cluster) > 1
            ]

            if len(clusters) <= 1:
                self.skipTest(
                    "In KVM, Live Migration needs two hosts within same cluster"
                )

            suitable_hosts = [
                host for host in hosts if host.clusterid == clusters[0]
            ]
        else:
            suitable_hosts = hosts

        target_host = suitable_hosts[0]
        migrate_host = suitable_hosts[1]

        #deploy VM on target host
        self.vm_to_migrate = VirtualMachine.create(
            self.api_client,
            self.services["small"],
            accountid=self.account.name,
            domainid=self.account.domainid,
            serviceofferingid=self.small_offering.id,
            mode=self.services["mode"],
            hostid=target_host.id)
        self.debug("Migrating VM-ID: %s to Host: %s" %
                   (self.vm_to_migrate.id, migrate_host.id))

        self.vm_to_migrate.migrate(self.api_client, migrate_host.id)

        list_vm_response = list_virtual_machines(self.apiclient,
                                                 id=self.vm_to_migrate.id)
        self.assertNotEqual(list_vm_response, None,
                            "Check virtual machine is listed")

        vm_response = list_vm_response[0]

        self.assertEqual(vm_response.id, self.vm_to_migrate.id,
                         "Check virtual machine ID of migrated VM")

        self.assertEqual(vm_response.hostid, migrate_host.id,
                         "Check destination hostID of migrated VM")
        return
    def test_04_change_offering_small(self):
        """Test to change service to a small capacity
        """
        # Validate the following
        # 1. Log in to the Vm .We should see that the CPU and memory Info of
        #    this Vm matches the one specified for "Small" service offering.
        # 2. Using  listVM command verify that this Vm
        #    has Small service offering Id.

        self.debug("Stopping VM - ID: %s" % self.medium_virtual_machine.id)
        self.medium_virtual_machine.stop(self.apiclient)
        # Ensure that VM is in stopped state
        list_vm_response = list_virtual_machines(
            self.apiclient,
            id=self.medium_virtual_machine.id
        )
        if isinstance(list_vm_response, list):
            vm = list_vm_response[0]
            if vm.state == 'Stopped':
                self.debug("VM state: %s" % vm.state)
            else:
                raise Exception(
                    "Failed to stop VM (ID: %s) in change service offering" % vm.id)

        self.debug("Change Service offering VM - ID: %s" %
                   self.medium_virtual_machine.id)

        cmd = changeServiceForVirtualMachine.changeServiceForVirtualMachineCmd()
        cmd.id = self.medium_virtual_machine.id
        cmd.serviceofferingid = self.small_offering.id
        self.apiclient.changeServiceForVirtualMachine(cmd)

        self.debug("Starting VM - ID: %s" % self.medium_virtual_machine.id)
        self.medium_virtual_machine.start(self.apiclient)
        # Ensure that VM is in running state
        list_vm_response = list_virtual_machines(
            self.apiclient,
            id=self.medium_virtual_machine.id
        )

        if isinstance(list_vm_response, list):
            vm = list_vm_response[0]
            if vm.state == 'Running':
                self.debug("VM state: %s" % vm.state)
            else:
                raise Exception(
                    "Failed to start VM (ID: %s) after changing service offering" % vm.id)

        try:
            ssh = self.medium_virtual_machine.get_ssh_client()
        except Exception as e:
            self.fail(
                "SSH Access failed for %s: %s" %\
                (self.medium_virtual_machine.ipaddress, e)
            )

        cpuinfo = ssh.execute("cat /proc/cpuinfo")
        cpu_cnt = len([i for i in cpuinfo if "processor" in i])
        #'cpu MHz\t\t: 2660.499'
        cpu_speed = [i for i in cpuinfo if "cpu MHz" in i][0].split()[3]
        meminfo = ssh.execute("cat /proc/meminfo")
        #MemTotal:        1017464 kB
        total_mem = [i for i in meminfo if "MemTotal" in i][0].split()[1]

        self.debug(
            "CPU count: %s, CPU Speed: %s, Mem Info: %s" % (
                cpu_cnt,
                cpu_speed,
                total_mem
                ))
        self.assertAlmostEqual(
            int(cpu_cnt),
            self.small_offering.cpunumber,
            "Check CPU Count for small offering"
        )
        self.assertAlmostEqual(
            list_vm_response[0].cpuspeed,
            self.small_offering.cpuspeed,
            "Check CPU Speed for small offering"
        )
        self.assertTrue(
            isAlmostEqual(int(int(total_mem) / 1024),
                int(self.small_offering.memory),
                range=20
            ),
            "Check Memory(kb) for small offering"
        )
        return
Exemple #28
0
    def create_vm(self, pfrule=False, egress_policy=True, RR=False):
        self.create_network_offering(egress_policy, RR)
         # Creating network using the network offering created
        self.debug("Creating network with network offering: %s" %
                                                    self.network_offering.id)
        self.network = Network.create(self.apiclient,
                                      self.services["network"],
                                      accountid=self.account.name,
                                      domainid=self.account.domainid,
                                      networkofferingid=self.network_offering.id,
                                      zoneid=self.zone.id)
        self.cleanup_networks.append(self.network)
        self.debug("Created network with ID: %s" % self.network.id)
        self.debug("Deploying instance in the account: %s" % self.account.name)

        project = None
        try:
            self.virtual_machine = VirtualMachine.create(self.apiclient,
                                                         self.services["virtual_machine"],
                                                         accountid=self.account.name,
                                                         domainid=self.domain.id,
                                                         serviceofferingid=self.service_offering.id,
                                                         mode=self.zone.networktype if pfrule else 'basic',
                                                         networkids=[str(self.network.id)],
                                                         projectid=project.id if project else None)
            self.cleanup_vms.append(self.virtual_machine)
        except Exception as e:
            self.fail("Virtual machine deployment failed with exception: %s" % e)
        self.debug("Deployed instance %s in account: %s" % (self.virtual_machine.id,self.account.name))

        # Checking if VM is running or not, in case it is deployed in error state, test case fails
        self.vm_list = list_virtual_machines(self.apiclient, id=self.virtual_machine.id)

        self.assertEqual(validateList(self.vm_list)[0], PASS, "vm list validation failed, vm list is %s" % self.vm_list)
        self.assertEqual(str(self.vm_list[0].state).lower(),'running',"VM state should be running, it is %s" % self.vm_list[0].state)

        self.public_ip = PublicIPAddress.create(
                                    self.apiclient,
                                    accountid=self.account.name,
                                    zoneid=self.zone.id,
                                    domainid=self.account.domainid,
                                    networkid=self.network.id
                                    )

        # Open up firewall port for SSH
        FireWallRule.create(
                            self.apiclient,
                            ipaddressid=self.public_ip.ipaddress.id,
                            protocol=self.services["natrule"]["protocol"],
                            cidrlist=['0.0.0.0/0'],
                            startport=self.services["natrule"]["publicport"],
                            endport=self.services["natrule"]["publicport"]
                            )

        self.debug("Creating NAT rule for VM ID: %s" % self.virtual_machine.id)
        #Create NAT rule
        NATRule.create(
                        self.apiclient,
                        self.virtual_machine,
                        self.services["natrule"],
                        self.public_ip.ipaddress.id
                        )
        return