container-structure-test

repository·main·Indexed 25 days ago

https://github.com/googlecontainertools/container-structure-test

A framework for validating the structure of container images, including command outputs, filesystem contents, and image metadata. It supports verifying command exit codes and stdout/stderr via regex, checking file existence and permissions, validating image labels and environment variables, and performing license checks. The tool can be run via a CLI, as a Google Cloud Build builder image, or through a Bazel rule. It supports both a Docker driver for full runtime testing and a tar driver for filesystem-only tests without a container runtime.

Tokens
5.1K
Snippets
12
Records
26
Agent score
81%

What's inside container-structure-test

  1. Run Structure Tests via Bazel

    main

    For Bazel users, you can declare a container_structure_test target in your BUILD file.

    Requirements:

    • For Bazel 6+ with bzlmod, use the registry module: https://registry.bazel.build/modules/container_structure_test.
    • For older versions, load the rule and dependencies in your WORKSPACE.
    load("@container_structure_test//:defs.bzl", "container_structure_test")
    
    container_structure_test(
        name = "hello_test",
        configs = ["testdata/hello.yaml"],
        image = ":hello",
    )
  2. Configure Docker socket and TLS for Bazel tests

    main

    By default, container_structure_test uses the socket at /var/run/docker.sock. If your environment uses a different path or requires TLS, you must pass these via --test_env flags during bazel test.

    Custom Socket Path

    If the socket is in a non-standard location: --test_env=DOCKER_HOST='unix://<path_to_sock>'

    Remote Docker with TLS

    If using a remote daemon protected by TLS: --test_env=DOCKER_TLS_VERIFY=1 --test_env=DOCKER_CERT_PATH=<path_to_certs>

    Permanent Configuration

    To avoid typing these flags every time, add the following to your .bazelrc file to instruct Bazel to read the DOCKER_HOST from your terminal environment: test --test_env=DOCKER_HOST

  3. Run container structure tests

    main

    To run tests, you need the container-structure-test binary (or Docker image), a target container image, and a .yaml or .json configuration file defining your tests.

    By default, the framework looks for the target image in your local Docker daemon. Use the --pull flag to force a pull of a remote image before running tests.

    Multiple configuration files can be specified in a single run, and they will be executed in the order they are provided.

  4. Install container-structure-test

    main

    You can install the container-structure-test binary on macOS or Linux.

    OS X

    Install via Homebrew:

    brew install container-structure-test

    Or via direct download:

    curl -LO https://github.com/GoogleContainerTools/container-structure-test/releases/latest/download/container-structure-test-darwin-arm64 && chmod +x container-structure-test-darwin-arm64 && sudo mv container-structure-test-darwin-arm64 /usr/local/bin/container-structure-test

    Linux

    Download the amd64 binary:

    curl -LO https://github.com/GoogleContainerTools/container-structure-test/releases/latest/download/container-structure-test-linux-amd64 && chmod +x container-structure-test-linux-amd64 && sudo mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test

    To install without sudo (into $HOME/bin):

    curl -LO https://github.com/GoogleContainerTools/container-structure-test/releases/latest/download/container-structure-test-linux-amd64 && chmod +x container-structure-test-linux-amd64 && mkdir -p $HOME/bin && export PATH=$PATH:$HOME/bin && mv container-structure-test-linux-amd64 $HOME/bin/container-structure-test
    brew install container-structure-test
  5. Run Tests on Google Cloud Build

    main

    You can use container-structure-test as a builder image in your cloudbuild.yaml workflow. The image is available at gcr.io/gcp-runtimes/container-structure-test.

    steps:
    # Build an image.
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '-t', 'gcr.io/$PROJECT_ID/image', '.']
    # Test the image.
    - name: 'gcr.io/gcp-runtimes/container-structure-test'
      args: ['test', '--image', 'gcr.io/$PROJECT_ID/image', '--config', 'test_config.yaml']
    
    # Push the image.
    images: ['gcr.io/$PROJECT_ID/image']
  6. Use the container-structure-test CLI

    main

    The container-structure-test CLI provides a framework to validate the structure of a container image. It allows you to verify the output of commands executed within the image, as well as validate image metadata and filesystem contents.

    Available commands:

    • test: The primary command used to run structure tests against a container image.
    • version: Displays the current version of the tool.
    container-structure-test
  7. Configure Container Run Options (Docker Driver Only)

    main

    The containerRunOptions field allows you to control how the container is executed during testing. This is useful for validating runtime behavior like UID/user permissions or volume mounts.

    Note: These options are currently only supported when using the docker driver.

    containerRunOptions:
      user: "root"                  # set the --user/-u flag
      privileged: true              # set the --privileged flag (default: false)
      allocateTty: true             # set the --tty flag (default: false)
      envFile: path/to/.env         # load environment variables from file
      envVars:                      # read each envVar from the host environment
        - SECRET_KEY_FOO
        - OTHER_SECRET_BAR
      capabilities:                 # Add list of Linux capabilities (--cap-add)
        - NET_BIND_SERVICE
      bindMounts:                   # Bind mount a volume (--volume, -v)
        - /etc/example/dir:/etc/dir
  8. Test images from an OCI layout

    main

    If your image is stored in an OCI layout rather than a registry or local Docker daemon, use the --image-from-oci-layout flag.

    Constraints and Requirements:

    • Driver Requirement: This feature is only supported when using the docker driver.
    • Mutual Exclusivity: You cannot use --pull when using --image-from-oci-layout.
    • Tagging: If the OCI layout does not contain a reference annotation (org.opencontainers.image.ref.name), you must provide a --default-image-tag to identify the image when it is loaded into the daemon.
    • Multi-arch: Multi-arch images are not currently supported via OCI layouts.
  9. Configure License Tests

    main

    License tests verify that a container image contains only allowed copyright files. You can check against the standard Debian license list or provide a custom list of files.

    Supported Fields:

    • debian (bool, required): If true, the tool checks where Debian lists all licenses.
    • files (string[], optional): A list of specific file paths to check for licenses.
    licenseTests:
    - debian: true
      files: ["/foo/bar", "/baz/bat"]