Skip to content

feat: add apko_lock_test to catch stale lock files under bazel test - #427

Open
officialasishkumar wants to merge 1 commit into
chainguard-dev:mainfrom
officialasishkumar:apko-lock-test
Open

officialasishkumar wants to merge 1 commit into
chainguard-dev:mainfrom
officialasishkumar:apko-lock-test

Conversation

@officialasishkumar

Copy link
Copy Markdown

Fixes #328.

apko build already rejects a lock file whose recorded config checksum no longer matches the config, but the drift only shows up when an image is built. This adds a first-class test target for it, so bazel test //... reports a stale lock without building images, fetching packages or touching the network.

This change:

  • apko/private/apko_lock_test.bzl: new apko_lock_test rule, exported from @rules_apko//apko:defs.bzl. It repeats apko's check. Like ImageConfiguration.parse, it hashes the bytes of the config followed by each file pulled in through include, in order, and compares sha256-<base64> with config.checksum in the lock file. Multi-file configs work through apko_config, the same way they do for apko_lock and apko_image.
  • apko/private/apko_lock.bzl: apko_lock gains an opt-in generate_test = False argument. When it's set, the macro also declares <name>_test for the lock file it writes, and the failure message names the apko_lock target to run. tags and visibility carry over to the test.
  • Toolchains: include is parsed with the yq toolchain and the hash is computed with the coreutils toolchain, both from aspect_bazel_lib, which is already a non-dev dependency. bzlmod registers both automatically. WORKSPACE users register them with register_yq_toolchains() / register_coreutils_toolchains(). The rule docs say this, and the e2e/smoke WORKSPACE snippet now does it.
  • Docs: regenerated docs/rules.md and added a short README paragraph.

A stale lock fails like this:

ERROR: multifile_config/apko.generated.lock.json is out of date with multifile_config/final_config: the lock file records config checksum sha256-vgNUWqDbYC0UoGQDHnd8qo3CVZAhNwSuH8xv9mZkrvQ=, but the config's checksum is sha256-NkUQ71ihYkOWe7wie7DLEaCbaEORrZIc7TN5xEcYNe8=.
Regenerate the lock file with `bazel run //multifile_config:lock_from_generated_config`.

A lock file with no recorded checksum also fails, because it can't be checked. apko only warns in that case.

Tests

  • apko/tests: lock_test checks an up-to-date lock whose config has an include. apko_lock_test_failure_test (an sh_test, which adds rules_shell 0.6.1 as a dev dependency; that version is already in the module graph) runs two manual apko_lock_test targets and asserts that each fails with the expected message. One target uses a lock made before an included file changed, and the other uses a lock with no checksum.
  • e2e/smoke: //:lock_test and //multifile_config:lock_from_generated_config_test. The second comes from generate_test = True on the existing apko_lock and runs through a two-level include chain of generated apko_config files.

Validation

  • The computed checksum matches the one apko recorded in every lock file in this repo: examples/*, e2e/smoke/apko.lock.json and the three-file multifile_config chain. For the new fixture I ran apko lock (v1.4.6) on apko/tests/testdata/apko.yaml, and it recorded the same checksum that is committed in testdata/apko.lock.json.
  • I appended a line to e2e/smoke/multifile_config/base_config.apko.yaml, two includes deep. lock_from_generated_config_test then failed with the message above, and it passed again after I reverted the line.
  • On Bazel 9.1.0 and 7.6.1 with bzlmod, //apko/..., //docs/... and the new e2e/smoke tests pass. //.github/workflows:buildifier.check is clean.
  • On Bazel 7.6.1 in WORKSPACE mode, the e2e/smoke lock tests pass, and the stale-include case fails as expected. This mode resolves the older aspect_bazel_lib 1.34.5 toolchains (uutils 0.0.16 and an older yq), and that run is why both files are read with explicit --input-format=yaml --output-format=yaml. For this run I had to change the e2e/smoke WORKSPACE's @bazel//tools/build_defs/repo:local.bzl load to @bazel_tools, because that label doesn't resolve on main. I didn't include that change here.

`apko build` rejects a lock file whose recorded config checksum no longer
matches the config, but that drift only surfaces when an image is
actually built. Repositories that gate freshness on `bazel test //...`
had no cheap way to notice it.

Add an `apko_lock_test` rule that repeats apko's check as a test. Like
apko, it hashes the bytes of the config followed by each file it pulls
in through `include`, and compares the result with `config.checksum` in
the lock file. It needs no image build, no packages and no network.
Includes are followed with the yq toolchain and hashed with the
coreutils toolchain from aspect_bazel_lib, both of which bzlmod
registers automatically. On failure the test prints both checksums and
the command that regenerates the lock file. A lock file without a
recorded checksum fails too, since it cannot be checked.

`apko_lock` gains an opt-in `generate_test` argument that declares a
`<name>_test` target for the lock file it writes, pointing users at
`bazel run <apko_lock target>` when the lock is stale.

Tests:
- apko/tests checks an up to date lock with an include chain, and an
  sh_test asserts that a lock made before an included file changed, and
  a lock without a checksum, both fail with actionable messages.
- e2e/smoke exercises the rule from a consuming module, including
  `generate_test` on the multi-file `apko_config` example. The smoke
  WORKSPACE registers the yq and coreutils toolchains for WORKSPACE
  users.

Fixes chainguard-dev#328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FR: optionally auto-generate a bazel test target for apko lock staleness

1 participant