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_copy_into_non_existent_container(self, tmpdir, docker_client: ContainerClient): local_path = tmpdir.mkdir("test_dir") file_path = local_path.join("test_file") with file_path.open(mode="w") as fd: fd.write("foobared\n") with pytest.raises(NoSuchContainer): docker_client.copy_into_container( "hopefully_non_existent_container_%s" % short_uid(), str(file_path), "test_file" )