itestcontainer

command module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

itestcontainer

itestcontainer is a runner shim invoked by rules_itest's itest_service as an exe to launch a container image as the system under test.

Example:

load("@rules_img//img:image.bzl", "image_manifest")
load("@rules_img//img:load.bzl", "image_load")
load("@rules_itest//:itest.bzl", "itest_service", "itest_task")

platform(
    name = "host_docker_platform",
    constraint_values = ["@platforms//os:linux"],  # linux OS inside docker host
    parents = ["@platforms//host"],  # use host CPU
)

# Construct a Postgresql for the container host
image_manifest(
    name = "pg_image",
    base = "@postgresql",
    platform = ":host_docker_platform",
)

# An executable target to load the image into the container host
image_load(
    name = "load_pg_image",
    image = ":pg_image",
    tag = "pg_image:latest",
)

# A task that invokes the previous executable target
itest_task(
    name = "load_pg_image_task",
    testonly = True,
    exe = ":load_pg_image",
)

# The service under test, which depends on the load task to load the image, and then invokes `itestcontainer` to run the image with the provided options.
itest_service(
    name = "sut",
    testonly = True,
    args = [
        # Name of the image to start
        "--name=pg_image:latest",
        # Environment variables to pass through to the container
        "--env=POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DB",
        # Ports to expose from the container
        "--ports=$${@@//:sut:db}:5432"
        # --labels
        # --volume
    ],
    env = {
        "POSTGRES_USER": "postgres",
        "POSTGRES_PASSWORD": "postgres",
        "POSTGRES_DB": "postgres",
    },
    exe = "@com_github_jaqx0r_itestcontainer//:itestcontainer",
    named_ports = [
        "db",
    ],
    deps = [":load_pg_image_task"],
)

Use the Go Tools Pattern to include itestcontainer into your dependencies.

See test/tools.go and the rest of the test directory for a full example.

Documentation

Overview

itestcontainer is a runner shim invoked by a `rules_itest`'s `itest_service` as an `exe` to launch a container image with `testcontainers`.

Pass the name of the container, any environment the container needs, volume mounts, port assignments, and labels.

Volumes exist in the Docker volume space on the host, but are identified internally with the prefix `bazel-itest-`. If run inside the Bazel test execution environment (i.e. with the environment variable `TEST_TARGET` set) then that string is hashed and appended to the volume name. This allows each `itest_service` to run concurrently, avoiding contention and potential locking issues.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL