forked from gruntwork-io/infrastructure-as-code-testing-talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_world_app_unit_test.go
50 lines (40 loc) · 1.69 KB
/
hello_world_app_unit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package test
import (
"fmt"
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"testing"
"time"
)
// An example of a unit test for the Terraform module in examples/hello-world-app
func TestHelloWorldAppUnit(t *testing.T) {
t.Parallel()
// A unique ID we can use to namespace all our resource names and ensure they don't clash across parallel tests
uniqueId := random.UniqueId()
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../examples/hello-world-app",
// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"name": fmt.Sprintf("hello-world-app-%s", uniqueId),
},
}
// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
// Check that the app is working as expected
validateHelloWorldApp(t, terraformOptions)
}
// Validate the "Hello, World" app is working
func validateHelloWorldApp(t *testing.T, terraformOptions *terraform.Options) {
// Run `terraform output` to get the values of output variables
url := terraform.Output(t, terraformOptions, "url")
// Verify the app returns a 200 OK with the text "Hello, World!"
expectedStatus := 200
expectedBody := "Hello, World!"
maxRetries := 10
timeBetweenRetries := 3 * time.Second
http_helper.HttpGetWithRetry(t, url, nil, expectedStatus, expectedBody, maxRetries, timeBetweenRetries)
}