Repository navigation
feat: add apko_lock_test to catch stale lock files under bazel test - #427
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #328.
apko buildalready 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, sobazel test //...reports a stale lock without building images, fetching packages or touching the network.This change:
apko/private/apko_lock_test.bzl: newapko_lock_testrule, exported from@rules_apko//apko:defs.bzl. It repeats apko's check. LikeImageConfiguration.parse, it hashes the bytes of the config followed by each file pulled in throughinclude, in order, and comparessha256-<base64>withconfig.checksumin the lock file. Multi-file configs work throughapko_config, the same way they do forapko_lockandapko_image.apko/private/apko_lock.bzl:apko_lockgains an opt-ingenerate_test = Falseargument. When it's set, the macro also declares<name>_testfor the lock file it writes, and the failure message names theapko_locktarget to run.tagsandvisibilitycarry over to the test.includeis parsed with theyqtoolchain and the hash is computed with thecoreutilstoolchain, both fromaspect_bazel_lib, which is already a non-dev dependency. bzlmod registers both automatically. WORKSPACE users register them withregister_yq_toolchains()/register_coreutils_toolchains(). The rule docs say this, and the e2e/smoke WORKSPACE snippet now does it.docs/rules.mdand added a short README paragraph.A stale lock fails like this:
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_testchecks an up-to-date lock whose config has an include.apko_lock_test_failure_test(ansh_test, which addsrules_shell0.6.1 as a dev dependency; that version is already in the module graph) runs twomanualapko_lock_testtargets 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_testand//multifile_config:lock_from_generated_config_test. The second comes fromgenerate_test = Trueon the existingapko_lockand runs through a two-level include chain of generatedapko_configfiles.Validation
examples/*,e2e/smoke/apko.lock.jsonand the three-filemultifile_configchain. For the new fixture I ranapko lock(v1.4.6) onapko/tests/testdata/apko.yaml, and it recorded the same checksum that is committed intestdata/apko.lock.json.e2e/smoke/multifile_config/base_config.apko.yaml, two includes deep.lock_from_generated_config_testthen failed with the message above, and it passed again after I reverted the line.//apko/...,//docs/...and the new e2e/smoke tests pass.//.github/workflows:buildifier.checkis clean.--input-format=yaml --output-format=yaml. For this run I had to change the e2e/smoke WORKSPACE's@bazel//tools/build_defs/repo:local.bzlload to@bazel_tools, because that label doesn't resolve on main. I didn't include that change here.