-
Notifications
You must be signed in to change notification settings - Fork 27
/
main_test.go
221 lines (209 loc) · 5.08 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
package main
import (
"fmt"
"log"
"os"
"reflect"
"testing"
)
func TestSplitYamlDocs(t *testing.T) {
cases := []struct {
name string
input string
want []string
}{
{
name: "single yaml document",
input: "---\nfoo: bar\n",
want: []string{"foo: bar\n"},
},
{
name: "two yaml documents",
input: "---\nfoo: bar\n---\nanother: doc\n",
want: []string{"foo: bar\n", "another: doc\n"},
},
{
name: "two yaml documents no doc separator",
input: "foo: bar\n---\nanother: doc\n",
want: []string{"foo: bar\n", "another: doc\n"},
},
{
name: "separator in the middle of the doc",
input: "foo: '---bar'\n",
want: []string{"foo: '---bar'\n"},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := splitYamlDocs(c.input)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("got: %#v\nwant: %#v\n", got, c.want)
}
})
}
}
func TestListDirectory(t *testing.T) {
cases := []struct {
name string
input string
want []string
}{
{
name: "Check yaml files exist",
input: "test/TestListDirectory/",
want: []string{"test/TestListDirectory/1-resource.yaml", "test/TestListDirectory/2-resource.yaml", "test/TestListDirectory/a.yaml", "test/TestListDirectory/b.yaml", "test/TestListDirectory/empty.yaml"},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := ListDirectory(c.input)
if err != nil {
fmt.Println("Testing if folder doesnt exist")
}
if !reflect.DeepEqual(got, c.want) {
t.Errorf("got: %#v\nwant: %#v\n", got, c.want)
}
})
}
}
func TestFilesExists(t *testing.T) {
cases := []struct {
name string
input string
want bool
}{
{
name: "Check Dockerfile exists",
input: "./test/deployment.yaml",
want: true,
},
{
name: "Check fake file doesnt exist",
input: "./imafakefile",
want: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := FilesExists(c.input)
if err != nil {
log.Fatal(err)
}
if !reflect.DeepEqual(got, c.want) {
t.Errorf("got: %#v\nwant: %#v\n", got, c.want)
}
})
}
}
func TestEnvToMap(t *testing.T) {
cases := []struct {
name string
input string
want string
}{
{
name: "Check env vars are in map",
input: "SHELL",
want: os.Getenv("SHELL"),
},
{
name: "Check unset env var is not in map",
input: "RandomUnsetEnvVar",
want: "",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
environmap := EnvToMap()
got := environmap[c.input]
if !reflect.DeepEqual(got, c.want) {
t.Errorf("got: %#v\nwant: %#v\n", got, c.want)
}
})
}
}
func TestGetConfigData(t *testing.T) {
type ConfigFile struct {
file string
scope string
}
cases := []struct {
name string
configFiles []ConfigFile
templateFile string
wantFile string
scope string
allowMissingValues bool
}{
{
name: "Check simple.yaml file works",
configFiles: []ConfigFile{
{file: "./test/TestConfigData/simple.yaml"}},
templateFile: "./test/TestConfigData/simple.yaml.tmpl",
wantFile: "./test/TestConfigData/simple.yaml",
},
{
name: "Check nested-data.yaml file works",
configFiles: []ConfigFile{
{file: "./test/TestConfigData/nested-data.yaml"},
},
templateFile: "./test/TestConfigData/nested-data.yaml.tmpl",
wantFile: "./test/TestConfigData/nested-data.yaml",
},
{
name: "Check data-with-env.yaml file works",
configFiles: []ConfigFile{
{file: "./test/TestConfigData/data-with-env.yaml"},
},
templateFile: "./test/TestConfigData/data-with-env.yaml.tmpl",
wantFile: "./test/TestConfigData/data-with-env.yaml",
},
{
name: "Check chart.yaml with variable.yaml works",
configFiles: []ConfigFile{
{file: "./test/TestConfigData/chart.yaml", scope: "Chart"},
{file: "./test/TestConfigData/chart-values.yaml", scope: "Values"},
},
templateFile: "./test/TestConfigData/chart-deploy.yaml.tmpl",
wantFile: "./test/TestConfigData/chart-deploy.yaml",
allowMissingValues: true,
},
}
os.Setenv("KD_TEST_DATA", "test-data")
// Probably want to test by rendering templates???
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
confMap := make(map[string]interface{})
var config interface{}
scoped := false
if len(c.configFiles) > 1 {
scoped = true
}
for _, cf := range c.configFiles {
var err error
// Get config and merge with environment !scoped
config, err = GetConfigData(cf.file, !scoped)
if err != nil {
log.Fatal(err)
}
if scoped {
// add the config to the right key to scope vars with
confMap[cf.scope] = config
}
}
if scoped {
config = confMap
}
api := NewK8ApiNoop()
wantText := readfile(c.wantFile)
allowMissingVariables = c.allowMissingValues
rendered, _, err := Render(api, readfile(c.templateFile), config)
if err != nil {
t.Errorf("Render error - %s", err)
}
if !reflect.DeepEqual(rendered, wantText) {
t.Errorf("got: %#v\nwant: %#v\n", rendered, wantText)
}
})
}
}