Skip to content

Instantly share code, notes, and snippets.

@MarkWarneke
Last active January 13, 2021 08:07
Show Gist options
  • Save MarkWarneke/53fa4645049a16584615c59632a1493c to your computer and use it in GitHub Desktop.
Save MarkWarneke/53fa4645049a16584615c59632a1493c to your computer and use it in GitHub Desktop.
Reusable generic Terratest for all Azure based Terraform modules. https://markwarneke.me/2020-10-14-Generic-Terraform-Module-Test-Using-Terratest/
package test
import (
"fmt"
"log"
"math/rand"
"os"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
)
/**
Creates a random name that is used for testing
Create terraform options (similar to the terraform command line arguments),
references a static test.vars, that contains the configuration for the test
Move provider.tf into the module (../)
Runs terraform plan & terraform apply
Move provider.tf back
Run terraform destroy
**/
// Used for moving the provider.tf between tests
func moveFile(oldLocation, newLocation string) {
err := os.Rename(oldLocation, newLocation)
if err != nil {
log.Panic(err)
}
}
// Used for orchestrating the test
func TestTerraformVmWithGpuModule(t *testing.T) {
// Save the location of the parent, SUT (subject under test)
terraformDir := "../"
// The original provider located in /$SUT/test/provider.tf
originalLocation := "provider.tf"
// Store position the providers has to be for testing in /$SUT/provider.tf
underTestLocation := strings.Join([]string{terraformDir, originalLocation}, "")
// Create a unique name based on a random id
expectedName := fmt.Sprintf("t%d", rand.Intn(9999))
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located in /$SUT
TerraformDir: terraformDir,
// Add variables via the test.vars file
VarFiles: []string{"./test/test.vars"},
// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"name": expectedName,
},
// Disable colors in Terraform commands so its easier to parse stdout/stderr
NoColor: false,
}
// defer: Move the provider _at the end_ of the test back to /$SUT/test
defer moveFile(underTestLocation, originalLocation)
// defer: At the end of the test, run `terraform destroy` to clean up the resources created
defer terraform.Destroy(t, terraformOptions)
// Move provider.tf from /$SUT/test/provider.tf to /$SUT/provider.tf
moveFile(originalLocation, underTestLocation)
// Is used mainly for debugging, fail early if plan is not possible
terraform.InitAndPlan(t, terraformOptions)
// This will run `terraform init` and `terraform apply`, fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
}
# Local provider for testing
provider "azurerm" {
version = "=2.3.0"
features {}
}
# Source necessary TF environment variables
docker run --env-file .env -it -v ${PWD}:"/source" -w "/source" aztfmod/rover
cd $SUT
cd test
# Setup go module
go mod init 'github.com/aztfmod'
# Make sure to set an appropriate timeout
go test # -timeout 30m
resource_group_name = "playground-test-resources"
location = "WestEurope"
tags = {
test = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment