|
| 1 | +# Testing Guide for Organizations Layer |
| 2 | + |
| 3 | +This guide explains how to test your Terraform and Terragrunt configurations in the organizations layer before committing changes. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Prerequisites](#prerequisites) |
| 8 | +- [Running Tests](#running-tests) |
| 9 | +- [Format Checking](#format-checking) |
| 10 | +- [Pre-commit Hooks](#pre-commit-hooks) |
| 11 | +- [Validation](#validation) |
| 12 | +- [Security Scanning](#security-scanning) |
| 13 | +- [Example Configurations](#example-configurations) |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Before running tests, ensure you have the following installed: |
| 18 | + |
| 19 | +- **Terraform** (v1.7.5 or later): [Installation Guide](https://developer.hashicorp.com/terraform/downloads) |
| 20 | +- **Terragrunt** (v0.55.18 or later): [Installation Guide](https://terragrunt.gruntwork.io/docs/getting-started/install/) |
| 21 | +- **TFLint** (optional but recommended): [Installation Guide](https://github.com/terraform-linters/tflint) |
| 22 | +- **pre-commit** (optional but recommended): [Installation Guide](https://pre-commit.com/#install) |
| 23 | + |
| 24 | +## Running Tests |
| 25 | + |
| 26 | +### Terraform Native Tests |
| 27 | + |
| 28 | +The organizations layer includes a comprehensive test file (`main.tftest.hcl`) that validates: |
| 29 | +- Organization settings configuration |
| 30 | +- Repository configuration |
| 31 | +- Team configuration |
| 32 | +- Security settings |
| 33 | +- Naming conventions |
| 34 | +- Email format validation |
| 35 | + |
| 36 | +To run the tests: |
| 37 | + |
| 38 | +```bash |
| 39 | +cd organizations/ |
| 40 | +terraform init |
| 41 | +terraform test -verbose |
| 42 | +``` |
| 43 | + |
| 44 | +The test output will show: |
| 45 | +- ✅ Passed tests (all assertions successful) |
| 46 | +- ❌ Failed tests (with specific error messages) |
| 47 | + |
| 48 | +### Example Test Output |
| 49 | + |
| 50 | +``` |
| 51 | +main.tftest.hcl... in progress |
| 52 | + run "organization_settings_validation"... pass |
| 53 | + run "repository_configuration_validation"... pass |
| 54 | + run "team_configuration_validation"... pass |
| 55 | + run "security_settings_validation"... pass |
| 56 | + run "naming_conventions_validation"... pass |
| 57 | + run "email_format_validation"... pass |
| 58 | +main.tftest.hcl... tearing down |
| 59 | +main.tftest.hcl... pass |
| 60 | +``` |
| 61 | + |
| 62 | +## Format Checking |
| 63 | + |
| 64 | +### Check Terraform Formatting |
| 65 | + |
| 66 | +Check if your Terraform files are properly formatted: |
| 67 | + |
| 68 | +```bash |
| 69 | +cd organizations/ |
| 70 | +terraform fmt -check -diff -recursive |
| 71 | +``` |
| 72 | + |
| 73 | +To automatically format files: |
| 74 | + |
| 75 | +```bash |
| 76 | +terraform fmt -recursive |
| 77 | +``` |
| 78 | + |
| 79 | +### Check Terragrunt Formatting |
| 80 | + |
| 81 | +Check Terragrunt HCL files: |
| 82 | + |
| 83 | +```bash |
| 84 | +cd organizations/ |
| 85 | +terragrunt hclfmt --terragrunt-check --terragrunt-diff |
| 86 | +``` |
| 87 | + |
| 88 | +To automatically format Terragrunt files: |
| 89 | + |
| 90 | +```bash |
| 91 | +terragrunt hclfmt |
| 92 | +``` |
| 93 | + |
| 94 | +## Pre-commit Hooks |
| 95 | + |
| 96 | +This repository includes pre-commit hooks that automatically run checks before each commit. |
| 97 | + |
| 98 | +### Setup Pre-commit Hooks |
| 99 | + |
| 100 | +1. Install pre-commit: |
| 101 | + ```bash |
| 102 | + pip install pre-commit |
| 103 | + # or |
| 104 | + brew install pre-commit |
| 105 | + ``` |
| 106 | + |
| 107 | +2. Install the hooks: |
| 108 | + ```bash |
| 109 | + cd /home/runner/work/github-foundations/github-foundations |
| 110 | + pre-commit install |
| 111 | + ``` |
| 112 | + |
| 113 | +3. (Optional) Run hooks manually: |
| 114 | + ```bash |
| 115 | + pre-commit run --all-files |
| 116 | + ``` |
| 117 | + |
| 118 | +### Included Hooks |
| 119 | + |
| 120 | +The pre-commit configuration includes: |
| 121 | +- **terraform_fmt**: Checks Terraform formatting |
| 122 | +- **terragrunt_fmt**: Checks Terragrunt HCL formatting |
| 123 | +- **terraform_tflint**: Runs TFLint on Terraform files |
| 124 | +- **terraform_validate**: Validates Terraform syntax |
| 125 | +- **terraform_trivy**: Security scanning of Terraform code |
| 126 | +- **gitleaks**: Detects secrets in code |
| 127 | +- **trailing-whitespace**: Removes trailing whitespace |
| 128 | +- **end-of-file-fixer**: Ensures files end with a newline |
| 129 | +- **check-added-large-files**: Prevents committing large files |
| 130 | +- **detect-private-key**: Detects private keys |
| 131 | + |
| 132 | +## Validation |
| 133 | + |
| 134 | +### Validate Terraform Configuration |
| 135 | + |
| 136 | +Validate that your Terraform configuration is syntactically valid: |
| 137 | + |
| 138 | +```bash |
| 139 | +cd organizations/ |
| 140 | +terraform init |
| 141 | +terraform validate |
| 142 | +``` |
| 143 | + |
| 144 | +### Validate Terragrunt Configuration |
| 145 | + |
| 146 | +Validate your Terragrunt configuration: |
| 147 | + |
| 148 | +```bash |
| 149 | +cd organizations/ |
| 150 | +terragrunt validate-inputs |
| 151 | +``` |
| 152 | + |
| 153 | +## Security Scanning |
| 154 | + |
| 155 | +### TFLint |
| 156 | + |
| 157 | +Run TFLint to check for potential issues: |
| 158 | + |
| 159 | +```bash |
| 160 | +cd organizations/ |
| 161 | +tflint --init |
| 162 | +tflint --recursive |
| 163 | +``` |
| 164 | + |
| 165 | +### Trivy |
| 166 | + |
| 167 | +Run Trivy for security scanning: |
| 168 | + |
| 169 | +```bash |
| 170 | +cd organizations/ |
| 171 | +trivy config . |
| 172 | +``` |
| 173 | + |
| 174 | +## Example Configurations |
| 175 | + |
| 176 | +The `organizations/` folder includes example configurations to help you get started: |
| 177 | + |
| 178 | +### Organization Settings |
| 179 | +- **Location**: `organizations/example-org/terragrunt.hcl` |
| 180 | +- **Purpose**: Example organization-level settings |
| 181 | + |
| 182 | +### Project Structure |
| 183 | +- **Location**: `projects/example-project/example-org/` |
| 184 | +- **Purpose**: Example project with repositories and teams |
| 185 | + |
| 186 | +#### Repositories |
| 187 | +- **Location**: `projects/example-project/example-org/repositories/terragrunt.hcl` |
| 188 | +- **Includes**: |
| 189 | + - Public repository example |
| 190 | + - Private repository example |
| 191 | + - Application repository with branch protection |
| 192 | + |
| 193 | +#### Teams |
| 194 | +- **Location**: `projects/example-project/example-org/teams/terragrunt.hcl` |
| 195 | +- **Includes**: |
| 196 | + - Admin team |
| 197 | + - Developer team |
| 198 | + - Viewer team |
| 199 | + - Security team (secret) |
| 200 | + |
| 201 | +### Provider Configuration |
| 202 | +- **Location**: `providers/example-org/providers.hcl` |
| 203 | +- **Purpose**: GitHub provider setup with GCP Secret Manager integration |
| 204 | + |
| 205 | +## Testing Workflow |
| 206 | + |
| 207 | +Follow this workflow before committing changes: |
| 208 | + |
| 209 | +1. **Make your changes** to Terraform/Terragrunt files |
| 210 | + |
| 211 | +2. **Format your code**: |
| 212 | + ```bash |
| 213 | + terraform fmt -recursive |
| 214 | + terragrunt hclfmt |
| 215 | + ``` |
| 216 | + |
| 217 | +3. **Validate syntax**: |
| 218 | + ```bash |
| 219 | + terraform validate |
| 220 | + ``` |
| 221 | + |
| 222 | +4. **Run tests**: |
| 223 | + ```bash |
| 224 | + terraform test -verbose |
| 225 | + ``` |
| 226 | + |
| 227 | +5. **Run security scans**: |
| 228 | + ```bash |
| 229 | + tflint --recursive |
| 230 | + trivy config . |
| 231 | + ``` |
| 232 | + |
| 233 | +6. **Commit your changes**: |
| 234 | + ```bash |
| 235 | + git add . |
| 236 | + git commit -m "feat: your commit message" |
| 237 | + ``` |
| 238 | + |
| 239 | + Pre-commit hooks will automatically run and validate your changes. |
| 240 | + |
| 241 | +## Continuous Integration |
| 242 | + |
| 243 | +When you push your changes or create a pull request, GitHub Actions will automatically: |
| 244 | +- Check Terraform formatting |
| 245 | +- Check Terragrunt formatting |
| 246 | +- Run Terraform plan |
| 247 | +- Validate configurations |
| 248 | +- Scan for security issues |
| 249 | + |
| 250 | +The CI pipeline uses the same tools and configurations as local development, ensuring consistency. |
| 251 | + |
| 252 | +## Troubleshooting |
| 253 | + |
| 254 | +### Common Issues |
| 255 | + |
| 256 | +**Issue**: `terraform test` fails with module not found |
| 257 | +- **Solution**: Run `terraform init` first to download required modules |
| 258 | + |
| 259 | +**Issue**: Pre-commit hooks fail |
| 260 | +- **Solution**: Run `pre-commit run --all-files` to see detailed errors |
| 261 | + |
| 262 | +**Issue**: Format check fails |
| 263 | +- **Solution**: Run `terraform fmt -recursive` and `terragrunt hclfmt` to auto-format |
| 264 | + |
| 265 | +**Issue**: TFLint fails |
| 266 | +- **Solution**: Run `tflint --init` to download required plugins |
| 267 | + |
| 268 | +## Additional Resources |
| 269 | + |
| 270 | +- [Terraform Testing Documentation](https://developer.hashicorp.com/terraform/language/tests) |
| 271 | +- [Terragrunt Documentation](https://terragrunt.gruntwork.io/docs/) |
| 272 | +- [TFLint Rules](https://github.com/terraform-linters/tflint/tree/master/docs/rules) |
| 273 | +- [Pre-commit Terraform Hooks](https://github.com/antonbabenko/pre-commit-terraform) |
0 commit comments