-
Notifications
You must be signed in to change notification settings - Fork 1
/
environment_loader_test.go
181 lines (131 loc) · 4.94 KB
/
environment_loader_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
package bconf_test
import (
"fmt"
"os"
"strings"
"testing"
"github.com/rheisen/bconf"
)
func TestEnvironmentLoaderFunctions(t *testing.T) {
l := bconf.NewEnvironmentLoader()
if l == nil {
t.Fatalf("unexpected nil environment loader")
}
l = bconf.NewEnvironmentLoaderWithKeyPrefix("key_prefix")
if l.KeyPrefix != "key_prefix" {
t.Fatalf("unexpected key prefix: %s", l.KeyPrefix)
}
}
func TestEnvironmentLoaderGetMap(t *testing.T) {
const appFieldSetKey = "app"
const idFieldKey = "id"
const secretFieldKey = "secret"
const appIDValue = "test-app-id"
const appSecretValue = "test-sensitive-value"
appIDEnvironmentVariable := strings.ToUpper(fmt.Sprintf("%s_%s", appFieldSetKey, idFieldKey))
appSecretEnvironmentVariable := strings.ToUpper(fmt.Sprintf("%s_%s", appFieldSetKey, secretFieldKey))
os.Setenv(appIDEnvironmentVariable, appIDValue)
os.Setenv(appSecretEnvironmentVariable, appSecretValue)
loader := bconf.NewEnvironmentLoader()
appMap := loader.GetMap(appFieldSetKey, []string{idFieldKey, secretFieldKey, "invalid_field_key"})
if len(appMap) != 2 {
t.Fatalf("unexpected appMap length '%d', expected '2'", len(appMap))
}
}
func TestEnvironmentLoader(t *testing.T) {
const sessionFieldSet = "session"
const sessionTokenKey = "token"
const logFieldSet = "log"
const logLevelKey = "level"
const sessionKeyValue = "abc123"
const logLevelValue = "error"
envSessionTokenKey := strings.ToUpper(fmt.Sprintf("%s_%s", sessionFieldSet, sessionTokenKey))
envLogLevelKey := strings.ToUpper(fmt.Sprintf("%s_%s", logFieldSet, logLevelKey))
os.Setenv(envSessionTokenKey, sessionKeyValue)
os.Setenv(envLogLevelKey, logLevelValue)
l := bconf.EnvironmentLoader{}
clone := l.Clone()
if l.Name() != "bconf_environment" {
t.Errorf("unexpected loader name: '%s'", l.Name())
}
if !strings.Contains(l.HelpString(sessionFieldSet, sessionTokenKey), envSessionTokenKey) {
t.Errorf("unexpected loader help string contents: '%s'", l.HelpString(sessionFieldSet, sessionTokenKey))
}
if !strings.Contains(clone.HelpString(sessionFieldSet, sessionTokenKey), envSessionTokenKey) {
t.Errorf(
"unexpected loader clone help string contents: '%s'",
clone.HelpString(sessionFieldSet, sessionTokenKey),
)
}
sessionKeyLookup, found := l.Get(sessionFieldSet, sessionTokenKey)
if !found {
t.Errorf("unexpected problem getting session_key value")
}
if sessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session key: '%s'", sessionKeyLookup)
}
logLevel, found := l.Get(logFieldSet, logLevelKey)
if !found {
t.Errorf("unexpected problem getting log_level value")
}
if logLevel != logLevelValue {
t.Errorf("unexpected value for log level: '%s'", logLevel)
}
cloneSessionKeyLookup, cloneFound := clone.Get(sessionFieldSet, sessionTokenKey)
if !cloneFound {
t.Errorf("unexpected problem getting session_key value from loader clone")
}
if cloneSessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session_key from loader clone: '%s'", cloneSessionKeyLookup)
}
}
func TestEnvironmentLoaderWithKeyPrefix(t *testing.T) {
const sessionFieldSet = "session"
const sessionTokenKey = "token"
const logFieldSet = "log"
const logLevelKey = "level"
const sessionKeyValue = "abc123"
const logLevelValue = "error"
const keyPrefix = "ext_http_api"
envSessionKey := strings.ToUpper(fmt.Sprintf("%s_%s_%s", keyPrefix, sessionFieldSet, sessionTokenKey))
envLogLevelKey := strings.ToUpper(fmt.Sprintf("%s_%s_%s", keyPrefix, logFieldSet, logLevelKey))
os.Setenv(envSessionKey, sessionKeyValue)
os.Setenv(envLogLevelKey, logLevelValue)
l := bconf.EnvironmentLoader{
KeyPrefix: keyPrefix,
}
clone := l.Clone()
if l.Name() != "bconf_environment" {
t.Errorf("unexpected loader name: '%s'", l.Name())
}
if !strings.Contains(l.HelpString(sessionFieldSet, sessionTokenKey), envSessionKey) {
t.Errorf("unexpected loader help string contents: '%s'", clone.HelpString(sessionFieldSet, sessionTokenKey))
}
if !strings.Contains(clone.HelpString(sessionFieldSet, sessionTokenKey), envSessionKey) {
t.Errorf(
"unexpected loader clone help string contents: '%s'",
clone.HelpString(sessionFieldSet, sessionTokenKey),
)
}
sessionKeyLookup, found := l.Get(sessionFieldSet, sessionTokenKey)
if !found {
t.Errorf("unexpected problem getting session_key value")
}
if sessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session key: '%s'", sessionKeyLookup)
}
logLevel, found := l.Get(logFieldSet, logLevelKey)
if !found {
t.Errorf("unexpected problem getting log_level value")
}
if logLevel != logLevelValue {
t.Errorf("unexpected value for log level: '%s'", logLevel)
}
cloneSessionKeyLookup, cloneFound := clone.Get(sessionFieldSet, sessionTokenKey)
if !cloneFound {
t.Errorf("unexpected problem getting session_key value from loader clone")
}
if cloneSessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session_key from loader clone: '%s'", cloneSessionKeyLookup)
}
}