-
Notifications
You must be signed in to change notification settings - Fork 0
feat(testing): poc, use bats #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3d4c270
chore: update underlying dependencies
westonplatter 1a506d4
feat: rename the folder, terraform -> tf
westonplatter b971ea5
feat: allow users to use opentofu or terraform
westonplatter e0cd7d5
Merge branch 'main' into feat/tf-to-use-terraform-opentofu
westonplatter ed45202
fix: remove cursor after we removed the directory
westonplatter 4009938
fix: trim down tf plan tests
westonplatter 7dcd93b
Merge branch 'main' into feat/tests
westonplatter 1b7628c
fix: update tests/README.md to use aqua
westonplatter a1895e4
fix: update versions and practice good hygene
westonplatter dd47e87
fix: remove uploading results
westonplatter 98996bc
fix: commit pin more GHAs
westonplatter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: {} | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| permissions: | ||
| actions: read | ||
| checks: write | ||
| contents: read | ||
| pull-requests: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Check out Git repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Install BATS | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y bats | ||
|
|
||
| - name: Install Task | ||
| uses: arduino/setup-task@v2 | ||
| with: | ||
| version: 3.x | ||
| repo-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Run BATS tests | ||
| run: bats tests/ | ||
|
|
||
| - name: Upload test results | ||
| if: always() | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: test-results | ||
| path: tests/ | ||
| retention-days: 30 | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| --- | ||
| registries: | ||
| - type: standard | ||
| ref: v4.78.0 # renovate: depName=aquaproj/aqua-registry | ||
| ref: v4.433.0 # renovate: depName=aquaproj/aqua-registry | ||
| packages: | ||
| - name: go-task/task@v3.31.0 | ||
| - name: TomWright/dasel@v2.4.1 | ||
| - name: stedolan/jq@jq-1.7.1 | ||
| - name: go-task/task@v3.45.4 | ||
| - name: TomWright/dasel@v2.8.1 | ||
| - name: stedolan/jq@jq-1.8.1 | ||
| tags: [cursor] | ||
| - name: cli/cli@v2.74.2 | ||
| - name: cli/cli@v2.83.0 | ||
| tags: [gh] |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Taskit Test Suite | ||
|
|
||
| ## Purpose | ||
|
|
||
| This directory contains the BATS (Bash Automated Testing System) test suite for Taskit. Tests validate Task command functionality using dry-run mode, ensuring reliability without requiring actual tool installations. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Install BATS: | ||
|
|
||
| ```bash | ||
| # macOS | ||
| brew install bats-core | ||
|
|
||
| # Debian/Ubuntu | ||
| apt-get install bats | ||
|
|
||
| # Manual installation | ||
| git clone https://github.com/bats-core/bats-core.git | ||
| cd bats-core | ||
| ./install.sh /usr/local | ||
| ``` | ||
|
westonplatter marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Running Tests | ||
|
|
||
| ### Local Development | ||
|
|
||
| ```bash | ||
| # Run all tests | ||
| bats tests/ | ||
|
|
||
| # Run specific test file | ||
| bats tests/terraform_plan.bats | ||
|
|
||
| # Run tests by tag | ||
| bats --filter-tags basic tests/ | ||
| bats --filter-tags precondition tests/ | ||
| ``` | ||
|
|
||
| ### Continuous Integration | ||
|
|
||
| Tests run automatically on: | ||
|
|
||
| - **Pull Requests**: All PRs trigger the test suite | ||
| - **Main Branch**: Tests run on every push to main | ||
|
|
||
| The GitHub Actions workflow (`.github/workflows/test.yaml`) handles: | ||
|
|
||
| 1. BATS and Task installation | ||
| 2. Test execution across the `tests/` directory | ||
| 3. Result artifacts upload (30-day retention) | ||
|
|
||
| View results in the GitHub Actions tab or PR checks section. | ||
|
|
||
| ## Writing Tests | ||
|
|
||
| Tests leverage Task's dry-run mode (`task -v -n`) to verify command construction without execution: | ||
|
|
||
| ```bash | ||
| @test "terraform:plan validates environment argument" { | ||
| # Arrange: Create test fixture | ||
| touch "tfvars/test-env.tfvars" | ||
|
|
||
| # Act: Execute task in dry-run mode | ||
| run task -v -n terraform:plan -- test-env | ||
|
|
||
| # Assert: Verify output | ||
| [ "$status" -eq 0 ] | ||
| [[ "$output" =~ "expected text" ]] | ||
| } | ||
| ``` | ||
|
|
||
| ### Test Structure | ||
|
|
||
| - `setup()`: Pre-test fixture creation and environment setup | ||
| - `@test "description"`: Individual test case | ||
| - `teardown()`: Post-test cleanup | ||
|
|
||
| ### Test Tags | ||
|
|
||
| Use tags for selective test execution: | ||
|
|
||
| - `terraform`: Terraform-related functionality | ||
| - `plan`: Plan command behavior | ||
| - `basic`: Core functionality | ||
| - `args`: Argument handling | ||
| - `precondition`: Validation logic | ||
| - `config`: Configuration management | ||
| - `error`: Error handling | ||
|
|
||
| ## Best Practices | ||
|
|
||
| 1. **Dry-run Testing**: Use `task -v -n` to test without requiring actual tool installations | ||
| 2. **Debug Output**: Include `echo "Output: $output" >&3` for troubleshooting failed tests | ||
| 3. **Fixture Management**: Create temporary files in `setup()`, clean up in `teardown()` | ||
| 4. **Assertions**: Use `[ ]` for exit codes, `[[ =~ ]]` for pattern matching | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| **Tests fail in CI but pass locally:** | ||
|
|
||
| - Ensure proper cleanup in `teardown()` | ||
| - Verify tests don't depend on local environment state | ||
| - Confirm all required files are committed | ||
|
|
||
| **Workflow doesn't run:** | ||
|
|
||
| - Verify `.github/workflows/test.yaml` exists | ||
| - Check GitHub Actions are enabled for the repository | ||
| - Review branch protection rules | ||
|
|
||
| ## Workflow Permissions | ||
|
|
||
| The CI workflow uses minimal permissions: | ||
|
|
||
| - `actions: read` - Access workflow logs | ||
| - `checks: write` - Report check results | ||
| - `contents: read` - Read repository | ||
| - `pull-requests: read` - Access PR information | ||
|
|
||
| No secrets or credentials required for test execution. | ||
|
|
||
| ## Resources | ||
|
|
||
| - [BATS Documentation](https://bats-core.readthedocs.io/en/stable/) | ||
| - [Writing BATS Tests](https://bats-core.readthedocs.io/en/stable/writing-tests.html) | ||
| - [Task Documentation](https://taskfile.dev/) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env bats | ||
| # Test suite for terraform:plan task command | ||
|
|
||
| setup() { | ||
| cd "$(dirname "$BATS_TEST_FILENAME")/.." | ||
| export TEST_WORKSPACE="test-env" | ||
| mkdir -p tfvars backend-configurations | ||
| touch "tfvars/${TEST_WORKSPACE}.tfvars" | ||
| touch "backend-configurations/${TEST_WORKSPACE}.backend.tf" | ||
| } | ||
|
|
||
| teardown() { | ||
| rm -rf tfvars backend-configurations | ||
| } | ||
|
|
||
| # bats test_tags=terraform,plan,basic | ||
| @test "terraform:plan generates correct command with workspace" { | ||
| run task -v -n tf:plan -- "$TEST_WORKSPACE" | ||
|
|
||
| echo "Output: $output" >&3 | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [[ "$output" =~ terraform.*workspace.*select.*-or-create.*${TEST_WORKSPACE} ]] | ||
| [[ "$output" =~ terraform.*plan.*-var-file.*tfvars/${TEST_WORKSPACE}.tfvars ]] | ||
| } | ||
|
|
||
| # bats test_tags=terraform,plan,args | ||
| @test "terraform:plan passes additional arguments to terraform" { | ||
| run task -v -n tf:plan -- "$TEST_WORKSPACE" -out=tfplan.out -lock=false | ||
|
|
||
| echo "Output: $output" >&3 | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [[ "$output" =~ -out=tfplan.out ]] | ||
| [[ "$output" =~ -lock=false ]] | ||
| [[ "$output" =~ -var-file.*tfvars/${TEST_WORKSPACE}.tfvars ]] | ||
| } | ||
|
|
||
| # bats test_tags=terraform,plan,precondition | ||
| @test "terraform:plan fails when tfvars file does not exist" { | ||
| rm -f "tfvars/${TEST_WORKSPACE}.tfvars" | ||
|
|
||
| run task -v -n tf:plan -- "$TEST_WORKSPACE" | ||
|
|
||
| echo "Output: $output" >&3 | ||
|
|
||
| [ "$status" -ne 0 ] | ||
| [[ "$output" =~ "Variables file does not exist" ]] | ||
| } | ||
|
|
||
| # bats test_tags=terraform,plan,config | ||
| @test "terraform:plan uses terraform when USE_TERRAFORM=true" { | ||
| run env USE_TERRAFORM=true task -v -n tf:plan -- "$TEST_WORKSPACE" | ||
|
|
||
| echo "Output: $output" >&3 | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [[ "$output" =~ \[tf:plan\]\ terraform\ workspace\ select ]] | ||
| [[ "$output" =~ \[tf:plan\]\ terraform\ plan ]] | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.