def test_copy_into_container_with_existing_target( self, tmpdir, docker_client: ContainerClient, dummy_container): local_path = tmpdir.join("myfile.txt") container_path = "/tmp/myfile.txt" with local_path.open(mode="w") as fd: fd.write("foo\n") docker_client.start_container(dummy_container.container_id) docker_client.exec_in_container( dummy_container.container_id, command=["sh", "-c", f"echo bar > {container_path}"]) out, _ = docker_client.exec_in_container( dummy_container.container_id, command=[ "cat", "/tmp/myfile.txt", ], ) assert "bar" in out.decode(config.DEFAULT_ENCODING) docker_client.copy_into_container(dummy_container.container_id, str(local_path), container_path) out, _ = docker_client.exec_in_container( dummy_container.container_id, command=[ "cat", "/tmp/myfile.txt", ], ) assert "foo" in out.decode(config.DEFAULT_ENCODING)
def test_copy_directory_content_into_container( self, tmpdir, docker_client: ContainerClient, dummy_container): local_path = tmpdir.join("fancy_folder") local_path.mkdir() file_path = local_path.join("myfile.txt") with file_path.open(mode="w") as fd: fd.write("foo\n") file_path = local_path.join("myfile2.txt") with file_path.open(mode="w") as fd: fd.write("bar\n") container_path = "/tmp/fancy_other_folder" docker_client.start_container(dummy_container.container_id) docker_client.exec_in_container( dummy_container.container_id, command=["mkdir", "-p", container_path]) docker_client.copy_into_container(dummy_container.container_id, f"{str(local_path)}/.", container_path) out, _ = docker_client.exec_in_container( dummy_container.container_id, command=[ "cat", "/tmp/fancy_other_folder/myfile.txt", "/tmp/fancy_other_folder/myfile2.txt", ], ) assert "foo" in out.decode(config.DEFAULT_ENCODING) assert "bar" in out.decode(config.DEFAULT_ENCODING)
def test_exec_in_container_not_running_raises_exception( self, docker_client: ContainerClient, dummy_container ): with pytest.raises(ContainerException): # can't exec into a non-running container docker_client.exec_in_container( dummy_container.container_id, command=["echo", "foobar"] )
def test_exec_error_in_container(self, docker_client: ContainerClient, dummy_container): docker_client.start_container(dummy_container.container_id) with pytest.raises(ContainerException) as ex: docker_client.exec_in_container(dummy_container.container_id, command=["./doesnotexist"]) assert ex.match("doesnotexist: no such file or directory")
def test_exec_in_container(self, docker_client: ContainerClient, dummy_container: ContainerInfo): docker_client.start_container(dummy_container.container_id) output, _ = docker_client.exec_in_container( dummy_container.container_id, command=["echo", "foobar"]) output = output.decode(config.DEFAULT_ENCODING) assert "foobar" == output.strip()
def _test_copy_from_container( self, local_path, file_path, container_file_name, docker_client: ContainerClient, dummy_container, ): docker_client.exec_in_container( dummy_container.container_id, command=["sh", "-c", f"echo TEST_CONTENT > {container_file_name}"], ) docker_client.copy_from_container( dummy_container.container_id, local_path=str(local_path), container_path=container_file_name, ) assert "TEST_CONTENT" == file_path.read().strip()
def test_exec_in_container_with_env(self, docker_client: ContainerClient, dummy_container): docker_client.start_container(dummy_container.container_id) env = {"MYVAR": "foo_var"} output, _ = docker_client.exec_in_container( dummy_container.container_id, env_vars=env, command=["env"]) output = output.decode(config.DEFAULT_ENCODING) assert "MYVAR=foo_var" in output
def test_exec_in_container_with_stdin(self, docker_client: ContainerClient, dummy_container): docker_client.start_container(dummy_container.container_id) message = "test_message_stdin" output, _ = docker_client.exec_in_container( dummy_container.container_id, interactive=True, stdin=message.encode(config.DEFAULT_ENCODING), command=["cat"], ) assert message == output.decode(config.DEFAULT_ENCODING).strip()
def test_exec_in_container_with_stdin_stdout_stderr( self, docker_client: ContainerClient, dummy_container): docker_client.start_container(dummy_container.container_id) message = "test_message_stdin" output, stderr = docker_client.exec_in_container( dummy_container.container_id, interactive=True, stdin=message.encode(config.DEFAULT_ENCODING), command=["sh", "-c", "cat; >&2 echo stderrtest"], ) assert message == output.decode(config.DEFAULT_ENCODING).strip() assert "stderrtest" == stderr.decode(config.DEFAULT_ENCODING).strip()