Example #1
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
Example #2
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
Example #3
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
Example #4
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
Example #5
0
def test_virtual_machine_winrm_upload_timeout(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 = 'Blah is being used by another process'.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)
    vm.timeout = 1
    with pytest.raises(TimeoutError):
        vm.winrm_upload('whatever', 'whatever', step=2)
Example #6
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)
Example #7
0
def test_virtual_machines_success(destroy, create, connection):
    vm = VirtualMachine()
    with virtual_machines([vm]):
        pass
    create.assert_called_once_with()
    destroy.assert_called_once_with()
Example #8
0
def test_str_repr():
    assert str(VirtualMachine(name='whatever')) == 'whatever'
    assert repr(VirtualMachine(name='whatever')) == 'whatever'
Example #9
0
def test_virtual_machine_summary(connection):
    print(VirtualMachine().summary())
Example #10
0
def test_set_autostart(init):
    vm = VirtualMachine()
    vm.set_autostart()
    vm.__setattr__('_vm_object', mock.MagicMock())
    vm.set_autostart()
Example #11
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')
Example #12
0
def test_schedule_vcenter_task_on_vm_fail_on_bad_type(connection):
    vm = VirtualMachine()
    with pytest.raises(TypeError):
        vm._schedule_vcenter_task_on_vm(vim.VirtualMachine.PowerOff,
                                        'Power off virtual machine "dummy"',
                                        "Wrong type")
Example #13
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
Example #14
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()
Example #15
0
def test_virtual_machine_find(get_vcenter_object_by_name, connection):
    vm = VirtualMachine()
    vm.find()
    vm.find()
    assert vm.__getattribute__('_vm_object') is not None
    assert get_vcenter_object_by_name.call_count == 1
Example #16
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')
Example #17
0
def test_virtual_machine_vm_id_return_none():
    vm = VirtualMachine()
    vm_id = vm.vm_id()
    assert vm_id is None