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
def test_run_cmd_sudo(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() # run command as user2 assert gateway_session.run_cmd('whoami', username='******').output == 'user2' # run bash builtins commands with sudo (here command 'source') gateway_session.file(remote_path='/home/user2/ssh_setenv', use_sudo=True, owner='user2', content='MY_VAR=variable_set') gateway_session.run_cmd('source /home/user2/ssh_setenv', username='******')