-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags_test.go
159 lines (151 loc) · 2.63 KB
/
flags_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
package etcdutil
import (
"flag"
"io"
"reflect"
"testing"
)
func testMakeConfig(c *Config) *Config {
cfg := NewConfig("foo")
if c == nil {
return cfg
}
if len(c.Endpoints) > 0 {
cfg.Endpoints = c.Endpoints
}
if len(c.Prefix) > 0 {
cfg.Prefix = c.Prefix
}
if len(c.Timeout) > 0 {
cfg.Timeout = c.Timeout
}
if len(c.Username) > 0 {
cfg.Username = c.Username
}
if len(c.Password) > 0 {
cfg.Password = c.Password
}
if len(c.TLSCAFile) > 0 {
cfg.TLSCAFile = c.TLSCAFile
}
if len(c.TLSCertFile) > 0 {
cfg.TLSCertFile = c.TLSCertFile
}
if len(c.TLSKeyFile) > 0 {
cfg.TLSKeyFile = c.TLSKeyFile
}
return cfg
}
func TestFlags(t *testing.T) {
t.Parallel()
testCases := []struct {
Name string
Input []string
Expected *Config
Err bool
}{
{
"valid endpoints 1",
[]string{"-etcd-endpoints=http://12.34.56.78:2379"},
&Config{
Endpoints: []string{"http://12.34.56.78:2379"},
},
false,
},
{
"valid endpoints 2",
[]string{"-etcd-endpoints=http://12.34.56.78:2379,https://12.34.56.79:2379"},
&Config{
Endpoints: []string{"http://12.34.56.78:2379", "https://12.34.56.79:2379"},
},
false,
},
{
"invalid endpoints 1",
[]string{"-etcd-endpoints="},
nil,
true,
},
{
"invalid endpoints 2",
[]string{"-etcd-endpoints=,"},
nil,
true,
},
{
"prefix",
[]string{"-etcd-prefix=t/"},
&Config{
Prefix: "t/",
},
false,
},
{
"timeout",
[]string{"-etcd-timeout=1m"},
&Config{
Timeout: "1m",
},
false,
},
{
"username",
[]string{"-etcd-username=cybozu"},
&Config{
Username: "cybozu",
},
false,
},
{
"password",
[]string{"-etcd-password=passwd"},
&Config{
Password: "passwd",
},
false,
},
{
"ca",
[]string{"-etcd-tls-ca=/etc/ca.crt"},
&Config{
TLSCAFile: "/etc/ca.crt",
},
false,
},
{
"cert",
[]string{"-etcd-tls-cert=/etc/user.crt"},
&Config{
TLSCertFile: "/etc/user.crt",
},
false,
},
{
"key",
[]string{"-etcd-tls-key=/etc/user.key"},
&Config{
TLSKeyFile: "/etc/user.key",
},
false,
},
}
for _, c := range testCases {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.SetOutput(io.Discard)
cfg := NewConfig("foo")
cfg.AddFlags(fs)
err := fs.Parse(c.Input)
if err != nil && !c.Err {
t.Errorf("%s: unexpected error: %v", c.Name, err)
continue
}
if err == nil && c.Err {
t.Errorf("%s: expected an error", c.Name)
continue
}
expected := testMakeConfig(c.Expected)
if !reflect.DeepEqual(expected, cfg) {
t.Errorf("%s: expected=%+v, actual=%+v", c.Name, expected, cfg)
}
}
}