Skip to content

Fix | tf: init --layers backend key per layer, validate-layout exit code + error path tests - #385

Merged
diego-ojeda-binbash merged 1 commit into
masterfrom
enhancement/tf-error-path-tests
Nov 20, 2026
Merged

diego-ojeda-binbash merged 1 commit into
masterfrom
enhancement/tf-error-path-tests

Conversation

@diego-ojeda-binbash

@diego-ojeda-binbash diego-ojeda-binbash commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What?

  • init --layers backend key (leverage/modules/tf.py): when a layer's config.tf has no key, init writes one. It was built from cwd, not the layer. Run from the account or a layers-group directory with --layers (and without --skip-validation), every new layer got the same key, e.g. account/us-east-1/terraform.tfstate for both security-base and base-tf-backend, so they would share one state. The key now follows each layer's path. The bug dates from Issue 141 s3 key multi region support #149.
  • validate-layout exit code: it exited 0 even when a check printed ✘ FAILED, since click drops the command's return value. It now exits 1, so it can be used in scripts and CI.
  • Tests (tests/test_modules/test_tf.py, 19 new):
    • --layers errors: inside a layer, missing flag, outside an account, a layer that doesn't exist. None runs the binary.
    • init stops on an invalid layout before running anything.
    • The backend key init writes, from a layer and with --layers.
    • _make_layer_backend_key: region, global, base-/tools- prefixes, tf-↔terraform-, -dr suffix.
    • validate-layout: ok, no environment, wrong backend key, wrong backend.tfvars, account directory name mismatch.

Why?

H7 in #346: CliRunner tests for tf, always asserting exit_code. Exit code propagation and --layers ordering were already covered; this adds the error paths. tf.py coverage goes from 87% to 99% (only refresh-credentials is left; auth has its own tests).

Run against master's tf.py, 3 of the new tests fail: the two bugs above.

Checks

  • Unit tests: 354 passed
  • ruff, black, mypy: clean

References

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • validate-layout now exits with a failure status when validation fails, so failed checks are reported correctly.
    • Multi-layer runs now generate backend state keys using each layer’s location within the project, keeping key generation consistent across invocation directories.

…ode + error path tests

- init --layers, from the account or a layers-group directory and without
  --skip-validation, wrote the same backend key to every new layer, built from
  cwd instead of the layer (e.g. account/us-east-1/terraform.tfstate), so the
  layers would share one state. The key now follows each layer's path.
- validate-layout exited 0 even when a check failed: click drops the command's
  return value. It now exits 1.
- Tests for the --layers errors, init on an invalid layout, the backend key that
  init writes, _make_layer_backend_key and validate-layout. tf.py coverage goes
  from 87% to 99%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Terraform module now generates multi-layer backend state keys from each layer’s project-relative path. It also exits with status 1 when layout validation fails. Tests cover these behaviors and related layer-selection cases.

Changes

Terraform module behavior

Layer / File(s) Summary
Multi-layer backend state keys
leverage/modules/tf.py, tests/test_modules/test_tf.py
Backend state keys use each layer’s path relative to the project root. Tests cover layer selection errors, key generation, and insertion into layer configuration.
Layout validation exit status
leverage/modules/tf.py, tests/test_modules/test_tf.py
validate-layout exits with status 1 when validation fails. Tests cover validation outcomes and verify that init does not run Terraform after a validation failure.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

  • Issue 346: This PR adds coverage for some --layers and layout-validation paths, but it does not cover the plan, apply, or destroy paths mentioned in the issue.

Possibly related PRs

  • binbashar/leverage#336: Both PRs change exit-code handling in leverage/modules/tf.py; this PR addresses the validate-layout failure case that PR 336 left out of scope.

Suggested reviewers: angelofenoglio, juanmatias

Merge Risk: Minimal · up to 37758

The change fixes shared backend state keys for layers initialized from an account or layers-group directory. It also makes validate-layout report failure with a nonzero exit code. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two main changes: the per-layer backend key fix for init --layers and the validate-layout exit code fix, plus the added error path tests. The changeset matches both. The title uses…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two main changes: the per-layer backend key fix for init --layers and the validate-layout exit code fix, plus the added error path tests. The changeset matches both. The title uses a 'Fix | tf:' prefix and is long at 91 characters, but it stays specific and on topic.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the layer path,
And plants each state key true.
When layout checks return a fault,
The exit code says so too.
Then hops through tests anew.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bf2808ef-7727-4b69-8634-4ea48e227578
Commits

Reviewing files that changed from the base of the PR and between 2d36686 and 37758bd.

Files selected for processing: 2

  • leverage/modules/tf.py
  • tests/test_modules/test_tf.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@diego-ojeda-binbash diego-ojeda-binbash self-assigned this Nov 19, 2026
@diego-ojeda-binbash

Copy link
Copy Markdown
Contributor Author

@claude review

@coveralls

coveralls commented Nov 19, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 44634027715

Coverage increased (+1.9%) to 79.947%

Details

  • Coverage increased (+1.9%) from the base build.
  • Patch coverage: 3 of 3 lines across 1 file are fully covered (100%).
  • 1 coverage regression across 1 file.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

1 previously-covered line in 1 file lost coverage.

File Lines Losing Coverage Coverage
modules/tf.py 1 99.71%

Coverage Stats

Coverage Status
Relevant Lines: 4144
Covered Lines: 3412
Line Coverage: 82.34%
Relevant Branches: 1092
Covered Branches: 774
Branch Coverage: 70.88%
Branches in Coverage %: Yes
Coverage Strength: 0.82 hits per line

💛 - Coveralls

@diego-ojeda-binbash

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@diego-ojeda-binbash
diego-ojeda-binbash merged commit 09e5b7a into master Nov 20, 2026
28 checks passed
@diego-ojeda-binbash
diego-ojeda-binbash deleted the enhancement/tf-error-path-tests branch November 20, 2026 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants