Skip to content

Commit

Permalink
Ability to run JAVA_HOME binaries inside test (#24)
Browse files Browse the repository at this point in the history
* java home binaries

* update

* update

* update

* update

* update

* ls

* update

* update

* update
  • Loading branch information
kriscfoster authored Nov 26, 2024
1 parent 961a0fe commit 365a98d
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bazel-*
.ijwb
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ junit_docker_compose_test(
test_srcs = glob(["**/*Test.java"]),
test_deps = ["@maven//:org_junit_jupiter_junit_jupiter_api"],
classpath_jars = ["@maven//:org_junit_platform_junit_platform_console_standalone"],
test_image_base = "@distroless_java",
test_image_base = "@openjdk",
)
```

Expand Down
12 changes: 9 additions & 3 deletions docker_compose_test/test_container_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

echo "[DEBUG] JAVA_HOME before discovery logic: $JAVA_HOME"

# if JAVA_HOME is not set, just default to /usr (works if /usr/bin/java exists)
if [[ -z "$JAVA_HOME" ]]; then
JAVA_HOME="/usr"
export JAVA_HOME="/usr"
# this is used if JAVA_HOME contains an * (if version changes regularly this can be useful)
elif [[ "$JAVA_HOME" == *"\*"* ]]; then
JAVA_HOME=$(find $JAVA_HOME -maxdepth 1 | head -n 1)
elif echo "$JAVA_HOME" | grep '*' > /dev/null; then
export JAVA_HOME=$(find $JAVA_HOME -maxdepth 1 | head -n 1)
fi

echo "[DEBUG] JAVA_HOME after discovery logic: $JAVA_HOME"
export PATH=$JAVA_HOME/bin:$PATH
echo "[DEBUG] PATH: $PATH"

TEST_UBER_JAR=$(find ./ -maxdepth 1 -name '*_uber_jar_deploy.jar')
JUNIT_PLATFORM_CONSOLE_STANDALONE_JAR=$(find ./ -maxdepth 1 -name '*junit-platform-console-standalone*.jar')

Expand Down
2 changes: 2 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
common --test_output=errors
common --verbose_explanations
11 changes: 7 additions & 4 deletions examples/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ oci_register_toolchains(
load("@rules_oci//oci:pull.bzl", "oci_pull")

oci_pull(
name = "distroless_java",
# tag = "debug", # debug distroless image can be debugged with --entrypoint "/busybox/sh"
digest = "sha256:73c3687a9d7277f480a560ae380ba16acbe8eb5a0f459560b4466bb71e6288a1",
image = "gcr.io/distroless/java17",
name = "openjdk",
digest = "sha256:29c44ad7bb159a29a4458b74e8d37c1995cb8dc32abdd35e6d3e3d493e682d10",
image = "openjdk",
platforms = [
"linux/amd64",
"linux/arm64/v8",
],
)

oci_pull(
Expand Down
2 changes: 1 addition & 1 deletion examples/junit-image-test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ junit_docker_compose_test(
test_srcs = glob(["**/*Test.java"]),
test_deps = ["@maven//:org_junit_jupiter_junit_jupiter_api"],
classpath_jars = ["@maven//:org_junit_platform_junit_platform_console_standalone"],
test_image_base = "@distroless_java",
test_image_base = "@openjdk",
)
13 changes: 10 additions & 3 deletions examples/junit-image-test/HelloTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@
package com.salesforce.rules_docker_compose_test.HelloTest;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class HelloTest {

@Test
void helloWorldContainsHello() {
void helloWorldContainsHello() throws IOException, InterruptedException {
// Testing that the $JAVA_HOME/bin binaries are available
ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"sh", "-c", "jstat --help"});
Process process = processBuilder.start();
int exitValue = process.waitFor();
assertEquals(0, exitValue);
assertTrue("Hello World!".contains("Hello"));
}
}
}
4 changes: 2 additions & 2 deletions examples/junit-image-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
services:
test_container:
image: junit-image-test:test_container
entrypoint: ["/busybox/sh", "./test_container_entrypoint.sh"]
entrypoint: ["/bin/bash", "./test_container_entrypoint.sh"]
environment:
- JAVA_HOME=/usr/
- JAVA_HOME=/usr/local/openjdk-*
2 changes: 1 addition & 1 deletion examples/locally-built-image-test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pkg_tar(

oci_image(
name = "java_image",
base = "@distroless_java",
base = "@openjdk",
tars = [
":files",
],
Expand Down

0 comments on commit 365a98d

Please sign in to comment.