Skip to content

Commit

Permalink
Added end to end tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncollinson2001 committed Sep 15, 2024
1 parent 06295cc commit a5382c3
Show file tree
Hide file tree
Showing 13 changed files with 1,339 additions and 18 deletions.
26 changes: 20 additions & 6 deletions .github/workflows/ci-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,27 @@ jobs:
run: terraform validate
working-directory: infrastructure

- name: Terraform Init (Tests)
run: terraform init -backend=false
working-directory: tests
- name: Run Integration Tests
run: |
terraform init -backend=false
terraform test
working-directory: tests/integration-tests

- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '^1.23.1'

- name: Terraform Test
run: terraform test
working-directory: tests
- name: Azure Login
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Run End to End Tests
run: |
go mod tidy
go test -v -timeout 10m
working-directory: tests/end-to-end-tests

static-code-analysis:
name: Static Code Analysis
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*.DS_Store
*.pem

# Terratest
.test-data/

# User-specific files
*.rsuser
Expand Down
56 changes: 48 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The following technologies are used:
* Azure CLI
* Azure Pipelines
* Terraform
* Go (used for end-to-end testing)

### Outstanding Questions

Expand Down Expand Up @@ -99,21 +100,20 @@ The repository consists of the following directories:
The following are pre-reqs to working with the solution:

* An Azure subscription
* Azure CLI installed
* Terraform installed
* An Azure identity with the following roles:
* Contributor role on the subscription (required to create resources)
* RBAC Administrator role on the resources being backed up (required to assign roles on the resource to the backup vault managed identity)
* An Azure identity assigned the subscription Contributor role (required to create resources)
* [Azure CLI installed](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli)
* [Terraform installed](https://developer.hashicorp.com/terraform/install)
* [Go installed (to run the end-to-end tests)](https://go.dev/dl/)

[See the following link for further information.](https://learn.microsoft.com/en-us/azure/developer/terraform/get-started-windows-powershell)
> Ensure all installed components have been added to the `%PATH%` - e.g. `az`, `terraform` and `go`.
### Getting Started

Take the following steps to get started in configuring and verify the infrastructure:

1. Login to Azure

Use the Azure CLI to login to Azure by running the following command:
Use Azure CLI to login to Azure by running the following command:

```pwsh
az login
Expand Down Expand Up @@ -175,6 +175,8 @@ Take the following steps to get started in configuring and verify the infrastruc

### Running the Tests

#### Terraform HCL Tests

The test suite consists of a number Terraform HCL tests that use a mock azurerm provider.

[See this link for more information.](https://developer.hashicorp.com/terraform/language/tests)
Expand All @@ -183,7 +185,7 @@ Take the following steps to run the test suite:

1. Initialise Terraform

Change the working directory to `./tests`.
Change the working directory to `./tests/integration-tests`.

Terraform can now be initialised by running the following command:

Expand All @@ -201,6 +203,44 @@ Take the following steps to run the test suite:
terraform test
````

#### End to End Tests

The end to end tests are written in go, and use the [terratest library](https://terratest.gruntwork.io/).

The tests depend on a connection to Azure so it can create an environment that the tests are executed against. The environment is torn down once the test run has completed.

To run the tests, take the following steps:

1. Install go packages

You only need to do this once when setting up your environment.

Change the working directory to `./tests/end-to-end-tests`.

Run the following command:

````pwsh
go mod tidy
````

2. Login to Azure

You only need to do this once when setting up your environment, and may have already completed it as part of the environment setup.

Use Azure CLI to login to Azure by running the following command:

```pwsh
az login
```

3. Run the Tests

Run the tests with the following command:

````pwsh
go test -v -timeout 10m
````

### Contributing

If you want to contribute to the project, raise a PR on GitHub.
Expand Down
11 changes: 11 additions & 0 deletions infrastructure/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "vault_name" {
value = azurerm_data_protection_backup_vault.backup_vault.name
}

output "vault_location" {
value = azurerm_data_protection_backup_vault.backup_vault.location
}

output "vault_redundancy" {
value = azurerm_data_protection_backup_vault.backup_vault.redundancy
}
65 changes: 65 additions & 0 deletions tests/end-to-end-tests/basic_deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package e2e_tests

import (
"fmt"
"testing"

"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/assert"
)

func TestBasicDeployment(t *testing.T) {
t.Parallel()

terraformFolder := "../../infrastructure"

vaultName := random.UniqueId()
vaultLocation := "uksouth"
vaultRedundancy := "LocallyRedundant"

// Setup stage
test_structure.RunTestStage(t, "setup", func() {
terraformOptions := &terraform.Options{
TerraformDir: terraformFolder,

// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"vault_name": vaultName,
"vault_location": vaultLocation,
"vault_redundancy": vaultRedundancy,
},
}

// Save options for later test stages
test_structure.SaveTerraformOptions(t, terraformFolder, terraformOptions)

terraform.InitAndApply(t, terraformOptions)
})

// Validate stage
test_structure.RunTestStage(t, "validate", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, terraformFolder)

// Check if the vault name is as expected
expectedVaultName := fmt.Sprintf("bvault-%s", vaultName)
actualVaultName := terraform.Output(t, terraformOptions, "vault_name")
assert.Equal(t, expectedVaultName, actualVaultName)

// Check if the vault location is as expected
actualVaultLocation := terraform.Output(t, terraformOptions, "vault_location")
assert.Equal(t, vaultLocation, actualVaultLocation)

// Check if the vault redundancy is as expected
actualVaultRedundancy := terraform.Output(t, terraformOptions, "vault_redundancy")
assert.Equal(t, vaultRedundancy, actualVaultRedundancy)
})

// Teardown stage
test_structure.RunTestStage(t, "teardown", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, terraformFolder)

terraform.Destroy(t, terraformOptions)
})
}
103 changes: 103 additions & 0 deletions tests/end-to-end-tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
module e2e_tests

go 1.21

toolchain go1.23.1

require (
github.com/gruntwork-io/terratest v0.47.1
github.com/stretchr/testify v1.9.0
)

require (
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go/compute v1.19.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.29.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/boombuler/barcode v1.0.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/gruntwork-io/go-commons v0.8.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.6 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.13.0 // indirect
github.com/onsi/gomega v1.28.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/otp v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/urfave/cli v1.22.2 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.114.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.4 // indirect
k8s.io/apimachinery v0.28.4 // indirect
k8s.io/client-go v0.28.4 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit a5382c3

Please sign in to comment.