diff --git a/test/bench/docker_overlay_test.rb b/test/bench/docker_overlay_test.rb index db99256805..1b44a7246b 100644 --- a/test/bench/docker_overlay_test.rb +++ b/test/bench/docker_overlay_test.rb @@ -1,4 +1,5 @@ require_relative "test_helper" +require "open3" class DockerOverlayTest < Minitest::Test def test_mastodon_plus_copies_the_gemfile_ruby_version_before_bundle_install @@ -28,7 +29,57 @@ def test_mastodon_plus_preserves_upstream_runtime_and_dependency_updates end end + def test_dspace_plus_loads_the_build_cache_and_always_runs_the_installer_assembly + awk_implementations.each do |awk| + prepare_dspace(dspace_dockerfile, awk:) do |dockerfile, status| + assert status.success?, "#{awk} preparation failed" + lines = dockerfile.lines(chomp: true) + copy = lines.index("COPY --chown=dspace . /app/") + assert_equal "COPY --chown=dspace <maven-build-cache-extension" + build = lines.index { it.start_with?("RUN mvn --no-transfer-progress package") } + assert_equal 'RUN mvn --no-transfer-progress package ${MAVEN_FLAGS} -Dmaven.build.cache.alwaysRunPlugins=maven-assembly-plugin:single && \\', lines[build], awk + assert_equal " mv /app/dspace/target/${TARGET_DIR}/* /install && \\", lines[build + 1], awk + assert_equal dspace_dockerfile.lines.size + 10, lines.size, awk + end + end + end + + def test_dspace_plus_refuses_an_upstream_build_line_it_cannot_change + changed = dspace_dockerfile.sub("RUN mvn --no-transfer-progress package ${MAVEN_FLAGS} && \\", "RUN mvn package ${MAVEN_FLAGS} && \\") + refute_equal dspace_dockerfile, changed + + prepare_dspace(changed) do |_dockerfile, status| + refute status.success? + end + end + private + def prepare_dspace(dockerfile, awk: "awk") + Dir.mktmpdir("dspace-overlay") do |dir| + FileUtils.mkdir_p([File.join(dir, "upstream"), File.join(dir, "bin")]) + File.symlink(executable(awk), File.join(dir, "bin/awk")) + File.write(File.join(dir, "upstream/Dockerfile"), dockerfile) + env = { "PATH" => [File.join(dir, "bin"), ENV.fetch("PATH")].join(File::PATH_SEPARATOR) } + _output, status = Open3.capture2e(env, "bash", File.join(Bench::ROOT, "tools/docker/dspace/prepare.sh"), chdir: dir) + target = File.join(dir, "overlay/dspace-maven.Dockerfile") + yield File.exist?(target) ? File.read(target) : "", status + assert_equal dockerfile, File.read(File.join(dir, "upstream/Dockerfile")) + end + end + + def awk_implementations + %w[awk mawk gawk].select { executable(it) } + end + + def executable(name) + ENV.fetch("PATH").split(File::PATH_SEPARATOR).map { File.join(it, name) }.find { File.executable?(it) } + end + + def dspace_dockerfile + File.read(File.join(Bench::ROOT, "test/bench/fixtures/dspace.Dockerfile")) + end + def prepare_mastodon(dockerfile) Dir.mktmpdir("mastodon-overlay") do |dir| FileUtils.mkdir_p(File.join(dir, "upstream")) diff --git a/test/bench/fixtures/dspace.Dockerfile b/test/bench/fixtures/dspace.Dockerfile new file mode 100644 index 0000000000..e4aa68aed2 --- /dev/null +++ b/test/bench/fixtures/dspace.Dockerfile @@ -0,0 +1,71 @@ +# This image will be published as dspace/dspace +# See https://github.com/DSpace/DSpace/tree/main/dspace/src/main/docker for usage details +# +# - note: default tag for branch: dspace/dspace: dspace/dspace:latest + +# This Dockerfile uses JDK21 by default. +# To build with other versions, use "--build-arg JDK_VERSION=[value]" +ARG JDK_VERSION=21 +# The Docker version tag to build from +ARG DSPACE_VERSION=latest +# The Docker registry to use for DSpace images. Defaults to "docker.io" +# NOTE: non-DSpace images are hardcoded to use "docker.io" and are not impacted by this build argument +ARG DOCKER_REGISTRY=docker.io + +# Step 1 - Run Maven Build +FROM ${DOCKER_REGISTRY}/dspace/dspace-dependencies:${DSPACE_VERSION} AS build +ARG TARGET_DIR=dspace-installer +WORKDIR /app +# The dspace-installer directory will be written to /install +USER root +RUN mkdir /install \ + && chown -Rv dspace: /install \ + && chown -Rv dspace: /app +USER dspace +# Copy the DSpace source code (from local machine) into the workdir (excluding .dockerignore contents) +COPY --chown=dspace . /app/ +# Build DSpace +# Copy the dspace-installer directory to /install. Clean up the build to keep the docker image small +# Maven flags here ensure that we skip building test environment and skip all code verification checks. +# These flags speed up this compilation as much as reasonably possible. +ENV MAVEN_FLAGS="-P-test-environment -Denforcer.skip=true -Dcheckstyle.skip=true -Dlicense.skip=true -Dxml.skip=true" +RUN mvn --no-transfer-progress package ${MAVEN_FLAGS} && \ + mv /app/dspace/target/${TARGET_DIR}/* /install && \ + mvn clean +# Remove the server webapp to keep image small. +RUN rm -rf /install/webapps/server/ + +# Step 2 - Run Ant Deploy +FROM docker.io/eclipse-temurin:${JDK_VERSION} AS ant_build +ARG TARGET_DIR=dspace-installer +# COPY the /install directory from 'build' container to /dspace-src in this container +COPY --from=build /install /dspace-src +WORKDIR /dspace-src +# Install Apache Ant +RUN apt-get update \ + && apt-get install -y --no-install-recommends ant \ + && apt-get purge -y --auto-remove \ + && rm -rf /var/lib/apt/lists/* +# Run necessary 'ant' deploy scripts +RUN ant init_installation update_configs update_code update_webapps + +# Step 3 - Start up DSpace via Runnable JAR +FROM docker.io/eclipse-temurin:${JDK_VERSION} +# Below syntax may look odd, but it is how to override dspace.cfg settings via env variables. +# See https://github.com/DSpace/DSpace/blob/main/dspace/config/config-definition.xml +# "dspace__P__dir" is setting the value of the "dspace.dir" configuration. This is our installation directory. +ENV dspace__P__dir=/dspace +# Copy the /dspace directory from 'ant_build' container to /dspace in this container +COPY --from=ant_build /dspace $dspace__P__dir +WORKDIR $dspace__P__dir +# Need host command for "[dspace]/bin/make-handle-config" +RUN apt-get update \ + && apt-get install -y --no-install-recommends host \ + && apt-get purge -y --auto-remove \ + && rm -rf /var/lib/apt/lists/* +# Expose Tomcat port (8080) & Handle Server HTTP port (8000) +EXPOSE 8080 8000 +# Give java extra memory (2GB) +ENV JAVA_OPTS=-Xmx2000m +# On startup, run DSpace Runnable JAR (uses the "dspace.dir" setting defined in "dspace__P__dir" env variable) +ENTRYPOINT ["java", "-jar", "webapps/server-boot.jar"] diff --git a/tools/docker/dspace/prepare.sh b/tools/docker/dspace/prepare.sh index a4f1b15b3d..50fd2958d8 100644 --- a/tools/docker/dspace/prepare.sh +++ b/tools/docker/dspace/prepare.sh @@ -4,6 +4,8 @@ source=upstream/Dockerfile target=overlay/dspace-maven.Dockerfile copy='COPY --chown=dspace . /app/' build='RUN mvn --no-transfer-progress package ${MAVEN_FLAGS} && \' +extension='COPY --chown=dspace <" print "" print " " @@ -28,9 +30,16 @@ awk -v copy="$copy" -v build="$build" ' print "EOF" next } - $0 == build { - print "RUN mvn --no-transfer-progress package ${MAVEN_FLAGS} -Dmaven.build.cache.alwaysRunPlugins=maven-assembly-plugin:single && \\" + $0 == ENVIRON["BUILD_LINE"] { + print ENVIRON["ALWAYS_RUN_LINE"] next } { print } ' "$source" > "$target" + +for line in "$extension" "$always_run"; do + if [ "$(grep -cxF -- "$line" "$target")" != 1 ]; then + echo "$target is missing this line: $line" >&2 + exit 1 + fi +done