This repository has been archived by the owner on Mar 19, 2022. It is now read-only.
generated from KohlsTechnology/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
284 lines (236 loc) · 10.1 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
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright 2021 Kohl's Department Stores, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
var cfgDefaults = config{
hierarchyFile: "hierarchy.lst",
basePath: "",
outputFile: "output.yaml",
filterExtension: defaultFileFilter,
printVersion: false,
diffOutput: false,
logDebug: false,
logTrace: false,
failMissingHierarchy: false,
failMissingPath: false,
failMissingEnvVar: false,
skipEnvVarContent: false,
}
// TestGetFilesSuccess verifies that we receive the correct list of files to be merged
// As part of this test, it will also check the proper functioning of the regex for the file filter
// fail.txt and fail.yaml.disabled should never be returned
func TestGetFilesSuccess(t *testing.T) {
expected := []string{"testdata/default/defaults.json", "testdata/default/defaults.yml"}
result := getFiles("testdata/default", defaultFileFilter)
assert.Equal(t, expected, result)
expected = []string{"testdata/yaml/one.yaml", "testdata/yaml/two.yml"}
result = getFiles("testdata/yaml", defaultFileFilter)
assert.Equal(t, expected, result)
}
// TestProcessHierarchySuccess verifies that all directories are correctly added to the list to be processed
// It will also test the correct handling of comments and different ways of specifying a relative path
func TestProcessHierarchySuccess(t *testing.T) {
cfg := cfgDefaults
cfg.basePath = "testdata/test1"
expected := []string{"testdata/default", "testdata/yaml", "testdata/json", "testdata/empty", "testdata/test1"}
result := processHierarchy(cfg)
assert.Equal(t, expected, result)
}
// TestFailMissingPath tests the correct behavior of the `--failmissing` command line option
// It spawns a new process to determine the exit code of the application.
// Anything other than a 1 is a problem
// It uses the environment variable TEST_FAIL_EMPTY to signal the actual execution of the functionality
func TestFailMissingPath(t *testing.T) {
if os.Getenv("TEST_FAIL_EMPTY") == "1" {
cfg := cfgDefaults
cfg.basePath = "testdata/test1"
cfg.failMissingPath = true
processHierarchy(cfg)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFailMissingPath")
cmd.Env = append(os.Environ(), "TEST_FAIL_EMPTY=1")
output, err := cmd.CombinedOutput()
fmt.Printf("%s\n", output)
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
fmt.Printf("Process correctly failed with %v\n", e)
return
}
t.Fatalf("process ran with err %v, want exit status 1.", err)
}
// TestEnd2EndSuccess runs through the full functionality end-to-end
// It compares the generated final file with one stored in git
func TestEnd2EndSuccess(t *testing.T) {
cfg := cfgDefaults
cfg.basePath = "testdata/test1"
// process the hierarchy and get the list of include files
hierarchy := processHierarchy(cfg)
// Lets do the deed
mergeFilesInHierarchy(hierarchy, cfg.filterExtension, cfg.outputFile, false, false)
expected, err := ioutil.ReadFile("testdata/test1/result/expected.yaml")
if err != nil {
t.Fatalf("Error reading file with expected test results: %v", err)
}
result, err := ioutil.ReadFile(cfg.outputFile)
if err != nil {
t.Fatalf("Error reading output file: %v", err)
}
assert.Equal(t, string(expected), string(result))
}
// TestFailHierarchyMissingEnvironmentVariable ensures that the application is correctly failing
// If an environment variable specified in `hierarchy.lst` is not found.
// It spawns a new process to determine the exit code of the application.
// Anything other than a 1 is a problem
// It uses the environment variable TEST_FAIL_EMPTY to signal the actual execution of the functionality
// Hierarchy will always fail if an environment variable as part of the hierarchy is not found.
// This is intentional, to prevent unexpected behaviors.
func TestFailHierarchyMissingEnvironmentVariable(t *testing.T) {
if os.Getenv("TEST_FAIL_EMPTY") == "1" {
cfg := cfgDefaults
cfg.basePath = "testdata/hierarchy-with-env-fail"
processHierarchy(cfg)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFailHierarchyMissingEnvironmentVariable")
cmd.Env = append(os.Environ(), "TEST_FAIL_EMPTY=1")
output, err := cmd.CombinedOutput()
fmt.Printf("%s\n", output)
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
fmt.Printf("Process correctly failed with %v\n", e)
return
}
t.Fatalf("process ran with err %v, want exit status 1.", err)
}
// TestEnd2EndHierarchyEnvironmentVariablesSuccess runs through the full functionality end-to-end,
// specifically testing the correct resolution of environment variables specified in `hierarchy.lst`
// It compares the generated final file with one stored in git
func TestEnd2EndHierarchyEnvironmentVariablesSuccess(t *testing.T) {
cfg := cfgDefaults
cfg.basePath = "testdata/hierarchy-with-env"
cfg.outputFile = "output.yaml"
// set the test environment variable
os.Setenv("JSON", "json")
// process the hierarchy and get the list of include files
hierarchy := processHierarchy(cfg)
// Merge files
mergeFilesInHierarchy(hierarchy, cfg.filterExtension, cfg.outputFile, false, false)
expected, err := ioutil.ReadFile("testdata/hierarchy-with-env/result/expected.yaml")
if err != nil {
t.Fatalf("Error reading file with expected test results: %v", err)
}
result, err := ioutil.ReadFile(cfg.outputFile)
if err != nil {
t.Fatalf("Error reading output file: %v", err)
}
assert.Equal(t, string(expected), string(result))
}
// TestMissingHierarchySuccess runs through the full functionality end-to-end
// It test the case where no hierarchy.lst is provided,
// meaning only the base directory is searched for files to merge
// It compares the generated final file with one stored in git
func TestMissingHierarchySuccess(t *testing.T) {
cfg := cfgDefaults
cfg.basePath = "testdata/no-hierarchy"
// process the hierarchy and get the list of include files
hierarchy := processHierarchy(cfg)
// Lets do the deed
mergeFilesInHierarchy(hierarchy, cfg.filterExtension, cfg.outputFile, false, false)
expected, err := ioutil.ReadFile("testdata/no-hierarchy/result/expected.yaml")
if err != nil {
t.Fatalf("Error reading file with expected test results: %v", err)
}
result, err := ioutil.ReadFile(cfg.outputFile)
if err != nil {
t.Fatalf("Error reading output file: %v", err)
}
assert.Equal(t, string(expected), string(result))
}
// TestFailMissingHierarchy runs through the full functionality end-to-end
// It test the case where no hierarchy.lst is provided,
// meaning only the base directory is searched for files to merge
// It compares the generated final file with one stored in git
func TestFailMissingHierarchy(t *testing.T) {
if os.Getenv("TEST_FAIL_MISSING_HIERARCHY") == "1" {
cfg := cfgDefaults
cfg.basePath = "testdata/no-hierarchy"
cfg.failMissingHierarchy = true
processHierarchy(cfg)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFailMissingHierarchy")
cmd.Env = append(os.Environ(), "TEST_FAIL_MISSING_HIERARCHY=1")
output, err := cmd.CombinedOutput()
fmt.Printf("%s\n", output)
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
fmt.Printf("Process correctly failed with %v\n", e)
return
}
t.Fatalf("process ran with err %v, want exit status 1.", err)
}
// TestFailContentMissingEnvironmentVariable ensures that the application is correctly failing
// If an environment variable specified in the yaml content is not defined.
// It spawns a new process to determine the exit code of the application.
// Anything other than a 1 is a problem
// It uses the environment variable TEST_FAIL_EMPTY to signal the actual execution of the functionality
func TestFailContentMissingEnvironmentVariable(t *testing.T) {
if os.Getenv("TEST_FAIL_EMPTY") == "1" {
cfg := cfgDefaults
cfg.basePath = "testdata/content-with-env"
cfg.failMissingEnvVar = true
// process the hierarchy and get the list of include files
hierarchy := processHierarchy(cfg)
// Merge files in hierarchy
mergeFilesInHierarchy(hierarchy, cfg.filterExtension, cfg.outputFile, cfg.skipEnvVarContent, cfg.failMissingEnvVar)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFailContentMissingEnvironmentVariable")
cmd.Env = append(os.Environ(), "TEST_FAIL_EMPTY=1", "EXISTING_VARIABLE1=one", "EXISTING_VARIABLE2=two")
output, err := cmd.CombinedOutput()
fmt.Printf("%s\n", output)
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
fmt.Printf("Process correctly failed with %v\n", e)
return
}
t.Fatalf("process ran with err %v, want exit status 1.", err)
}
// TestContentMissingEnvironmentVariableSuccess runs through the full functionality end-to-end
// It test the case where no hierarchy.lst is provided,
// meaning only the base directory is searched for files to merge
// It compares the generated final file with one stored in git
func TestContentMissingEnvironmentVariableSuccess(t *testing.T) {
cfg := cfgDefaults
cfg.basePath = "testdata/content-with-env/"
// process the hierarchy and get the list of include files
hierarchy := processHierarchy(cfg)
// set the test environment variables
os.Setenv("EXISTING_VARIABLE1", "one")
os.Setenv("EXISTING_VARIABLE2", "two")
// merge files in hierarchy
mergeFilesInHierarchy(hierarchy, cfg.filterExtension, cfg.outputFile, cfg.skipEnvVarContent, cfg.failMissingEnvVar)
expected, err := ioutil.ReadFile("testdata/content-with-env/result/expected.yaml")
if err != nil {
t.Fatalf("Error reading file with expected test results: %v", err)
}
result, err := ioutil.ReadFile(cfg.outputFile)
if err != nil {
t.Fatalf("Error reading output file: %v", err)
}
assert.Equal(t, string(expected), string(result))
}