forked from bazel-contrib/rules_oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBUILD.bazel
362 lines (325 loc) · 7.89 KB
/
BUILD.bazel
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
load("@aspect_bazel_lib//lib:tar.bzl", "tar")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_json_matches")
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("//examples:assert.bzl", "assert_oci_config")
# Case 1: image name containing a capital case.
oci_image(
name = "imagE",
architecture = "amd64",
cmd = ["noop"],
os = "linux",
)
# Case 2: existing layer added again.
tar(
name = "empty_tar",
srcs = [],
)
oci_image(
name = "same_tar_multiple_times",
architecture = "amd64",
cmd = ["noop"],
os = "linux",
tars = [
":empty_tar",
],
)
oci_image(
name = "case2",
base = ":same_tar_multiple_times",
tars = [
":empty_tar",
],
)
# Case 3: Adding an identical tar from another directory multiple times
oci_image(
name = "case3",
architecture = "arm64",
os = "linux",
tars = [
# layer_0 and layer_1 is identical
"//examples/just_tar:layer_0",
"//examples/just_tar:layer_1",
],
)
# Case 4: Transition an oci_image and feed to oci_tarball
oci_image(
name = "case4",
architecture = "arm64",
os = "linux",
)
platform_transition_filegroup(
name = "case4_transition",
srcs = [":case4"],
target_platform = "//examples:linux_arm64",
)
oci_tarball(
name = "case4_tarball",
image = ":case4_transition",
repo_tags = ["case4:example"],
)
filegroup(
name = "case4_tarball_tar",
srcs = [":case4_tarball"],
output_group = "tarball",
)
# Case 5: An oci_image directly fed into oci_tarball
oci_image(
name = "case5",
architecture = "arm64",
os = "linux",
)
oci_tarball(
name = "case5_tarball",
image = ":case5",
repo_tags = ["case5:example"],
)
filegroup(
name = "case5_tarball_tar",
srcs = [":case5_tarball"],
output_group = "tarball",
)
# Case 6: test all cases that might break
oci_image(
name = "case6",
architecture = "arm64",
env = {
"TEST": "VALUE=",
"TEST2": "=VALUE=",
"TEST3": "=VALUE",
"TEST4": "=V=VALUE",
# an env that includes previously set $TEST env with a leading `=`
"LEAD_WITH_REF": "=$TEST",
"JUST_EQUALS": "======$$$$",
"1": "VAL",
# referencing non-existent env vars is just empty string.
"REFS": "$1:${1}:${NONEXISTENT}",
},
os = "linux",
)
assert_oci_config(
name = "test_case6",
env_eq = {
"TEST": "VALUE=",
"TEST2": "=VALUE=",
"TEST3": "=VALUE",
"TEST4": "=V=VALUE",
"LEAD_WITH_REF": "=VALUE=",
"JUST_EQUALS": "======$$$$",
"1": "VAL",
"REFS": "VAL:VAL:",
},
image = ":case6",
)
# Case 7: oci_image inheriting env from the base and appending to it.
oci_image(
name = "case7_base",
architecture = "arm64",
env = {
"PATH": "/usr/local/bin",
},
os = "linux",
)
oci_image(
name = "case7",
base = ":case7_base",
env = {
"PATH": "${PATH}:/usr/bin",
"PATH1": "$PATH:/test",
"PATH2": "/prepend:${PATH}:/test2",
"PATH3": "key1=value1 key2=value2",
},
)
assert_oci_config(
name = "test_case7",
env_eq = {
"PATH": "/usr/local/bin:/usr/bin",
"PATH1": "/usr/local/bin:/usr/bin:/test",
"PATH2": "/prepend:/usr/local/bin:/usr/bin:/test2",
"PATH3": "key1=value1 key2=value2",
},
image = ":case7",
)
# Case 8: Test all attributes of oci_image
# buildifier: leave-alone
oci_image(
name = "case8_base",
# platform attributes
os = "linux",
architecture = "amd64",
variant = "v8",
# entrypoint and args
entrypoint = ["/custom_bin"],
cmd = [
"--arg1",
"--arg2",
],
# env
env = {
"ENV": "/test", # ENV=/test
"ENV2": "/test:$ENV", # ENV=/test:/test
"PATH": "/usr/local/bin:$PATH", # PATH=/usr/local/bin:
},
# exposed_ports
exposed_ports = [
"1234/tcp",
"5678/udp",
"5000",
],
volumes = ["/srv/data"],
# user & workdir
user = "root",
workdir = "/root",
# labels & annotations
labels = {
"org.opencontainers.image.version": "0.0.0",
"org.opencontainers.image.source": "https://github.com/bazel-contrib/rules_oci=",
},
annotations = {
"org.opencontainers.image.version": "0.0.0",
"org.opencontainers.image.source": "https://github.com/bazel-contrib/rules_oci=",
},
)
oci_image(
name = "case8",
base = ":case8_base",
# adding a new env should preserve the previously set envs!
env = {
"LOCAL": "true", # LOCAL=/test
},
)
assert_oci_config(
name = "test_case8",
cmd_eq = [
"--arg1",
"--arg2",
],
entrypoint_eq = [
"/custom_bin",
],
env_eq = {
"ENV": "/test",
"ENV2": "/test:/test",
"PATH": "/usr/local/bin:",
"LOCAL": "true",
},
exposed_ports_eq = [
"1234/tcp",
"5678/udp",
"5000",
],
volumes_eq = [
"/srv/data",
],
image = ":case8",
labels_eq = {
"org.opencontainers.image.version": "0.0.0",
"org.opencontainers.image.source": "https://github.com/bazel-contrib/rules_oci=",
},
user_eq = "root",
workdir_eq = "/root",
)
# Case 9: Test oci_tarball repotags
repo_tags = [
"gcr.io/empty_base:latest",
"two:is_a_company",
"three:is_a_crowd", # Used to test support for more than two repo_tags.
]
oci_image(
name = "case9_image",
architecture = "amd64",
os = "linux",
)
# Intended to be `bazel run` to load the tarball into a container runtime.
# Produces only an mtree specification as the default output.
oci_tarball(
name = "case9_tarball",
image = ":case9_image",
repo_tags = repo_tags,
)
# Not typically recommended: ask the tarball rule to write the .tar file
# that would have been created when `bazel run`.
filegroup(
name = "case9_tarball.tar",
srcs = [":case9_tarball"],
output_group = "tarball",
)
genrule(
name = "case9_manifest",
srcs = [":case9_tarball.tar"],
outs = ["case9_manifest.json"],
cmd = "$(BSDTAR_BIN) -xOf ./$(location :case9_tarball.tar) manifest.json > $@",
toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"],
)
write_file(
name = "case9_expected_repo_tags",
out = "case9_expected_repo_tags.json",
content = [str(repo_tags)],
)
assert_json_matches(
name = "test_case9",
file1 = ":case9_manifest",
file2 = ":case9_expected_repo_tags",
filter1 = ".[0].RepoTags",
)
# Case 10: an oci_tarball run as part of a genrule
oci_image(
name = "case10",
architecture = "arm64",
os = "linux",
)
oci_tarball(
name = "case10_tarball",
image = ":case10",
repo_tags = ["case10:example"],
)
genrule(
name = "case10_run",
outs = ["out.txt"],
cmd = """
$(location :case10_tarball) && echo "worked" > $@
""",
tools = [":case10_tarball"],
)
# Case 11: an oci_tarball run as part of sh_test
oci_image(
name = "case11",
architecture = "arm64",
os = "linux",
)
oci_tarball(
name = "case11_tarball",
image = ":case11",
repo_tags = ["case11:example"],
)
write_file(
name = "case11_test_sh",
out = "case11_test.sh",
content = [
"output=$($1)",
'if [[ "$output" != "Loaded image: case11:example" ]]; then',
' echo "failed to load: $output"',
" exit 1",
"fi",
],
)
sh_test(
name = "case11_test",
srcs = ["case11_test.sh"],
args = ["$(location :case11_tarball)"],
data = [":case11_tarball"],
)
# build them as test.
build_test(
name = "test",
targets = [
":imagE",
":case2",
":case3",
":case4_tarball_tar",
":case5_tarball_tar",
":case10_run",
],
)