|
| 1 | +# Copyright 2025 Canonical Ltd. |
| 2 | +# See LICENSE file for licensing details. |
| 3 | + |
| 4 | +name: Terraform module tests |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + detect-changes: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + terraform: ${{ steps.filter.outputs.terraform }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v6.0.1 |
| 19 | + - uses: dorny/paths-filter@v3 |
| 20 | + id: filter |
| 21 | + with: |
| 22 | + filters: | |
| 23 | + terraform: |
| 24 | + - "**/terraform/**" |
| 25 | +
|
| 26 | + test-terraform: |
| 27 | + needs: detect-changes |
| 28 | + if: needs.detect-changes.outputs.terraform == 'true' |
| 29 | + name: Test Terraform with Juju |
| 30 | + runs-on: self-hosted-linux-amd64-noble-edge |
| 31 | + env: |
| 32 | + WORKING_DIR: "terraform/charm/tests" |
| 33 | + steps: |
| 34 | + - uses: actions/checkout@v6.0.1 |
| 35 | + - uses: charmed-kubernetes/actions-operator@main |
| 36 | + with: |
| 37 | + provider: "lxd" |
| 38 | + juju-channel: 3.6/stable |
| 39 | + - name: Prepare juju tf provider environment |
| 40 | + run: | |
| 41 | + set -e |
| 42 | + CONTROLLER=$(juju whoami | yq .Controller) |
| 43 | + JUJU_CONTROLLER_ADDRESSES="$(juju show-controller | yq '.[$CONTROLLER]'.details.\"api-endpoints\" | tr -d "[]' "|tr -d '"'|tr -d '\n')" |
| 44 | + JUJU_USERNAME="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.user|tr -d '"')" |
| 45 | + JUJU_PASSWORD="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.password|tr -d '"')" |
| 46 | +
|
| 47 | + echo "JUJU_CONTROLLER_ADDRESSES=$JUJU_CONTROLLER_ADDRESSES" >> "$GITHUB_ENV" |
| 48 | + echo "JUJU_USERNAME=$JUJU_USERNAME" >> "$GITHUB_ENV" |
| 49 | + echo "JUJU_PASSWORD=$JUJU_PASSWORD" >> "$GITHUB_ENV" |
| 50 | + { |
| 51 | + echo 'JUJU_CA_CERT<<EOF' |
| 52 | + juju show-controller $(echo $CONTROLLER|tr -d '"') | yq '.[$CONTROLLER]'.details.\"ca-cert\"|tr -d '"' |
| 53 | + echo EOF |
| 54 | + } >> "$GITHUB_ENV" |
| 55 | + - run: juju add-model test-github-runner |
| 56 | + - uses: hashicorp/setup-terraform@v3.1.2 |
| 57 | + - run: terraform init |
| 58 | + working-directory: ${{env.WORKING_DIR}} |
| 59 | + - run: terraform validate |
| 60 | + working-directory: ${{env.WORKING_DIR}} |
| 61 | + - run: terraform plan -out=tfplan |
| 62 | + working-directory: ${{env.WORKING_DIR}} |
| 63 | + - run: terraform show tfplan |
| 64 | + working-directory: ${{env.WORKING_DIR}} |
| 65 | + - run: terraform test |
| 66 | + working-directory: ${{env.WORKING_DIR}} |
| 67 | + |
| 68 | + terraform-status-check: |
| 69 | + runs-on: ubuntu-latest |
| 70 | + needs: [detect-changes, test-terraform] |
| 71 | + if: always() |
| 72 | + steps: |
| 73 | + - name: Check terraform test results |
| 74 | + run: | |
| 75 | + # If tests were skipped because no changes, that's success |
| 76 | + if [ "${{ needs.detect-changes.outputs.terraform }}" != "true" ]; then |
| 77 | + echo "No terraform changes detected, skipping tests is expected" |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | +
|
| 81 | + # If tests ran, check their results |
| 82 | + test_result="${{ needs.test-terraform.result }}" |
| 83 | +
|
| 84 | + echo "Terraform test result: $test_result" |
| 85 | +
|
| 86 | + # Fail if test that ran actually failed (not skipped) |
| 87 | + if [ "$test_result" = "failure" ]; then |
| 88 | + echo "Terraform tests failed" |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | +
|
| 92 | + echo "Terraform tests passed or were skipped appropriately" |
| 93 | + exit 0 |
0 commit comments