def test_create_and_destroy(self):
        # create resource pool
        try:
            try:
                self.manager.create_resource_pool(name=self.rpname)
            except Manager.ExistenceException:
                pass

            # create vm
            vm = None
            try:
                vm = VirtualMachine(name='net witn space', connected_networks=[''])
                vm.create(manager=self.manager,
                          host_name=self.host_name,
                          resource_pool_name=self.rpname)
            except Manager.ExistenceException:
                pass

                # # destroy vm
                # try:
                #     vm.destroy(self.manager)
                # except Manager.ExistenceException as error:
                #     pass

        except Manager.CreatorException as error:
            self.assertTrue(False, error.message)
        finally:
            pass
    def test_add_serial_port(self):
        try:
            # create resource pool
            try:
                self.manager.create_resource_pool(name=self.rpname)
            except Manager.ExistenceException:
                pass

            # create vm
            vm = None
            try:
                vm = VirtualMachine(name=self.vmname)
                vm.create(manager=self.manager, host_name=self.host_name, resource_pool_name=self.rpname)
            except Manager.ExistenceException:
                pass

            # add serial port to vm
            try:
                vm.add_serial_port(self.manager, self.host_address, self.host_user, self.host_password)
            except Manager.ExistenceException:
                pass

            # turn vm power on
            vm.power_on(self.manager)

            #turn vm power off
            vm.power_off(self.manager)

        except Manager.CreatorException as error:
            self.assertTrue(False, error.message)
        except Exception as error:
            self.assertTrue(False, error.message)
        finally:
            self.manager.destroy_resource_pool_with_vms(self.rpname, self.host_name)
    def test_create_power_on_power_off_and_destroy(self):
        try:

            # create resource pool
            try:
                self.manager.create_resource_pool(name=self.rpname)
            except Manager.ExistenceException:
                pass

            # create vm
            vm = None
            try:
                vm = VirtualMachine(name=self.vmname)
                vm.create(manager=self.manager, host_name=self.host_name, resource_pool_name=self.rpname)
            except Manager.ExistenceException:
                pass

            # turn vm power on
            vm.power_on(self.manager)

            # turn vm power off
            vm.power_off(self.manager)

            # destroy vm
            try:
                vm.destroy(self.manager)
            except Manager.ExistenceException:
                pass

        except Exception as error:
            self.assertTrue(False, error.message)
        finally:
            self.manager.destroy_resource_pool_with_vms(self.rpname, self.host_name)
    def test_add_hard_drive_temp(self):
        virtual_machine = VirtualMachine('t123')

        try:
            # virtual_machine.disk_space = 0
            # virtual_machine.create(self.manager, resource_pool_name='test_RP', host_name='172.18.93.30')
            virtual_machine.add_hard_disk(self.manager, self.host_address, self.host_user,
                                          self.host_password, hard_disk='vmfs/volumes/datastore1/tmp01/tmp01.vldk')
        except Manager.CreatorException as error:
            self.assertTrue(False, error.message)
        except NameError as e:
            print e
    def test_connect_to_vm_host(self):
        virtual_machine = VirtualMachine(self.vmname, self.vmuser, self.vmpassword)
        rctrl = None
        try:
            rctrl = virtual_machine._connect_to_vm_host(self.host_address,
                                                        self.host_user,
                                                        self.host_password)
            output = rctrl.sendline('ls')
            self.assertIsNotNone(output)

        except Manager.CreatorException as error:
            self.assertTrue(False, error.message)
        finally:
            rctrl.close()
 def test_try_to_get_path_with_invalid_manager(self):
     try:
         vm = VirtualMachine(self.vmname, self.vmuser, self.vmpassword)
         try:
             self.manager.create_resource_pool(name=self.rpname)
         except Manager.ExistenceException:
             pass
         try:
             vm.create(manager=self.manager, resource_pool_name=self.rpname, host_name=self.host_name)
         except Manager.ExistenceException:
             pass
         try:
             manager = None
             vm.get_path(manager)
         except AttributeError as error:
             self.assertTrue(True, error.message)
     except Manager.CreatorException as error:
         self.assertTrue(False, error.message)
     except Exception as error:
         self.assertTrue(False, error.message)
     finally:
         self.manager.destroy_resource_pool_with_vms(self.rpname, self.host_name)
    def test_create_and_get_path(self):
        try:
            vm = VirtualMachine(self.vmname, self.vmuser, self.vmpassword)
            try:
                self.manager.create_resource_pool(name=self.rpname)
            except Manager.ExistenceException:
                pass
            try:
                vm.create(manager=self.manager, resource_pool_name=self.rpname, host_name=self.host_name)
            except Manager.ExistenceException:
                pass

            vm_path = vm.get_path(self.manager)
            self.assertIn(vm.name, vm_path)
            self.assertEqual(vm_path, vm.path)

        except Manager.CreatorException as error:
            self.assertTrue(False, error.message)
        except Exception as error:
            self.assertTrue(False, error.message)
        finally:
            self.manager.destroy_resource_pool_with_vms(self.rpname, self.host_name)
    def test_add_hard_disk(self):
        clear_vm_name = self.vmname + '_clear'
        donor_vm_name = self.vmname + '_donor'
        clear_vm = VirtualMachine(clear_vm_name, self.vmuser, self.vmpassword)
        donor_vm = VirtualMachine(donor_vm_name, self.vmuser, self.vmpassword)
        try:
            # create resource pool
            try:
                self.manager.create_resource_pool(name=self.rpname)
            except Manager.ExistenceException:
                pass

            # create
            try:
                clear_vm.create(manager=self.manager, resource_pool_name=self.rpname, host_name=self.host_name)
            except Manager.ExistenceException:
                pass
            try:
                donor_vm.create(manager=self.manager, resource_pool_name=self.rpname, host_name=self.host_name)
            except Manager.ExistenceException:
                pass

            vm_path = None
            vm_path = donor_vm.get_path(manager=self.manager)

            vm_path_spilt = vm_path.split(" ")
            vmdk_donor = "/vmfs/volumes/%s/%s.vmdk" % (vm_path_spilt[0][1:-1], vm_path_spilt[1][:-4] )

            try:
                clear_vm.add_hard_disk(self.manager, self.host_name, self.host_user, self.host_password,
                                       hard_disk=vmdk_donor)
            except ExistenceException:
                pass
        except Manager.CreatorException as error:
            self.assertTrue(False, error.message)
        except Exception as error:
            self.assertTrue(False, error.message)
        finally:
            self.manager.destroy_resource_pool_with_vms(self.rpname, self.host_name)
    def test_add_vnc_and_configure(self):
        virtual_machine = VirtualMachine('t123')

        virtual_machine.vnc_port = 5935
        virtual_machine.disk_space = 0
        virtual_machine.hard_disk = '/vmfs/volumes/datastore1/SourceA-2/SourceA-2.vmdk'
        virtual_machine.connected_networks.append('VLAN1002')
        virtual_machine.connected_networks.append('net11.1_2')
        # try:
        #     virtual_machine.destroy(self.manager)
        # except:
        #     pass
        # virtual_machine.create(manager=self.manager, resource_pool_name='test_RP', host_name='172.18.93.30')
        # virtual_machine.add_vnc_access(self.manager, self.host_address, self.host_user,
        #                                self.host_password)
        # virtual_machine.add_hard_disk(self.manager, self.host_address, self.host_user,
        #                                self.host_password)
        #virtual_machine.power_on(self.manager)
        virtual_machine.configuration.append('automator @exp 2')
        virtual_machine.configuration.append('swordfish @exp 2')
        virtual_machine.configuration.append('ls -al @exp 2')
        virtual_machine.configure_via_vnc(self.host_address)