-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
271 lines (237 loc) · 7.17 KB
/
main_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"strings"
"testing"
)
const (
testVersion string = "test"
testExecutable string = "terraputs-test"
expectedEmptyOutput string = `## foo
Terraform state outputs.
| Output | Value | Type
| --- | --- | --- |
`
)
func TestMain(m *testing.M) {
// compile a 'terraputs' for use in running tests
exe := exec.Command("go", "build", "-ldflags", fmt.Sprintf("-X main.version=%s", testVersion), "-o", testExecutable)
err := exe.Run()
if err != nil {
os.Exit(1)
}
m.Run()
// delete the compiled terraputs
err = os.Remove(testExecutable)
if err != nil {
log.Fatal(err)
}
}
func TestHelpFlag(t *testing.T) {
help := []string{
"-state string",
stateDesc,
"-state-file string",
stateFileDesc,
"-heading string",
headingDesc,
"-output string",
outputDesc,
}
tests := []struct {
arg string
outputs []string
}{{
arg: "-h",
outputs: help,
}, {
arg: "-help",
outputs: help,
}}
for _, test := range tests {
t.Run(fmt.Sprintf("when terraputs is passed '%s'", test.arg), func(t *testing.T) {
output, err := exec.Command(fmt.Sprintf("./%s", testExecutable), test.arg).CombinedOutput()
if err != nil {
t.Errorf("expected '%s' not to error; got '%v'", test.arg, err)
}
for _, o := range test.outputs {
if !strings.Contains(string(output), o) {
t.Errorf("expected '%s' to include output '%s'; got '%s'", test.arg, o, output)
}
}
})
}
}
func TestVersionArg(t *testing.T) {
args := []string{
"version",
" version ",
"VERSION",
"vERsION",
}
for _, arg := range args {
t.Run(fmt.Sprintf("when terraputs is passed '%s'", arg), func(t *testing.T) {
output, err := exec.Command(fmt.Sprintf("./%s", testExecutable), arg).CombinedOutput()
if err != nil {
t.Errorf("expected '%s' not to cause error; got '%v'", arg, err)
}
if !strings.Contains(string(output), testVersion) {
t.Errorf("expected '%s' to output version '%s'; got '%s'", arg, testVersion, output)
}
})
}
}
// These should all be ok and functionally equivalent:
//
// terraputs -state "$(cat stateFile)"
// terraputs < stateFile
// cat stateFile | terraputs
func TestTerraputs(t *testing.T) {
command := func(cmd string) string {
return fmt.Sprintf("./%s ", testExecutable) + cmd
}
tests := []struct {
command string
expectedError error
expectedOutput string
}{{
command: command("-state $(cat testdata/basic/show.json)"),
expectedOutput: expectedOutput("Outputs"),
}, {
command: command("< testdata/basic/show.json"),
expectedOutput: expectedOutput("Outputs"),
}, {
command: fmt.Sprintf("cat testdata/basic/show.json | ./%s", testExecutable),
expectedOutput: expectedOutput("Outputs"),
}, {
command: command("-state $(cat testdata/basic/show.json) -heading foo"),
expectedOutput: expectedOutput("foo"),
}, {
command: command(`-state $(cat testdata/basic/show.json) -description "A custom description."`),
expectedOutput: strings.Replace(expectedOutput("Outputs"), "Terraform state outputs", "A custom description", 1),
}, {
command: command("-state $(cat testdata/nooutputs/show.json) -heading foo"),
expectedOutput: expectedEmptyOutput,
}, {
command: command("-state $(cat testdata/emptyconfig/show.json) -heading foo"),
expectedOutput: expectedEmptyOutput,
}, {
command: command("-state-file testdata/basic/show.json -heading foo"),
expectedOutput: expectedOutput("foo"),
}, {
command: command("-state-file testdata/basic/show.json -heading foo -output html"),
expectedOutput: expectedHTMLOutput("foo"),
}, {
command: command(`-state-file testdata/basic/show.json -description "A custom description." -output html`),
expectedOutput: strings.Replace(expectedHTMLOutput("Outputs"), "Terraform state outputs", "A custom description", 1),
}, {
command: command("-state-file testdata/nooutputs/show.json -heading foo"),
expectedOutput: expectedEmptyOutput,
}, {
command: command("-state-file testdata/emptyconfig/show.json -heading foo"),
expectedOutput: expectedEmptyOutput,
}, {
command: command("-state-file testdata/basic/i-do-not-exist.json -heading foo"),
expectedError: errors.New("exit status 1"),
expectedOutput: "open testdata/basic/i-do-not-exist.json: no such file or directory",
}, {
command: command("-state $(cat testdata/basic/show.json) -state-file testdata/basic/show.json -heading foo"),
expectedError: errors.New("exit status 1"),
expectedOutput: "'-state' and '-state-file' are mutually exclusive; specify just one",
}, {
command: command("-state $(cat testdata/basic/show.json) -output foo"),
expectedError: errors.New("exit status 1"),
expectedOutput: "'foo' is not a supported output format. Supported formats: 'md' (default), 'html'",
}, {
command: command("-state-file testdata/emptyconfig-1.1.5/show.json -heading foo"),
expectedOutput: expectedEmptyOutput,
}, {
command: command("-state $(cat testdata/basic-1.1.5/show.json)"),
expectedOutput: expectedOutput("Outputs"),
}}
for _, test := range tests {
t.Run(fmt.Sprintf("when terraputs is passed '%q'", test.command), func(t *testing.T) {
output, err := exec.Command("/bin/sh", "-c", test.command).CombinedOutput()
if err != nil && test.expectedError == nil {
t.Errorf("expected '%s' not to error; got '%v'", test.command, err)
}
if test.expectedError != nil && err == nil {
t.Errorf("expected error '%s'; got '%v'", test.expectedError.Error(), err)
}
if test.expectedError != nil && err != nil && test.expectedError.Error() != err.Error() {
t.Errorf("expected error '%s'; got '%v'", test.expectedError.Error(), err.Error())
}
if string(output) != test.expectedOutput {
t.Logf("expected output: \n%s", test.expectedOutput)
t.Logf("got output: \n%s", output)
t.Errorf("received unexpected output from '%s'", test.command)
}
})
}
}
func expectedOutput(heading string) string {
return fmt.Sprintf(`## %s
Terraform state outputs.
| Output | Value | Type
| --- | --- | --- |
| a_basic_map | map[foo:bar number:42] | map[string]interface {}
| a_list | [foo bar] | []interface {}
| a_nested_map | map[baz:map[bar:baz id:123] foo:bar number:42] | map[string]interface {}
| a_sensitive_value | sensitive; redacted | string
| a_string | foo | string
`, heading)
}
func expectedHTMLOutput(heading string) string {
return fmt.Sprintf(`<h2>%s</h2>
<p>Terraform state outputs.</p>
<table>
<tr>
<th>Output</th>
<th>Value</th>
<th>Type</th>
</tr>
<tr>
<td>a_basic_map</td>
<td><pre>{
"foo": "bar",
"number": 42
}</pre></td>
<td>map[string]interface {}</td>
</tr>
<tr>
<td>a_list</td>
<td><pre>[
"foo",
"bar"
]</pre></td>
<td>[]interface {}</td>
</tr>
<tr>
<td>a_nested_map</td>
<td><pre>{
"baz": {
"bar": "baz",
"id": "123"
},
"foo": "bar",
"number": 42
}</pre></td>
<td>map[string]interface {}</td>
</tr>
<tr>
<td>a_sensitive_value</td>
<td><pre>sensitive; redacted</pre></td>
<td>string</td>
</tr>
<tr>
<td>a_string</td>
<td><pre>"foo"</pre></td>
<td>string</td>
</tr>
</table>
`, heading)
}