コード例 #1
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    def test_list_containers_filter(self, docker_client: ContainerClient,
                                    create_container):
        name_prefix = "filter_tests_"
        cn1 = name_prefix + _random_container_name()
        cn2 = name_prefix + _random_container_name()
        cn3 = name_prefix + _random_container_name()

        c1 = create_container("alpine", name=cn1, command=["echo", "1"])
        c2 = create_container("alpine", name=cn2, command=["echo", "2"])
        c3 = create_container("alpine", name=cn3, command=["echo", "3"])

        # per id
        container_list = docker_client.list_containers(
            filter=f"id={c2.container_id}")
        assert 1 == len(container_list)
        assert c2.container_id.startswith(container_list[0]["id"])
        assert c2.container_name == container_list[0]["name"]
        assert "created" == container_list[0]["status"]

        # per name pattern
        container_list = docker_client.list_containers(
            filter=f"name={name_prefix}")
        assert 3 == len(container_list)
        image_names = [info["name"] for info in container_list]
        assert c1.container_name in image_names
        assert c2.container_name in image_names
        assert c3.container_name in image_names

        # multiple patterns
        container_list = docker_client.list_containers(filter=[
            f"id={c1.container_id}",
            f"name={container_name_prefix}",
        ])
        assert 1 == len(container_list)
        assert c1.container_name == container_list[0]["name"]
コード例 #2
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
    def test_get_logs_non_existent_container(self, docker_client: ContainerClient):
        with pytest.raises(NoSuchContainer):
            docker_client.get_container_logs("container_hopefully_does_not_exist", safe=False)

        assert "" == docker_client.get_container_logs(
            "container_hopefully_does_not_exist", safe=True
        )
コード例 #3
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
 def test_inspect_network(self, docker_client: ContainerClient,
                          create_network):
     network_name = "ls_test_network_%s" % short_uid()
     network_id = create_network(network_name)
     assert network_name == docker_client.inspect_network(
         network_name)["Name"]
     assert network_id == docker_client.inspect_network(network_name)["Id"]
コード例 #4
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
 def test_copy_from_non_existent_container(self, tmpdir,
                                           docker_client: ContainerClient):
     local_path = tmpdir.mkdir("test_dir")
     with pytest.raises(NoSuchContainer):
         docker_client.copy_from_container(
             "hopefully_non_existent_container_%s" % short_uid(),
             str(local_path), "test_file")
コード例 #5
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    def test_container_lifecycle_commands(self,
                                          docker_client: ContainerClient):
        container_name = _random_container_name()
        output = docker_client.create_container(
            "alpine",
            name=container_name,
            command=[
                "sh", "-c", "for i in `seq 30`; do sleep 1; echo $i; done"
            ],
        )
        container_id = output.strip()
        assert container_id

        try:
            docker_client.start_container(container_id)
            assert DockerContainerStatus.UP == docker_client.get_container_status(
                container_name)
            docker_client.stop_container(container_id)
            assert DockerContainerStatus.DOWN == docker_client.get_container_status(
                container_name)
        finally:
            docker_client.remove_container(container_id)

        assert DockerContainerStatus.NON_EXISTENT == docker_client.get_container_status(
            container_name)
コード例 #6
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_get_container_ip(self, docker_client: ContainerClient, dummy_container):
     docker_client.start_container(dummy_container.container_id)
     ip = docker_client.get_container_ip(dummy_container.container_id)
     assert re.match(
         r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
         ip,
     )
     assert "127.0.0.1" != ip
コード例 #7
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 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"]
         )
コード例 #8
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_copy_from_container_to_different_file(
     self, tmpdir, docker_client: ContainerClient, dummy_container
 ):
     docker_client.start_container(dummy_container.container_id)
     local_path = tmpdir.join("test_file_2")
     self._test_copy_from_container(
         local_path, local_path, "test_file", docker_client, dummy_container
     )
コード例 #9
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_inspect_container(self, docker_client: ContainerClient, dummy_container):
     docker_client.start_container(dummy_container.container_id)
     for identifier in [dummy_container.container_id, dummy_container.container_name]:
         assert dummy_container.container_id == docker_client.inspect_container(identifier)["Id"]
         assert (
             f"/{dummy_container.container_name}"
             == docker_client.inspect_container(identifier)["Name"]
         )
コード例 #10
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
 def test_copy_from_container_into_directory(self, tmpdir,
                                             docker_client: ContainerClient,
                                             dummy_container):
     docker_client.start_container(dummy_container.container_id)
     local_path = tmpdir.mkdir("test_dir")
     file_path = local_path.join("test_file")
     self._test_copy_from_container(local_path, file_path, "test_file",
                                    docker_client, dummy_container)
コード例 #11
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    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()
コード例 #12
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    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")
コード例 #13
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 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"
         )
コード例 #14
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    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
コード例 #15
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
    def test_create_with_volume(self, tmpdir, docker_client: ContainerClient, create_container):
        mount_volumes = [(tmpdir.realpath(), "/tmp/mypath")]

        c = create_container(
            "alpine",
            command=["sh", "-c", "echo 'foobar' > /tmp/mypath/foo.log"],
            mount_volumes=mount_volumes,
        )
        docker_client.start_container(c.container_id)

        assert tmpdir.join("foo.log").isfile(), "foo.log was not created in mounted dir"
コード例 #16
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
 def test_run_container_error(self, docker_client: ContainerClient):
     container_name = _random_container_name()
     try:
         with pytest.raises(ContainerException):
             docker_client.run_container(
                 "alpine",
                 name=container_name,
                 command=["./doesnotexist"],
             )
     finally:
         docker_client.remove_container(container_name)
コード例 #17
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
    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()
コード例 #18
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
 def test_run_container(self, docker_client: ContainerClient):
     container_name = _random_container_name()
     try:
         output, _ = docker_client.run_container(
             "alpine",
             name=container_name,
             command=["echo", "foobared"],
         )
         output = output.decode(config.DEFAULT_ENCODING)
         assert "foobared" in output
     finally:
         docker_client.remove_container(container_name)
コード例 #19
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    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()
コード例 #20
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    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)
コード例 #21
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    def test_run_container_with_stdin(self, docker_client: ContainerClient):
        container_name = _random_container_name()
        message = "test_message_stdin"
        try:
            output, _ = docker_client.run_container(
                "alpine",
                name=container_name,
                interactive=True,
                stdin=message.encode(config.DEFAULT_ENCODING),
                command=["cat"],
            )

            assert message == output.decode(config.DEFAULT_ENCODING).strip()
        finally:
            docker_client.remove_container(container_name)
コード例 #22
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_get_container_ip_with_network(
     self, docker_client: ContainerClient, create_container, create_network
 ):
     network_name = "ls_test_network_%s" % short_uid()
     create_network(network_name)
     container = create_container(
         "alpine", network=network_name, command=["sh", "-c", "while true; do sleep 1; done"]
     )
     docker_client.start_container(container.container_id)
     ip = docker_client.get_container_ip(container.container_id)
     assert re.match(
         r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
         ip,
     )
     assert "127.0.0.1" != ip
コード例 #23
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
    def test_create_container_remove_removes_container(
        self, docker_client: ContainerClient, create_container
    ):
        info = create_container("alpine", remove=True, command=["echo", "foobar"])
        # make sure it was correctly created
        assert 1 == len(docker_client.list_containers(f"id={info.container_id}"))

        # start the container
        output, _ = docker_client.start_container(info.container_id, attach=True)
        output = output.decode(config.DEFAULT_ENCODING)
        time.sleep(1)  # give the docker daemon some time to remove the container after execution

        assert 0 == len(docker_client.list_containers(f"id={info.container_id}"))

        # it takes a while for it to be removed
        assert "foobar" in output
コード例 #24
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    def test_run_detached_with_logs(self, docker_client: ContainerClient):
        container_name = _random_container_name()
        message = "test_message"
        try:
            output, _ = docker_client.run_container(
                "alpine",
                name=container_name,
                detach=True,
                command=["echo", message],
            )
            container_id = output.decode(config.DEFAULT_ENCODING).strip()
            logs = docker_client.get_container_logs(container_id)

            assert message == logs.strip()
        finally:
            docker_client.remove_container(container_name)
コード例 #25
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_run_with_additional_arguments(self, docker_client: ContainerClient):
     env_variable = "TEST_FLAG=test_str"
     stdout, _ = docker_client.run_container(
         "alpine", remove=True, command=["env"], additional_flags=f"-e {env_variable}"
     )
     assert env_variable in stdout.decode(config.DEFAULT_ENCODING)
     stdout, _ = docker_client.run_container(
         "alpine",
         remove=True,
         command=["env"],
         additional_flags=f"-e {env_variable}",
         env_vars={"EXISTING_VAR": "test_var"},
     )
     stdout = stdout.decode(config.DEFAULT_ENCODING)
     assert env_variable in stdout
     assert "EXISTING_VAR=test_var" in stdout
コード例 #26
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_docker_image_names(self, docker_client: ContainerClient):
     try:
         safe_run([config.DOCKER_CMD, "rmi", "alpine"])
     except CalledProcessError:
         pass
     assert "alpine:latest" not in docker_client.get_docker_image_names()
     assert "alpine" not in docker_client.get_docker_image_names()
     docker_client.pull_image("alpine")
     assert "alpine:latest" in docker_client.get_docker_image_names()
     assert "alpine:latest" not in docker_client.get_docker_image_names(include_tags=False)
     assert "alpine" in docker_client.get_docker_image_names(include_tags=False)
     assert "alpine" in docker_client.get_docker_image_names()
     assert "alpine" not in docker_client.get_docker_image_names(strip_latest=False)
コード例 #27
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_run_container_automatic_pull(self, docker_client: ContainerClient):
     try:
         safe_run([config.DOCKER_CMD, "rmi", "alpine"])
     except CalledProcessError:
         pass
     message = "test message"
     stdout, _ = docker_client.run_container("alpine", command=["echo", message], remove=True)
     assert message == stdout.decode(config.DEFAULT_ENCODING).strip()
コード例 #28
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
    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)
コード例 #29
0
ファイル: test_docker.py プロジェクト: cursedaxes/localstack
 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()
コード例 #30
0
ファイル: test_docker.py プロジェクト: strongjz/localstack
 def test_run_container_non_existent_image(self, docker_client: ContainerClient):
     try:
         safe_run([config.DOCKER_CMD, "rmi", "alpine"])
     except CalledProcessError:
         pass
     with pytest.raises(NoSuchImage):
         stdout, _ = docker_client.run_container(
             "localstack_non_existing_image_for_tests", command=["echo", "test"], remove=True
         )