Skip to content

Commit

Permalink
Added tfstate and backend to end to end tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncollinson2001 committed Sep 20, 2024
1 parent 1d76d60 commit a80bb06
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
30 changes: 18 additions & 12 deletions .github/workflows/ci-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@ jobs:
terraform_version: 1.9.3
terraform_wrapper: false


#BRANCH_NAME="${{ github.head_ref != '' && github.head_ref || github.ref }}"
#TF_STATE_KEY="${BRANCH_NAME//\//_}.tfstate"
#terraform init -backend=true -backend-config="resource_group_name=$TF_STATE_RESOURCE_GROUP" -backend-config="storage_account_name=$TF_STATE_STORAGE_ACCOUNT" -backend-config="container_name=github-actions" -backend-config="key=$TF_STATE_KEY"

- name: Terraform Init
run: |
BRANCH_NAME="${{ github.head_ref != '' && github.head_ref || github.ref }}"
TF_STATE_KEY="${BRANCH_NAME//\//_}.tfstate"
terraform init -backend=true -backend-config="resource_group_name=$TF_STATE_RESOURCE_GROUP" -backend-config="storage_account_name=$TF_STATE_STORAGE_ACCOUNT" -backend-config="container_name=github-actions" -backend-config="key=$TF_STATE_KEY"
run: |
terraform init -backend=false
working-directory: infrastructure
env:
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
TF_STATE_RESOURCE_GROUP: ${{ secrets.TF_STATE_RESOURCE_GROUP }}
TF_STATE_STORAGE_ACCOUNT: ${{ secrets.TF_STATE_STORAGE_ACCOUNT }}
# env:
# ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
# ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
# ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
# ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# TF_STATE_RESOURCE_GROUP: ${{ secrets.TF_STATE_RESOURCE_GROUP }}
# TF_STATE_STORAGE_ACCOUNT: ${{ secrets.TF_STATE_STORAGE_ACCOUNT }}

- name: Terraform Validate
run: terraform validate
Expand All @@ -67,6 +70,9 @@ jobs:
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
TF_STATE_RESOURCE_GROUP: ${{ secrets.TF_STATE_RESOURCE_GROUP }}
TF_STATE_STORAGE_ACCOUNT: ${{ secrets.TF_STATE_STORAGE_ACCOUNT }}
BRANCH_NAME: ${{ github.head_ref != '' && github.head_ref || github.ref }}

static-code-analysis:
name: Static Code Analysis
Expand Down
12 changes: 12 additions & 0 deletions tests/end-to-end-tests/basic_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e_tests

import (
"fmt"
"os"
"testing"

"github.com/gruntwork-io/terratest/modules/random"
Expand All @@ -14,7 +15,11 @@ import (
* TestBasicDeployment tests a basic deployment of the infrastructure using Terraform using the TF output variables.
*/
func TestBasicDeployment(t *testing.T) {
t.Parallel()

terraformFolder := "../../infrastructure"
terraformStateResourceGroup := os.Getenv("TF_STATE_RESOURCE_GROUP")
terraformStateStorageAccount := os.Getenv("TF_STATE_STORAGE_ACCOUNT")

vaultName := random.UniqueId()
vaultLocation := "uksouth"
Expand All @@ -33,6 +38,13 @@ func TestBasicDeployment(t *testing.T) {
"vault_location": vaultLocation,
"vault_redundancy": vaultRedundancy,
},

BackendConfig: map[string]interface{}{
"resource_group_name": terraformStateResourceGroup,
"storage_account_name": terraformStateStorageAccount,
"container_name": "tfstate",
"key": vaultName + ".tfstate",
},
}

// Save options for later test stages
Expand Down
11 changes: 11 additions & 0 deletions tests/end-to-end-tests/full_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import (
* TestFullDeployment tests the full deployment of the infrastructure using Terraform.
*/
func TestFullDeployment(t *testing.T) {
t.Parallel()

terraformFolder := "../../infrastructure"
terraformStateResourceGroup := os.Getenv("TF_STATE_RESOURCE_GROUP")
terraformStateStorageAccount := os.Getenv("TF_STATE_STORAGE_ACCOUNT")

vaultName := random.UniqueId()
vaultLocation := "uksouth"
Expand All @@ -38,6 +42,13 @@ func TestFullDeployment(t *testing.T) {
"vault_location": vaultLocation,
"vault_redundancy": vaultRedundancy,
},

BackendConfig: map[string]interface{}{
"resource_group_name": terraformStateResourceGroup,
"storage_account_name": terraformStateStorageAccount,
"container_name": "tfstate",
"key": vaultName + ".tfstate",
},
}

// Save options for later test stages
Expand Down

0 comments on commit a80bb06

Please sign in to comment.