forked from LeanerCloud/AutoSpotting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
79 lines (68 loc) · 1.71 KB
/
config_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
// Copyright (c) 2016-2022 Cristian Măgherușan-Stanciu
// Licensed under the Open Software License version 3.0
package autospotting
import (
"os"
"strings"
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestParseConfig(t *testing.T) {
tests := []struct {
name string
environment map[string]string
}{
{
name: "default settings",
},
{
name: "with AWS_REGION set",
environment: map[string]string{
"AWS_REGION": "us-west-2",
},
},
{
name: "with LICENSE set",
environment: map[string]string{
"LICENSE": "I_built_it_from_source_code",
},
},
}
// save copy of environment before we run any tests
envVars := make(map[string]string)
for _, item := range os.Environ() {
e := strings.SplitN(item, "=", 2)
envVars[e[0]] = e[1]
}
for _, tt := range tests {
if tt.environment != nil {
for key, value := range tt.environment {
os.Setenv(key, value)
}
}
t.Run(tt.name, func(t *testing.T) {
config := Config{}
ParseConfig(&config)
if tt.environment != nil {
if tt.environment["AWS_REGION"] != "" {
assert.Equal(t, config.MainRegion, tt.environment["AWS_REGION"])
} else {
assert.Equal(t, config.MainRegion, "us-east-1", "MainRegion should default to us-east-1")
}
if tt.environment["LICENSE"] != "" {
assert.Equal(t, config.LicenseType, tt.environment["LICENSE"])
}
}
assert.Equal(t, config.LogFile, os.Stdout)
assert.Equal(t, config.SleepMultiplier, time.Duration(1))
assert.Assert(t, config.InstanceData != nil, "expected InstanceData to be initialized")
})
// reset environment variables
if tt.environment != nil {
for name := range tt.environment {
os.Setenv(name, envVars[name])
}
}
}
}