-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathweb_service_unit_test.go
More file actions
51 lines (40 loc) · 1.84 KB
/
web_service_unit_test.go
File metadata and controls
51 lines (40 loc) · 1.84 KB
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
51
package test
import (
"fmt"
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
"github.com/gruntwork-io/terratest/modules/random"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"strings"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/terraform"
)
// An example of a unit test for the Terraform module in examples/web-service
func TestWebServiceUnit(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()
// Since we want to be able to run multiple tests in parallel on the same modules, we need to copy them into
// temp folders so that the state files and .terraform folders don't clash
webServicePath := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/web-service")
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: webServicePath,
// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"name": strings.ToLower(fmt.Sprintf("web-service-test-%s", uniqueId)),
},
}
// At the end of the test, 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)
// Run `terraform output` to get the values of output variables
url := terraform.Output(t, terraformOptions, "url")
// Verify the app returns a 200 OK with JSON data that contains the text "Hello, World!"
expectedStatus := 200
expectedBody := `{"text":"Hello, World!"}`
maxRetries := 10
timeBetweenRetries := 3 * time.Second
http_helper.HttpGetWithRetry(t, url, nil, expectedStatus, expectedBody, maxRetries, timeBetweenRetries)
}