This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
/
test_module.go
141 lines (118 loc) · 3.86 KB
/
test_module.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Package common contains shared resources for terratest tests
package common
import (
"fmt"
"os"
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
)
const KubeConfigPath = "kube_config_path"
// TestModule structure for module to test
type TestModule struct {
GoTest *testing.T
Name string
TerraformDir string
Setup testCallback
Tests testCallback
Teardown testCallback
terraformOptions *terraform.Options
}
type testCallback func(*TestModule)
func (tm *TestModule) SetString(name string, value string) {
path := tm.getDataPath()
test_structure.SaveString(tm.GoTest, path, name, value)
}
func (tm *TestModule) GetString(name string) string {
path := tm.getDataPath()
return test_structure.LoadString(tm.GoTest, path, name)
}
func (tm *TestModule) SetStringGlobal(name string, value string) {
path, err := os.Getwd()
if err != nil {
tm.GoTest.Fatalf("Failed setting global string '%s'. Could not get working directory: %s", name, err)
}
test_structure.SaveString(tm.GoTest, path, name, value)
}
func (tm *TestModule) GetStringGlobal(name string) string {
path, err := os.Getwd()
if err != nil {
tm.GoTest.Fatalf("Failed getting global string '%s'. Could not get working directory: %s", name, err)
}
return test_structure.LoadString(tm.GoTest, path, name)
}
func (tm *TestModule) SetTerraformVar(name string, value string) {
tm.terraformOptions.Vars[name] = value
}
func (tm *TestModule) GetTerraformVar(name string) string {
return tm.terraformOptions.Vars[name].(string)
}
func (tm *TestModule) SetTerraformOptions(terraformOptions *terraform.Options) {
path := tm.getDataPath()
test_structure.SaveTerraformOptions(tm.GoTest, path, terraformOptions)
}
func (tm *TestModule) GetTerraformOptions() *terraform.Options {
path := tm.getDataPath()
return test_structure.LoadTerraformOptions(tm.GoTest, path)
}
func (tm *TestModule) GetTerraformOutput(name string) string {
return terraform.Output(tm.GoTest, tm.terraformOptions, name)
}
func (tm *TestModule) GetTerraformOutputMap(name string) map[string]string {
return terraform.OutputMap(tm.GoTest, tm.terraformOptions, name)
}
func (tm *TestModule) RunTests() {
path := tm.getDataPath()
if test_structure.IsTestDataPresent(tm.GoTest, test_structure.FormatTestDataPath(path, "TerraformOptions.json")) {
tm.terraformOptions = tm.GetTerraformOptions()
} else {
noColor := os.Getenv("TERRATEST_NO_COLOR")
tm.terraformOptions = &terraform.Options{
TerraformDir: tm.TerraformDir,
Vars: map[string]interface{}{},
NoColor: noColor == "true",
}
}
stage := fmt.Sprintf("%s_%s", tm.Name, "run")
test_structure.RunTestStage(tm.GoTest, stage, func() {
if tm.Setup != nil {
tm.Setup(tm)
}
tm.SetTerraformOptions(tm.terraformOptions)
terraform.InitAndApply(tm.GoTest, tm.terraformOptions)
if tm.Tests != nil {
tm.Tests(tm)
}
})
}
func (tm *TestModule) TeardownTests() {
path := tm.getDataPath()
stage := fmt.Sprintf("%s_%s", tm.Name, "teardown")
test_structure.RunTestStage(tm.GoTest, stage, func() {
terraformOptions := tm.GetTerraformOptions()
terraform.Destroy(tm.GoTest, terraformOptions)
if tm.Teardown != nil {
tm.Teardown(tm)
}
test_structure.CleanupTestDataFolder(tm.GoTest, path)
})
}
func (tm *TestModule) IsTestDataPresent() bool {
path := tm.getDataPath()
return test_structure.IsTestDataPresent(tm.GoTest, test_structure.FormatTestDataPath(path, "TerraformOptions.json"))
}
func (tm *TestModule) getDataPath() string {
return tm.TerraformDir
}
func TestModuleSetStringGlobal(t *testing.T, name string, value string) {
tm := TestModule{
GoTest: t,
}
tm.SetStringGlobal(name, value)
}
func TestModuleGetStringGlobal(t *testing.T, name string) string {
tm := TestModule{
GoTest: t,
}
return tm.GetStringGlobal(name)
}