1
1
"assertion rules to test metadata"
2
2
3
+ load ("@aspect_bazel_lib//lib:diff_test.bzl" , "diff_test" )
3
4
load ("@bazel_skylib//rules:native_binary.bzl" , "native_test" )
4
5
load ("@bazel_skylib//rules:write_file.bzl" , "write_file" )
5
6
7
+ # THIS LOAD STATEMENT DEPENDS ON setup_assertion_repos.bzl
8
+ load ("@docker_configure//:defs.bzl" , "TARGET_COMPATIBLE_WITH" )
9
+ load ("//oci:defs.bzl" , "oci_tarball" )
10
+
6
11
DIGEST_CMD = """
7
12
image_path="$(location {image})"
8
13
manifest_digest=$$($(JQ_BIN) -r '.manifests[0].digest | sub(":"; "/")' $$image_path/index.json)
@@ -18,7 +23,7 @@ def assert_oci_config(
18
23
entrypoint_eq = None ,
19
24
cmd_eq = None ,
20
25
env_eq = None ,
21
- ports_eq = None ,
26
+ exposed_ports_eq = None ,
22
27
user_eq = None ,
23
28
workdir_eq = None ,
24
29
architecture_eq = None ,
@@ -38,9 +43,9 @@ def assert_oci_config(
38
43
if env_eq :
39
44
config ["Env" ] = ["=" .join (e ) for e in env_eq .items ()]
40
45
if workdir_eq :
41
- config ["Workdir " ] = workdir_eq
42
- if ports_eq :
43
- config ["Ports " ] = ports_eq
46
+ config ["WorkingDir " ] = workdir_eq
47
+ if exposed_ports_eq :
48
+ config ["ExposedPorts " ] = { port : {} for port in exposed_ports_eq }
44
49
if user_eq :
45
50
config ["User" ] = user_eq
46
51
if labels_eq :
@@ -98,3 +103,79 @@ def assert_oci_config(
98
103
}),
99
104
out = name ,
100
105
)
106
+
107
+ def assert_oci_image_command (
108
+ name ,
109
+ image ,
110
+ args = [],
111
+ tags = [],
112
+ exit_code_eq = None ,
113
+ output_eq = None ):
114
+ "assert a that a container works with the given command."
115
+
116
+ tag = "oci.local/assert/" + native .package_name ().replace ("/" , "_" ) + ":latest"
117
+ oci_tarball (
118
+ name = name + "_tarball" ,
119
+ image = image ,
120
+ repo_tags = [tag ],
121
+ tags = tags + ["manual" ],
122
+ )
123
+
124
+ native .filegroup (
125
+ name = name + "_tarball_archive" ,
126
+ srcs = [name + "_tarball" ],
127
+ output_group = "tarball" ,
128
+ tags = tags + ["manual" ],
129
+ )
130
+
131
+ docker_args = " " .join (['"' + arg + '"' for arg in ([tag ] + args )])
132
+
133
+ native .genrule (
134
+ name = name + "_gen" ,
135
+ srcs = [
136
+ name + "_tarball_archive" ,
137
+ ],
138
+ output_to_bindir = True ,
139
+ cmd = """
140
+ docker=$(location //examples:docker_cli)
141
+ $$docker load -i $(location :{name}_tarball_archive)
142
+ container_id=$$($$docker run -d {docker_args})
143
+ $$docker wait $$container_id > $(location :{name}_exit_code)
144
+ $$docker logs $$container_id > $(location :{name}_output)
145
+
146
+ """ .format (name = name , docker_args = docker_args ),
147
+ outs = [
148
+ name + "_output" ,
149
+ name + "_exit_code" ,
150
+ ],
151
+ target_compatible_with = TARGET_COMPATIBLE_WITH ,
152
+ tools = ["//examples:docker_cli" ],
153
+ )
154
+
155
+ if output_eq :
156
+ write_file (
157
+ name = name + "_output_eq_expected" ,
158
+ out = name + "_output_eq_expected.txt" ,
159
+ content = [output_eq ],
160
+ tags = tags + ["manual" ],
161
+ )
162
+ diff_test (
163
+ name = name + "_assert_output_eq" ,
164
+ file1 = name + "_output" ,
165
+ file2 = name + "_output_eq_expected" ,
166
+ tags = tags ,
167
+ )
168
+
169
+ if exit_code_eq != None :
170
+ write_file (
171
+ name = name + "_exit_code_expected" ,
172
+ out = name + "_exit_code_expected.txt" ,
173
+ content = [str (exit_code_eq ) + "\n " ],
174
+ tags = tags + ["manual" ],
175
+ )
176
+ diff_test (
177
+ name = name + "_assert_exit_code_eq" ,
178
+ file1 = name + "_exit_code" ,
179
+ file2 = name + "_exit_code_expected" ,
180
+ tags = tags ,
181
+ )
0 commit comments