This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ceph_test.go
731 lines (632 loc) · 22.4 KB
/
ceph_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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
// +build small
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 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 ceph
import (
"errors"
"os"
"testing"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
. "github.com/smartystreets/goconvey/convey"
)
var ns_prefix = []string{nsVendor, nsClass, nsType}
var mockOut = []byte(`{
"a": {
"aa": 1,
"ab": 2
},
"b": {
"bavg": {
"avgcount": 6,
"sum": 3.000000000
}
},
"c": {
"ca": 1,
"cb": {
"cba": 123456.1234567890,
"cbb": 12345678901234567
},
"cc": 3
}
}`)
var mockOutSchema = []byte(`{
"a": {
"aa": {
"description": "Some metric called aa",
"nick": "",
"type": 10
},
"ab": {
"description": "Some metric called ab",
"nick": "",
"type": 10
}
},
"b": {
"bavg": {
"description": "Some delta metric",
"nick": "",
"type": 5
}
},
"c": {
"ca": {
"description": "Some metric called ca",
"nick": "",
"type": 10
},
"cb": {
"cba": {
"description": "Some metric called cba",
"nick": "",
"type": 1
},
"cbb": {
"no_description": "",
"nick": "",
"type": 10
}
},
"cc": {
"description": "Some metric called cc",
"nick": "",
"type": 10
}
}
}`)
var mockOutInvalid = []byte(`{
"a": {
"aa": 1,
"ab": 2
}`)
var mockInUnmarshal = [][]byte{
[]byte(`{ "a": { "aa": 1, "ab": 2 }, "b": { "ba": 0 } }`), //correct
[]byte(`{ {"a": { "aa": 1, "ab": 2 }, "b": { "ba": 0 } }`), //invalid character '{'
[]byte(`{ "a": { "aa": 1 "ab": 2 }, "b": { "ba": 0 } }`), //lack of a comma after value
[]byte(`{ "a": { "aa": 1, "ab": 2 } "b": { "ba": 0 } }`), //lack of a comma after }
[]byte(`{ "a" { "aa" 1, "ab" 2 } "b" { "ba" 0 } }`), //lack of ":"
}
// mocking exec command
type TestCmd struct {
outDump []byte
outSchema []byte
err error
look_path string
look_err error
env_ceph_path string
env_socket_path string
env_socket_prefix string
env_socket_ext string
}
type TestSocket struct {
path string
prefix string
ext string
}
func (t *TestCmd) perfDump(cephPath string, socketPath string) ([]byte, error) {
return t.outDump, t.err
}
func (t *TestCmd) perfSchema(cephPath string, socketPath string) ([]byte, error) {
return t.outSchema, t.err
}
func (t *TestCmd) lookPath(file string) (string, error) {
return t.look_path, t.look_err
}
func Test_checkBit(t *testing.T) {
Convey("checking enabled bits for 170", t, func() {
var number uint8 = 170
So(checkBit(number, 0), ShouldBeFalse)
So(checkBit(number, 1), ShouldBeTrue)
So(checkBit(number, 2), ShouldBeFalse)
So(checkBit(number, 3), ShouldBeTrue)
So(checkBit(number, 4), ShouldBeFalse)
So(checkBit(number, 5), ShouldBeTrue)
So(checkBit(number, 6), ShouldBeFalse)
So(checkBit(number, 7), ShouldBeTrue)
})
Convey("checking enabled bits for 85", t, func() {
var number uint8 = 85
So(checkBit(number, 0), ShouldBeTrue)
So(checkBit(number, 1), ShouldBeFalse)
So(checkBit(number, 2), ShouldBeTrue)
So(checkBit(number, 3), ShouldBeFalse)
So(checkBit(number, 4), ShouldBeTrue)
So(checkBit(number, 5), ShouldBeFalse)
So(checkBit(number, 6), ShouldBeTrue)
So(checkBit(number, 7), ShouldBeFalse)
})
Convey("checking enabled bits for 255", t, func() {
var number uint8 = 255
So(checkBit(number, 0), ShouldBeTrue)
So(checkBit(number, 1), ShouldBeTrue)
So(checkBit(number, 2), ShouldBeTrue)
So(checkBit(number, 3), ShouldBeTrue)
So(checkBit(number, 4), ShouldBeTrue)
So(checkBit(number, 5), ShouldBeTrue)
So(checkBit(number, 6), ShouldBeTrue)
So(checkBit(number, 7), ShouldBeTrue)
})
Convey("checking enabled bits for 0", t, func() {
var number uint8 = 0
So(checkBit(number, 0), ShouldBeFalse)
So(checkBit(number, 1), ShouldBeFalse)
So(checkBit(number, 2), ShouldBeFalse)
So(checkBit(number, 3), ShouldBeFalse)
So(checkBit(number, 4), ShouldBeFalse)
So(checkBit(number, 5), ShouldBeFalse)
So(checkBit(number, 6), ShouldBeFalse)
So(checkBit(number, 7), ShouldBeFalse)
})
}
func Test_trimPrefixAndSuffix(t *testing.T) {
Convey("trimming", t, func() {
So(trimPrefixAndSuffix("", "", ""), ShouldEqual, "")
So(trimPrefixAndSuffix(" ", " ", ""), ShouldEqual, "")
So(trimPrefixAndSuffix(" ", " ", ""), ShouldEqual, " ")
So(trimPrefixAndSuffix(" ", " ", " "), ShouldEqual, "")
So(trimPrefixAndSuffix("ceph-mon.a.asok", "ceph-", ".asok"), ShouldEqual, "mon.a")
So(trimPrefixAndSuffix("ceph-osd.0.asok", "ceph-", ".asok"), ShouldEqual, "osd.0")
So(trimPrefixAndSuffix("mon.a.asok", "ceph-", ".asok"), ShouldEqual, "mon.a")
So(trimPrefixAndSuffix("mon.a.asok", "", ".asok"), ShouldEqual, "mon.a")
So(trimPrefixAndSuffix("ceph-mon.a.asok", "", ".asok"), ShouldEqual, "ceph-mon.a")
So(trimPrefixAndSuffix("ceph-mon.a.asok", "", ""), ShouldEqual, "ceph-mon.a.asok")
So(trimPrefixAndSuffix("ceph-mon.a.asok", "Ceph-", ".asok"), ShouldEqual, "ceph-mon.a")
So(trimPrefixAndSuffix("Ceph-Mon.a.asok", "Ceph-", ".asok"), ShouldEqual, "Mon.a")
So(trimPrefixAndSuffix("ceph-Mon.a.ASOK", "ceph-", ".ASOK"), ShouldEqual, "Mon.a")
So(trimPrefixAndSuffix("", "ABC", "XYZ"), ShouldEqual, "")
So(trimPrefixAndSuffix("A CtestX_Z", "A C", "X_Z"), ShouldEqual, "test")
So(trimPrefixAndSuffix("ABCtestX_Z", "CBA", "X_Z"), ShouldEqual, "ABCtest")
So(trimPrefixAndSuffix(" testX_Z", "A C", "X_Z"), ShouldEqual, " test")
So(trimPrefixAndSuffix(" testX_Z", "A C", "X_Z"), ShouldEqual, " test")
So(trimPrefixAndSuffix("12Atest3 ", "12A", "3 "), ShouldEqual, "test")
})
}
func daemonsToStrings(daemons []Daemon) []string {
result := []string{}
for _, d := range daemons {
result = append(result, d.name+"."+d.id)
}
return result
}
func Test_getCephDaemonNames(t *testing.T) {
socketTestA := &Socket{path: "./test/", prefix: "prefix-", ext: "ext"}
socketTestB := &Socket{path: "./test/", prefix: "", ext: "ext"}
socketTestC := &Socket{path: "./test/", prefix: "prefix-", ext: "extB"}
socketTestD := &Socket{path: "./test/", prefix: "", ext: ""}
Convey("obtaining daemon names when there is no directory", t, func() {
daemons, err := socketTestA.getCephDaemons()
So(daemons, ShouldBeEmpty)
So(err, ShouldNotBeNil)
})
os.Mkdir("test", os.ModePerm)
Convey("obtaining daemon names when there is no file in the indicated directory", t, func() {
So(func() { socketTestA.getCephDaemons() }, ShouldNotPanic)
result, err := socketTestA.getCephDaemons()
So(err, ShouldBeNil)
So(result, ShouldBeEmpty)
})
os.Create("test/socket.aaa")
os.Create("test/socket.bbb")
Convey("obtaining daemon names when there is no proper file in the indicated directory", t, func() {
So(func() { socketTestA.getCephDaemons() }, ShouldNotPanic)
result, err := socketTestA.getCephDaemons()
So(err, ShouldBeNil)
So(result, ShouldBeEmpty)
})
os.Create("test/prefix-socket.1.ext")
os.Create("test/prefix-socket.2.ext")
os.Create("test/socket.3.ext")
os.Create("test/prefix-socket.4.extB")
daemonName := make(map[string]Daemon)
daemonName["socket3.ext"] = Daemon{id: "", name: "socket3", fullName: "socket3.ext"}
Convey("obtaining daemon names from existing files (1)", t, func() {
result, err := socketTestA.getCephDaemons()
So(err, ShouldBeNil)
daemonNames := daemonsToStrings(result)
So(daemonNames, ShouldContain, "socket.1")
So(daemonNames, ShouldContain, "socket.2")
})
Convey("obtaining daemon names from existing files (2)", t, func() {
result, err := socketTestB.getCephDaemons()
So(err, ShouldBeNil)
daemonNames := daemonsToStrings(result)
So(daemonNames, ShouldContain, "prefix-socket.1")
So(daemonNames, ShouldContain, "prefix-socket.2")
So(daemonNames, ShouldContain, "socket.3")
})
Convey("obtaining daemon names from existing files (3)", t, func() {
result, err := socketTestC.getCephDaemons()
So(err, ShouldBeNil)
daemonNames := daemonsToStrings(result)
So(daemonNames, ShouldContain, "socket.4")
})
Convey("obtaining daemon names from existing files (4)", t, func() {
result, err := socketTestD.getCephDaemons()
So(err, ShouldBeNil)
daemonNames := daemonsToStrings(result)
So(daemonNames, ShouldBeEmpty)
})
os.RemoveAll("test/")
}
func createMockMetrics(count uint64, hostname string) []plugin.Metric {
metrics := make([]plugin.Metric, count)
for i, _ := range metrics {
metrics[i] = plugin.Metric{
Namespace: plugin.NewNamespace("test", "metric", "namespace"),
Data: 1.01 + float32(i),
Tags: map[string]string{"source": hostname},
}
}
return metrics
}
func checkMetricsResemblance(metrics []plugin.Metric, metricsMock []plugin.Metric) {
for i, mock := range metricsMock {
So(metrics[i].Namespace, ShouldResemble, mock.Namespace)
So(metrics[i].Tags, ShouldResemble, mock.Tags)
So(metrics[i].Data, ShouldEqual, mock.Data)
}
}
func Test_getCephDaemonMetrics(t *testing.T) {
mts := []plugin.Metric{
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "mds", "a", "a", "aa"),
},
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "mds", "a", "a", "cc"),
},
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "mds", "a", "c", "cb", "cba"),
},
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "mds", "a", "c", "cb", "cbb"),
},
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "mds", "b", "b", "bavg"),
},
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "mds", "c", "no", "nono"),
},
}
mds := make(map[string]Daemon)
mds["a"] = Daemon{id: "a", name: "mds", fullName: "mds.a.asok"}
mds["b"] = Daemon{id: "b", name: "mds", fullName: "mds.b.asok"}
mds["c"] = Daemon{id: "c", name: "mds", fullName: "mds.c.asok"}
mds["d"] = Daemon{id: "d", name: "mds", fullName: "mds.d.asok"}
Convey("invalid getting metrics, perf dump command execution error", t, func() {
cmd = &TestCmd{err: errors.New("exit status 1")}
testCeph := &Ceph{}
So(func() { testCeph.getCephDaemonMetrics(mts, mds["a"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["a"])
So(result, ShouldBeEmpty)
So(err, ShouldNotBeNil)
})
Convey("invalid getting metrics, empty perf dump output", t, func() {
cmd = &TestCmd{}
testCeph := &Ceph{}
So(func() { testCeph.getCephDaemonMetrics(mts, mds["a"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["a"])
So(result, ShouldBeEmpty)
So(err, ShouldNotBeNil)
})
Convey("unmarshal perf dump output", t, func() {
cmd = &TestCmd{outDump: mockInUnmarshal[0], outSchema: mockOutSchema}
testCeph := &Ceph{}
testCeph.getCephDaemonSchema(mds["a"])
//first mock in mockInUnmarshal is correct
So(func() { testCeph.getCephDaemonMetrics(mts, mds["a"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["a"])
So(result, ShouldNotBeEmpty)
So(err, ShouldBeNil)
})
Convey("invalid unmarshal perf dump output, incorrect json format", t, func() {
testCeph := &Ceph{}
for _, moi := range mockInUnmarshal[1:] {
cmd = &TestCmd{outDump: moi, outSchema: mockOutSchema}
testCeph.getCephDaemonSchema(mds["a"])
So(func() { testCeph.getCephDaemonMetrics(mts, mds["a"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["a"])
So(result, ShouldBeEmpty)
So(err, ShouldNotBeNil)
}
})
Convey("no defined desired metrics for daemon", t, func() {
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
testCeph := &Ceph{}
testCeph.getCephDaemonSchema(mds["d"])
So(func() { testCeph.getCephDaemonMetrics(mts, mds["d"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["d"])
So(result, ShouldBeEmpty)
So(err, ShouldNotBeNil)
})
Convey("no available desired metrics for daemon", t, func() {
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
testCeph := &Ceph{}
testCeph.getCephDaemonSchema(mds["c"])
So(func() { testCeph.getCephDaemonMetrics(mts, mds["c"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["c"])
So(len(result), ShouldEqual, 0)
So(err, ShouldNotBeNil)
})
Convey("get ceph-daemon metrics", t, func() {
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
testCeph := &Ceph{}
testCeph.getCephDaemonSchema(mds["a"])
So(func() { testCeph.getCephDaemonMetrics(mts, mds["a"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["a"])
So(result, ShouldNotBeEmpty)
So(len(result), ShouldEqual, 3)
for _, r := range result {
So(r.Data, ShouldNotBeNil)
}
So(err, ShouldBeNil)
})
Convey("get ceph-daemon metrics (delta metrics)", t, func() {
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
testCeph := &Ceph{}
testCeph.getCephDaemonSchema(mds["b"])
So(func() { testCeph.getCephDaemonMetrics(mts, mds["b"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics(mts, mds["b"])
So(result, ShouldNotBeEmpty)
So(len(result), ShouldEqual, 1)
So(result[0].Data, ShouldEqual, 0.5)
So(result[0].Description, ShouldNotBeEmpty)
So(err, ShouldBeNil)
})
Convey("check ceph-daemon float64 number metrics", t, func() {
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
testCeph := &Ceph{}
testCeph.getCephDaemonSchema(mds["a"])
So(func() { testCeph.getCephDaemonMetrics([]plugin.Metric{mts[2]}, mds["a"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics([]plugin.Metric{mts[2]}, mds["a"])
So(result, ShouldNotBeEmpty)
var checkData float64 = 123456.1234567890
So(result[0].Data, ShouldHaveSameTypeAs, checkData)
So(result[0].Data, ShouldEqual, checkData)
So(result[0].Description, ShouldNotBeEmpty)
So(err, ShouldBeNil)
})
Convey("check ceph-daemon large number metrics", t, func() {
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
testCeph := &Ceph{}
testCeph.getCephDaemonSchema(mds["a"])
So(func() { testCeph.getCephDaemonMetrics([]plugin.Metric{mts[3]}, mds["a"]) }, ShouldNotPanic)
result, err := testCeph.getCephDaemonMetrics([]plugin.Metric{mts[3]}, mds["a"])
So(result, ShouldNotBeEmpty)
var checkData int64 = 12345678901234567
So(result[0].Data, ShouldHaveSameTypeAs, checkData)
So(result[0].Data, ShouldEqual, checkData)
So(result[0].Description, ShouldBeEmpty)
So(err, ShouldBeNil)
})
}
func TestGetMetricTypes(t *testing.T) {
// Testing GetMetricTypes function covers checking initialization of Ceph plugin
// mock ceph socket
os.Mkdir("test", os.ModePerm)
os.Create("test/ceph-mds.a.asok")
os.Create("test/ceph-mds.b.asok")
os.Create("test/ceph-mds.c.asok")
// set ceph socket conf value
cfg := plugin.Config{}
cfg["path"] = "/usr/bin"
cfg["socket_path"] = "./test"
cfg["socket_prefix"] = "ceph-"
cfg["socket_ext"] = "asok"
Convey("init ceph plugin and getting metric namespace", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
So(func() { ceph.GetMetricTypes(cfg) }, ShouldNotPanic)
result, err := ceph.GetMetricTypes(cfg)
So(result, ShouldNotBeNil)
So(err, ShouldBeNil)
})
Convey("invalid path to ceph-daemon sockets", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
cfg_invalid := plugin.Config{}
cfg_invalid["socket_path"] = "./test_invalid"
cfg_invalid["socket_prefix"] = "ceph-"
cfg_invalid["socket_ext"] = "asok"
err := ceph.init(cfg_invalid)
So(err, ShouldNotBeNil)
})
Convey("no ceph-daemon sockets available with set prefix", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
cfg_inv_prefix := plugin.Config{}
cfg_inv_prefix["socket_path"] = "./test"
cfg_inv_prefix["socket_prefix"] = "ceph_inv-"
So(func() { ceph.init(cfg_inv_prefix) }, ShouldNotPanic)
err := ceph.init(cfg_inv_prefix)
So(err, ShouldNotBeNil)
})
Convey("no ceph-daemon sockets available with set extension", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
cfg_inv_ext := plugin.Config{}
cfg_inv_ext["socket_path"] = "./test"
cfg_inv_ext["socket_ext"] = "asok_inv"
So(func() { ceph.init(cfg_inv_ext) }, ShouldNotPanic)
err := ceph.init(cfg_inv_ext)
So(err, ShouldNotBeNil)
})
os.RemoveAll("test/")
}
func Test_getCephBinaryPath(t *testing.T) {
path := "/path/to/ceph/bin"
Convey("path Ceph executable in Snap Global Config", t, func() {
cmd = &TestCmd{}
cfg := plugin.Config{}
cfg["path"] = path
So(func() { getCephBinaryPath(cfg) }, ShouldNotPanic)
result, err := getCephBinaryPath(cfg)
So(err, ShouldBeNil)
So(result, ShouldEqual, path)
})
}
func Test_getSocketConf(t *testing.T) {
ceph := New()
Convey("check Ceph socket default config", t, func() {
cmd = &TestCmd{}
testPolicy := plugin.NewConfigPolicy()
testPolicy.AddNewStringRule([]string{"intel", "storage", "ceph"}, "socket_path", false, plugin.SetDefaultString(socketPathDefault))
testPolicy.AddNewStringRule([]string{"intel", "storage", "ceph"}, "socket_prefix", false, plugin.SetDefaultString(socketPrefixDefault))
testPolicy.AddNewStringRule([]string{"intel", "storage", "ceph"}, "socket_ext", false, plugin.SetDefaultString(socketExtDefault))
testPolicy.AddNewStringRule([]string{"intel", "storage", "ceph"}, "path", false, plugin.SetDefaultString(cephBinPathDefault))
cp, _ := ceph.GetConfigPolicy()
So(cp, ShouldResemble, *testPolicy)
})
Convey("customize Ceph socket conf in Snap Global Conf", t, func() {
path := "path/to/ceph/socket"
prefix := "test-"
extension := "asdf"
cmd = &TestCmd{}
cfg := plugin.Config{}
cfg["socket_path"] = path
cfg["socket_prefix"] = prefix
cfg["socket_ext"] = extension
So(func() { getCephSocketConf(cfg) }, ShouldNotPanic)
result, err := getCephSocketConf(cfg)
So(err, ShouldBeNil)
So(result.path, ShouldEqual, path)
So(result.prefix, ShouldEqual, prefix)
So(result.ext, ShouldEqual, extension)
})
Convey("customize Ceph socket prefix, set none", t, func() {
cmd = &TestCmd{}
cfg := plugin.Config{}
cfg["socket_path"] = ""
cfg["socket_prefix"] = "none"
cfg["socket_ext"] = ""
So(func() { getCephSocketConf(cfg) }, ShouldNotPanic)
result, err := getCephSocketConf(cfg)
So(err, ShouldBeNil)
So(result.prefix, ShouldBeEmpty)
})
}
func Test_CollectMetrics(t *testing.T) {
// folder for mocked ceph socket, empty in the beginning
os.Mkdir("test", os.ModePerm)
// set ceph socket conf value
config := plugin.Config{}
config["path"] = "/usr/bin"
config["socket_path"] = "./test"
config["socket_prefix"] = "none"
config["socket_ext"] = "asok"
// Test data
mts := []plugin.Metric{
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "osd", "2", "a", "aa"), Config: config,
},
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "osd", "3", "c", "cb", "cba"), Config: config,
},
}
mtsDynamic := []plugin.Metric{
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "osd", "*", "a", "aa"), Config: config,
},
}
mtsDynamicErr := []plugin.Metric{
plugin.Metric{
Namespace: plugin.NewNamespace("intel", "storage", "ceph", "osd", "*", "mnopqrst", "aabbcc"), Config: config,
},
}
Convey("no metrics defined to collect", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
ceph.daemons = []Daemon{}
mts_empty := []plugin.Metric{}
So(func() { ceph.CollectMetrics(mts_empty) }, ShouldNotPanic)
result, err := ceph.CollectMetrics(mts_empty)
So(result, ShouldBeNil)
So(err, ShouldNotBeNil)
})
Convey("no ceph daemon is running", t, func() {
// none mocked ceph socket in ./test
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
ceph.daemons = []Daemon{}
So(func() { ceph.CollectMetrics(mts) }, ShouldNotPanic)
result, err := ceph.CollectMetrics(mts)
So(result, ShouldBeEmpty)
So(err, ShouldNotBeNil)
})
// create ceph socket
os.Create("test/osd.0.asok")
os.Create("test/osd.1.asok")
Convey("try collecting metrics for not available socket", t, func() {
// metrics for osd.2 and osd.3 are not available
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
So(func() { ceph.CollectMetrics(mts) }, ShouldNotPanic)
result, err := ceph.CollectMetrics(mts)
So(result, ShouldBeEmpty)
So(err, ShouldBeNil)
})
// create additional ceph socket
os.Create("test/osd.2.asok")
os.Create("test/osd.3.asok")
Convey("collect metrics with no errors", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
So(func() { ceph.CollectMetrics(mts) }, ShouldNotPanic)
result, err := ceph.CollectMetrics(mts)
So(len(result), ShouldEqual, len(mts))
for _, r := range result {
So(r.Data, ShouldNotBeNil)
}
So(err, ShouldBeNil)
})
Convey("collect dynamic metrics with no errors", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
So(func() { ceph.CollectMetrics(mtsDynamic) }, ShouldNotPanic)
result, err := ceph.CollectMetrics(mtsDynamic)
So(len(result), ShouldEqual, 4)
for _, r := range result {
So(r.Data, ShouldEqual, 1)
}
So(err, ShouldBeNil)
})
Convey("collect dynamic metrics for not existing metric", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
So(func() { ceph.CollectMetrics(mtsDynamicErr) }, ShouldNotPanic)
result, err := ceph.CollectMetrics(mtsDynamicErr)
So(err, ShouldBeNil)
So(len(result), ShouldEqual, 0)
})
os.Remove("test/osd.0.asok")
os.Remove("test/osd.1.asok")
os.Remove("test/osd.2.asok")
os.Remove("test/osd.3.asok")
Convey("collect dynamic metrics with no daemons available", t, func() {
ceph := &Ceph{}
cmd = &TestCmd{outDump: mockOut, outSchema: mockOutSchema}
So(func() { ceph.CollectMetrics(mtsDynamic) }, ShouldNotPanic)
result, err := ceph.CollectMetrics(mtsDynamic)
So(result, ShouldBeEmpty)
So(err, ShouldNotBeNil)
})
os.Remove("test/")
}
func Test_New(t *testing.T) {
Convey("create new Ceph structure", t, func() {
So(func() { New() }, ShouldNotPanic)
result := New()
So(result, ShouldNotBeNil)
})
}