From 8f6a2aa6e906f518603848a59917d46c972ce99e Mon Sep 17 00:00:00 2001 From: Jake McCrary Date: Wed, 10 Nov 2021 09:01:05 -0600 Subject: [PATCH] Make jar_app_layer._classpath_as_file public (#1947) * Rename jar_app_layer._classpath_as_file to classpath_as_file This is being done to make this attribute public so that callers can specify it. * Add classpath_as_file as argument to java_image * Add classpath_as_file as argument to groovy_image * Add classpath_as_file as argument to kt_jvm_image * Add classpath_as_file as argument to scala_image * Adds test for scala_image with classpath_as_file=True * Adds test for java_image with classpath_as_file = True * Adds test for groovy_image with classpath_as_file = True * Adds test for kt_jvm_image with classpath_as_file = True * Ignore new image tests under macos * Trigger build * Trigger build --- .bazelci/presubmit.yml | 4 ++++ groovy/image.bzl | 2 ++ java/image.bzl | 6 +++-- kotlin/image.bzl | 2 ++ scala/image.bzl | 2 ++ tests/container/groovy/BUILD | 21 ++++++++++++++++ .../groovy_classpath_as_file_image.yaml | 13 ++++++++++ tests/container/java/BUILD | 23 ++++++++++++++++++ .../configs/java_classpath_as_file_image.yaml | 15 ++++++++++++ tests/container/kotlin/BUILD | 24 +++++++++++++++++++ .../kt_jvm_classpath_as_file_image.yaml | 13 ++++++++++ tests/container/scala/BUILD | 21 ++++++++++++++++ .../scala_classpath_as_file_image.yaml | 13 ++++++++++ 13 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 tests/container/groovy/configs/groovy_classpath_as_file_image.yaml create mode 100644 tests/container/java/configs/java_classpath_as_file_image.yaml create mode 100644 tests/container/kotlin/configs/kt_jvm_classpath_as_file_image.yaml create mode 100644 tests/container/scala/configs/scala_classpath_as_file_image.yaml diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 09617f889..42ffbfac7 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -69,12 +69,15 @@ tasks: - -//tests/container/go:go_image_test - -//tests/container/go:go_static_image_test - -//tests/container/groovy:groovy_image_test + - -//tests/container/groovy:groovy_classpath_as_file_image_test - -//tests/container/java:java_image_test + - -//tests/container/java:java_classpath_as_file_image_test - -//tests/container/java:java_partial_entrypoint_image_test - -//tests/container/java:java_runfiles_as_lib_image_test - -//tests/container/java:java_runfiles_image_test - -//tests/container/java:simple_java_entrypoint_image_test - -//tests/container/kotlin:kotlin_image_test + - -//tests/container/kotlin:kotlin_classpath_as_file_image_test - -//tests/container/nodejs:nodejs_image_custom_binary_test - -//tests/container/nodejs:nodejs_image_custom_binary_with_args_test - -//tests/container/nodejs:nodejs_image_empty_list_args_test @@ -86,6 +89,7 @@ tasks: - -//tests/container/python3:py3_image_test - -//tests/container/rust:rust_image_test - -//tests/container/scala:scala_image_test + - -//tests/container/scala:scala_classpath_as_file_image_test - -//tests/contrib:derivative_with_volume_repro_test - -//tests/contrib:random_file_img_non_repro_test - -//tests/contrib:rbe-test-xenial_repro_test diff --git a/groovy/image.bzl b/groovy/image.bzl index 588908313..aca12e2cb 100644 --- a/groovy/image.bzl +++ b/groovy/image.bzl @@ -33,6 +33,7 @@ def groovy_image( deps = [], layers = [], jvm_flags = [], + classpath_as_file = None, **kwargs): """Builds a container image overlaying the groovy_binary. @@ -93,6 +94,7 @@ def groovy_image( args = kwargs.get("args"), data = kwargs.get("data"), testonly = kwargs.get("testonly"), + classpath_as_file = classpath_as_file, ) def repositories(): diff --git a/java/image.bzl b/java/image.bzl index 2c26e1f12..87b220ae4 100644 --- a/java/image.bzl +++ b/java/image.bzl @@ -201,7 +201,7 @@ def _jar_app_layer_impl(ctx): "/usr/bin/java", "-cp", # Support optionally passing the classpath as a file. - "@" + classpath_path if ctx.attr._classpath_as_file else classpath, + "@" + classpath_path if ctx.attr.classpath_as_file else classpath, ] + jvm_flags + ([ctx.attr.main_class] + args if ctx.attr.main_class != "" else []) file_map = { @@ -245,7 +245,7 @@ jar_app_layer = rule( "workdir": attr.string(default = ""), # Whether the classpath should be passed as a file. - "_classpath_as_file": attr.bool(default = False), + "classpath_as_file": attr.bool(default = False), "_jdk": attr.label( default = Label("@bazel_tools//tools/jdk:current_java_runtime"), providers = [java_common.JavaRuntimeInfo], @@ -265,6 +265,7 @@ def java_image( runtime_deps = [], layers = [], jvm_flags = [], + classpath_as_file = None, **kwargs): """Builds a container image overlaying the java_binary. @@ -327,6 +328,7 @@ def java_image( args = kwargs.get("args"), data = kwargs.get("data"), testonly = kwargs.get("testonly"), + classpath_as_file = classpath_as_file, ) def _war_dep_layer_impl(ctx): diff --git a/kotlin/image.bzl b/kotlin/image.bzl index ce1e0026f..b5339998b 100644 --- a/kotlin/image.bzl +++ b/kotlin/image.bzl @@ -34,6 +34,7 @@ def kt_jvm_image( deps = [], layers = [], jvm_flags = [], + classpath_as_file = None, **kwargs): """Builds a container image overlaying the kt_jvm_binary. @@ -92,6 +93,7 @@ def kt_jvm_image( args = kwargs.get("args"), data = kwargs.get("data"), testonly = kwargs.get("testonly"), + classpath_as_file = classpath_as_file, ) def repositories(): diff --git a/scala/image.bzl b/scala/image.bzl index f3c8172ee..160699cce 100644 --- a/scala/image.bzl +++ b/scala/image.bzl @@ -33,6 +33,7 @@ def scala_image( runtime_deps = [], layers = [], jvm_flags = [], + classpath_as_file = None, **kwargs): """Builds a container image overlaying the scala_binary. @@ -85,6 +86,7 @@ def scala_image( args = kwargs.get("args"), data = kwargs.get("data"), testonly = kwargs.get("testonly"), + classpath_as_file = classpath_as_file, ) def repositories(): diff --git a/tests/container/groovy/BUILD b/tests/container/groovy/BUILD index c6991dd56..620202dbb 100644 --- a/tests/container/groovy/BUILD +++ b/tests/container/groovy/BUILD @@ -38,8 +38,29 @@ groovy_image( main_class = "examples.images.Binary", ) +groovy_image( + name = "groovy_classpath_as_file_image", + srcs = ["//testdata:Binary.groovy"], + args = [ + "arg0", + "arg1", + "$(location :BUILD)", + ], + classpath_as_file = True, + data = [":BUILD"], + jvm_flags = ["-Dbuild.location=$(location :BUILD)"], + layers = [":groovy_image_library"], + main_class = "examples.images.Binary", +) + container_test( name = "groovy_image_test", configs = ["//tests/container/groovy/configs:groovy_image.yaml"], image = ":groovy_image", ) + +container_test( + name = "groovy_classpath_as_file_image_test", + configs = ["//tests/container/groovy/configs:groovy_classpath_as_file_image.yaml"], + image = ":groovy_classpath_as_file_image", +) diff --git a/tests/container/groovy/configs/groovy_classpath_as_file_image.yaml b/tests/container/groovy/configs/groovy_classpath_as_file_image.yaml new file mode 100644 index 000000000..45329183e --- /dev/null +++ b/tests/container/groovy/configs/groovy_classpath_as_file_image.yaml @@ -0,0 +1,13 @@ +schemaVersion: 2.0.0 + +metadataTest: + entrypoint: [ + '/usr/bin/java', + '-cp', + '@/app/io_bazel_rules_docker/tests/container/groovy/groovy_classpath_as_file_image.classpath', + '-Dbuild.location=tests/container/groovy/BUILD', + 'examples.images.Binary', + 'arg0', + 'arg1', + 'tests/container/groovy/BUILD', + ] diff --git a/tests/container/java/BUILD b/tests/container/java/BUILD index 74ec48f4d..ea4841fda 100644 --- a/tests/container/java/BUILD +++ b/tests/container/java/BUILD @@ -51,6 +51,23 @@ java_image( main_class = "examples.images.Binary", ) +java_image( + name = "java_classpath_as_file_image", + srcs = ["//testdata:Binary.java"], + args = [ + "arg0", + "arg1", + "$(location :BUILD)", + ], + classpath_as_file = True, + data = [":BUILD"], + jvm_flags = [ + "-Dbuild.location=$(location :BUILD)", + ], + layers = [":java_image_library"], + main_class = "examples.images.Binary", +) + java_image( name = "java_runfiles_image", srcs = ["//testdata:Runfiles.java"], @@ -91,6 +108,12 @@ container_test( image = ":java_image", ) +container_test( + name = "java_classpath_as_file_image_test", + configs = ["//tests/container/java/configs:java_classpath_as_file_image.yaml"], + image = ":java_classpath_as_file_image", +) + container_test( name = "java_runfiles_image_test", configs = ["//tests/container/java/configs:java_runfiles_image.yaml"], diff --git a/tests/container/java/configs/java_classpath_as_file_image.yaml b/tests/container/java/configs/java_classpath_as_file_image.yaml new file mode 100644 index 000000000..aef0bb3fc --- /dev/null +++ b/tests/container/java/configs/java_classpath_as_file_image.yaml @@ -0,0 +1,15 @@ +schemaVersion: 2.0.0 + +metadataTest: + env: + - key: JAVA_RUNFILES + value: "/app" + entrypoint: [ + '/usr/bin/java', + '-cp', + '@/app/io_bazel_rules_docker/tests/container/java/java_classpath_as_file_image.classpath', + '-Dbuild.location=tests/container/java/BUILD', + 'examples.images.Binary', + 'arg0', + 'arg1', + 'tests/container/java/BUILD'] diff --git a/tests/container/kotlin/BUILD b/tests/container/kotlin/BUILD index ccfcbb61e..82b724ba6 100644 --- a/tests/container/kotlin/BUILD +++ b/tests/container/kotlin/BUILD @@ -34,8 +34,32 @@ kt_jvm_image( deps = [], ) +kt_jvm_image( + name = "kt_jvm_classpath_as_file_image", + srcs = [ + "//testdata:Binary.kt", + "//testdata:Library.kt", + ], + args = [ + "arg0", + "arg1", + "$(location :BUILD)", + ], + classpath_as_file = True, + data = [":BUILD"], + jvm_flags = ["-Dbuild.location=$(location :BUILD)"], + main_class = "examples.images.Binary", + deps = [], +) + container_test( name = "kotlin_image_test", configs = ["//tests/container/kotlin/configs:kt_jvm_image.yaml"], image = ":kt_jvm_image", ) + +container_test( + name = "kotlin_classpath_as_file_image_test", + configs = ["//tests/container/kotlin/configs:kt_jvm_classpath_as_file_image.yaml"], + image = ":kt_jvm_classpath_as_file_image", +) diff --git a/tests/container/kotlin/configs/kt_jvm_classpath_as_file_image.yaml b/tests/container/kotlin/configs/kt_jvm_classpath_as_file_image.yaml new file mode 100644 index 000000000..026bed8bf --- /dev/null +++ b/tests/container/kotlin/configs/kt_jvm_classpath_as_file_image.yaml @@ -0,0 +1,13 @@ +schemaVersion: 2.0.0 + +metadataTest: + entrypoint: [ + '/usr/bin/java', + '-cp', + '@/app/io_bazel_rules_docker/tests/container/kotlin/kt_jvm_classpath_as_file_image.classpath', + '-Dbuild.location=tests/container/kotlin/BUILD', + 'examples.images.Binary', + 'arg0', + 'arg1', + 'tests/container/kotlin/BUILD', + ] diff --git a/tests/container/scala/BUILD b/tests/container/scala/BUILD index 717abea67..78d0ea043 100644 --- a/tests/container/scala/BUILD +++ b/tests/container/scala/BUILD @@ -43,3 +43,24 @@ container_test( configs = ["//tests/container/scala/configs:scala_image.yaml"], image = ":scala_image", ) + +scala_image( + name = "scala_classpath_as_file_image", + srcs = ["//testdata:Binary.scala"], + args = [ + "arg0", + "arg1", + "$(location :BUILD)", + ], + classpath_as_file = True, + data = [":BUILD"], + jvm_flags = ["-Dbuild.location=$(location :BUILD)"], + layers = [":scala_image_library"], + main_class = "examples.images.Binary", +) + +container_test( + name = "scala_classpath_as_file_image_test", + configs = ["//tests/container/scala/configs:scala_classpath_as_file_image.yaml"], + image = ":scala_classpath_as_file_image", +) diff --git a/tests/container/scala/configs/scala_classpath_as_file_image.yaml b/tests/container/scala/configs/scala_classpath_as_file_image.yaml new file mode 100644 index 000000000..c09e85548 --- /dev/null +++ b/tests/container/scala/configs/scala_classpath_as_file_image.yaml @@ -0,0 +1,13 @@ +schemaVersion: 2.0.0 + +metadataTest: + entrypoint: [ + '/usr/bin/java', + '-cp', + '@/app/io_bazel_rules_docker/tests/container/scala/scala_classpath_as_file_image.classpath', + '-Dbuild.location=tests/container/scala/BUILD', + 'examples.images.Binary', + 'arg0', + 'arg1', + 'tests/container/scala/BUILD', + ]