-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_builders.py
259 lines (242 loc) · 8.33 KB
/
test_builders.py
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
254
255
256
257
258
259
import os
from unittest import mock
import uuid
import pytest
import tempfile
import yaml
from buildrunner.config.models import DEFAULT_TO_LEGACY_BUILDER
from buildrunner.docker.image_info import BuiltImageInfo, BuiltTaggedImage
from tests import test_runner
test_dir_path = os.path.realpath(os.path.dirname(__file__))
TEST_DIR = os.path.dirname(__file__)
top_dir_path = os.path.realpath(os.path.dirname(test_dir_path))
def _test_buildrunner_file(test_dir, file_name, args, exit_code):
print(f"\n>>>> Testing Buildrunner file: {file_name}")
with tempfile.TemporaryDirectory(prefix="buildrunner.results-") as temp_dir:
command_line = [
"buildrunner-test",
"-d",
top_dir_path,
"-b",
temp_dir,
# Since we are using a fresh temp directory, don't delete it first
"--keep-step-artifacts",
"-f",
os.path.join(test_dir, file_name),
# Do not push in tests
]
if args:
command_line.extend(args)
assert exit_code == test_runner.run_tests(
command_line,
master_config_file=f"{test_dir_path}/config-files/etc-buildrunner.yaml",
global_config_files=[
f"{test_dir_path}/config-files/etc-buildrunner.yaml",
f"{test_dir_path}/config-files/dot-buildrunner.yaml",
],
)
@pytest.fixture(autouse=True)
def fixture_set_env():
# Sets an environment variable that can be used from a buildrunner file
os.environ["IS_BR_TEST"] = "true"
# Also sets an environment variable that is available in regular jinja without using the `env` instance
os.environ["BUILDRUNNER_IS_TEST"] = "true"
yield
# Cleanup
del os.environ["IS_BR_TEST"]
del os.environ["BUILDRUNNER_IS_TEST"]
@pytest.mark.parametrize(
"description, use_legacy_builder, config,",
[
(
"Use buildx builder with platform",
False,
"""
use-legacy-builder: false
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
platform: linux/arm64
""",
),
(
"Use buildx builder",
False,
"""
use-legacy-builder: false
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
""",
),
(
"Overwrite use-legacy-builder with platforms",
False,
"""
use-legacy-builder: true
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
platforms:
- linux/amd64
- linux/arm64
""",
),
(
"Use buildx builder with platforms",
False,
"""
use-legacy-builder: false
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
platforms:
- linux/amd64
- linux/arm64
""",
),
(
"Default builder with platforms",
False,
"""
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
platforms:
- linux/amd64
- linux/arm64
""",
),
(
"Default builder",
DEFAULT_TO_LEGACY_BUILDER,
"""
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
""",
),
(
"Use legacy builder with platform",
True,
"""
use-legacy-builder: true
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
platform: linux/arm64
""",
),
(
"Use legacy builder with use-legacy-builder",
True,
"""
use-legacy-builder: true
steps:
build-container-single-platform:
build:
path: .
dockerfile: |
FROM python:3.10
CMD python3
""",
),
],
)
@mock.patch(
"tests.test_runner.buildrunner.steprunner.tasks.build.legacy_builder.build_image"
)
@mock.patch(
"tests.test_runner.buildrunner.steprunner.MultiplatformImageBuilder.build_multiple_images"
)
def test_builders(
mock_buildx_builder,
mock_legacy_build,
description,
use_legacy_builder,
config,
):
_ = description
with tempfile.TemporaryDirectory() as tmpdirname:
tmp_filename = f"{tmpdirname}/config.yaml"
with open(tmp_filename, "w") as f:
f.write(config)
args = None
exit_code = 0
# legacy builder args
mock_legacy_build.return_value = "52fc1c92b555"
config = yaml.load(config, Loader=yaml.SafeLoader)
# default builder args
if config.get("steps", {}):
for step_name, step in config.get("steps", {}).items():
built_image = BuiltImageInfo(id=str(uuid.uuid4()))
if step.get("build", {}).get("platforms"):
for platform in step.get("build", {}).get("platforms", []):
built_image.add_platform_image(
platform,
BuiltTaggedImage(
repo=f"repo-{platform}",
tag=f"tag-{platform}",
digest=f"digest-{platform}",
platform="linux/arm64",
),
)
else:
if step.get("build", {}).get("platform"):
platform = step.get("build", {}).get("platform")
built_image.add_platform_image(
platform,
BuiltTaggedImage(
repo=f"repo-{platform}",
tag=f"tag-{platform}",
digest=f"digest-{platform}",
platform=platform,
),
)
else:
platform = "linux/arm64"
built_image.add_platform_image(
"linux/arm64",
BuiltTaggedImage(
repo=f"repo-{platform}",
tag=f"tag-{platform}",
digest=f"digest-{platform}",
platform=platform,
),
)
mock_buildx_builder.return_value = built_image
_test_buildrunner_file(tmpdirname, tmp_filename, args, exit_code)
if use_legacy_builder:
mock_buildx_builder.assert_not_called()
mock_legacy_build.assert_called()
else:
mock_buildx_builder.assert_called()
mock_legacy_build.assert_not_called()