Skip to content

Commit 4009938

Browse files
committed
fix: trim down tf plan tests
1 parent ed45202 commit 4009938

3 files changed

Lines changed: 229 additions & 0 deletions

File tree

.github/workflows/test.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Test
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
branches:
7+
- main
8+
9+
permissions:
10+
actions: read
11+
checks: write
12+
contents: read
13+
pull-requests: read
14+
15+
jobs:
16+
test:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Check out Git repository
20+
uses: actions/checkout@v4
21+
22+
- name: Install BATS
23+
run: |
24+
sudo apt-get update
25+
sudo apt-get install -y bats
26+
27+
- name: Install Task
28+
uses: arduino/setup-task@v2
29+
with:
30+
version: 3.x
31+
repo-token: ${{ secrets.GITHUB_TOKEN }}
32+
33+
- name: Run BATS tests
34+
run: bats tests/
35+
36+
- name: Upload test results
37+
if: always()
38+
uses: actions/upload-artifact@v4
39+
with:
40+
name: test-results
41+
path: tests/
42+
retention-days: 30

tests/README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Taskit Test Suite
2+
3+
## Purpose
4+
5+
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.
6+
7+
## Prerequisites
8+
9+
Install BATS:
10+
11+
```bash
12+
# macOS
13+
brew install bats-core
14+
15+
# Debian/Ubuntu
16+
apt-get install bats
17+
18+
# Manual installation
19+
git clone https://github.com/bats-core/bats-core.git
20+
cd bats-core
21+
./install.sh /usr/local
22+
```
23+
24+
## Running Tests
25+
26+
### Local Development
27+
28+
```bash
29+
# Run all tests
30+
bats tests/
31+
32+
# Run specific test file
33+
bats tests/terraform_plan.bats
34+
35+
# Run tests by tag
36+
bats --filter-tags basic tests/
37+
bats --filter-tags precondition tests/
38+
```
39+
40+
### Continuous Integration
41+
42+
Tests run automatically on:
43+
44+
- **Pull Requests**: All PRs trigger the test suite
45+
- **Main Branch**: Tests run on every push to main
46+
47+
The GitHub Actions workflow (`.github/workflows/test.yaml`) handles:
48+
49+
1. BATS and Task installation
50+
2. Test execution across the `tests/` directory
51+
3. Result artifacts upload (30-day retention)
52+
53+
View results in the GitHub Actions tab or PR checks section.
54+
55+
## Writing Tests
56+
57+
Tests leverage Task's dry-run mode (`task -v -n`) to verify command construction without execution:
58+
59+
```bash
60+
@test "terraform:plan validates environment argument" {
61+
# Arrange: Create test fixture
62+
touch "tfvars/test-env.tfvars"
63+
64+
# Act: Execute task in dry-run mode
65+
run task -v -n terraform:plan -- test-env
66+
67+
# Assert: Verify output
68+
[ "$status" -eq 0 ]
69+
[[ "$output" =~ "expected text" ]]
70+
}
71+
```
72+
73+
### Test Structure
74+
75+
- `setup()`: Pre-test fixture creation and environment setup
76+
- `@test "description"`: Individual test case
77+
- `teardown()`: Post-test cleanup
78+
79+
### Test Tags
80+
81+
Use tags for selective test execution:
82+
83+
- `terraform`: Terraform-related functionality
84+
- `plan`: Plan command behavior
85+
- `basic`: Core functionality
86+
- `args`: Argument handling
87+
- `precondition`: Validation logic
88+
- `config`: Configuration management
89+
- `error`: Error handling
90+
91+
## Best Practices
92+
93+
1. **Dry-run Testing**: Use `task -v -n` to test without requiring actual tool installations
94+
2. **Debug Output**: Include `echo "Output: $output" >&3` for troubleshooting failed tests
95+
3. **Fixture Management**: Create temporary files in `setup()`, clean up in `teardown()`
96+
4. **Assertions**: Use `[ ]` for exit codes, `[[ =~ ]]` for pattern matching
97+
98+
## Troubleshooting
99+
100+
**Tests fail in CI but pass locally:**
101+
102+
- Ensure proper cleanup in `teardown()`
103+
- Verify tests don't depend on local environment state
104+
- Confirm all required files are committed
105+
106+
**Workflow doesn't run:**
107+
108+
- Verify `.github/workflows/test.yaml` exists
109+
- Check GitHub Actions are enabled for the repository
110+
- Review branch protection rules
111+
112+
## Workflow Permissions
113+
114+
The CI workflow uses minimal permissions:
115+
116+
- `actions: read` - Access workflow logs
117+
- `checks: write` - Report check results
118+
- `contents: read` - Read repository
119+
- `pull-requests: read` - Access PR information
120+
121+
No secrets or credentials required for test execution.
122+
123+
## Resources
124+
125+
- [BATS Documentation](https://bats-core.readthedocs.io/en/stable/)
126+
- [Writing BATS Tests](https://bats-core.readthedocs.io/en/stable/writing-tests.html)
127+
- [Task Documentation](https://taskfile.dev/)

tests/terraform_plan.bats

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bats
2+
# Test suite for terraform:plan task command
3+
4+
setup() {
5+
cd "$(dirname "$BATS_TEST_FILENAME")/.."
6+
export TEST_WORKSPACE="test-env"
7+
mkdir -p tfvars backend-configurations
8+
touch "tfvars/${TEST_WORKSPACE}.tfvars"
9+
touch "backend-configurations/${TEST_WORKSPACE}.backend.tf"
10+
}
11+
12+
teardown() {
13+
rm -rf tfvars backend-configurations
14+
}
15+
16+
# bats test_tags=terraform,plan,basic
17+
@test "terraform:plan generates correct command with workspace" {
18+
run task -v -n tf:plan -- "$TEST_WORKSPACE"
19+
20+
echo "Output: $output" >&3
21+
22+
[ "$status" -eq 0 ]
23+
[[ "$output" =~ terraform.*workspace.*select.*-or-create.*${TEST_WORKSPACE} ]]
24+
[[ "$output" =~ terraform.*plan.*-var-file.*tfvars/${TEST_WORKSPACE}.tfvars ]]
25+
}
26+
27+
# bats test_tags=terraform,plan,args
28+
@test "terraform:plan passes additional arguments to terraform" {
29+
run task -v -n tf:plan -- "$TEST_WORKSPACE" -out=tfplan.out -lock=false
30+
31+
echo "Output: $output" >&3
32+
33+
[ "$status" -eq 0 ]
34+
[[ "$output" =~ -out=tfplan.out ]]
35+
[[ "$output" =~ -lock=false ]]
36+
[[ "$output" =~ -var-file.*tfvars/${TEST_WORKSPACE}.tfvars ]]
37+
}
38+
39+
# bats test_tags=terraform,plan,precondition
40+
@test "terraform:plan fails when tfvars file does not exist" {
41+
rm -f "tfvars/${TEST_WORKSPACE}.tfvars"
42+
43+
run task -v -n tf:plan -- "$TEST_WORKSPACE"
44+
45+
echo "Output: $output" >&3
46+
47+
[ "$status" -ne 0 ]
48+
[[ "$output" =~ "Variables file does not exist" ]]
49+
}
50+
51+
# bats test_tags=terraform,plan,config
52+
@test "terraform:plan uses terraform when USE_TERRAFORM=true" {
53+
run env USE_TERRAFORM=true task -v -n tf:plan -- "$TEST_WORKSPACE"
54+
55+
echo "Output: $output" >&3
56+
57+
[ "$status" -eq 0 ]
58+
[[ "$output" =~ \[tf:plan\]\ terraform\ workspace\ select ]]
59+
[[ "$output" =~ \[tf:plan\]\ terraform\ plan ]]
60+
}

0 commit comments

Comments
 (0)