forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraformExecute_test.go
158 lines (142 loc) · 4.27 KB
/
terraformExecute_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package cmd
import (
"fmt"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
"testing"
)
type terraformExecuteMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func newTerraformExecuteTestsUtils() terraformExecuteMockUtils {
utils := terraformExecuteMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunTerraformExecute(t *testing.T) {
t.Parallel()
tt := []struct {
terraformExecuteOptions
expectedArgs []string
expectedEnvVars []string
}{
{
terraformExecuteOptions{
Command: "apply",
}, []string{"apply", "-auto-approve", "-no-color"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
TerraformSecrets: "/tmp/test",
}, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-no-color"}, []string{},
},
{
terraformExecuteOptions{
Command: "plan",
}, []string{"plan", "-no-color"}, []string{},
},
{
terraformExecuteOptions{
Command: "plan",
TerraformSecrets: "/tmp/test",
}, []string{"plan", "-var-file=/tmp/test", "-no-color"}, []string{},
},
{
terraformExecuteOptions{
Command: "plan",
TerraformSecrets: "/tmp/test",
AdditionalArgs: []string{"-arg1"},
}, []string{"plan", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
TerraformSecrets: "/tmp/test",
AdditionalArgs: []string{"-arg1"},
}, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
TerraformSecrets: "/tmp/test",
AdditionalArgs: []string{"-arg1"},
GlobalOptions: []string{"-chgdir=src"},
}, []string{"-chgdir=src", "apply", "-auto-approve", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
Init: true,
}, []string{"apply", "-auto-approve", "-no-color"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
GlobalOptions: []string{"-chgdir=src"},
Init: true,
}, []string{"-chgdir=src", "apply", "-auto-approve", "-no-color"}, []string{},
},
{
terraformExecuteOptions{
Command: "apply",
CliConfigFile: ".pipeline/.terraformrc",
}, []string{"apply", "-auto-approve", "-no-color"}, []string{"TF_CLI_CONFIG_FILE=.pipeline/.terraformrc"},
},
{
terraformExecuteOptions{
Command: "plan",
Workspace: "any-workspace",
}, []string{"plan", "-no-color"}, []string{"TF_WORKSPACE=any-workspace"},
},
}
for i, test := range tt {
t.Run(fmt.Sprintf("That arguments are correct %d", i), func(t *testing.T) {
t.Parallel()
// init
config := test.terraformExecuteOptions
utils := newTerraformExecuteTestsUtils()
utils.StdoutReturn = map[string]string{}
utils.StdoutReturn["terraform output -json"] = "{}"
utils.StdoutReturn["terraform -chgdir=src output -json"] = "{}"
runner := utils.ExecMockRunner
// test
err := runTerraformExecute(&config, nil, utils, &terraformExecuteCommonPipelineEnvironment{})
// assert
assert.NoError(t, err)
if config.Init {
assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: append(config.GlobalOptions, "init", "-no-color")}, utils.Calls[0])
assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: test.expectedArgs}, utils.Calls[1])
} else {
assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: test.expectedArgs}, utils.Calls[0])
}
assert.Subset(t, runner.Env, test.expectedEnvVars)
})
}
t.Run("Outputs get injected into CPE", func(t *testing.T) {
t.Parallel()
cpe := terraformExecuteCommonPipelineEnvironment{}
config := terraformExecuteOptions{
Command: "plan",
}
utils := newTerraformExecuteTestsUtils()
utils.StdoutReturn = map[string]string{}
utils.StdoutReturn["terraform output -json"] = `{
"sample_var": {
"sensitive": true,
"value": "a secret value",
"type": "string"
}
}
`
// test
err := runTerraformExecute(&config, nil, utils, &cpe)
// assert
assert.NoError(t, err)
assert.Equal(t, 1, len(cpe.custom.terraformOutputs))
assert.Equal(t, "a secret value", cpe.custom.terraformOutputs["sample_var"])
})
}