-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraformer_test.go
85 lines (63 loc) · 1.82 KB
/
terraformer_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
package main
import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"
)
func TestGenerate(t *testing.T) {
os.Args = []string{"terraformer",
"generate",
"examples/unit-testing/tf.template",
"examples/unit-testing/context.yml"}
s := captureStdout(main)
assert.Contains(t, s, "map[context")
assert.Contains(t, s, "[\"1.1.1.1\", \"2.2.2.2\", \"3.3.3.3\"]")
}
func TestGenerateContext(t *testing.T) {
os.Args = []string{"terraformer",
"generate-context",
"--state=examples/unit-testing/tf.state.json",
"--callback=examples/unit-testing/callback-yaml.sh",
"--template=examples/unit-testing/context.yml.template",
"examples/unit-testing/config1.yml",
"examples/unit-testing/config2.yml"}
s := captureStdout(main)
assert.Contains(t, s, "context:")
assert.Contains(t, s, "env:")
assert.Contains(t, s, "HOME:")
assert.Contains(t, s, "state:")
assert.Contains(t, s, "terraform_version:")
assert.Contains(t, s, "callback:")
assert.Contains(t, s, "callback-yaml-value")
}
func TestTfStringListFormater(t *testing.T) {
// var array []interface{}{1, 2, "4", 1.4}
var result string
result = tfStringListFormater(nil)
assert.Equal(t, "[]", result)
result = tfStringListFormater([]interface{}{})
assert.Equal(t, "[]", result)
result = tfStringListFormater([]interface{}{1})
assert.Equal(t, "[\"1\"]", result)
result = tfStringListFormater([]interface{}{1.3, "1.1.1.1", 2})
assert.Equal(t, "[\"1.3\", \"1.1.1.1\", \"2\"]", result)
}
func TestTfCallback(t *testing.T) {
// var array []interface{}{1, 2, "4", 1.4}
var result string
result = tfCallback("callback-value.sh")
assert.Equal(t, "4711", result)
}
func captureStdout(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}