-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_loader_test.go
69 lines (60 loc) · 2.06 KB
/
config_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
package go_config_extender_test
import (
"github.com/stretchr/testify/assert"
gce "github.com/wissance/go-config-extender"
sf "github.com/wissance/stringFormatter"
"os"
"path"
"testing"
)
type testServerConfig struct {
Address string `json:"address"`
Port int `json:"port"`
}
type testHttpLoggingConfig struct {
Enabled bool `json:"enabled"`
}
type testLoggingConfig struct {
Level string `json:"level"`
HttpLog testHttpLoggingConfig `json:"httpLog"`
}
type testSensorsConfig struct {
Threshold float64 `json:"threshold"`
}
type testConfig struct {
Server testServerConfig `json:"server"`
Logging testLoggingConfig `json:"logging"`
Sensors testSensorsConfig `json:"sensors"`
}
func TestLoadJSONConfigWithEnvOverride(t *testing.T) {
// 1. Set some tech vars
portEnvVar := "__server.port"
httpLoggingEnabledEnvVar := "__logging.httpLog.enabled"
sensorsThresholdEnvVar := "__sensors.threshold"
expectedPortValue := 6000
expectedEnabledValues := true
expectedThresholdValue := 0.25
err := addTechEnvVarForTest(portEnvVar, sf.Format("{0}", expectedPortValue))
assert.NoError(t, err)
err = addTechEnvVarForTest(httpLoggingEnabledEnvVar, sf.Format("{0}", expectedEnabledValues))
assert.NoError(t, err)
err = addTechEnvVarForTest(sensorsThresholdEnvVar, sf.Format("{0}", expectedThresholdValue))
assert.NoError(t, err)
// 2. Load config, check envs were applied
configFile := path.Join(".", "testConfigs", "testConfig1.json")
cfg, err := gce.LoadJSONConfigWithEnvOverride[testConfig](configFile)
assert.NoError(t, err)
assert.Equal(t, expectedPortValue, cfg.Server.Port)
assert.Equal(t, expectedEnabledValues, cfg.Logging.HttpLog.Enabled)
assert.Equal(t, expectedThresholdValue, cfg.Sensors.Threshold)
// 3. Drop all tech vars
_ = removeTechEnvVarForTest(portEnvVar)
_ = removeTechEnvVarForTest(httpLoggingEnabledEnvVar)
_ = removeTechEnvVarForTest(sensorsThresholdEnvVar)
}
func addTechEnvVarForTest(key string, value string) error {
return os.Setenv(key, value)
}
func removeTechEnvVarForTest(key string) error {
return os.Unsetenv(key)
}