Terraform Tests #16
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
| # ============================================================================= | |
| # THREE HORIZONS ACCELERATOR - TERRAFORM MODULE TESTS | |
| # ============================================================================= | |
| # | |
| # Runs Terratest tests for Terraform modules. | |
| # | |
| # Test Types: | |
| # - Unit tests: Run on every PR (no Azure resources) | |
| # - Integration tests: Run on merge to main (creates real resources) | |
| # | |
| # ============================================================================= | |
| name: Terraform Tests | |
| on: | |
| push: | |
| branches: [main] | |
| paths: | |
| - 'terraform/**' | |
| - 'tests/terraform/**' | |
| pull_request: | |
| branches: [main, develop] | |
| paths: | |
| - 'terraform/**' | |
| - 'tests/terraform/**' | |
| schedule: | |
| # Run full test suite weekly on Sundays at 2 AM UTC | |
| - cron: '0 2 * * 0' | |
| workflow_dispatch: | |
| inputs: | |
| test_type: | |
| description: 'Test type to run' | |
| required: true | |
| default: 'unit' | |
| type: choice | |
| options: | |
| - unit | |
| - integration | |
| - all | |
| env: | |
| TF_VERSION: "1.6.6" | |
| GO_VERSION: "1.21" | |
| TERRATEST_PARALLELISM: "4" | |
| permissions: | |
| contents: read | |
| id-token: write | |
| jobs: | |
| # =========================================================================== | |
| # UNIT TESTS | |
| # =========================================================================== | |
| unit-tests: | |
| name: Unit Tests | |
| runs-on: ubuntu-latest | |
| if: | | |
| github.event_name == 'pull_request' || | |
| github.event.inputs.test_type == 'unit' || | |
| github.event.inputs.test_type == 'all' || | |
| github.event_name == 'schedule' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: ${{ env.GO_VERSION }} | |
| cache-dependency-path: tests/terraform/go.sum | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ${{ env.TF_VERSION }} | |
| terraform_wrapper: false | |
| - name: Download Go Dependencies | |
| working-directory: tests/terraform | |
| run: go mod download | |
| - name: Run Unit Tests | |
| working-directory: tests/terraform | |
| run: | | |
| go test -v -tags=unit -timeout 30m ./... 2>&1 | tee test-output.txt | |
| continue-on-error: true | |
| - name: Generate Test Report | |
| if: always() | |
| working-directory: tests/terraform | |
| run: | | |
| echo "## Unit Test Results" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| tail -50 test-output.txt >> $GITHUB_STEP_SUMMARY || echo "No output" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| - name: Upload Test Output | |
| uses: actions/upload-artifact@v4 | |
| if: always() | |
| with: | |
| name: unit-test-output | |
| path: tests/terraform/test-output.txt | |
| # =========================================================================== | |
| # INTEGRATION TESTS | |
| # =========================================================================== | |
| integration-tests: | |
| name: Integration Tests | |
| runs-on: ubuntu-latest | |
| if: | | |
| github.event_name == 'push' && github.ref == 'refs/heads/main' || | |
| github.event.inputs.test_type == 'integration' || | |
| github.event.inputs.test_type == 'all' || | |
| github.event_name == 'schedule' | |
| environment: testing | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: ${{ env.GO_VERSION }} | |
| cache-dependency-path: tests/terraform/go.sum | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ${{ env.TF_VERSION }} | |
| terraform_wrapper: false | |
| - name: Azure Login | |
| uses: azure/login@v2 | |
| with: | |
| client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
| tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
| subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
| - name: Download Go Dependencies | |
| working-directory: tests/terraform | |
| run: go mod download | |
| - name: Run Integration Tests | |
| working-directory: tests/terraform | |
| env: | |
| ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
| ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} | |
| ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} | |
| ARM_USE_OIDC: true | |
| run: | | |
| go test -v -tags=integration -timeout 60m -parallel ${{ env.TERRATEST_PARALLELISM }} ./... 2>&1 | tee test-output.txt | |
| continue-on-error: true | |
| - name: Generate Test Report | |
| if: always() | |
| working-directory: tests/terraform | |
| run: | | |
| echo "## Integration Test Results" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| tail -100 test-output.txt >> $GITHUB_STEP_SUMMARY || echo "No output" >> $GITHUB_STEP_SUMMARY | |
| echo '```' >> $GITHUB_STEP_SUMMARY | |
| - name: Cleanup Azure Resources | |
| if: always() | |
| run: | | |
| # Find and delete any leftover test resource groups | |
| echo "Cleaning up test resources..." | |
| az group list --query "[?contains(name, 'terratest')].name" -o tsv | while read rg; do | |
| echo "Deleting resource group: $rg" | |
| az group delete --name "$rg" --yes --no-wait || true | |
| done | |
| - name: Upload Test Output | |
| uses: actions/upload-artifact@v4 | |
| if: always() | |
| with: | |
| name: integration-test-output | |
| path: tests/terraform/test-output.txt | |
| # =========================================================================== | |
| # TEST SUMMARY | |
| # =========================================================================== | |
| test-summary: | |
| name: Test Summary | |
| runs-on: ubuntu-latest | |
| needs: [unit-tests, integration-tests] | |
| if: always() | |
| steps: | |
| - name: Download Unit Test Artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: unit-test-output | |
| path: unit-tests | |
| continue-on-error: true | |
| - name: Download Integration Test Artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: integration-test-output | |
| path: integration-tests | |
| continue-on-error: true | |
| - name: Generate Summary | |
| run: | | |
| echo "# Terraform Test Summary" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "| Test Type | Status |" >> $GITHUB_STEP_SUMMARY | |
| echo "|-----------|--------|" >> $GITHUB_STEP_SUMMARY | |
| echo "| Unit Tests | ${{ needs.unit-tests.result || 'skipped' }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Integration Tests | ${{ needs.integration-tests.result || 'skipped' }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "---" >> $GITHUB_STEP_SUMMARY | |
| echo "*Test run completed at: $(date -u)*" >> $GITHUB_STEP_SUMMARY | |
| - name: Check Results | |
| if: contains(needs.*.result, 'failure') | |
| run: | | |
| echo "::error::One or more test suites failed" | |
| exit 1 |