-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcmd_test.go
237 lines (228 loc) · 7.51 KB
/
cmd_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"os"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestAllFlags(t *testing.T) {
testCases := []struct {
name string
env map[string]string
cli []string
want globalConfig
}{
{
name: "defaults only",
env: nil,
cli: nil,
want: globalConfig{
endpoints: []string{"127.0.0.1:2379"},
useClusterEndpoints: false,
excludeLocalhost: false,
moveLeader: false,
dialTimeout: 2 * time.Second,
commandTimeout: 30 * time.Second,
keepAliveTime: 2 * time.Second,
keepAliveTimeout: 6 * time.Second,
insecure: true,
insecureSkepVerify: false,
certFile: "",
keyFile: "",
caFile: "",
username: "",
password: "",
dnsDomain: "",
dnsService: "",
insecureDiscovery: true,
compaction: true,
continueOnError: true,
dbQuotaBytes: 2 * 1024 * 1024 * 1024,
defragRule: "",
printVersion: false,
dryRun: false,
},
},
{
name: "all from environment",
env: map[string]string{
"ETCD_DEFRAG_ENDPOINTS": "10.0.0.1:2379,10.0.0.2:2379",
"ETCD_DEFRAG_CLUSTER": "true",
"ETCD_DEFRAG_EXCLUDE_LOCALHOST": "true",
"ETCD_DEFRAG_MOVE_LEADER": "true",
"ETCD_DEFRAG_DIAL_TIMEOUT": "5s",
"ETCD_DEFRAG_COMMAND_TIMEOUT": "45s",
"ETCD_DEFRAG_KEEPALIVE_TIME": "3s",
"ETCD_DEFRAG_KEEPALIVE_TIMEOUT": "8s",
"ETCD_DEFRAG_INSECURE_TRANSPORT": "false",
"ETCD_DEFRAG_INSECURE_SKIP_TLS_VERIFY": "true",
"ETCD_DEFRAG_CERT": "/path/to/cert",
"ETCD_DEFRAG_KEY": "/path/to/key",
"ETCD_DEFRAG_CACERT": "/path/to/ca",
"ETCD_DEFRAG_USER": "envuser",
"ETCD_DEFRAG_PASSWORD": "envpassword",
"ETCD_DEFRAG_DISCOVERY_SRV": "mydomain.com",
"ETCD_DEFRAG_DISCOVERY_SRV_NAME": "etcd",
"ETCD_DEFRAG_INSECURE_DISCOVERY": "false",
"ETCD_DEFRAG_COMPACTION": "false",
"ETCD_DEFRAG_CONTINUE_ON_ERROR": "false",
"ETCD_DEFRAG_ETCD_STORAGE_QUOTA_BYTES": "1073741824",
"ETCD_DEFRAG_DEFRAG_RULE": "size(db) > 500MB",
"ETCD_DEFRAG_VERSION": "true",
"ETCD_DEFRAG_DRY_RUN": "true",
},
cli: nil,
want: globalConfig{
endpoints: []string{"10.0.0.1:2379", "10.0.0.2:2379"},
useClusterEndpoints: true,
excludeLocalhost: true,
moveLeader: true,
dialTimeout: 5 * time.Second,
commandTimeout: 45 * time.Second,
keepAliveTime: 3 * time.Second,
keepAliveTimeout: 8 * time.Second,
insecure: false,
insecureSkepVerify: true,
certFile: "/path/to/cert",
keyFile: "/path/to/key",
caFile: "/path/to/ca",
username: "envuser",
password: "envpassword",
dnsDomain: "mydomain.com",
dnsService: "etcd",
insecureDiscovery: false,
compaction: false,
continueOnError: false,
dbQuotaBytes: 1073741824,
defragRule: "size(db) > 500MB",
printVersion: true,
dryRun: true,
},
},
{
name: "all from CLI (override environment)",
env: map[string]string{
"ETCD_DEFRAG_ENDPOINTS": "shouldBeOverridden:9999",
"ETCD_DEFRAG_CLUSTER": "false",
"ETCD_DEFRAG_MOVE_LEADER": "false",
"ETCD_DEFRAG_INSECURE_TRANSPORT": "true",
},
cli: []string{
"--endpoints=192.168.1.100:2379,192.168.1.101:2379",
"--cluster=true",
"--exclude-localhost=true",
"--move-leader=true",
"--dial-timeout=7s",
"--command-timeout=50s",
"--keepalive-time=4s",
"--keepalive-timeout=10s",
"--insecure-transport=false",
"--insecure-skip-tls-verify=true",
"--cert=/cli/cert",
"--key=/cli/key",
"--cacert=/cli/ca",
"--user=cliuser",
"--password=clipass",
"--discovery-srv=cli.mydomain",
"--discovery-srv-name=clietcd",
"--insecure-discovery=false",
"--compaction=false",
"--continue-on-error=false",
"--etcd-storage-quota-bytes=999999999",
"--defrag-rule=size(db) >= 1GB",
"--version=true",
"--dry-run=true",
},
want: globalConfig{
endpoints: []string{"192.168.1.100:2379", "192.168.1.101:2379"},
useClusterEndpoints: true,
excludeLocalhost: true,
moveLeader: true,
dialTimeout: 7 * time.Second,
commandTimeout: 50 * time.Second,
keepAliveTime: 4 * time.Second,
keepAliveTimeout: 10 * time.Second,
insecure: false,
insecureSkepVerify: true,
certFile: "/cli/cert",
keyFile: "/cli/key",
caFile: "/cli/ca",
username: "cliuser",
password: "clipass",
dnsDomain: "cli.mydomain",
dnsService: "clietcd",
insecureDiscovery: false,
compaction: false,
continueOnError: false,
dbQuotaBytes: 999999999,
defragRule: "size(db) >= 1GB",
printVersion: true,
dryRun: true,
},
},
{
name: "mixed env + CLI",
env: map[string]string{
"ETCD_DEFRAG_ENDPOINTS": "env:2379",
"ETCD_DEFRAG_CLUSTER": "false",
"ETCD_DEFRAG_MOVE_LEADER": "true",
"ETCD_DEFRAG_COMPACTION": "false",
"ETCD_DEFRAG_ETCD_STORAGE_QUOTA_BYTES": "555555555",
},
cli: []string{
"--exclude-localhost=true", // override the default
"--dial-timeout=10s", // override the default
"--compaction=true", // override the env
},
want: globalConfig{
endpoints: []string{"env:2379"},
useClusterEndpoints: false, // env sets cluster=false
excludeLocalhost: true, // from CLI
moveLeader: true, // from env
dialTimeout: 10 * time.Second,
commandTimeout: 30 * time.Second, // default
keepAliveTime: 2 * time.Second, // default
keepAliveTimeout: 6 * time.Second, // default
insecure: true, // default
insecureSkepVerify: false, // default
certFile: "",
keyFile: "",
caFile: "",
username: "",
password: "",
dnsDomain: "",
dnsService: "",
insecureDiscovery: true,
compaction: true, // CLI override
continueOnError: true, // default
dbQuotaBytes: 555555555, // from env
defragRule: "",
printVersion: false, // default
dryRun: false, // default
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
viper.Reset()
os.Clearenv()
for key, val := range tc.env {
if err := os.Setenv(key, val); err != nil {
t.Fatalf("failed to set env %s=%s: %v", key, val, err)
}
}
cmd := newDefragCommand()
// Set empty Run function to avoid actual execution
cmd.Run = func(cmd *cobra.Command, args []string) {}
if tc.cli != nil {
cmd.SetArgs(tc.cli)
}
if err := cmd.Execute(); err != nil {
t.Fatalf("command execution failed: %v", err)
}
require.Equal(t, tc.want, globalCfg)
})
}
}