Files
kafka/docker/docker_build_test.py
Maros Orsak 2e4d25ee13 MINOR: Allow overriding the base image of the JVM Docker image (#23560)
Makes the jvm image's base image overridable (`--base-image`, default
unchanged), so it can be built on another JDK version or an own Alpine
JRE image without forking the Dockerfile. The build fails fast with a
clear message when the base image has no `java` on PATH.

Checked by building on the default base (JDK 25) and on
temurin:21-jre-alpine, and that a base without a JRE (alpine:3) fails
at the check. The generated Docker Official Image source differs from
trunk only by that check line.

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>, YANG-SYUAN CHOU
<dino2895un@gmail.com>, Federico Valeri <fedevaleri@gmail.com>, Luke
Chen <showuon@gmail.com>, liuliu <liuliugit@gmail.com>
2026-10-11 16:24:23 +08:00

106 lines
6.1 KiB
Python
Executable File

#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Python script to build and test a docker image
This script is used to generate a test report
Usage:
docker_build_test.py --help
Get detailed description of each option
Example command:-
docker_build_test.py <image_name> --image-tag <image_tag> --image-type <image_type> --kafka-url <kafka_url>
docker_build_test.py <image_name> --image-tag <image_tag> --image-type <image_type> --kafka-archive <kafka_archive>
This command will build an image with <image_name> as image name, <image_tag> as image_tag (it will be latest by default),
<image_type> as image type (jvm by default), <kafka_url> for the kafka inside the image and run tests on the image.
<kafka_archive> can be passed as an alternative to <kafka_url> to use a local kafka archive. The path of kafka_archive should be absolute.
-b can be passed as additional argument if you just want to build the image.
-t can be passed if you just want to run tests on the image.
--base-image can be passed to build the jvm image on a different Alpine-based JRE image than the default.
"""
from datetime import date
import argparse
import shutil
from common import (
execute,
build_docker_image_runner,
detect_compose_command,
detect_container_runtime,
)
import tempfile
import os
def run_docker_tests(image, tag, kafka_url, kafka_archive, image_type, container_runtime="docker"):
# Imported here rather than at module scope so that building an image (-b) does not require the
# test dependencies (pytest, pytest-html). Only the test path needs them.
from test.docker_sanity_test import run_tests
compose_command = detect_compose_command(container_runtime)
temp_dir_path = tempfile.mkdtemp()
try:
current_dir = os.path.dirname(os.path.realpath(__file__))
shutil.copytree(f"{current_dir}/test/fixtures", f"{temp_dir_path}/fixtures", dirs_exist_ok=True)
if kafka_url is not None:
execute(["wget", "-nv", "-O", f"{temp_dir_path}/kafka.tgz", kafka_url])
elif kafka_archive is not None:
shutil.copy(kafka_archive, f"{temp_dir_path}/kafka.tgz")
else:
raise ValueError("Either --kafka-url or --kafka-archive must be passed")
execute(["mkdir", f"{temp_dir_path}/fixtures/kafka"])
execute(["tar", "xfz", f"{temp_dir_path}/kafka.tgz", "-C", f"{temp_dir_path}/fixtures/kafka", "--strip-components", "1"])
test_exit_code = run_tests(f"{image}:{tag}", image_type, temp_dir_path, container_runtime, compose_command)
except:
raise SystemError("Failed to run the tests")
finally:
shutil.rmtree(temp_dir_path)
test_report_location_text = f"To view test report please check {current_dir}/test/report_{image_type}.html"
if test_exit_code != 0:
raise SystemError(f"Tests failed with pytest exit code {test_exit_code}. {test_report_location_text}")
print(f"All tests passed successfully. {test_report_location_text}")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("image", help="Image name that you want to keep for the Docker image")
parser.add_argument("--image-tag", "-tag", default="latest", dest="tag", help="Image tag that you want to add to the image")
parser.add_argument("--image-type", "-type", choices=["jvm", "native"], default="jvm", dest="image_type", help="Image type you want to build")
parser.add_argument("--build", "-b", action="store_true", dest="build_only", default=False, help="Only build the image, don't run tests")
parser.add_argument("--test", "-t", action="store_true", dest="test_only", default=False, help="Only run the tests, don't build the image")
parser.add_argument("--base-image", dest="base_image", default=None, help="Base image for the jvm image type, overriding the Dockerfile's default (e.g. docker.io/library/eclipse-temurin:25-jre-alpine)")
archive_group = parser.add_mutually_exclusive_group(required=True)
archive_group.add_argument("--kafka-url", "-u", dest="kafka_url", help="Kafka url to be used to download kafka binary tarball in the docker image")
archive_group.add_argument("--kafka-archive", "-a", dest="kafka_archive", help="Kafka archive to be used to extract kafka binary tarball in the docker image")
args = parser.parse_args()
if args.base_image and args.image_type != "jvm":
parser.error("--base-image is only supported for the jvm image type")
base_image_arg = f"--build-arg base_image={args.base_image} " if args.base_image else ""
container_runtime = detect_container_runtime()
if args.build_only or not (args.build_only or args.test_only):
if args.kafka_url:
build_docker_image_runner(f"{container_runtime} build -f $DOCKER_FILE -t {args.image}:{args.tag} {base_image_arg}--build-arg kafka_url={args.kafka_url} --build-arg build_date={date.today()} --no-cache $DOCKER_DIR", args.image_type)
elif args.kafka_archive:
build_docker_image_runner(f"{container_runtime} build -f $DOCKER_FILE -t {args.image}:{args.tag} {base_image_arg}--build-arg build_date={date.today()} --no-cache $DOCKER_DIR", args.image_type, args.kafka_archive)
if args.test_only or not (args.build_only or args.test_only):
run_docker_tests(args.image, args.tag, args.kafka_url, args.kafka_archive, args.image_type, container_runtime)