File size: 7,872 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import gzip
import io
import shutil

import pytest

import docker
from docker import auth, errors
from docker.api.build import process_dockerfile

from ..helpers import make_tree
from .api_test import BaseAPIClientTest, fake_request, url_prefix


class BuildTest(BaseAPIClientTest):
    def test_build_container(self):
        script = io.BytesIO(
            "\n".join(
                [
                    "FROM busybox",
                    "RUN mkdir -p /tmp/test",
                    "EXPOSE 8080",
                    "ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz"
                    " /tmp/silence.tar.gz",
                ]
            ).encode("ascii")
        )

        self.client.build(fileobj=script)

    def test_build_container_pull(self):
        script = io.BytesIO(
            "\n".join(
                [
                    "FROM busybox",
                    "RUN mkdir -p /tmp/test",
                    "EXPOSE 8080",
                    "ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz"
                    " /tmp/silence.tar.gz",
                ]
            ).encode("ascii")
        )

        self.client.build(fileobj=script, pull=True)

    def test_build_container_custom_context(self):
        script = io.BytesIO(
            "\n".join(
                [
                    "FROM busybox",
                    "RUN mkdir -p /tmp/test",
                    "EXPOSE 8080",
                    "ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz"
                    " /tmp/silence.tar.gz",
                ]
            ).encode("ascii")
        )
        context = docker.utils.mkbuildcontext(script)

        self.client.build(fileobj=context, custom_context=True)

    def test_build_container_custom_context_gzip(self):
        script = io.BytesIO(
            "\n".join(
                [
                    "FROM busybox",
                    "RUN mkdir -p /tmp/test",
                    "EXPOSE 8080",
                    "ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz"
                    " /tmp/silence.tar.gz",
                ]
            ).encode("ascii")
        )
        context = docker.utils.mkbuildcontext(script)
        gz_context = gzip.GzipFile(fileobj=context)

        self.client.build(fileobj=gz_context, custom_context=True, encoding="gzip")

    def test_build_remote_with_registry_auth(self):
        self.client._auth_configs = auth.AuthConfig(
            {
                "auths": {
                    "https://example.com": {
                        "user": "example",
                        "password": "example",
                        "email": "example@example.com",
                    }
                }
            }
        )

        expected_params = {
            "t": None,
            "q": False,
            "dockerfile": None,
            "rm": False,
            "nocache": False,
            "pull": False,
            "forcerm": False,
            "remote": "https://github.com/docker-library/mongo",
        }
        expected_headers = {
            "X-Registry-Config": auth.encode_header(self.client._auth_configs.auths)
        }

        self.client.build(path="https://github.com/docker-library/mongo")

        fake_request.assert_called_with(
            "POST",
            f"{url_prefix}build",
            stream=True,
            data=None,
            headers=expected_headers,
            params=expected_params,
            timeout=None,
        )

    def test_build_container_with_named_dockerfile(self):
        self.client.build(".", dockerfile="nameddockerfile")

    def test_build_with_invalid_tag(self):
        with pytest.raises(errors.DockerException):
            self.client.build(".", tag="https://example.com")

    def test_build_container_with_container_limits(self):
        self.client.build(
            ".",
            container_limits={
                "memory": 1024 * 1024,
                "cpusetcpus": 1,
                "cpushares": 1000,
                "memswap": 1024 * 1024 * 8,
            },
        )

    def test_build_container_invalid_container_limits(self):
        with pytest.raises(docker.errors.DockerException):
            self.client.build(".", container_limits={"foo": "bar"})

    def test_set_auth_headers_with_empty_dict_and_auth_configs(self):
        self.client._auth_configs = auth.AuthConfig(
            {
                "auths": {
                    "https://example.com": {
                        "user": "example",
                        "password": "example",
                        "email": "example@example.com",
                    }
                }
            }
        )

        headers = {}
        expected_headers = {
            "X-Registry-Config": auth.encode_header(self.client._auth_configs.auths)
        }

        self.client._set_auth_headers(headers)
        assert headers == expected_headers

    def test_set_auth_headers_with_dict_and_auth_configs(self):
        self.client._auth_configs = auth.AuthConfig(
            {
                "auths": {
                    "https://example.com": {
                        "user": "example",
                        "password": "example",
                        "email": "example@example.com",
                    }
                }
            }
        )

        headers = {"foo": "bar"}
        expected_headers = {
            "X-Registry-Config": auth.encode_header(self.client._auth_configs.auths),
            "foo": "bar",
        }

        self.client._set_auth_headers(headers)
        assert headers == expected_headers

    def test_set_auth_headers_with_dict_and_no_auth_configs(self):
        headers = {"foo": "bar"}
        expected_headers = {"foo": "bar"}

        self.client._set_auth_headers(headers)
        assert headers == expected_headers

    @pytest.mark.skipif(
        not docker.constants.IS_WINDOWS_PLATFORM, reason="Windows-specific syntax"
    )
    def test_process_dockerfile_win_longpath_prefix(self):
        dirs = [
            "foo",
            "foo/bar",
            "baz",
        ]

        files = [
            "Dockerfile",
            "foo/Dockerfile.foo",
            "foo/bar/Dockerfile.bar",
            "baz/Dockerfile.baz",
        ]

        base = make_tree(dirs, files)
        self.addCleanup(shutil.rmtree, base)

        def pre(path):
            return docker.constants.WINDOWS_LONGPATH_PREFIX + path

        assert process_dockerfile(None, pre(base)) == (None, None)
        assert process_dockerfile("Dockerfile", pre(base)) == ("Dockerfile", None)
        assert process_dockerfile("foo/Dockerfile.foo", pre(base)) == (
            "foo/Dockerfile.foo",
            None,
        )
        assert process_dockerfile("../Dockerfile", pre(f"{base}\\foo"))[1] is not None
        assert process_dockerfile("../baz/Dockerfile.baz", pre(f"{base}/baz")) == (
            "../baz/Dockerfile.baz",
            None,
        )

    def test_process_dockerfile(self):
        dirs = [
            "foo",
            "foo/bar",
            "baz",
        ]

        files = [
            "Dockerfile",
            "foo/Dockerfile.foo",
            "foo/bar/Dockerfile.bar",
            "baz/Dockerfile.baz",
        ]

        base = make_tree(dirs, files)
        self.addCleanup(shutil.rmtree, base)

        assert process_dockerfile(None, base) == (None, None)
        assert process_dockerfile("Dockerfile", base) == ("Dockerfile", None)
        assert process_dockerfile("foo/Dockerfile.foo", base) == (
            "foo/Dockerfile.foo",
            None,
        )
        assert process_dockerfile("../Dockerfile", f"{base}/foo")[1] is not None
        assert process_dockerfile("../baz/Dockerfile.baz", f"{base}/baz") == (
            "../baz/Dockerfile.baz",
            None,
        )