Beispiel #1
0
def test_virtual_machine_ip_timeout(connection):
    vm = VirtualMachine(timeout=1)
    vm_object_mock = mock.MagicMock()
    vm_object_mock.summary.guest.ipAddress = None
    vm.__setattr__('_vm_object', vm_object_mock)
    with pytest.raises(TimeoutError):
        vm.ip()
Beispiel #2
0
def test_virtual_machine_vm_id():
    vm = VirtualMachine()
    vm_object_mock = mock.MagicMock()
    vm_object_mock.summary.vm = "'vim.VirtualMachine:vm-83288'"
    vm.__setattr__('_vm_object', vm_object_mock)
    vm_id = vm.vm_id()
    assert vm_id == 'vm-83288'
Beispiel #3
0
def test_virtual_machine_ip(connection):
    vm = VirtualMachine()
    vm_object_mock = mock.MagicMock()
    vm_object_mock.summary.guest.ipAddress = '127.0.0.1'
    assert vm.ip() is None
    vm.__setattr__('_vm_object', vm_object_mock)
    assert vm.ip() == '127.0.0.1'
    assert vm.ip() == '127.0.0.1'
Beispiel #4
0
def test_virtual_machine_ip_with_dhcp_wait(validate_ip, timeout_loop,
                                           connection):
    vm = VirtualMachine()
    validate_ip.side_effect = lambda x: x
    vm_object_mock = mock.MagicMock()
    vm_object_mock.summary.guest.ipAddress = None
    vm.__setattr__('_vm_object', vm_object_mock)
    assert vm.ip() is None
Beispiel #5
0
def test_virtual_machine_destroy_vm_on(wait_for_vcenter_task, connection):
    vm = VirtualMachine()
    vm_object_mock = mock.MagicMock()
    vm.__setattr__('_vm_object', vm_object_mock)
    vm.destroy()
    vm.destroy()
    assert vm.__getattribute__('_vm_object') is None
    assert wait_for_vcenter_task.call_count == 2
Beispiel #6
0
def test_virtual_machine_reset_wrong_power_state(wait_for_vcenter_task,
                                                 connection):
    wait_for_vcenter_task.side_effect = vim.fault.InvalidPowerState
    vm = VirtualMachine()
    vm.reset()
    vm.__setattr__('_vm_object', mock.MagicMock())
    vm.reset()
    assert wait_for_vcenter_task.call_count == 1
Beispiel #7
0
def test_virtual_machine_destroy_vm_off(wait_for_vcenter_task, connection):
    vm = VirtualMachine()
    vm_object_mock = mock.MagicMock()
    vm.__setattr__('_vm_object', vm_object_mock)
    wait_for_vcenter_task.side_effect = [vim.fault.InvalidPowerState, None]
    vm.destroy()
    vm.destroy()
    assert vm.__getattribute__('_vm_object') is None
    assert wait_for_vcenter_task.call_count == 2
Beispiel #8
0
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)
Beispiel #9
0
def test_virtual_machine_power_off(wait_for_vcenter_task, connection):
    vm = VirtualMachine()
    mock_schedule_vcenter_task_on_vm = mock.MagicMock()
    vm.__setattr__('_schedule_vcenter_task_on_vm',
                   mock_schedule_vcenter_task_on_vm)
    vm.power_off()
    vm.__setattr__('_vm_object', mock.MagicMock())
    vm.power_off()
    assert wait_for_vcenter_task.call_count == 1
    assert mock_schedule_vcenter_task_on_vm.call_count == 0
Beispiel #10
0
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')
Beispiel #11
0
def test_virtual_machine_shutdown_wrong_power_state(connection):
    vm = VirtualMachine()
    vm_object_mock = mock.MagicMock()
    shutdown_mock = mock.MagicMock()
    vm_object_mock.ShutdownGuest = shutdown_mock
    vm_object_mock.summary.runtime.powerState = 'poweredOff'
    vm_object_mock.summary.guest.toolsRunningStatus = 'guestToolsRunning'
    vm.shutdown()
    vm.__setattr__('_vm_object', vm_object_mock)
    vm.shutdown()
    assert shutdown_mock.call_count == 0
Beispiel #12
0
def test_virtual_machine_reboot(connection):
    vm = VirtualMachine()
    vm_object_mock = mock.MagicMock()
    reboot_mock = mock.MagicMock()
    vm_object_mock.RebootGuest = reboot_mock
    vm_object_mock.summary.runtime.powerState = 'poweredOn'
    vm_object_mock.summary.guest.toolsRunningStatus = 'guestToolsRunning'
    vm.reboot()
    vm.__setattr__('_vm_object', vm_object_mock)
    vm.reboot()
    assert reboot_mock.call_count == 1
Beispiel #13
0
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())
Beispiel #14
0
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())
Beispiel #15
0
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)
Beispiel #16
0
def test_virtual_machine_power_off_with_delay(wait_for_vcenter_task,
                                              connection):
    vm = VirtualMachine()
    mock_schedule_vcenter_task_on_vm = mock.MagicMock()
    vm.__setattr__('_schedule_vcenter_task_on_vm',
                   mock_schedule_vcenter_task_on_vm)
    vm.power_off(delay_by=datetime.timedelta(hours=1))
    vm_object_mock = mock.MagicMock()
    vm_object_mock.summary.vm = "'vim.VirtualMachine:vm-83288'"
    vm.__setattr__('_vm_object', vm_object_mock)
    vm.power_off(delay_by=datetime.timedelta(hours=1))
    assert wait_for_vcenter_task.call_count == 0
    assert mock_schedule_vcenter_task_on_vm.call_count == 1
Beispiel #17
0
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
Beispiel #18
0
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
Beispiel #19
0
def test_virtual_machine_create_snapshot(wait_for_vcenter_task):
    fake_snapshot = mock.MagicMock()
    fake_snapshot.name = 'snapshot'
    fake_snapshot.childSnapshotList = []
    vm = VirtualMachine()
    assert vm.create_snapshot('snapshot', True) is None
    vm_object_mock = mock.MagicMock()
    vm_object_mock.snapshot.rootSnapshotList = []
    vm.__setattr__('_vm_object', vm_object_mock)
    vm.create_snapshot('snapshot', True)
    vm_object_mock.snapshot.rootSnapshotList = [fake_snapshot]
    with pytest.raises(TooManyObjectsFound):
        vm.create_snapshot('snapshot', True)
Beispiel #20
0
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
Beispiel #21
0
def destroy_virtual_machines(folder_name, timeout=600):
    """
    Destroy all the virtual machines in the folder with the given name
    :param folder_name: The folder name
    :param timeout: The timeout for vcenter tasks in seconds

    :return: A list with the destroyed vms
    """
    folder = get_vcenter_object_by_name(connection(), vim.Folder, folder_name)
    destroyed_vms = []
    for entity in folder.childEntity:
        if isinstance(entity, vim.VirtualMachine):
            vm = VirtualMachine(name=entity.summary.config.name,
                                timeout=timeout)
            vm.__setattr__('_vm_object', entity)
            vm.destroy()
            destroyed_vms.append(vm)
    return destroyed_vms
Beispiel #22
0
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)
Beispiel #23
0
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
Beispiel #24
0
def test_virtual_machine_find_snapshot(wait_for_vcenter_task):
    fake_snapshots = [mock.MagicMock(), mock.MagicMock(), mock.MagicMock()]
    for fake_snapshot in fake_snapshots[:-1]:
        fake_snapshot.name = 'snapshot'
        fake_snapshot.childSnapshotList = []
    fake_snapshots[-1].name = 'other'
    fake_snapshots[-1].childSnapshotList = []
    vm = VirtualMachine()
    assert vm.find_snapshot('snapshot') is None
    vm_object_mock = mock.MagicMock()
    vm.__setattr__('_vm_object', vm_object_mock)
    vm_object_mock.snapshot.rootSnapshotList = []
    with pytest.raises(NoObjectFound):
        vm.find_snapshot('snapshot')
    vm_object_mock.snapshot.rootSnapshotList = fake_snapshots[:-2]
    vm.find_snapshot('snapshot')
    vm_object_mock.snapshot.rootSnapshotList = fake_snapshots
    with pytest.raises(TooManyObjectsFound):
        vm.find_snapshot('snapshot')
    vm_object_mock.snapshot = None
    with pytest.raises(NoObjectFound):
        vm.find_snapshot('snapshot')
Beispiel #25
0
def test_virtual_machine_refresh(close, get_vcenter_object_by_name,
                                 connection):
    vm = VirtualMachine()
    assert vm.__getattribute__('_vm_object') is None

    # Test that refresh does nothing if no _vm_object
    vm.refresh()
    assert vm.__getattribute__('_vm_object') is None

    # Test that refresh assigns a new _vm_object
    vm.__setattr__('_vm_object', mock.Mock())

    initial__vm_object = vm.__getattribute__('_vm_object')
    assert initial__vm_object is not None
    vm.refresh()

    refreshed__vm_object = vm.__getattribute__('_vm_object')
    assert refreshed__vm_object is not None

    assert initial__vm_object != refreshed__vm_object

    close.assert_called_once()
    get_vcenter_object_by_name.assert_called_once()
Beispiel #26
0
def test_virtual_machine_reset(wait_for_vcenter_task, connection):
    vm = VirtualMachine()
    vm.reset()
    vm.__setattr__('_vm_object', mock.MagicMock())
    vm.reset()
    assert wait_for_vcenter_task.call_count == 1
Beispiel #27
0
def test_created_at(connection):
    vm = VirtualMachine()
    vm_object_mock = mock.MagicMock()
    vm_object_mock.config.changeVersion = '2018-06-13T15:12:43.700814Z'
    vm.__setattr__('_vm_object', vm_object_mock)
    assert vm.created_at == datetime.datetime(2018, 6, 13, 15, 12, 43, 700814)
Beispiel #28
0
def test_set_autostart(init):
    vm = VirtualMachine()
    vm.set_autostart()
    vm.__setattr__('_vm_object', mock.MagicMock())
    vm.set_autostart()
Beispiel #29
0
def test_virtual_machine_remove_snapshot(wait_for_vcenter_task):
    vm = VirtualMachine()
    assert vm.remove_snapshot('snapshot') is None
    vm.find_snapshot = mock.MagicMock()
    vm.__setattr__('_vm_object', mock.MagicMock())
    vm.remove_snapshot('snapshot')