def test_virtual_machine_ssh_download_fail(helpers_run, vm_run, get, session): os.environ['vcdriver_vm_ssh_username'] = '******' os.environ['vcdriver_vm_ssh_password'] = '******' load() vm = VirtualMachine() vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) with pytest.raises(DownloadError): vm.ssh_download('from', 'to')
def test_virtual_machine_ssh_fail(sudo, helpers_run, vm_run, connection): os.environ['vcdriver_vm_ssh_username'] = '******' os.environ['vcdriver_vm_ssh_password'] = '******' load() vm = VirtualMachine() vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = 'fe80::250:56ff:febf:1a0a' vm.__setattr__('_vm_object', vm_object_mock) with pytest.raises(SshError): vm.ssh('whatever', use_sudo=True)
def test_virtual_machine_winrm_timeout(run_ps, connection): os.environ['vcdriver_vm_winrm_username'] = '******' os.environ['vcdriver_vm_winrm_password'] = '******' load() vm = VirtualMachine(timeout=1) vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) run_ps.side_effect = Exception with pytest.raises(TimeoutError): vm.winrm('script', dict())
def test_virtual_machine_winrm_fail(run_ps, connection): os.environ['vcdriver_vm_winrm_username'] = '******' os.environ['vcdriver_vm_winrm_password'] = '******' load() vm = VirtualMachine() vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) run_ps.return_value.status_code = 1 with pytest.raises(WinRmError): vm.winrm('script', dict())
def test_virtual_machine_ssh_timeout(helpers_run, vm_run, connection): os.environ['vcdriver_vm_ssh_username'] = '******' os.environ['vcdriver_vm_ssh_password'] = '******' load() vm = VirtualMachine(timeout=1) vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) helpers_run.side_effect = Exception vm_run.side_effect = Exception with pytest.raises(TimeoutError): vm.ssh('whatever', use_sudo=True)
def test_virtual_machine_create_not_enough_disk_space( wait_for_vcenter_task, relocate_spec, clone_spec, get_vcenter_object_by_name, connection): os.environ['vcdriver_resource_pool'] = 'something' os.environ['vcdriver_data_store'] = 'something' os.environ['vcdriver_data_store_threshold'] = '120' os.environ['vcdriver_folder'] = 'something' load() vm = VirtualMachine() with pytest.raises(NotEnoughDiskSpace): vm.create() assert vm.__getattribute__('_vm_object') is None assert wait_for_vcenter_task.call_count == 0
def test_virtual_machine_create(wait_for_vcenter_task, relocate_spec, clone_spec, get_vcenter_object_by_name, connection): os.environ['vcdriver_resource_pool'] = 'something' os.environ['vcdriver_data_store'] = 'something' os.environ['vcdriver_data_store_threshold'] = '20' os.environ['vcdriver_folder'] = 'something' load() vm = VirtualMachine() vm.create() vm.create() assert vm.__getattribute__('_vm_object') is not None assert wait_for_vcenter_task.call_count == 1
def test_virtual_machine_download_success(helpers_run, vm_run, get, session): os.environ['vcdriver_vm_ssh_username'] = '******' os.environ['vcdriver_vm_ssh_password'] = '******' load() vm = VirtualMachine() assert vm.download('from', 'to') is None vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) result_mock = mock.MagicMock() result_mock.failed = False get.return_value = result_mock assert vm.download('from', 'to') == result_mock
def test_virtual_machine_winrm_success(run_ps, connection): os.environ['vcdriver_vm_winrm_username'] = '******' os.environ['vcdriver_vm_winrm_password'] = '******' load() vm = VirtualMachine() assert vm.winrm('whatever', dict()) is None vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) run_ps.return_value.status_code = 0 vm.winrm('script', dict()) run_ps.assert_called_with('script') assert run_ps.call_count == 2
def vms(): os.environ['vcdriver_folder'] = os.getenv('vcdriver_test_folder') load(os.getenv('vcdriver_test_config_file')) unix = VirtualMachine(template=os.getenv('vcdriver_test_unix_template')) windows = VirtualMachine( template=os.getenv('vcdriver_test_windows_template')) vms = {'unix': unix, 'windows': windows} for vm in vms.values(): try: vm.find() vm.destroy() except NoObjectFound: pass with virtual_machines(vms.values()): yield vms
def test_virtual_machine_ssh_success(helpers_run, vm_run, sudo, connection): os.environ['vcdriver_vm_ssh_username'] = '******' os.environ['vcdriver_vm_ssh_password'] = '******' load() vm = VirtualMachine() assert vm.ssh('whatever') is None vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) result_mock = mock.MagicMock() result_mock.return_code = 3 result_mock.failed = False helpers_run.return_value = result_mock vm_run.return_value = result_mock sudo.return_value = result_mock assert vm.ssh('whatever', use_sudo=False).return_code == 3 assert vm.ssh('whatever', use_sudo=True).return_code == 3
def vms(): load(os.getenv('vcdriver_test_config_file')) unix = VirtualMachine( name='test-integration-vcdriver-unix', template=os.getenv('vcdriver_test_unix_template') ) windows = VirtualMachine( name='test-integration-vcdriver-windows', template=os.getenv('vcdriver_test_windows_template') ) vms = {'unix': unix, 'windows': windows} yield vms for vm in vms.values(): try: vm.find() vm.destroy() except: pass
def test_read(config_files): assert read() == { 'Vsphere Session': { 'vcdriver_host': '', 'vcdriver_port': '443', 'vcdriver_username': '', 'vcdriver_password': '' }, 'Virtual Machine Deployment': { 'vcdriver_resource_pool': '', 'vcdriver_data_store': '', 'vcdriver_data_store_threshold': '0', 'vcdriver_folder': '' }, 'Virtual Machine Remote Management': { 'vcdriver_vm_ssh_username': '', 'vcdriver_vm_ssh_password': '', 'vcdriver_vm_winrm_username': '', 'vcdriver_vm_winrm_password': '' } } load('config_file_3.cfg') assert read() == { 'Vsphere Session': { 'vcdriver_host': '', 'vcdriver_port': '443', 'vcdriver_username': '', 'vcdriver_password': '******' }, 'Virtual Machine Deployment': { 'vcdriver_resource_pool': '', 'vcdriver_data_store': '', 'vcdriver_data_store_threshold': '0', 'vcdriver_folder': '' }, 'Virtual Machine Remote Management': { 'vcdriver_vm_ssh_username': '', 'vcdriver_vm_ssh_password': '', 'vcdriver_vm_winrm_username': '', 'vcdriver_vm_winrm_password': '' } }
def test_virtual_machine_winrm_upload_fail(run_ps, connection, open, os_stat): st_size_mock = mock.Mock() st_size_mock.st_size = 3 os_stat.return_value = st_size_mock code_mock = mock.Mock() code_mock.status_code = 1 code_mock.std_err = 'Whatever'.encode('ascii') run_ps.return_value = code_mock read_mock = mock.Mock read_mock.read = lambda x, y: b'\0\0\0' open.__enter__ = read_mock open.__exit__ = mock.Mock() os.environ['vcdriver_vm_winrm_username'] = '******' os.environ['vcdriver_vm_winrm_password'] = '******' load() vm = VirtualMachine() vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) with pytest.raises(WinRmError): vm.winrm_upload('whatever', 'whatever', step=2)
def test_virtual_machine_winrm_upload_success(run_ps, connection, open, os_stat): st_size_mock = mock.Mock() st_size_mock.st_size = 3 os_stat.return_value = st_size_mock code_mock = mock.Mock() code_mock.status_code = 0 run_ps.return_value = code_mock read_mock = mock.Mock read_mock.read = lambda x, y: b'\0\0\0' open.__enter__ = read_mock open.__exit__ = mock.Mock() os.environ['vcdriver_vm_winrm_username'] = '******' os.environ['vcdriver_vm_winrm_password'] = '******' load() vm = VirtualMachine() assert vm.winrm_upload('whatever', 'whatever') is None vm_object_mock = mock.MagicMock() vm_object_mock.summary.guest.ipAddress = '127.0.0.1' vm.__setattr__('_vm_object', vm_object_mock) assert vm.winrm_upload('whatever', 'whatever', step=2) is None assert vm.winrm_upload('whatever', 'whatever', step=2, quiet=True) is None
def test_configurable(config_files): load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() load('config_file_1.cfg') require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() os.environ['vcdriver_username'] = '******' load() require_nothing() require_username() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() getpass_mock.assert_called_once() os.environ['vcdriver_username'] = '' load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() load('config_file_2.cfg') require_nothing() require_username() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() getpass_mock.assert_called_once() load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() os.environ['vcdriver_username'] = '******' load('config_file_3.cfg') require_nothing() require_username() require_username_and_password() os.environ['vcdriver_username'] = '' load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() require_username_and_password(vcdriver_username='******', vcdriver_password='******') with mock.patch('vcdriver.config.input') as input_mock: require_bad_section() input_mock.assert_called_once()