Best Python code snippet using localstack_python
test_docker.py
Source:test_docker.py  
...467        with pytest.raises(NoSuchImage):468            docker_client.get_image_entrypoint("thisdoesnotexist")469    def test_get_container_entrypoint_not_pulled_image(self, docker_client: ContainerClient):470        try:471            docker_client.get_image_cmd("alpine", pull=False)472            safe_run([config.DOCKER_CMD, "rmi", "alpine"])473        except ContainerException:474            pass475        entrypoint = docker_client.get_image_entrypoint("alpine")476        assert "" == entrypoint477    def test_get_container_command(self, docker_client: ContainerClient):478        command = docker_client.get_image_cmd("alpine")479        assert ["/bin/sh"] == command480    def test_get_container_command_not_pulled_image(self, docker_client: ContainerClient):481        try:482            docker_client.get_image_cmd("alpine", pull=False)483            safe_run([config.DOCKER_CMD, "rmi", "alpine"])484        except ContainerException:485            pass486        command = docker_client.get_image_cmd("alpine")487        assert ["/bin/sh"] == command488    def test_get_container_command_non_existing_image(self, docker_client: ContainerClient):489        with pytest.raises(NoSuchImage):490            docker_client.get_image_cmd("thisdoesnotexist")491    def test_create_start_container_with_stdin_to_stdout(self, docker_client: ContainerClient):492        container_name = _random_container_name()493        message = "test_message_stdin"494        try:495            docker_client.create_container(496                "alpine",497                name=container_name,498                interactive=True,499                command=["cat"],500            )501            output, _ = docker_client.start_container(502                container_name, interactive=True, stdin=message.encode(config.DEFAULT_ENCODING)503            )504            assert message == output.decode(config.DEFAULT_ENCODING).strip()505        finally:506            docker_client.remove_container(container_name)507            pass508    def test_create_start_container_with_stdin_to_file(509        self, tmpdir, docker_client: ContainerClient510    ):511        container_name = _random_container_name()512        message = "test_message_stdin"513        try:514            docker_client.create_container(515                "alpine",516                name=container_name,517                interactive=True,518                command=["sh", "-c", "cat > test_file"],519            )520            output, _ = docker_client.start_container(521                container_name, interactive=True, stdin=message.encode(config.DEFAULT_ENCODING)522            )523            target_path = tmpdir.join("test_file")524            docker_client.copy_from_container(container_name, str(target_path), "test_file")525            assert message == target_path.read().strip()526        finally:527            docker_client.remove_container(container_name)528    def test_run_container_with_stdin(self, docker_client: ContainerClient):529        container_name = _random_container_name()530        message = "test_message_stdin"531        try:532            output, _ = docker_client.run_container(533                "alpine",534                name=container_name,535                interactive=True,536                stdin=message.encode(config.DEFAULT_ENCODING),537                command=["cat"],538            )539            assert message == output.decode(config.DEFAULT_ENCODING).strip()540        finally:541            docker_client.remove_container(container_name)542    def test_exec_in_container_with_stdin(self, docker_client: ContainerClient, dummy_container):543        docker_client.start_container(dummy_container.container_id)544        message = "test_message_stdin"545        output, _ = docker_client.exec_in_container(546            dummy_container.container_id,547            interactive=True,548            stdin=message.encode(config.DEFAULT_ENCODING),549            command=["cat"],550        )551        assert message == output.decode(config.DEFAULT_ENCODING).strip()552    def test_exec_in_container_with_stdin_stdout_stderr(553        self, docker_client: ContainerClient, dummy_container554    ):555        docker_client.start_container(dummy_container.container_id)556        message = "test_message_stdin"557        output, stderr = docker_client.exec_in_container(558            dummy_container.container_id,559            interactive=True,560            stdin=message.encode(config.DEFAULT_ENCODING),561            command=["sh", "-c", "cat; >&2 echo stderrtest"],562        )563        assert message == output.decode(config.DEFAULT_ENCODING).strip()564        assert "stderrtest" == stderr.decode(config.DEFAULT_ENCODING).strip()565    def test_run_detached_with_logs(self, docker_client: ContainerClient):566        container_name = _random_container_name()567        message = "test_message"568        try:569            output, _ = docker_client.run_container(570                "alpine",571                name=container_name,572                detach=True,573                command=["echo", message],574            )575            container_id = output.decode(config.DEFAULT_ENCODING).strip()576            logs = docker_client.get_container_logs(container_id)577            assert message == logs.strip()578        finally:579            docker_client.remove_container(container_name)580    def test_get_logs_non_existent_container(self, docker_client: ContainerClient):581        with pytest.raises(NoSuchContainer):582            docker_client.get_container_logs("container_hopefully_does_not_exist", safe=False)583        assert "" == docker_client.get_container_logs(584            "container_hopefully_does_not_exist", safe=True585        )586    @pytest.mark.skip_offline587    def test_pull_docker_image(self, docker_client: ContainerClient):588        try:589            docker_client.get_image_cmd("alpine", pull=False)590            safe_run([config.DOCKER_CMD, "rmi", "alpine"])591        except ContainerException:592            pass593        with pytest.raises(NoSuchImage):594            docker_client.get_image_cmd("alpine", pull=False)595        docker_client.pull_image("alpine")596        assert ["/bin/sh"] == docker_client.get_image_cmd("alpine", pull=False)597    @pytest.mark.skip_offline598    def test_pull_non_existent_docker_image(self, docker_client: ContainerClient):599        with pytest.raises(NoSuchImage):600            docker_client.pull_image("localstack_non_existing_image_for_tests")601    @pytest.mark.skip_offline602    def test_pull_docker_image_with_tag(self, docker_client: ContainerClient):603        try:604            docker_client.get_image_cmd("alpine", pull=False)605            safe_run([config.DOCKER_CMD, "rmi", "alpine"])606        except ContainerException:607            pass608        with pytest.raises(NoSuchImage):609            docker_client.get_image_cmd("alpine", pull=False)610        docker_client.pull_image("alpine:3.13")611        assert ["/bin/sh"] == docker_client.get_image_cmd("alpine:3.13", pull=False)612        assert "alpine:3.13" in docker_client.inspect_image("alpine:3.13", pull=False)["RepoTags"]613    @pytest.mark.skip_offline614    def test_pull_docker_image_with_hash(self, docker_client: ContainerClient):615        try:616            docker_client.get_image_cmd("alpine", pull=False)617            safe_run([config.DOCKER_CMD, "rmi", "alpine"])618        except ContainerException:619            pass620        with pytest.raises(NoSuchImage):621            docker_client.get_image_cmd("alpine", pull=False)622        docker_client.pull_image(623            "alpine@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a"624        )625        assert ["/bin/sh"] == docker_client.get_image_cmd(626            "alpine@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a",627            pull=False,628        )629        assert (630            "alpine@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a"631            in docker_client.inspect_image(632                "alpine@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a",633                pull=False,634            )["RepoDigests"]635        )636    @pytest.mark.skip_offline637    def test_run_container_automatic_pull(self, docker_client: ContainerClient):638        try:639            safe_run([config.DOCKER_CMD, "rmi", "alpine"])...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
