Exemple #1
0
def test_exists(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')

    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    assert not gateway_session.exists('/home/user1/non_existing_file')

    gateway_session.run_cmd('touch /home/user1/existing_file')
    assert gateway_session.exists('/home/user1/existing_file')

    gateway_session.run_cmd('rm /home/user1/existing_file')
    assert not gateway_session.exists('/home/user1/existing_file')

    # create file visible only by user2
    gateway_session.run_cmd([
        'sudo mkdir /etc/user2_private_dir',
        'sudo touch /etc/user2_private_dir/existing_file',
        'sudo chown user2:user2 /etc/user2_private_dir',
        'sudo chmod 600 /etc/user2_private_dir'
    ])

    # check it is not visible by user1 by default
    assert not gateway_session.exists('/etc/user2_private_dir/existing_file')

    # check it is readable with root access
    assert gateway_session.exists('/etc/user2_private_dir/existing_file',
                                  use_sudo=True)

    # cleanup
    gateway_session.run_cmd('sudo rm -rf /etc/user2_private_dir')
def test_request_with_body(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()
    remotehost_ip, remotehost_port = docker_env.get_host_ip_port(
        'remotehost', private_port=5000)
    rest_client = RestSshClient(gateway_session)

    uri = 'http://%s:%s/echo-body' % (tests_util.get_host_ip(),
                                      remotehost_port)

    json_file_content = tests_util.create_random_json(5)

    # test body specified in input
    http_response = rest_client.post(uri, data=json.dumps(json_file_content))
    assert http_response.status_code == 200
    assert http_response.json() == json_file_content

    # test body from local file
    # 1. error raised when local file does not exist
    with pytest.raises(exception.RestClientError) as exc_info:
        rest_client.post(uri, local_file='missing_file')
    assert 'Invalid file path given' in str(exc_info.value)
    # 2. create file locally
    with tempfile.NamedTemporaryFile() as tmp_local_file:
        tmp_local_file.write(json.dumps(json_file_content).encode('utf-8'))
        tmp_local_file.seek(0)

        http_response = rest_client.post(uri, local_file=tmp_local_file.name)
        assert http_response.status_code == 200
        assert http_response.json() == json_file_content

        # check file copied on remote host has been properly removed
        assert not gateway_session.exists(path=tmp_local_file.name)

    # test body from remote file
    remote_file = 'remote_file.json'
    # 1. error raised when remote file does not exist
    with pytest.raises(exception.RestClientError) as exc_info:
        rest_client.post(uri, remote_file=remote_file)
    assert 'Invalid remote file path given' in str(exc_info.value)
    # 2. create file remotely
    gateway_session.file(remote_path=remote_file,
                         content=json.dumps(json_file_content))
    http_response = rest_client.post(uri, remote_file=remote_file)
    assert http_response.status_code == 200
    assert http_response.json() == json_file_content