forked from skynetservices/skynet-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
176 lines (139 loc) · 3.97 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
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
package skynet
import (
"flag"
"fmt"
"os"
"testing"
)
func clearEnv() {
os.Setenv("SKYNET_BIND_IP", "")
os.Setenv("SKYNET_MIN_PORT", "")
os.Setenv("SKYNET_MAX_PORT", "")
os.Setenv("SKYNET_DZHOST", "")
os.Setenv("SKYNET_DZNSHOST", "")
os.Setenv("SKYNET_DZDISCOVER", "")
os.Setenv("SKYNET_MGOSERVER", "")
os.Setenv("SKYNET_MGODB", "")
os.Setenv("SKYNET_REGION", "")
os.Setenv("SKYNET_VERSION", "")
}
func TestGetServiceConfigFromFlags(t *testing.T) {
clearEnv()
os.Args = []string{"test", "--l=localhost:1234", "--region=TestRegion",
"--doozer=localhost:8046", "--doozerboot=localhost:1232",
"--autodiscover=true",
}
config, _ := GetServiceConfigFromFlags(os.Args[1:])
if config.ServiceAddr.IPAddress != "localhost" {
t.Error("Address not set through flag")
}
if config.ServiceAddr.Port != 1234 {
t.Error("Port not set through flag")
}
if config.Region != "TestRegion" {
t.Error("Region not set through flag")
}
if config.DoozerConfig.Uri != "localhost:8046" {
t.Error("DoozerUri not set through flag")
}
if config.DoozerConfig.BootUri != "localhost:1232" {
t.Error("DoozerBootUri not set through flag")
}
if config.DoozerConfig.AutoDiscover != true {
t.Error("DoozerAutoDiscover not set through flag")
}
}
func TestGetServiceConfigFromFlagsDefaults(t *testing.T) {
clearEnv()
os.Args = []string{"test"}
config, _ := GetServiceConfigFromFlags(os.Args[1:])
if config.ServiceAddr.IPAddress != "127.0.0.1" {
t.Error("Address not set to default value")
}
if config.ServiceAddr.Port != 9000 {
t.Error("Port not set to default value")
}
if config.Region != "unknown" {
t.Error("Region not set to default value")
}
if config.DoozerConfig.Uri != "127.0.0.1:8046" {
fmt.Println(config.DoozerConfig.Uri)
t.Error("DoozerUri not set to default value")
}
if config.DoozerConfig.BootUri != "127.0.0.1:8046" {
t.Error("DoozerBootUri not set to default value")
}
if config.DoozerConfig.AutoDiscover != true {
t.Error("DoozerAutoDiscover not set to default value")
}
}
func TestSplitFlagsetFromArgs(t *testing.T) {
var a, b, c, e, f, g string
var d, i bool
flagset1 := flag.NewFlagSet("TestFlagFilter1", flag.ExitOnError)
flagset1.StringVar(&a, "a", "x", "a")
flagset1.StringVar(&b, "b", "x", "b")
flagset1.StringVar(&c, "c", "x", "c")
flagset1.BoolVar(&d, "d", false, "d")
flagset2 := flag.NewFlagSet("TestFlagFilter2", flag.ExitOnError)
flagset2.StringVar(&e, "e", "x", "e")
flagset2.StringVar(&f, "f", "x", "f")
flagset2.StringVar(&g, "g", "x", "g")
flagset2.BoolVar(&i, "i", false, "i")
args := []string{
// in flagset
"--a=a",
"-b=b",
"-c=c",
"-d",
// not in flagset
"--e=e",
"-f=f",
"-g=g",
"-i",
}
flagsetArgs, additionalArgs := SplitFlagsetFromArgs(flagset1, args)
// Validate flags
if len(flagsetArgs) != 4 {
t.Error("didn't split flagset")
}
if flagsetArgs[0] != args[0] {
t.Error("didn't properly split flagset")
}
if flagsetArgs[1] != args[1] {
t.Error("didn't properly split flagset")
}
if flagsetArgs[2] != args[2] {
t.Error("didn't properly split flagset")
}
if flagsetArgs[3] != args[3] {
t.Error("didn't properly split flagset")
}
if flagset1.Parse(flagsetArgs) != nil {
t.Error("didn't return proper flagset arguments, flags failed to parse")
}
if len(flagset1.Args()) > 0 {
t.Error("unparsed flags returned from parse")
}
if len(additionalArgs) != 4 {
t.Error("didn't return proper additional arguments")
}
if additionalArgs[0] != args[4] {
t.Error("didn't return proper additional arguments")
}
if additionalArgs[1] != args[5] {
t.Error("didn't return proper additional arguments")
}
if additionalArgs[2] != args[6] {
t.Error("didn't return proper additional arguments")
}
if additionalArgs[3] != args[7] {
t.Error("didn't return proper additional arguments")
}
if flagset2.Parse(additionalArgs) != nil {
t.Error("didn't return proper additional arguments, flags failed to parse")
}
if len(flagset2.Args()) > 0 {
t.Error("unparsed flags returned from parse")
}
}