Skip to content

Commit d67c8d8

Browse files
committed
Add a test to check if ruby_binary can run in a container
1 parent e231134 commit d67c8d8

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

WORKSPACE

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,35 @@ gazelle_dependencies()
5454
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
5555

5656
protobuf_deps()
57+
58+
# rules_docker
59+
60+
http_archive(
61+
name = "io_bazel_rules_docker",
62+
sha256 = "14ac30773fdb393ddec90e158c9ec7ebb3f8a4fd533ec2abbfd8789ad81a284b",
63+
strip_prefix = "rules_docker-0.12.1",
64+
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.12.1/rules_docker-v0.12.1.tar.gz"],
65+
)
66+
67+
load(
68+
"@io_bazel_rules_docker//repositories:repositories.bzl",
69+
container_repositories = "repositories",
70+
)
71+
72+
container_repositories()
73+
74+
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
75+
76+
container_deps()
77+
78+
load(
79+
"@io_bazel_rules_docker//container:container.bzl",
80+
"container_pull",
81+
)
82+
83+
container_pull(
84+
name = "ruby_base_container",
85+
digest = "sha256:da560e130d6a4b75b099e932a98331ec3b2420b914d51a88edc4fe3c60aee9b1", # alpine linux/amd64
86+
registry = "docker.io",
87+
repository = "library/ruby",
88+
)

ruby/tests/BUILD.bazel

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ load(
33
"ruby_binary",
44
"ruby_test",
55
)
6+
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
7+
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
68

79
# Checks if args are correctly passed to the ruby script.
810
ruby_test(
@@ -69,13 +71,13 @@ genrule(
6971
"echo '#!/bin/sh -e' > $@",
7072
"echo true >> $@",
7173
]),
74+
message = "Running ruby_bin in genrule",
7275
output_to_bindir = 1,
7376
tools = [
7477
"args_check.rb",
7578
"@org_ruby_lang_ruby_host//:ruby_bin",
7679
"@org_ruby_lang_ruby_host//:runtime",
7780
],
78-
message = "Running ruby_bin in genrule",
7981
)
8082

8183
sh_test(
@@ -154,15 +156,15 @@ sh_test(
154156

155157
genrule(
156158
name = "dummy_genfile_load_path_in_runfiles",
157-
message = "Running :load_path_in_runfiles in genrule",
158159
outs = ["load_path_in_runfiles_test_5a.sh"],
159-
tools = [":load_path_in_runfiles"],
160160
cmd = " && ".join([
161161
"$(location :load_path_in_runfiles)",
162162
"echo '#!/bin/sh -e' > $@",
163163
"echo 'true' >> $@",
164164
]),
165165
executable = True,
166+
message = "Running :load_path_in_runfiles in genrule",
167+
tools = [":load_path_in_runfiles"],
166168
)
167169

168170
# TODO(yugui) Add a test for case 6a.
@@ -225,3 +227,40 @@ ruby_test(
225227
}),
226228
main = "ext_test.rb",
227229
)
230+
231+
## Containerization test
232+
233+
# TODO(yugui) Make it easier to build a tar with the right runfiles structure
234+
pkg_tar(
235+
name = "load_path_in_runfiles_container_layer",
236+
srcs = [":load_path_in_runfiles"],
237+
include_runfiles = True,
238+
package_dir = "/app",
239+
remap_paths = {
240+
"ruby": "load_path_in_runfiles.runfiles/bazelruby_ruby_rules/ruby",
241+
".": "load_path_in_runfiles.runfiles/",
242+
},
243+
strip_prefix = "dummy",
244+
symlinks = {
245+
"/app/load_path_in_runfiles.runfiles/bazelruby_ruby_rules/external": "/app/load_path_in_runfiles.runfiles",
246+
"/app/load_path_in_runfiles": "/app/load_path_in_runfiles.runfiles/bazelruby_ruby_rules/ruby/tests/load_path_in_runfiles",
247+
},
248+
)
249+
250+
container_image(
251+
name = "load_path_in_runfiles_container_image",
252+
base = "@ruby_base_container//image",
253+
entrypoint = ["/app/load_path_in_runfiles"],
254+
tars = [":load_path_in_runfiles_container_layer"],
255+
)
256+
257+
sh_test(
258+
name = "load_path_in_runfiles_container_test",
259+
srcs = ["container_test.sh"],
260+
args = [
261+
"$(location :load_path_in_runfiles_container_image)",
262+
"bazel/ruby/tests:load_path_in_runfiles_container_image",
263+
],
264+
data = [":load_path_in_runfiles_container_image"],
265+
tags = ["docker"],
266+
)

ruby/tests/container_test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh -eu
2+
3+
if [ $# -lt 2 ]; then
4+
echo "Usage: $0 CONTAINER_IMAGE_LOADER CONTAINER_IMAGE_NAME" >&2
5+
exit 1
6+
fi
7+
8+
CONTAINER_IMAGE_LOADER=$1
9+
CONTAINER_IMAGE_NAME=$2
10+
11+
$CONTAINER_IMAGE_LOADER
12+
docker run $CONTAINER_IMAGE_NAME

0 commit comments

Comments
 (0)