예제 #1
0
    def test_exec_in_container(self, docker_client: DockerClient, 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()
예제 #2
0
    def test_exec_error_in_container(self, docker_client: DockerClient, 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")
예제 #3
0
    def test_exec_in_container_with_env(self, docker_client: DockerClient, 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
예제 #4
0
    def test_exec_in_container_with_stdin(self, docker_client: DockerClient, 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()
예제 #5
0
    def test_create_with_volume(self, tmpdir, docker_client: DockerClient, 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"
예제 #6
0
    def test_container_lifecycle_commands(self, docker_client: DockerClient):
        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
        )
예제 #7
0
    def test_copy_into_container(self, tmpdir, docker_client: DockerClient, create_container):
        local_path = tmpdir.join("myfile.txt")
        container_path = "/tmp/myfile.txt"

        c = create_container("alpine", command=["cat", container_path])

        with local_path.open(mode="w") as fd:
            fd.write("foobared\n")

        docker_client.copy_into_container(c.container_name, str(local_path), container_path)

        output, _ = docker_client.start_container(c.container_id, attach=True)
        output = output.decode(config.DEFAULT_ENCODING)

        assert "foobared" in output
예제 #8
0
    def test_create_container_remove_removes_container(
        self, docker_client: DockerClient, 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
        # TODO: how should interactive behave if there is no tty?
        output, _ = docker_client.start_container(info.container_id, interactive=True)
        output = output.decode(config.DEFAULT_ENCODING)

        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
예제 #9
0
    def test_create_container_with_max_env_vars(
        self, docker_client: DockerClient, create_container
    ):
        # default ARG_MAX=131072 in Docker
        env = [(f"IVAR_{i:05d}", f"VAL_{i:05d}") for i in range(2000)]

        # make sure we're really triggering the relevant code
        assert len(str(dict(env))) >= Util.MAX_ENV_ARGS_LENGTH

        info = create_container("alpine", env_vars=env, command=["env"])
        output, _ = docker_client.start_container(info.container_id, attach=True)
        output = output.decode(config.DEFAULT_ENCODING)

        assert "IVAR_00001=VAL_00001" in output
        assert "IVAR_01000=VAL_01000" in output
        assert "IVAR_01999=VAL_01999" in output
예제 #10
0
    def test_create_start_container_with_stdin(self, docker_client: DockerClient):
        container_name = _random_container_name()
        message = "test_message_stdin"
        try:
            docker_client.create_container(
                "alpine",
                name=container_name,
                interactive=True,
                command=["cat"],
            )

            output, _ = docker_client.start_container(
                container_name, interactive=True, stdin=message.encode(config.DEFAULT_ENCODING)
            )

            assert message == output.decode(config.DEFAULT_ENCODING).strip()
        finally:
            docker_client.remove_container(container_name)
예제 #11
0
 def test_start_non_existing_container(self, docker_client: DockerClient):
     with pytest.raises(NoSuchContainer):
         docker_client.start_container("this_container_does_not_exist")