Skip to content

fix(scripts): generate the tofu test inside the module directory - #27

Merged
agustincelentano merged 1 commit into
mainfrom
fix/setup-layer-tftest-location
Sep 10, 2026
Merged

fix(scripts): generate the tofu test inside the module directory#27
agustincelentano merged 1 commit into
mainfrom
fix/setup-layer-tftest-location

Conversation

@agustincelentano

Copy link
Copy Markdown
Contributor

Problem

setup-layer writes the generated .tftest.hcl to deployment/tests/{type}/{name}/, but run_tofu_tests.sh runs tofu test from whichever directory holds the file:

find . -name "*.tftest.hcl" | xargs -I{} dirname {} | sort -u

That directory has no .tf files in it, so the generated test runs against an empty module and asserts nothing. All seven tofu tests that exist today live in {type}/{name}/modules/, next to the code they exercise.

You hit this the first time you scaffold a new layer, which makes it easy to ship a layer that looks tested and is not.

Change

  • Write the generated .tftest.hcl to modules/ instead. The tests/{type}/{name}/ directory is still created, since that is where the BATS unit test for the setup script belongs.
  • Update the usage docstring and the summary output so both reflect where the files actually land.

Regression coverage

Two new BATS files, 12 tests, all passing.

tests/scripts/setup-layer_test.bats covers the generator itself: test placement, module scaffolding, name normalization, and argument handling. It runs the script inside a temp directory, so it writes nothing into the repo.

tests/specs/layer_selection_test.bats checks the scope-configuration schema against the layer tree. layer_executor resolves an implementation by joining the configured value straight onto a path:

setup_script="$SERVICE_PATH/deployment/${LAYER_TYPE}/${layer_value}/setup"

So every value the schema offers must exist as a directory with that exact name. A mismatch is invisible until the first deployment fails with Unknown implementation. This is the class of bug that let the azure_distribution enum drift to blob_cdn while the directory was blob-cdn, fixed separately in 895246e.

Verified the check actually fails on that case by reintroducing it temporarily:

Schema offers implementations that layer_executor cannot resolve:
  distribution/blob_cdn -> directory not found
Each value must match a directory under deployment/<layer>/ exactly.

Testing

./testing/run_bats_tests.sh static-files
165 tests, 0 failures    # static-files/deployment/tests
19 tests, 0 failures     # static-files/utils/tests

Follow-up, not in this PR

The generated boilerplate uses mock_provider "${LAYER_NAME}", which is only right by coincidence. The cloudfront layer mocks aws, and blob-cdn mocks azurerm. Worth turning into a TODO placeholder so nobody copies it as-is.

🤖 Generated with Claude Code

setup-layer wrote the generated .tftest.hcl to
deployment/tests/{type}/{name}/, but run_tofu_tests.sh runs `tofu test`
from whichever directory holds the file. That directory contains no .tf
files, so the generated test ran against an empty module and asserted
nothing. All seven existing tofu tests live in {type}/{name}/modules/.

Write it to modules/ instead, and keep creating the tests/ directory for
the BATS unit tests that belong there.

Also add a consistency check between the scope-configuration schema and
the layer tree. layer_executor resolves an implementation by joining the
configured value straight onto deployment/{type}/{value}/setup, so any
value the schema offers that has no matching directory fails only at the
first deployment. This is the class of bug that let the azure_distribution
enum drift to blob_cdn while the directory was blob-cdn.
@agustincelentano
agustincelentano merged commit ca4edd7 into main Sep 10, 2026
5 checks passed
@agustincelentano
agustincelentano deleted the fix/setup-layer-tftest-location branch September 10, 2026 18:58
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.

2 participants