-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
138 lines (131 loc) · 3.2 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
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
package lugo4go
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/lugobots/lugo4go/v3/proto"
)
func TestLoadConfig(t *testing.T) {
caseList := map[string]struct {
args []string
env map[string]string
expectedError string
expectedConfig Config
}{
"default values": {
expectedError: "",
expectedConfig: Config{
GRPCAddress: "localhost:5000",
TeamSide: proto.Team_HOME,
Number: 0,
Token: "",
Insecure: false,
},
},
"every thing changed from flags": {
args: []string{
"--grpc_address", "localhost:1212",
"--team", "home",
"--number", "7",
"--token", "a-token",
"--insecure", "no",
},
expectedError: "",
expectedConfig: Config{
GRPCAddress: "localhost:1212",
TeamSide: proto.Team_AWAY,
Number: 7,
Token: "a-token",
Insecure: false,
},
},
"every thing changed from environment variables": {
env: map[string]string{
EnvVarBotGrpcUrl: "another-host:5000",
EnvVarBotTeam: "HOME",
EnvVarBotNumber: "5",
EnvVarBotToken: "another-token",
EnvVarBotGrpcInsecure: "false",
},
expectedError: "",
expectedConfig: Config{
GRPCAddress: "another-host:5000",
TeamSide: proto.Team_HOME,
Number: 4,
Token: "another-token",
Insecure: false,
},
},
"env variables should override params": {
args: []string{
"--grpc_address", "localhost:1212",
"--team", "away",
"--number", "7",
"--token", "a-token",
"--insecure", "yes",
},
env: map[string]string{
EnvVarBotGrpcUrl: "another-host:5000",
EnvVarBotTeam: "HOME",
EnvVarBotNumber: "5",
EnvVarBotToken: "another-token",
EnvVarBotGrpcInsecure: "false",
},
expectedError: "",
expectedConfig: Config{
GRPCAddress: "another-host:5000",
TeamSide: proto.Team_HOME,
Number: 4,
Token: "another-token",
Insecure: false,
},
},
"invalid team": {
env: map[string]string{
EnvVarBotTeam: "another",
},
expectedError: "invalid team option",
expectedConfig: Config{},
},
"invalid player number - greater than 11": {
env: map[string]string{
EnvVarBotNumber: "12",
},
expectedError: "invalid player number",
expectedConfig: Config{},
},
"invalid player number - less than 1": {
env: map[string]string{
EnvVarBotNumber: "0",
},
expectedError: "invalid player number",
expectedConfig: Config{},
},
"invalid insecure flag": {
env: map[string]string{
EnvVarBotGrpcInsecure: "maybe",
},
expectedError: "invalid gRPC insecure flag",
expectedConfig: Config{},
},
}
for caseName, tc := range caseList {
t.Run(caseName, func(t *testing.T) {
defer func() {
for varName := range tc.env {
_ = os.Unsetenv(varName)
}
}()
for varName, varValue := range tc.env {
_ = os.Setenv(varName, varValue)
}
config := Config{}
err := config.loadConfig(tc.args)
if err == nil {
assert.Equal(t, tc.expectedConfig.GRPCAddress, config.GRPCAddress, caseName)
} else {
assert.Contains(t, err.Error(), tc.expectedError, caseName)
}
})
}
}