-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathentrypoint_test.go
127 lines (111 loc) · 2.85 KB
/
entrypoint_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
package main
import (
"os"
"regexp"
"testing"
)
func TestUpdateConfigContent(t *testing.T) {
original := `
example TEST
another FIELD
`
expected := `
example val
another green
`
replacements := []Replacement{
Replacement{
Key: "TEST",
Value: "val",
},
Replacement{
Key: "FIELD",
Value: "green",
},
}
results := UpdateConfigContent([]byte(original), replacements)
if string(results) != expected {
t.Error("Results to not match expected. Results:", results)
t.FailNow()
}
}
func TestBuildReplacementsFromEnv(t *testing.T) {
// Test failure for required env var
_, err := BuildReplacementsFromEnv()
if err == nil {
t.Error("BuildReplacementsFromEnv should have failed because no env vars have been set")
t.FailNow()
}
setRequiredEnvVars()
replacements, err := BuildReplacementsFromEnv()
if err != nil {
t.Error(err)
t.FailNow()
}
replacementsCount := len(replacements)
if replacementsCount != 6 {
t.Error("Replacements did not have enough entries, only found", replacementsCount, "but expected 6")
t.FailNow()
}
}
func TestReadUpdateWrite(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Error("Unable to get current working directory for TestReadUpdateWrite")
t.FailNow()
}
readFile := dir + "/traefik.toml"
writeFile := dir + "/traefik_test.toml"
// If writeFile already exists, delete it
if _, err := os.Stat(writeFile); err == nil {
os.Remove(writeFile)
}
configToml, err := ReadTraefikToml(readFile)
if err != nil {
t.Error(err)
t.FailNow()
}
// Make sure placeholders for env vars exist
envVars := GetEnvVarModels()
for _, envvar := range envVars {
search := regexp.MustCompile(envvar.Name)
found := search.Find(configToml)
if found == nil {
t.Error("Did not find key in configToml template for env var", envvar.Name)
t.FailNow()
}
}
// Update config with required env var values
setRequiredEnvVars()
replacements, err := BuildReplacementsFromEnv()
if err != nil {
t.Error(err)
t.FailNow()
}
configToml = UpdateConfigContent(configToml, replacements)
// Make sure placeholders for required env vars no longer exist in configToml
for _, envvar := range envVars {
if envvar.Required {
search := regexp.MustCompile(envvar.Name)
found := search.Find(configToml)
if found != nil {
t.Error("Uh oh, placeholder for required env var still present after update for env var:", envvar.Name)
t.FailNow()
}
}
}
// Write out test file for manual reivew
err = WriteTraefikToml(writeFile, configToml)
if err != nil {
t.Error(err)
t.FailNow()
}
}
func setRequiredEnvVars() {
os.Setenv("LETS_ENCRYPT_EMAIL", "test@testing.com")
os.Setenv("LETS_ENCRYPT_CA", "staging")
os.Setenv("TLD", "testing.com")
os.Setenv("SANS", "test.testing.com,another.testing.com")
os.Setenv("BACKEND1_URL", "http://app:80")
os.Setenv("FRONTEND1_DOMAIN", "test.testing.com")
}