Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Testing and Security

Testing the Images You Build

Test Packer images before release: provisioner-time checks, InSpec and Goss specs, boot smoke tests, test instances, a test pyramid for images, and failing the build on bad results.

A golden image is a dependency for every server that boots from it, so a broken image breaks many things at once. Yet teams often test images only by launching one and "looking around". This lesson shows how to turn that into automated tests at three levels: inside the build, against a booted instance and in the surrounding pipeline.

Packer, InSpec and Goss cannot run on this site. Config snippets are read-only; the runnable examples are JavaScript stand-ins of a spec runner so you can practise writing expectations. Nothing shows real tool output.

What to test

Think of an image test pyramid:

  1. Build-time assertions (fast, inside the build): the packages you expect are installed, the version file exists, the service is enabled, unwanted users are absent.
  2. Boot tests (minutes): launch an instance from the image and check it boots, networking works, the service answers, the agent registers.
  3. Integration tests (slowest): the image works with your real infrastructure code, load balancers, configuration management and application release.

Most failures are caught by the cheap level; reserve the expensive tests for a few essential flows.

Build-time assertions with provisioners

A final shell provisioner can verify things and fail the build with a non-zero exit code. A failed provisioner means no artifact is published.

HCL
provisioner "shell" {
  inline = [
    "set -eu",
    "nginx -v",
    "systemctl is-enabled nginx",
    "test -f /etc/image-release",
    "! getent passwd packer-temp"
  ]
}

Each command exits non-zero on failure, and set -e stops at the first one. This is a surprisingly effective test suite for little effort.

Spec tools

  • InSpec (Chef): a Ruby DSL with controls, profiles and compliance content (CIS and STIG baselines). Runs over SSH or locally; Packer community plugins and a shell-local step can run it against the builder instance.
  • Goss: a YAML-based checker, a single static binary, very fast; ideal for "is the package/port/service/file as expected".
  • Serverspec / Testinfra: Ruby and Python frameworks for server state.
  • Ansible assert tasks if you already provision with Ansible.

A Goss spec looks like this:

YAML
package:
  nginx:
    installed: true
service:
  nginx:
    enabled: true
    running: false
file:
  /etc/image-release:
    exists: true
    mode: "0644"
user:
  packer-temp:
    exists: false

Upload the binary and spec with file provisioners, run it with shell, and remove them afterwards so the image does not contain test tools (or keep them deliberately for later health checks).

Running tests against a booted instance

A build-time test sees the machine before shutdown and snapshot. It cannot prove that the image boots, because a launched instance goes through the cloud's init, new hostnames and fresh network. A boot test is a separate pipeline job:

  1. Read the new image id from the manifest.
  2. Launch a short-lived instance in an isolated subnet (Terraform, the cloud CLI or a test framework).
  3. Wait for a health signal: SSH, an HTTP endpoint, a cloud-init completion marker.
  4. Run the spec (InSpec with --target ssh://..., or Goss over a remote shell).
  5. Terminate the instance, always, even when tests fail.
  6. Only on success, promote the image (versioning and promotion).

Add a time budget; an image that takes twenty minutes to be healthy is a defect too.

Good test habits

  • Test behavior and security posture, not implementation details like package file paths.
  • Keep specs in the same repository as the template and run them in the same pull request.
  • Make tests fast and deterministic; avoid depending on the public internet where you can.
  • Fail loudly with readable messages, and attach reports as pipeline artifacts.
  • Test the negative: no SSH password login, no leftover temp users or keys, no build logs in /root.

Try it: a tiny spec runner

JS Goss-style spec runner (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: a boot-test wait loop

JS Wait for health with a time budget (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a build-time test not enough?

    Boot behavior (init, hostname, networking) only appears when an instance is launched from the image.
  2. 2. How does a provisioner make a build fail on a bad check?

    Packer stops the build when a provisioner returns a non-zero exit code.
  3. 3. Which tool uses a YAML spec and a single binary for quick state checks?

    Goss is YAML-based; InSpec uses a Ruby DSL.
  4. 4. What must always happen to a boot-test instance?

    Always clean up, otherwise failed pipelines pile up costs and attack surface.

Summary

  • Test at three levels: build-time assertions, boot tests, integration tests.
  • A failing provisioner blocks publication; use it for cheap checks.
  • Goss and InSpec express expectations declaratively; keep specs in the same repository.
  • Boot tests launch a short-lived instance, check health with a time budget and always clean up.

Next: Hardening and securing images