File: test_Docker__check_output.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (71 lines) | stat: -rw-r--r-- 2,518 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import subprocess
from unittest.mock import call

import pytest


@pytest.mark.usefixtures("mock_docker")
def test_check_output(mock_tools, sub_check_output_kw):
    """A command can be invoked on a bare Docker image."""
    # mock image already being cached in Docker
    mock_tools.subprocess._subprocess.check_output.side_effect = [
        "1ed313b0551f",
        "output",
    ]

    # Run the command in a container
    mock_tools.docker.check_output(["cmd", "arg1", "arg2"], image_tag="ubuntu:jammy")

    mock_tools.subprocess._subprocess.check_output.assert_has_calls(
        [
            # Verify image is cached in Docker
            call(
                ["docker", "images", "-q", "ubuntu:jammy"],
                env={"PROCESS_ENV_VAR": "VALUE", "DOCKER_CLI_HINTS": "false"},
                **sub_check_output_kw,
            ),
            # Run command in Docker using image
            call(
                ["docker", "run", "--rm", "ubuntu:jammy", "cmd", "arg1", "arg2"],
                env={"PROCESS_ENV_VAR": "VALUE", "DOCKER_CLI_HINTS": "false"},
                **sub_check_output_kw,
            ),
        ]
    )


@pytest.mark.usefixtures("mock_docker")
def test_check_output_fail(mock_tools, sub_check_output_kw):
    """Any subprocess errors are passed back through directly."""
    # mock image already being cached in Docker and check_output() call fails
    mock_tools.subprocess._subprocess.check_output.side_effect = [
        "1ed313b0551f",
        subprocess.CalledProcessError(
            returncode=1,
            cmd=["cmd", "arg1", "arg2"],
            output="This didn't work\n",
        ),
    ]

    # The CalledProcessError surfaces from Docker().check_output()
    with pytest.raises(subprocess.CalledProcessError):
        mock_tools.docker.check_output(
            ["cmd", "arg1", "arg2"], image_tag="ubuntu:jammy"
        )

    mock_tools.subprocess._subprocess.check_output.assert_has_calls(
        [
            # Verify image is cached in Docker
            call(
                ["docker", "images", "-q", "ubuntu:jammy"],
                env={"PROCESS_ENV_VAR": "VALUE", "DOCKER_CLI_HINTS": "false"},
                **sub_check_output_kw,
            ),
            # Command errors in Docker using image
            call(
                ["docker", "run", "--rm", "ubuntu:jammy", "cmd", "arg1", "arg2"],
                env={"PROCESS_ENV_VAR": "VALUE", "DOCKER_CLI_HINTS": "false"},
                **sub_check_output_kw,
            ),
        ]
    )