Exemplo n.º 1
0
def test_key_file_keep_key_file(ssh):
    s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
    f1 = s.key_file()
    f2 = s.keep_key_file()
    with pytest.raises(IOError):
        open(f1, 'r')
    assert 'secret' == open(f2, 'r').read()
    os.remove(f2)
Exemplo n.º 2
0
def test_env_vars(ssh):
    s = ssh.SSH('192.168.0.1', 'user', 'secret')
    env = s.env_vars('TEST_')
    assert env['TEST_SSH_USERNAME'] == 'user'
    assert env['TEST_SSH_IP'] == '192.168.0.1'
    assert env['TEST_SSH_PORT'] == '22'
    assert open(env['TEST_SSH_PRIVATE_KEY'], 'r').read() == 'secret'
    assert "Host" in open(env['TEST_SSH_CONFIG'], 'r').read()
    assert "192.168.0.1" in env['TEST_SSH']
    del s
Exemplo n.º 3
0
def test_command_line(ssh):
    s = ssh.SSH('192.168.0.1', 'user', 'secret')
    cmdline = " ".join(s.command_line())
    assert '[email protected]' in cmdline
    assert "-o StrictHostKeyChecking=no" in cmdline
    assert "-o UserKnownHostsFile=/dev/null" in cmdline
    assert "-o UpdateHostKeys=no" in cmdline
    assert "-o PasswordAuthentication=no" in cmdline
    assert "-i " in cmdline
    del s
Exemplo n.º 4
0
def test_shell_simple_run(ssh):
    with mock.patch.object(ssh.SSH, "COMMAND_NAME", "echo"):
        s = ssh.SSH('192.168.0.1', 'user', 'secret')
        rfd, wfd = os.pipe()
        w = os.fdopen(wfd, 'w', 0)
        with mock.patch.multiple(ssh.sys, stdout=w, stderr=w, stdin=None):
            s.shell({}, 'test message')
        output = os.read(rfd, 1000)
        assert 'echo' in output  # should be ssh, but test demands
        assert '[email protected]' in output
Exemplo n.º 5
0
def test_keep_key_file_with_override(ssh):
    t = tempfile.NamedTemporaryFile()
    t.write('secret')
    t.flush()
    s = ssh.SSH(sentinel.ip,
                sentinel.user,
                None,
                sentinel.port,
                override_ssh_key_filename=t.name)
    assert s.keep_key_file() == t.name
    del t
Exemplo n.º 6
0
def test_config_content(ssh):
    s = ssh.SSH('192.168.0.1', 'user', 'secret', port=99)
    cfg = s.config()
    with open(cfg, 'r') as c:
        data = c.read()
        assert "User user" in data
        assert "Host 192.168.0.1" in data
        assert "StrictHostKeyChecking no" in data
        assert "UserKnownHostsFile /dev/null" in data
        assert "UpdateHostKeys no" in data
        assert "PasswordAuthentication no" in data
        assert "Port 99" in data
        assert "IdentityFile" in data
    del s
Exemplo n.º 7
0
def test_runner_empty_tests(shell_runner, ssh):
    tos = mock.MagicMock()
    tos.ip = '192.168.1.1'
    tos.os_key_private_file = '~/.ssh/config'
    vars = {}
    s = ssh.SSH('192.168.1.1', 'user', 'secret')
    with mock.patch.object(shell_runner, "gather_tests", return_value=[]):
        assert shell_runner.runner(sentinel.path,
                                   s,
                                   tos,
                                   vars,
                                   sentinel.timeout,
                                   continue_on_fail=False) is True
    del s
Exemplo n.º 8
0
def dcp(pytest_runner, ssh):
    tos = mock.MagicMock()
    tos.ip = '192.168.0.1'
    tos.os_instance.interface_list.return_value = [
        sentinel.iface1, sentinel.iface2
    ]
    tos.flavor.return_value.get_keys.return_value = {'name': 'value'}
    tos.key_name = 'foo-key-name'
    tos.os_key_private_file = 'private-file'
    tos.ips.return_value = [sentinel.ip1, sentinel.ip2]
    tos.ips_by_version.return_value = [sentinel.ip3, sentinel.ip4]
    tos.get_image_info.return_value = sentinel.image_info
    tos.image = sentinel.image
    tos.os_instance.get_console_output.return_value = sentinel.console_out
    s = ssh.SSH('192.168.0.1', 'root', 'secret')
    dcp = pytest_runner.DibCtlPlugin(s, tos, {})
    return dcp
Exemplo n.º 9
0
def test_runner_all_with_continue(shell_runner, ssh):
    tos = mock.MagicMock()
    tos.ip = '192.168.1.1'
    tos.os_key_private_file = '~/.ssh/config'
    vars = {}
    s = ssh.SSH('192.168.1.1', 'user', 'secret')
    with mock.patch.object(shell_runner,
                           "gather_tests",
                           return_value=["test1", "test2"]):
        with mock.patch.object(shell_runner,
                               "run_shell_test",
                               return_value=False) as mock_run:
            assert shell_runner.runner(sentinel.path,
                                       s,
                                       tos,
                                       vars,
                                       sentinel.timeout,
                                       continue_on_fail=True) is False
            assert mock_run.call_count == 2
    del s
Exemplo n.º 10
0
def test_connector(ssh):
    s = ssh.SSH('192.168.0.1', sentinel.user, sentinel.key)
    assert s.connector() == 'ssh://192.168.0.1'
Exemplo n.º 11
0
def test_user_host_and_port(ssh, ip, port, user, output):
    s = ssh.SSH(ip, user, None, port)
    assert s.user_host_and_port() == output
Exemplo n.º 12
0
def test_keep_key_file_content(ssh):
    s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
    f = s.keep_key_file()
    assert 'secret' in open(f, 'r').read()
    os.remove(f)
Exemplo n.º 13
0
def test_keep_key_file_kept_after_removal(ssh):
    s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
    f = s.keep_key_file()
    del s
    assert 'secret' in open(f, 'r').read()
    os.remove(f)
Exemplo n.º 14
0
def test_config_name(ssh):
    s = ssh.SSH('192.168.0.1', 'user', 'secret')
    assert 'dibctl_config_' in s.config()
    del s
Exemplo n.º 15
0
def test_keep_key_file_name(ssh):
    s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
    f = s.keep_key_file()
    assert 'saved_dibctl_key_' in f
    os.remove(f)
Exemplo n.º 16
0
def test_key_file_content(ssh):
    s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
    f = s.key_file()
    assert open(f, 'r').read() == "secret"
Exemplo n.º 17
0
def test_key_file_remove_afterwards(ssh):
    s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
    f = s.key_file()
    del s
    with pytest.raises(IOError):
        open(f, 'r')
Exemplo n.º 18
0
def test_key_file_name(ssh):
    s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
    f = s.key_file()
    assert 'dibctl_key' in f
Exemplo n.º 19
0
def test_key_file(ssh):
    with mock.patch.object(ssh.tempfile, 'NamedTemporaryFile') as mock_tmp:
        mock_tmp.return_value.name = sentinel.filename
        s = ssh.SSH(sentinel.ip, sentinel.user, "secret", sentinel.port)
        assert s.key_file() == sentinel.filename
        assert mock_tmp.return_value.write.call_args == mock.call('secret')
Exemplo n.º 20
0
def test_config_afterwards(ssh):
    s = ssh.SSH('192.168.0.1', 'user', 'secret')
    cfg = s.config()
    del s
    with pytest.raises(IOError):
        open(cfg, 'r')
Exemplo n.º 21
0
def test_info_2(ssh, key):
    s = ssh.SSH('192.168.0.1', 'user', 'secret')
    i = s.info()
    assert key in i
    del s
Exemplo n.º 22
0
def test_info(ssh, key, value):
    s = ssh.SSH('192.168.0.1', 'user', 'secret')
    i = s.info()
    assert i[key] == value
    del s