Skip to main content

Patched build. This wheel is built from upstream v1.19.1 with the following patches applied:

  • fix: Repair --pull for private registries and Docker Engine 29+

Packaging source: https://github.com/FlavioAmurrioCS/container-structure-test

Container Structure Tests

The Container Structure Tests provide a powerful framework to validate the structure of a container image. These tests can be used to check the output of commands in an image, as well as verify metadata and contents of the filesystem.

Tests can be run either through a standalone binary, or through a Docker image.

Note: container-structure-test is not an officially supported Google project, and is currently in maintainence mode. Contributions are still welcome!

Installation

OS X

Install via brew:

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

Linux

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

If you want to avoid using sudo:

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

[!warning] Container builds are currently not updated with new releases

Additionally, a container image for running tests through Google Cloud Builder can be found at gcr.io/gcp-runtimes/container-structure-test:latest.

Setup

To use container structure tests to validate your containers, you'll need the following:

  • The container structure test binary or docker image
  • A container image to test against
  • A test .yaml or .json file with user defined structure tests to run inside of the specified container image

Note that the test framework looks for the provided image in the local Docker daemon (if it is not provided as a tar). The --pull flag can optionally be provided to force a pull of a remote image before running the tests.

Example Run

An example run of the test framework:

container-structure-test test --image gcr.io/registry/image:latest \
--config config.yaml

Tests within this framework are specified through a YAML or JSON config file, which is provided to the test driver via a CLI flag. Multiple config files may be specified in a single test run. The config file will be loaded in by the test driver, which will execute the tests in order. Within this config file, four types of tests can be written:

  • Command Tests (testing output/error of a specific command issued)
  • File Existence Tests (making sure a file is, or isn't, present in the file system of the image)
  • File Content Tests (making sure files in the file system of the image contain, or do not contain, specific contents)
  • Metadata Test, singular (making sure certain container metadata is correct)

Command Tests

Command tests ensure that certain commands run properly in the target image. Regexes can be used to check for expected or excluded strings in both stdout and stderr. Additionally, any number of flags can be passed to the argument as normal. Each command in the setup section will run in a separate container and then commits a modified image to be the new base image for the test run.

Supported Fields:

NOTE: schemaVersion must be specified in all container-structure-test yamls. The current version is 2.0.0.

  • Name (string, required): The name of the test
  • Setup ([][]string, optional): A list of commands (each with optional flags) to run before the actual command under test.
  • Teardown ([][]string, optional): A list of commands (each with optional flags) to run after the actual command under test.
  • Command (string, required): The command to run in the test.
  • Args ([]string, optional): The arguments to pass to the command.
  • EnvVars ([]EnvVar, optional): A list of environment variables to set for the individual test. See the Environment Variables section for more info.
  • Expected Output ([]string, optional): List of regexes that should match the stdout from running the command.
  • Excluded Output ([]string, optional): List of regexes that should not match the stdout from running the command.
  • Expected Error ([]string, optional): List of regexes that should match the stderr from running the command.
  • Excluded Error ([]string, optional): List of regexes that should not match the stderr from running the command.
  • Exit Code (int, optional): Exit code that the command should exit with.

Example:

commandTests:
  - name: "gunicorn flask"
    setup: [["virtualenv", "/env"], ["pip", "install", "gunicorn", "flask"]]
    command: "which"
    args: ["gunicorn"]
    expectedOutput: ["/env/bin/gunicorn"]
  - name:  "apt-get upgrade"
    command: "apt-get"
    args: ["-qqs", "upgrade"]
    excludedOutput: [".*Inst.*Security.* | .*Security.*Inst.*"]
    excludedError: [".*Inst.*Security.* | .*Security.*Inst.*"]

Depending on your command the argument section can get quite long. Thus, you can make use of YAML's list style option for separation of arguments and the literal style option to preserve newlines like so:

commandTests:
  - name: "say hello world"
    command: "bash"
    args:
      - -c
      - |
         echo hello &&
         echo world

Image Entrypoint

To avoid unexpected behavior and output when running commands in the containers, all entrypoints are overwritten by default. If your entrypoint is necessary for the structure of your container, use the setup field to call any scripts or commands manually before running the tests.

commandTests:
  ...
  setup: [["my_image_entrypoint.sh"]]
  ...

Intermediate Artifacts

Each command test run creates either a container (with the docker driver) or tar artifact (with the tar driver). By default, these are deleted after the test run finishes, but the --save flag can optionally be passed to keep these around. This would normally be used for debugging purposes.

File Existence Tests

File existence tests check to make sure a specific file (or directory) exist within the file system of the image. No contents of the files or directories are checked. These tests can also be used to ensure a file or directory is not present in the file system.

Supported Fields:

  • Name (string, required): The name of the test
  • Path (string, required): Path to the file or directory under test
  • ShouldExist (boolean, required): Whether or not the specified file or directory should exist in the file system
  • Permissions (string, optional): The expected Unix permission string (e.g. drwxrwxrwx) of the files or directory.
  • Uid (int, optional): The expected Unix user ID of the owner of the file or directory.
  • Gid (int, optional): The expected Unix group ID of the owner of the file or directory.
  • IsExecutableBy (string, optional): Checks if file is executable by a given user. One of owner, group, other or any

Example:

fileExistenceTests:
- name: 'Root'
  path: '/'
  shouldExist: true
  permissions: '-rw-r--r--'
  uid: 1000
  gid: 1000
  isExecutableBy: 'group'

File Content Tests

File content tests open a file on the file system and check its contents. These tests assume the specified file is a file, and that it exists (if unsure about either or these criteria, see the above File Existence Tests section). Regexes can again be used to check for expected or excluded content in the specified file.

Supported Fields:

  • Name (string, required): The name of the test
  • Path (string, required): Path to the file under test
  • ExpectedContents (string[], optional): List of regexes that should match the contents of the file
  • ExcludedContents (string[], optional): List of regexes that should not match the contents of the file

Example:

fileContentTests:
- name: 'Debian Sources'
  path: '/etc/apt/sources.list'
  expectedContents: ['.*httpredir\.debian\.org.*']
  excludedContents: ['.*gce_debian_mirror.*']

Metadata Test

The Metadata test ensures the container is configured correctly. All of these checks are optional.

Supported Fields:

  • EnvVars ([]EnvVar): A list of environment variable key/value pairs that should be set in the container. isRegex (optional) interpretes the value as regex.
  • UnboundEnvVars ([]EnvVar): A list of environment variable keys that should NOT be set in the container.
  • Labels ([]Label): A list of image labels key/value pairs that should be set on the container. isRegex (optional) interpretes the value as regex.
  • Entrypoint ([]string): The entrypoint of the container.
  • Cmd ([]string): The CMD specified in the container.
  • Exposed Ports ([]string): The ports exposed in the container.
  • Unexposed Ports ([]string): The ports NOT exposed in the container.
  • Volumes ([]string): The volumes exposed in the container.
  • UnmountedVolumes ([]string): The volumes NOT exposed in the container.
  • Workdir (string): The default working directory of the container.
  • User (user): The default user of the container.

Example:

metadataTest:
  envVars:
    - key: foo
      value: baz
  labels:
    - key: 'com.example.vendor'
      value: 'ACME Incorporated'
    - key: 'build-date'
      value: '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$'
      isRegex: true
  exposedPorts: ["8080", "2345"]
  volumes: ["/test"]
  entrypoint: []
  cmd: ["/bin/bash"]
  workdir: "/app"
  user: "luke"

License Tests

License tests check a list of copyright files and makes sure all licenses are allowed at Google. By default it will look at where Debian lists all copyright files, but can also look at an arbitrary list of files.

Supported Fields:

  • Debian (bool, required): If the image is based on Debian, check where Debian lists all licenses.
  • Files (string[], optional): A list of other files to check.

Example:

licenseTests:
- debian: true
  files: ["/foo/bar", "/baz/bat"]

Environment Variables

A list of environment variables can optionally be specified as part of the test setup. They can either be set up globally (for all test runs), or test-local as part of individual command test runs (see the Command Tests section above). Each environment variable is specified as a key-value pair. Unix-style environment variable substitution is supported.

To specify, add a section like this to your config:

globalEnvVars:
  - key: "VIRTUAL_ENV"
    value: "/env"
  - key: "PATH"
    value: "/env/bin:$PATH"

Additional Options

The following fields are used to control various options and flags that may be desirable to set for the running container used to perform a structure test against an image. This allows an image author to validate certain runtime behavior that cannot be modified in the image-under-test such as running a container with an alternative user/UID or mounting a volume.

Note that these options are currently only supported with the docker driver.

The following list of options are currently supported:

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 and pass to container (equivalent to --env-file)
  envVars:                      # if not empty, read each envVar from the environment and pass to test (equivalent to --env/e)
    - 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

Running Tests On Google Cloud Build

This tool is released as a builder image, tagged as gcr.io/gcp-runtimes/container-structure-test, so you can specify tests in your cloudbuild.yaml:

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']

Running File Tests Without Docker

Container images can be represented in multiple formats, and the Docker image is just one of them. At their core, images are just a series of layers, each of which is a tarball, and so can be interacted with without a working Docker daemon. While running command tests currently requires a functioning Docker daemon on the host machine, File Existence/Content tests do not. This can be useful when dealing with images which have been docker exported or saved in a different image format than the Docker format, or when you're simply trying to run structure tests in an environment where Docker can't be installed.

To run tests without using a Docker daemon, users can specify a different "driver" to use in the tests, with the --driver flag.

An example test run with a different driver looks like:

container-structure-test test --driver tar --image gcr.io/registry/image:latest \
--config config.yaml

The currently supported drivers in the framework are:

  • docker: the default driver. Supports all tests, and uses the Docker daemon on the host to run them. You can set the runtime to use (by example runsc to run with gVisor) using --runtime flag.
  • tar: a tar driver, which extracts an image filesystem to wherever tests are running, and runs file/metadata tests against it. Does not support command tests.

Running Structure Tests Through Bazel

Structure tests can also be run through bazel.

With Bazel 6 and bzlmod, just see https://registry.bazel.build/modules/container_structure_test. Otherwise, load the rule and its dependencies in your WORKSPACE, see bazel/test/WORKSPACE.bazel in this repo.

Load the rule definition in your BUILD file and declare a container_structure_test target, passing in your image and config file as parameters:

load("@container_structure_test//:defs.bzl", "container_structure_test")

container_structure_test(
    name = "hello_test",
    configs = ["testdata/hello.yaml"],
    image = ":hello",
)

Flags:

container-structure-test test -h

  -c, --config stringArray             test config files
      --default-image-tag string       default image tag to used when loading images to the daemon. required when --image-from-oci-layout refers to a oci layout lacking the reference annotation.
  -d, --driver string                  driver to use when running tests (default "docker")
  -f, --force                          force run of host driver (without user prompt)
  -h, --help                           help for test
  -i, --image string                   path to test image
      --image-from-oci-layout string   path to the oci layout to test against
      --metadata string                path to image metadata file
      --no-color                       no color in the output
  -o, --output string                  output format for the test report (available format: text, json, junit) (default "text")
      --platform string                Set platform if host is multi-platform capable (default "linux/amd64")
      --pull                           force a pull of the image before running tests
  -q, --quiet                          flag to suppress output
      --runtime string                 runtime to use with docker driver
      --save                           preserve created containers after test run
      --test-report string             generate test report and write it to specified file (supported format: json, junit; default: json)

See this example repo for a full working example.

Output formats

Reports are generated using one of the following output formats: text, json or junit. Formats like json and junit can also be used to write a report to a specified file using the --test-report.

Output samples

Text

====================================
====== Test file: config.yaml ======
====================================
=== RUN: File Existence Test: whoami
--- PASS
duration: 0s
=== RUN: Metadata Test
--- PASS
duration: 0s

=====================================
============== RESULTS ==============
=====================================
Passes:      2
Failures:    0
Duration:    0s
Total tests: 2

PASS

JSON

The following sample has been formatted.

{
  "Pass": 2,
  "Fail": 0,
  "Total": 2,
  "Duration": 0,
  "Results": [
    {
      "Name": "File Existence Test: whoami",
      "Pass": true,
      "Duration": 0
    },
    {
      "Name": "Metadata Test",
      "Pass": true,
      "Duration": 0
    }
  ]
}

JUnit

The following sample has been formatted.

<?xml version="1.0"?>
<testsuites failures="0" tests="2" time="0">
  <testsuite>
    <testcase name="File Existence Test: whoami" time="0"/>
    <testcase name="Metadata Test" time="0"/>
  </testsuite>
</testsuites>

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

container_structure_test-1.19.1-py3-none-win_arm64.whl (11.6 MB view details)

Uploaded Python 3Windows ARM64

container_structure_test-1.19.1-py3-none-win_amd64.whl (12.3 MB view details)

Uploaded Python 3Windows x86-64

container_structure_test-1.19.1-py3-none-musllinux_1_2_x86_64.whl (11.8 MB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

container_structure_test-1.19.1-py3-none-musllinux_1_2_aarch64.whl (11.3 MB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

container_structure_test-1.19.1-py3-none-manylinux_2_17_x86_64.whl (11.8 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

container_structure_test-1.19.1-py3-none-manylinux_2_17_aarch64.whl (11.3 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

container_structure_test-1.19.1-py3-none-macosx_11_0_arm64.whl (11.7 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

container_structure_test-1.19.1-py3-none-macosx_10_9_x86_64.whl (12.2 MB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

Details for the file container_structure_test-1.19.1-py3-none-win_arm64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 6b513a958b53a9b16f1e1d0967e188603377b6b37a55e95341dd8d81d9d96cff
MD5 1dc3e0cf171d22a39a3beac7b2e5692f
BLAKE2b-256 9d13ba275e7272a6dda5ef44f64b704509a04849f3c1061b6649c75648681103

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-win_arm64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file container_structure_test-1.19.1-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 21686f79c02db4cf376c43cb4d6eeda68efca655176ccfa5fc22c6229ea2e32b
MD5 16c2c01f4712157fbf217cf273cabbb3
BLAKE2b-256 f3efd3215d6ce4a0fcb203e51b4921f803f1777e4ab50e43bb9762343507d8f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-win_amd64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file container_structure_test-1.19.1-py3-none-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 29891fc5d12bb7d7093eb7f5ccf3468a0c98f129379faedcc1b05b5ba03a0399
MD5 580265f6cb16ae5131c4a7f76a95def7
BLAKE2b-256 6f43c4a2416888f6f127ebb8bceb70c8586c761bbf80d554a46a135a0523b8cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-musllinux_1_2_x86_64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file container_structure_test-1.19.1-py3-none-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a79873ba23e7859697fa1ce0c413bfbb1318f7755f149c1086429b9aee490da7
MD5 3f9a2ab4082fcd02cd25b741e3644221
BLAKE2b-256 8231cc279fb2cacf56e27194a521b53c26395257f1bc895416cbe7c38039d0b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-musllinux_1_2_aarch64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file container_structure_test-1.19.1-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 5eaab94043597ceac37a7acdcea76229a18b7911247678b1474528f06d4d5466
MD5 a3bdda4c4e46e8d639de368c32af5926
BLAKE2b-256 2793178c7f7d4533fd8f6854c06e5f4e24a57251addfeece684b71b827bf6427

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-manylinux_2_17_x86_64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file container_structure_test-1.19.1-py3-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 1add0a1d756a18775dc85083d89f9cb1e0a949162095eab821bfe0ba70c38edc
MD5 edd06689258807ea501047b807e06acc
BLAKE2b-256 3e7a6f189b044158d66fdb64d929e7932f3d4e18812f05d1ce517dec3ecda1a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-manylinux_2_17_aarch64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file container_structure_test-1.19.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9390e1f2c1cc5ddc9d3206a28ba53da62bc499ad8db5b01ff2f18e146c28e337
MD5 4044b8d73d4f190100fa7fa8e77ba6de
BLAKE2b-256 7f4c174020c592e2cc0acaf06f0f75042a644b98be775e5d25b43e1a7e36837c

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-macosx_11_0_arm64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file container_structure_test-1.19.1-py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for container_structure_test-1.19.1-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9988cbb1334348dc1dcd65a28f8cbfe76621449cf7bb76ddf69c3133f681a25a
MD5 bb06e60fe59ef3da60ef503de1334d9d
BLAKE2b-256 e06554dc790990346df9408277520a99737d743817da570f6a6f9761fe6304bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for container_structure_test-1.19.1-py3-none-macosx_10_9_x86_64.whl:

Publisher: main.yaml on FlavioAmurrioCS/container-structure-test

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.19.1 This release

8 files

1.19.0

8 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page