forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapteld_test.go
397 lines (365 loc) · 10.4 KB
/
snapteld_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// +build small
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"strconv"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"github.com/vrischmann/jsonutil"
"github.com/intelsdi-x/snap/control"
"github.com/intelsdi-x/snap/mgmt/rest"
"github.com/intelsdi-x/snap/mgmt/tribe"
"github.com/intelsdi-x/snap/pkg/cfgfile"
"github.com/intelsdi-x/snap/scheduler"
)
var validCmdlineFlags_input = mockFlags{
"max-procs": "11",
"log-level": "1",
"log-path": "/no/logs/allowed",
"log-truncate": "true",
"log-colors": "true",
"max-running-plugins": "12",
"plugin-load-timeout": "20",
"plugin-trust": "1",
"auto-discover": "/no/plugins/here",
"keyring-paths": "/no/keyrings/here",
"cache-expiration": "30ms",
"control-listen-addr": "100.101.102.103",
"control-listen-port": "10400",
"pprof": "true",
"temp_dir_path": "/no/temp/files",
"tls-cert": "/no/cert/here",
"tls-key": "/no/key/here",
"ca-cert-paths": "/no/root/certs",
"disable-api": "false",
"api-port": "12400",
"api-addr": "120.121.122.123",
"rest-https": "true",
"rest-cert": "/no/rest/cert",
"rest-key": "/no/rest/key",
"rest-auth": "true",
"rest-auth-pwd": "noway",
"allowed_origins": "140.141.142.143",
"work-manager-queue-size": "70",
"work-manager-pool-size": "71",
"tribe-node-name": "bonk",
"tribe": "true",
"tribe-addr": "160.161.162.163",
"tribe-port": "16400",
"tribe-seed": "180.181.182.183",
}
var validCmdlineFlags_expected = &Config{
Control: &control.Config{
MaxRunningPlugins: 12,
PluginLoadTimeout: 20,
PluginTrust: 1,
AutoDiscoverPath: "/no/plugins/here",
KeyringPaths: "/no/keyrings/here",
CacheExpiration: jsonutil.Duration{30 * time.Millisecond},
ListenAddr: "100.101.102.103",
ListenPort: 10400,
Pprof: true,
TempDirPath: "/no/temp/files",
TLSCertPath: "/no/cert/here",
TLSKeyPath: "/no/key/here",
CACertPaths: "/no/root/certs",
},
RestAPI: &rest.Config{
Enable: true,
Port: 12400,
Address: "120.121.122.123:12400",
HTTPS: true,
RestCertificate: "/no/rest/cert",
RestKey: "/no/rest/key",
RestAuth: true,
RestAuthPassword: "noway",
Pprof: true,
Corsd: "140.141.142.143",
},
Tribe: &tribe.Config{
Name: "bonk",
Enable: true,
BindAddr: "160.161.162.163",
BindPort: 16400,
Seed: "180.181.182.183",
},
Scheduler: &scheduler.Config{
WorkManagerQueueSize: 70,
WorkManagerPoolSize: 71,
},
GoMaxProcs: 11,
LogLevel: 1,
LogPath: "/no/logs/allowed",
LogTruncate: true,
LogColors: true,
}
func TestSnapConfig(t *testing.T) {
Convey("Test Config", t, func() {
Convey("with defaults", func() {
cfg := getDefaultConfig()
jb, _ := json.Marshal(cfg)
serrs := cfgfile.ValidateSchema(CONFIG_CONSTRAINTS, string(jb))
So(len(serrs), ShouldEqual, 0)
})
})
}
type mockFlags map[string]string
func (m mockFlags) String(key string) string {
return m[key]
}
func (m mockFlags) Int(key string) int {
if v, err := strconv.Atoi(m[key]); err == nil {
return v
}
return 0
}
func (m mockFlags) Bool(key string) bool {
if v, err := strconv.ParseBool(m[key]); err == nil {
return v
}
return false
}
func (m mockFlags) IsSet(key string) bool {
_, gotIt := m[key]
return gotIt
}
func (m mockFlags) getCopy() mockFlags {
r := mockFlags{}
for k, v := range m {
r[k] = v
}
return r
}
func (m mockFlags) copyWithout(keys ...string) mockFlags {
r := m.getCopy()
for _, k := range keys {
delete(r, k)
}
return r
}
func (m mockFlags) update(key, value string) mockFlags {
m[key] = value
return m
}
type mockCfg Config
func (c *mockCfg) setTLSCert(tlsCertPath string) *mockCfg {
c.Control.TLSCertPath = tlsCertPath
return c
}
func (c *mockCfg) setTLSKey(tlsKeyPath string) *mockCfg {
c.Control.TLSKeyPath = tlsKeyPath
return c
}
func (c *mockCfg) setCACertPaths(caCertPaths string) *mockCfg {
c.Control.CACertPaths = caCertPaths
return c
}
func (c *mockCfg) setApiAddr(apiAddr string) *mockCfg {
c.RestAPI.Address = apiAddr
return c
}
func (c *mockCfg) getCopy() (r *mockCfg) {
r = &mockCfg{}
b, err := json.Marshal(*c)
if err != nil {
panic(err)
}
err = json.Unmarshal(b, r)
if err != nil {
panic(err)
}
return r
}
func (c *mockCfg) export() *Config {
return (*Config)(c)
}
func Test_checkCmdLineFlags(t *testing.T) {
testCtx := mockFlags{
"tls-cert": "mock-cli.crt",
"tls-key": "mock-cli.key",
"ca-cert-paths": "mock-ca.crt",
"api-addr": "localhost",
"api-port": "9000"}
tests := []struct {
name string
msg func(func(string))
ctx runtimeFlagsContext
wantErr bool
wantPort int
wantPortInAddr bool
}{
{name: "CmdlineArgsParseWell",
msg: func(f func(string)) {
f("Having valid command line flags, parsing suceeds")
},
ctx: testCtx.getCopy(),
wantErr: false,
wantPort: 9000,
wantPortInAddr: false},
{name: "CmdlineArgsWithoutTLSConfigParseWell",
msg: func(f func(string)) {
f("Having valid command line flags without any TLS parameters, parsing suceeds")
},
ctx: testCtx.
copyWithout("tls-cert", "tls-key", "ca-cert-paths", "api-port").
update("api-addr", "127.0.0.1:9002"),
wantErr: false,
wantPort: 9002,
wantPortInAddr: true},
{name: "ArgsWithTLSCertWithoutKey_Fail",
msg: func(f func(string)) {
f("Having command line flags with TLS cert without key, parsing fails")
},
ctx: testCtx.copyWithout("tls-key"),
wantErr: true,
},
{name: "ArgsWithTLSKeyWithoutCert_Fail",
msg: func(f func(string)) {
f("Having command line flags with TLS key without cert, parsing fails")
},
ctx: testCtx.copyWithout("tls-cert"),
wantErr: true,
},
}
for _, tc := range tests {
runThisCase := func(f func(msg string)) {
t.Run(tc.name, func(_ *testing.T) {
tc.msg(f)
})
}
runThisCase(func(msg string) {
Convey(msg, t, func() {
gotPort, gotPortInAddr, err := checkCmdLineFlags(tc.ctx)
if tc.wantErr {
So(err, ShouldNotBeNil)
return
}
So(err, ShouldBeNil)
So(gotPort, ShouldEqual, tc.wantPort)
So(gotPortInAddr, ShouldEqual, tc.wantPortInAddr)
})
})
}
}
func Test_checkCfgSettings(t *testing.T) {
const DontCheckInt = -99
testCfg := &mockCfg{
Control: &control.Config{},
RestAPI: &rest.Config{},
}
tests := []struct {
name string
msg func(func(string))
cfg *Config
wantErr bool
wantPort int
wantPortInAddr bool
}{
{name: "DefaultConfigSettingsValidateWell",
msg: func(f func(string)) {
f("Having all default (empty) values for config, validation succeeds")
},
cfg: (&mockCfg{Control: control.GetDefaultConfig(), RestAPI: rest.GetDefaultConfig()}).export(),
wantErr: false,
wantPort: DontCheckInt,
wantPortInAddr: false},
{name: "ConfigSettingsValidateWell",
msg: func(f func(string)) {
f("Having correct values, config validation succeeds")
},
cfg: testCfg.getCopy().
setApiAddr("localhost:9000").
setTLSCert("mock-cli.crt").
setTLSKey("mock-cli.key").
setCACertPaths("mock-ca.crt").
export(),
wantErr: false,
wantPort: 9000,
wantPortInAddr: true},
{name: "ConfigSettingsWithoutTLSConfigValidateWell",
msg: func(f func(string)) {
f("Having correct values without any TLS parameters, config validation succeeds")
},
cfg: testCfg.getCopy().
setApiAddr("localhost:9000").
export(),
wantErr: false,
wantPort: 9000,
wantPortInAddr: true},
{name: "ConfigSettingsWithTLSCertWithoutKey_Fail",
msg: func(f func(string)) {
f("Having config with TLS cert without key, config fails to validate")
},
cfg: testCfg.getCopy().
setApiAddr("localhost:9000").
setTLSCert("mock-cli.crt").
export(),
wantErr: true,
wantPort: 9000,
wantPortInAddr: true},
{name: "ConfigSettingsWithTLSKeyWithoutCert_Fail",
msg: func(f func(string)) {
f("Having config with TLS key without cert, config fails to validate")
},
cfg: testCfg.getCopy().
setApiAddr("localhost:9000").
setTLSKey("mock-cli.crt").
export(),
wantErr: true,
wantPort: 9000,
wantPortInAddr: true},
}
for _, tc := range tests {
runThisCase := func(f func(msg string)) {
t.Run(tc.name, func(_ *testing.T) {
tc.msg(f)
})
}
runThisCase(func(msg string) {
Convey(msg, t, func() {
gotPort, gotPortInAddr, err := checkCfgSettings(tc.cfg)
if tc.wantErr {
So(err, ShouldNotBeNil)
return
}
So(err, ShouldBeNil)
if tc.wantPort != DontCheckInt {
So(gotPort, ShouldEqual, tc.wantPort)
}
So(gotPortInAddr, ShouldEqual, tc.wantPortInAddr)
})
})
}
}
func Test_applyCmdLineFlags(t *testing.T) {
Convey("Having arguments given on command line", t, func() {
gotConfig := Config{
Control: &control.Config{},
RestAPI: &rest.Config{},
Tribe: &tribe.Config{},
Scheduler: &scheduler.Config{},
}
applyCmdLineFlags(&gotConfig, validCmdlineFlags_input)
Convey("config should be filled with correct values", func() {
So(*gotConfig.Control, ShouldResemble, *validCmdlineFlags_expected.Control)
So(*gotConfig.RestAPI, ShouldResemble, *validCmdlineFlags_expected.RestAPI)
So(*gotConfig.Tribe, ShouldResemble, *validCmdlineFlags_expected.Tribe)
So(*gotConfig.Scheduler, ShouldResemble, *validCmdlineFlags_expected.Scheduler)
So(gotConfig, ShouldResemble, *validCmdlineFlags_expected)
})
})
}