Skip to content

Commit

Permalink
test: thorough test for jkube-java-xx (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa authored Jun 20, 2024
1 parent 013e6f2 commit 62c6e5d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 28 deletions.
7 changes: 7 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

TAG_OR_LATEST="${TAG:-latest}"

# Run command in docker container for configured image, the first argument is the command to run in the container
function dockerRun() {
read -ra COMMAND <<<"$1"
output=$(docker run --rm --pull never "$IMAGE" "${COMMAND[@]}" 2>&1)
echo "$output";
}

# Run command in docker container for configured allowing to pass arguments including quotes and spaces
function dockerRunE() {
output=$(docker run --rm --pull never "$IMAGE" "${@}" 2>&1)
echo "$output";
}

function assertContains() {
echo "$1" | grep "$2" > /dev/null
}
Expand Down
53 changes: 39 additions & 14 deletions scripts/test-jkube-java-17.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,58 @@ BASEDIR=$(dirname "$BASH_SOURCE")
source "$BASEDIR/common.sh"

IMAGE="quay.io/jkube/jkube-java-17:$TAG_OR_LATEST"
env_variables="$(dockerRun 'env')"

# User
assertContains "$(dockerRun 'id')" "uid=1000 gid=0(root) groups=0(root)" || reportError "Invalid run user, should be 1000"
assertMatches "$(dockerRun 'pwd')" '/home/jboss' || reportError "Invalid home directory"

# Java (xxx.openjdk.jdk)
java_version="$(dockerRun 'java -version')"
assertMatches "$java_version" 'openjdk version "17.0.[0-9]+' || reportError "Invalid Java version:\n\n$java_version"
assertContains "$env_variables" "JAVA_HOME=/usr/lib/jvm/java-17$" \
|| reportError "JAVA_HOME invalid"
assertContains "$env_variables" "JAVA_VERSION=17$" \
|| reportError "JAVA_VERSION invalid"
assertContains "$env_variables" "JBOSS_CONTAINER_OPENJDK_JDK_MODULE=/opt/jboss/container/openjdk/jdk$" \
|| reportError "JBOSS_CONTAINER_OPENJDK_JDK_MODULE invalid"
jvm_options="$(dockerRunE /bin/bash -c '. /opt/jboss/container/openjdk/jdk/jvm-options && jvm_specific_diagnostics')" || reportError "Failed to get jvm_options"
assertMatches "$jvm_options" '\-Xlog:gc::utctime -XX:NativeMemoryTracking=summary$' || reportError "Invalid jvm_options:\n\n$jvm_options"

maven_version="$(dockerRun 'mvn -version')"
assertMatches "$maven_version" 'Apache Maven 3.8.[0-9]+' || reportError "Invalid Maven version:\n\n$maven_version"

# run-java dependent scripts
# run-java dependent scripts (xxx.java.jvm.bash)
jvm_tools="$(dockerRun 'ls -la /opt/jboss/container/java/jvm/')"
assertContains "$jvm_tools" "container-limits$" || reportError "container-limits not found"
assertContains "$jvm_tools" "debug-options$" || reportError "debug-options not found"
assertContains "$jvm_tools" "java-default-options$" || reportError "java-default-options not found"
# java-default-options default
java_default_options="$(dockerRunE '/opt/jboss/container/java/jvm/java-default-options')" || reportError "Failed to get java_default_options"
assertMatches "$java_default_options" '^-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError$' \
|| reportError "Invalid java_default_options:\n\n$java_default_options"
# java-default-options override with JAVA_DIAGNOSTICS
java_default_options="$(dockerRunE /bin/bash -c 'JAVA_DIAGNOSTICS=true /opt/jboss/container/java/jvm/java-default-options')"|| reportError "Failed to get java_default_options"
assertMatches "$java_default_options" '^-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Xlog:gc::utctime -XX:NativeMemoryTracking=summary -XX:\+ExitOnOutOfMemoryError$' \
|| reportError "Invalid java_default_options (JAVA_DIAGNOSTICS):\n\n$java_default_options"
# java-default-options override with blank GC_CONTAINER_OPTIONS env (Unsupported -XX:+UseParallelOldGC is listed)
java_default_options="$(dockerRunE /bin/bash -c 'GC_CONTAINER_OPTIONS="" /opt/jboss/container/java/jvm/java-default-options')"|| reportError "Failed to get java_default_options"
assertMatches "$java_default_options" "^-XX:\+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError$" \
|| reportError "Invalid java_default_options (GC_CONTAINER_OPTIONS):\n\n$java_default_options"

# debug-options-override
debug_options="$(dockerRun 'cat /opt/jboss/container/java/jvm/debug-options')"
assertContains "$debug_options" "[-]agentlib:jdwp=transport=dt_socket,server=y,suspend=\${suspend_mode},address=\${debug_port}" \
|| reportError "Overridden debug-options is wrong"

# java-default-options override via GC_CONTAINER_OPTIONS env (Unsupported -XX:+UseParallelOldGC is not listed)
java_default_options="$(dockerRun '/opt/jboss/container/java/jvm/java-default-options')"
assertContains "$java_default_options" "^-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError$" \
|| reportError "Overridden GC_CONTAINER_OPTIONS is not working"

# Default run-java module
# Default run-java module (xxx.java.run.bash)
run_java="$(dockerRun 'ls -la /opt/jboss/container/java/run/')"
assertContains "$run_java" "run-env.sh" || reportError "run-env.sh not found"
assertContains "$run_java" "run-java.sh" || reportError "run-java.sh not found"
# shellcheck disable=SC2016
run_java_exec="$(dockerRunE /bin/bash -c 'JAVA_APP_JAR=$JAVA_HOME/lib/jrt-fs.jar /opt/jboss/container/java/run/run-java.sh')" || reportError "Failed to get run_java_exec"
assertMatches "$run_java_exec" ".+java -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError -cp \".\" -jar.+" \
|| reportError "Invalid run_java_exec:\n\n$run_java_exec"

# Jolokia module
jolokia_jar="$(dockerRun 'ls -la /usr/share/java/jolokia-jvm-agent/')"
Expand All @@ -50,18 +73,20 @@ prometheus="$(dockerRun 'ls -la /opt/jboss/container/prometheus/')"
assertContains "$prometheus" "prometheus-opts" || reportError "prometheus-opts not found"
assertContains "$prometheus" "etc" || reportError "etc not found"

# S2I scripts
# S2I (xxx.java.s2i.bash)
s2i="$(dockerRun 'ls -la /usr/local/s2i/')"
assertContains "$s2i" "assemble" || reportError "assemble not found"
assertContains "$s2i" "run" || reportError "run not found"
assertContains "$(dockerRun 'cat /usr/local/s2i/assemble')" 'maven_s2i_build$' || reportError "Invalid s2i assemble script"
# shellcheck disable=SC2016
s2i_run="$(dockerRunE /bin/bash -c 'JAVA_APP_JAR=$JAVA_HOME/lib/jrt-fs.jar /usr/local/s2i/run')" || reportError "Failed to get s2i_run"
assertJolokia="-javaagent:/usr/share/java/jolokia-jvm-agent/jolokia-jvm.jar=config=/opt/jboss/container/jolokia/etc/jolokia.properties"
assertPrometheus="-javaagent:/usr/share/java/prometheus-jmx-exporter/jmx_prometheus_javaagent.jar=9779:/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml"
assertJavaExec="-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError -cp \".\" -jar"
assertMatches "$s2i_run" ".+java $assertJolokia $assertPrometheus $assertJavaExec.+" \
|| reportError "Invalid run_java_exec:\n\n$s2i_run"

# Env
env_variables="$(dockerRun 'env')"
assertContains "$env_variables" "JAVA_HOME=/usr/lib/jvm/java-17$" \
|| reportError "JAVA_HOME invalid"
assertContains "$env_variables" "JAVA_VERSION=17$" \
|| reportError "JAVA_VERSION invalid"
# Generic environment variables
assertContains "$env_variables" "DEPLOYMENTS_DIR=/deployments$" \
|| reportError "DEPLOYMENTS_DIR invalid"
assertContains "$env_variables" "JBOSS_CONTAINER_JAVA_RUN_MODULE=/opt/jboss/container/java/run$" \
Expand Down
53 changes: 39 additions & 14 deletions scripts/test-jkube-java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,58 @@ BASEDIR=$(dirname "$BASH_SOURCE")
source "$BASEDIR/common.sh"

IMAGE="quay.io/jkube/jkube-java:$TAG_OR_LATEST"
env_variables="$(dockerRun 'env')"

# User
assertContains "$(dockerRun 'id')" "uid=1000 gid=0(root) groups=0(root)" || reportError "Invalid run user, should be 1000"
assertMatches "$(dockerRun 'pwd')" '/home/jboss' || reportError "Invalid home directory"

# Java (xxx.openjdk.jdk)
java_version="$(dockerRun 'java -version')"
assertMatches "$java_version" 'openjdk version "21.0.[0-9]+' || reportError "Invalid Java version:\n\n$java_version"
assertContains "$env_variables" "JAVA_HOME=/usr/lib/jvm/java-21$" \
|| reportError "JAVA_HOME invalid"
assertContains "$env_variables" "JAVA_VERSION=21$" \
|| reportError "JAVA_VERSION invalid"
assertContains "$env_variables" "JBOSS_CONTAINER_OPENJDK_JDK_MODULE=/opt/jboss/container/openjdk/jdk$" \
|| reportError "JBOSS_CONTAINER_OPENJDK_JDK_MODULE invalid"
jvm_options="$(dockerRunE /bin/bash -c '. /opt/jboss/container/openjdk/jdk/jvm-options && jvm_specific_diagnostics')" || reportError "Failed to get jvm_options"
assertMatches "$jvm_options" '\-Xlog:gc::utctime -XX:NativeMemoryTracking=summary$' || reportError "Invalid jvm_options:\n\n$jvm_options"

maven_version="$(dockerRun 'mvn -version')"
assertMatches "$maven_version" 'Apache Maven 3.8.[0-9]+' || reportError "Invalid Maven version:\n\n$maven_version"

# run-java dependent scripts
# run-java dependent scripts (xxx.java.jvm.bash)
jvm_tools="$(dockerRun 'ls -la /opt/jboss/container/java/jvm/')"
assertContains "$jvm_tools" "container-limits$" || reportError "container-limits not found"
assertContains "$jvm_tools" "debug-options$" || reportError "debug-options not found"
assertContains "$jvm_tools" "java-default-options$" || reportError "java-default-options not found"
# java-default-options default
java_default_options="$(dockerRunE '/opt/jboss/container/java/jvm/java-default-options')" || reportError "Failed to get java_default_options"
assertMatches "$java_default_options" '^-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError$' \
|| reportError "Invalid java_default_options:\n\n$java_default_options"
# java-default-options override with JAVA_DIAGNOSTICS
java_default_options="$(dockerRunE /bin/bash -c 'JAVA_DIAGNOSTICS=true /opt/jboss/container/java/jvm/java-default-options')"|| reportError "Failed to get java_default_options"
assertMatches "$java_default_options" '^-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Xlog:gc::utctime -XX:NativeMemoryTracking=summary -XX:\+ExitOnOutOfMemoryError$' \
|| reportError "Invalid java_default_options (JAVA_DIAGNOSTICS):\n\n$java_default_options"
# java-default-options override with blank GC_CONTAINER_OPTIONS env (Unsupported -XX:+UseParallelOldGC is listed)
java_default_options="$(dockerRunE /bin/bash -c 'GC_CONTAINER_OPTIONS="" /opt/jboss/container/java/jvm/java-default-options')"|| reportError "Failed to get java_default_options"
assertMatches "$java_default_options" "^-XX:\+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError$" \
|| reportError "Invalid java_default_options (GC_CONTAINER_OPTIONS):\n\n$java_default_options"

# debug-options-override
debug_options="$(dockerRun 'cat /opt/jboss/container/java/jvm/debug-options')"
assertContains "$debug_options" "[-]agentlib:jdwp=transport=dt_socket,server=y,suspend=\${suspend_mode},address=\${debug_port}" \
|| reportError "Overridden debug-options is wrong"

# java-default-options override via GC_CONTAINER_OPTIONS env (Unsupported -XX:+UseParallelOldGC is not listed)
java_default_options="$(dockerRun '/opt/jboss/container/java/jvm/java-default-options')"
assertContains "$java_default_options" "^-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError$" \
|| reportError "Overridden GC_CONTAINER_OPTIONS is not working"

# Default run-java module
# Default run-java module (xxx.java.run.bash)
run_java="$(dockerRun 'ls -la /opt/jboss/container/java/run/')"
assertContains "$run_java" "run-env.sh" || reportError "run-env.sh not found"
assertContains "$run_java" "run-java.sh" || reportError "run-java.sh not found"
# shellcheck disable=SC2016
run_java_exec="$(dockerRunE /bin/bash -c 'JAVA_APP_JAR=$JAVA_HOME/lib/jrt-fs.jar /opt/jboss/container/java/run/run-java.sh')" || reportError "Failed to get run_java_exec"
assertMatches "$run_java_exec" ".+java -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError -cp \".\" -jar.+" \
|| reportError "Invalid run_java_exec:\n\n$run_java_exec"

# Jolokia module
jolokia_jar="$(dockerRun 'ls -la /usr/share/java/jolokia-jvm-agent/')"
Expand All @@ -50,18 +73,20 @@ prometheus="$(dockerRun 'ls -la /opt/jboss/container/prometheus/')"
assertContains "$prometheus" "prometheus-opts" || reportError "prometheus-opts not found"
assertContains "$prometheus" "etc" || reportError "etc not found"

# S2I scripts
# S2I (xxx.java.s2i.bash)
s2i="$(dockerRun 'ls -la /usr/local/s2i/')"
assertContains "$s2i" "assemble" || reportError "assemble not found"
assertContains "$s2i" "run" || reportError "run not found"
assertContains "$(dockerRun 'cat /usr/local/s2i/assemble')" 'maven_s2i_build$' || reportError "Invalid s2i assemble script"
# shellcheck disable=SC2016
s2i_run="$(dockerRunE /bin/bash -c 'JAVA_APP_JAR=$JAVA_HOME/lib/jrt-fs.jar /usr/local/s2i/run')" || reportError "Failed to get s2i_run"
assertJolokia="-javaagent:/usr/share/java/jolokia-jvm-agent/jolokia-jvm.jar=config=/opt/jboss/container/jolokia/etc/jolokia.properties"
assertPrometheus="-javaagent:/usr/share/java/prometheus-jmx-exporter/jmx_prometheus_javaagent.jar=9779:/opt/jboss/container/prometheus/etc/jmx-exporter-config.yaml"
assertJavaExec="-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:\+ExitOnOutOfMemoryError -cp \".\" -jar"
assertMatches "$s2i_run" ".+java $assertJolokia $assertPrometheus $assertJavaExec.+" \
|| reportError "Invalid run_java_exec:\n\n$s2i_run"

# Env
env_variables="$(dockerRun 'env')"
assertContains "$env_variables" "JAVA_HOME=/usr/lib/jvm/java-21$" \
|| reportError "JAVA_HOME invalid"
assertContains "$env_variables" "JAVA_VERSION=21$" \
|| reportError "JAVA_VERSION invalid"
# Generic environment variables
assertContains "$env_variables" "DEPLOYMENTS_DIR=/deployments$" \
|| reportError "DEPLOYMENTS_DIR invalid"
assertContains "$env_variables" "JBOSS_CONTAINER_JAVA_RUN_MODULE=/opt/jboss/container/java/run$" \
Expand Down

0 comments on commit 62c6e5d

Please sign in to comment.