File: test_DockerAppContext__dockerize_args.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 (233 lines) | stat: -rw-r--r-- 7,025 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import sys

import pytest


@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_dockerize_args(mock_tools, my_app, tmp_path):
    """A command to run in Docker is dockerized."""
    dockerize_args = mock_tools[my_app].app_context._dockerize_args(["hello", "world"])

    assert dockerize_args == dict(
        args=[
            "docker",
            "run",
            "--rm",
            "--volume",
            f"{tmp_path / 'bundle'}:/app:z",
            "--volume",
            f"{tmp_path / 'briefcase'}:/briefcase:z",
            "briefcase/com.example.myapp:py3.X",
            "hello",
            "world",
        ],
        env={"DOCKER_CLI_HINTS": "false"},
    )


@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_dockerize_args_sys_executable(mock_tools, my_app, tmp_path):
    """A command to run in Docker using the current Python is dockerized."""
    dockerize_args = mock_tools[my_app].app_context._dockerize_args(
        [sys.executable, "-m", "pip"]
    )

    assert dockerize_args == dict(
        args=[
            "docker",
            "run",
            "--rm",
            "--volume",
            f"{tmp_path / 'bundle'}:/app:z",
            "--volume",
            f"{tmp_path / 'briefcase'}:/briefcase:z",
            "briefcase/com.example.myapp:py3.X",
            "python3.X",
            "-m",
            "pip",
        ],
        env={"DOCKER_CLI_HINTS": "false"},
    )


@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_dockerize_args_mounts(mock_tools, my_app, tmp_path):
    """A command to run in Docker with mounts is dockerized."""
    dockerize_args = mock_tools[my_app].app_context._dockerize_args(
        ["hello", "world"],
        mounts=[
            ("/path/to/first", "/container/first"),
            ("/path/to/second", "/container/second"),
        ],
    )

    assert dockerize_args == dict(
        args=[
            "docker",
            "run",
            "--rm",
            "--volume",
            "/path/to/first:/container/first:z",
            "--volume",
            "/path/to/second:/container/second:z",
            "--volume",
            f"{tmp_path / 'bundle'}:/app:z",
            "--volume",
            f"{tmp_path / 'briefcase'}:/briefcase:z",
            "briefcase/com.example.myapp:py3.X",
            "hello",
            "world",
        ],
        env={"DOCKER_CLI_HINTS": "false"},
    )


@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_dockerize_args_mounts_path(mock_tools, my_app, tmp_path):
    """A command to run in Docker with mounts and a path inside a mount is
    dockerized."""
    dockerize_args = mock_tools[my_app].app_context._dockerize_args(
        ["hello", "world", "/path/to/second/bin"],
        mounts=[
            ("/path/to/first", "/container/first"),
            ("/path/to/second", "/container/second"),
        ],
    )

    assert dockerize_args == dict(
        args=[
            "docker",
            "run",
            "--rm",
            "--volume",
            "/path/to/first:/container/first:z",
            "--volume",
            "/path/to/second:/container/second:z",
            "--volume",
            f"{tmp_path / 'bundle'}:/app:z",
            "--volume",
            f"{tmp_path / 'briefcase'}:/briefcase:z",
            "briefcase/com.example.myapp:py3.X",
            "hello",
            "world",
            "/container/second/bin",
        ],
        env={"DOCKER_CLI_HINTS": "false"},
    )


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_dockerize_args_cwd(mock_tools, my_app, tmp_path):
    """A command to run in Docker with a CWD is dockerized."""
    dockerize_args = mock_tools[my_app].app_context._dockerize_args(
        ["hello", "world"],
        cwd=tmp_path / "bundle/foobar",
    )

    assert dockerize_args == dict(
        args=[
            "docker",
            "run",
            "--rm",
            "--volume",
            f"{tmp_path / 'bundle'}:/app:z",
            "--volume",
            f"{tmp_path / 'briefcase'}:/briefcase:z",
            "--workdir",
            "/app/foobar",
            "briefcase/com.example.myapp:py3.X",
            "hello",
            "world",
        ],
        env={"DOCKER_CLI_HINTS": "false"},
    )


@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_dockerize_args_arg_and_env(mock_tools, my_app, tmp_path):
    """A command to run in Docker with an extra keyword arg is dockerized."""
    dockerize_args = mock_tools[my_app].app_context._dockerize_args(
        ["hello", "world"],
        env={
            "MAGIC": "True",
            "IMPORTANCE": "super high",
        },
        check=True,
        steam_output=True,
    )

    assert dockerize_args == dict(
        args=[
            "docker",
            "run",
            "--rm",
            "--volume",
            f"{tmp_path / 'bundle'}:/app:z",
            "--volume",
            f"{tmp_path / 'briefcase'}:/briefcase:z",
            "--env",
            "MAGIC=True",
            "--env",
            "IMPORTANCE=super high",
            "briefcase/com.example.myapp:py3.X",
            "hello",
            "world",
        ],
        check=True,
        steam_output=True,
        env={"DOCKER_CLI_HINTS": "false"},
    )


@pytest.mark.skipif(
    sys.platform == "win32", reason="Windows paths aren't converted in Docker context"
)
@pytest.mark.usefixtures("mock_docker")
@pytest.mark.usefixtures("mock_docker_app_context")
def test_dockerize_args_path_arg_and_env(mock_tools, my_app, tmp_path):
    """A command to run in Docker with paths inside the bundle and Briefcase data
    directory are dockerized."""
    dockerize_args = mock_tools[my_app].app_context._dockerize_args(
        ["hello", tmp_path / "location"],
        env={
            "MAGIC": "True",
            "PATH": (
                "/somewhere/safe"
                f":{tmp_path / 'briefcase' / 'tools'}"
                f":{tmp_path / 'bundle' / 'location'}"
            ),
        },
        cwd=tmp_path / "cwd",
    )

    assert dockerize_args == dict(
        args=[
            "docker",
            "run",
            "--rm",
            "--volume",
            f"{tmp_path / 'bundle'}:/app:z",
            "--volume",
            f"{tmp_path / 'briefcase'}:/briefcase:z",
            "--env",
            "MAGIC=True",
            "--env",
            "PATH=/somewhere/safe:/briefcase/tools:/app/location",
            "--workdir",
            f"{tmp_path / 'cwd'}",
            "briefcase/com.example.myapp:py3.X",
            "hello",
            os.fsdecode(tmp_path / "location"),
        ],
        env={"DOCKER_CLI_HINTS": "false"},
    )