-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathkeys_test.go
796 lines (706 loc) · 21 KB
/
keys_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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
package exporter
import (
"fmt"
"net/http/httptest"
"net/url"
"os"
"reflect"
"sort"
"strings"
"testing"
"github.com/gomodule/redigo/redis"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
// defaultCount is used for `SCAN whatever COUNT defaultCount` command
const (
defaultCount int64 = 10
invalidCount int64 = 0
)
func TestKeyValuesAndSizes(t *testing.T) {
e, _ := NewRedisExporter(
os.Getenv("TEST_REDIS_URI"),
Options{
Namespace: "test",
CheckSingleKeys: dbNumStrFull + "=" + url.QueryEscape(testKeys[0]),
Registry: prometheus.NewRegistry()},
)
ts := httptest.NewServer(e)
defer ts.Close()
setupTestKeys(t, os.Getenv("TEST_REDIS_URI"))
defer deleteTestKeys(t, os.Getenv("TEST_REDIS_URI"))
chM := make(chan prometheus.Metric, 10000)
go func() {
e.Collect(chM)
close(chM)
}()
body := downloadURL(t, ts.URL+"/metrics")
for _, want := range []string{
"test_key_size",
"test_key_value",
} {
if !strings.Contains(body, want) {
t.Fatalf("didn't find %s, body: %s", want, body)
return
}
}
}
func TestKeyValuesAsLabel(t *testing.T) {
setupTestKeys(t, os.Getenv("TEST_REDIS_URI"))
defer deleteTestKeys(t, os.Getenv("TEST_REDIS_URI"))
for _, exc := range []bool{true, false} {
e, _ := NewRedisExporter(
os.Getenv("TEST_REDIS_URI"),
Options{
Namespace: "test",
CheckSingleKeys: dbNumStrFull + "=" + url.QueryEscape(testKeySingleString),
DisableExportingKeyValues: exc,
Registry: prometheus.NewRegistry()},
)
ts := httptest.NewServer(e)
chM := make(chan prometheus.Metric, 10000)
go func() {
e.Collect(chM)
close(chM)
}()
body := downloadURL(t, ts.URL+"/metrics")
for _, match := range []string{
"key_value_as_string",
"test_key_value",
} {
if exc && strings.Contains(body, match) {
t.Fatalf("didn't expect %s with DisableExportingKeyValues enabled, body: %s", match, body)
} else if !exc && !strings.Contains(body, match) {
t.Fatalf("didn't find %s with DisableExportingKeyValues disabled, body: %s", match, body)
}
}
ts.Close()
}
}
func TestClusterKeyValuesAndSizes(t *testing.T) {
clusterUri := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
if clusterUri == "" {
t.Skipf("Skipping TestClusterKeyValuesAndSizes, don't have env var TEST_REDIS_CLUSTER_MASTER_URI")
}
setupTestKeysCluster(t, clusterUri)
defer deleteTestKeysCluster(clusterUri)
for _, disableExportingValues := range []bool{true, false} {
e, _ := NewRedisExporter(
clusterUri,
Options{
Namespace: "test", DisableExportingKeyValues: disableExportingValues,
CheckSingleKeys: fmt.Sprintf(
"%s=%s,%s=%s",
dbNumStrFull, url.QueryEscape(testKeys[0]),
dbNumStrFull, url.QueryEscape(TestKeysSetName),
),
IsCluster: true,
},
)
chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
foundExpectedKey := map[string]bool{
"test_key_size": false,
"test_key_value": false,
"test_key_memory_usage_bytes": false,
}
for m := range chM {
for k := range foundExpectedKey {
if strings.Contains(m.Desc().String(), k) {
foundExpectedKey[k] = true
}
}
}
for k, found := range foundExpectedKey {
if k == "test_key_value" {
if found && disableExportingValues {
t.Errorf("didn't expect %s with DisableExportingKeyValues enabled", k)
} else if !found && !disableExportingValues {
t.Errorf("didn't find %s with DisableExportingKeyValues disabled", k)
}
} else if !found {
t.Errorf("didn't find %s", k)
}
}
}
}
func TestParseKeyArg(t *testing.T) {
for _, test := range []struct {
name string
keyArgs string
expected []dbKeyPair
expectSuccess bool
}{
// positive tests
{"empty_args", "", []dbKeyPair{}, true},
{"default_database", "my-key", []dbKeyPair{{"0", "my-key"}}, true},
{"prefixed_database", "db0=my-key", []dbKeyPair{{"0", "my-key"}}, true},
{"indexed_database", "0=my-key", []dbKeyPair{{"0", "my-key"}}, true},
{"triple_key", "check-key-01", []dbKeyPair{{"0", "check-key-01"}}, true},
{
name: "default_database_multiple_keys",
keyArgs: "my-key1,my-key2",
expected: []dbKeyPair{
{"0", "my-key1"},
{"0", "my-key2"},
},
expectSuccess: true,
},
{
name: "key_with_leading_space",
keyArgs: "my-key-noSpace, my-key-withSpace",
expected: []dbKeyPair{
{"0", "my-key-noSpace"},
{"0", "my-key-withSpace"},
},
expectSuccess: true,
},
{
name: "key_with_spaces",
keyArgs: "my-key-noSpace1, my-key-withSpaces ,my-key-noSpace2",
expected: []dbKeyPair{
{"0", "my-key-noSpace1"},
{"0", "my-key-withSpaces"},
{"0", "my-key-noSpace2"},
},
expectSuccess: true,
},
{
name: "different_databases",
keyArgs: "db0=key1,db1=key1",
expected: []dbKeyPair{
{"0", "key1"},
{"1", "key1"},
},
expectSuccess: true,
},
{
name: "dbdb_replace",
keyArgs: "dbdbdb0=key1,db1=key1",
expected: []dbKeyPair{
{"0", "key1"},
{"1", "key1"},
},
expectSuccess: true,
},
{
name: "default_database_with_another",
keyArgs: "key1,db1=key1",
expected: []dbKeyPair{
{"0", "key1"},
{"1", "key1"},
},
expectSuccess: true,
},
{
"invalid_args_with_args_separator_skipped",
"=", []dbKeyPair{}, true,
},
{
"empty_args_with_comma_separators_skipped",
",,,my-key", []dbKeyPair{{"0", "my-key"}}, true,
},
{
"multiple_invalid_args_skipped",
"=,=,,0=my-key", []dbKeyPair{{"0", "my-key"}}, true,
},
{
"empty_key_with_args_separator_skipped",
"0=", []dbKeyPair{}, true,
},
{
"empty_database_with_args_separator_skipped",
"=my-key", []dbKeyPair{}, true,
},
// negative tests
{
"string_database_index",
"wrong=my-key", []dbKeyPair{}, false,
},
{
"prefixed_string_database_index",
"dbwrong=my-key", []dbKeyPair{}, false,
},
{
"wrong_args_count",
"wrong=wrong=wrong", []dbKeyPair{}, false,
},
{
"wrong_args",
"wrong=wrong=1", []dbKeyPair{}, false,
},
{
"negative_database_index",
"db-1=my-key", []dbKeyPair{}, false,
},
} {
t.Run(test.name, func(t *testing.T) {
parsed, err := parseKeyArg(test.keyArgs)
if test.expectSuccess && err != nil {
t.Errorf("Expected success for test: %s, got err: %s", test.name, err)
return
}
if len(parsed) != len(test.expected) {
t.Errorf("Parsed elements count don't match expected: parsed %d; expected %d", len(parsed), len(test.expected))
return
}
for i, pair := range test.expected {
if pair != parsed[i] {
t.Errorf("Parsed elements don't match expected dbKeyPair:\n parsed %#v;\nexpected %#v", parsed[i], pair)
return
}
}
if !test.expectSuccess && err == nil {
t.Errorf("Expected failure for test: %s, got no err", test.name)
return
}
if !test.expectSuccess && err != nil {
t.Logf("Expected failure for test: %s, got err: %s", test.name, err)
return
}
})
}
}
type keyFixture struct {
command string
key string
args []interface{}
}
func newKeyFixture(command string, key string, args ...interface{}) keyFixture {
return keyFixture{command, key, args}
}
func createKeyFixtures(t *testing.T, c redis.Conn, fixtures []keyFixture) {
for _, f := range fixtures {
args := append([]interface{}{f.key}, f.args...)
if _, err := c.Do(f.command, args...); err != nil {
t.Fatalf("Error creating fixture: %#v, %#v", f, err)
}
}
}
func deleteKeyFixtures(t *testing.T, c redis.Conn, fixtures []keyFixture) {
for _, f := range fixtures {
if _, err := c.Do("DEL", f.key); err != nil {
t.Errorf("Error deleting fixture: %#v, %#v", f, err)
}
}
}
func TestScanKeys(t *testing.T) {
numKeys := 1000
var fixtures []keyFixture
// Make 1000 keys that match
for i := 0; i < numKeys; i++ {
key := fmt.Sprintf("get_keys_test_shouldmatch_%v", i)
fixtures = append(fixtures, newKeyFixture("SET", key, "Woohoo!"))
}
// And 1000 that don't
for i := 0; i < numKeys; i++ {
key := fmt.Sprintf("get_keys_test_shouldnotmatch_%v", i)
fixtures = append(fixtures, newKeyFixture("SET", key, "Rats!"))
}
addr := os.Getenv("TEST_REDIS_URI")
db := dbNumStr
c, err := redis.DialURL(addr)
if err != nil {
t.Fatalf("Couldn't connect to %#v: %#v", addr, err)
}
_, err = c.Do("SELECT", db)
if err != nil {
t.Errorf("Couldn't select database %#v", db)
}
defer func() {
deleteKeyFixtures(t, c, fixtures)
c.Close()
}()
createKeyFixtures(t, c, fixtures)
matches, err := redis.Strings(scanKeys(c, "get_keys_test_*shouldmatch*", defaultCount))
if err != nil {
t.Errorf("Error getting keys matching a pattern: %#v", err)
}
numMatches := len(matches)
if numMatches != numKeys {
t.Errorf("Expected %#v matches, got %#v.", numKeys, numMatches)
}
for _, match := range matches {
if !strings.HasPrefix(match, "get_keys_test_shouldmatch") {
t.Errorf("Expected match to have prefix: get_keys_test_shouldmatch")
}
}
// Test expected errors separately
invalidFixtures := map[string]int64{
// empty string is a string after all
"": 100,
"pattern": invalidCount,
}
for pattern, count := range invalidFixtures {
got, err := redis.Strings(scanKeys(c, pattern, count))
if err != nil {
t.Logf("\"Passed\" expected, got error: %#v", err)
if pattern == "" && err.Error() != "Pattern shouldn't be empty" {
t.Errorf("\"Empty pattern\" error message expected, but got: %s", err.Error())
}
} else {
if len(got) >= 0 {
t.Errorf("Error expected, got valid response: %#v", got)
}
}
}
}
func TestGetKeysFromPatterns(t *testing.T) {
addr := os.Getenv("TEST_REDIS_URI")
dbMain := dbNumStr
dbAlt := altDBNumStr
dbInvalid := invalidDBNumStr
dbMainFixtures := []keyFixture{
newKeyFixture("SET", "dbMainNoPattern1", "woohoo!"),
newKeyFixture("SET", "dbMainSomePattern1", "woohoo!"),
newKeyFixture("SET", "dbMainSomePattern2", "woohoo!"),
}
dbAltFixtures := []keyFixture{
newKeyFixture("SET", "dbAltNoPattern1", "woohoo!"),
newKeyFixture("SET", "dbAltSomePattern1", "woohoo!"),
newKeyFixture("SET", "dbAltSomePattern2", "woohoo!"),
}
keys := []dbKeyPair{
{db: dbMain, key: "dbMainNoPattern1"},
{db: dbMain, key: "*SomePattern*"},
{db: dbAlt, key: "dbAltNoPattern1"},
{db: dbAlt, key: "*SomePattern*"},
}
invalidKeys := []dbKeyPair{
{db: dbInvalid, key: "someUnusedPattern*"},
}
c, err := redis.DialURL(addr)
if err != nil {
t.Fatalf("Couldn't connect to %#v: %#v", addr, err)
}
defer func() {
_, err = c.Do("SELECT", dbMain)
if err != nil {
t.Errorf("Couldn't select database %#v", dbMain)
}
deleteKeyFixtures(t, c, dbMainFixtures)
_, err = c.Do("SELECT", dbAlt)
if err != nil {
t.Errorf("Couldn't select database %#v", dbAlt)
}
deleteKeyFixtures(t, c, dbAltFixtures)
c.Close()
}()
_, err = c.Do("SELECT", dbMain)
if err != nil {
t.Errorf("Couldn't select database %#v", dbMain)
}
createKeyFixtures(t, c, dbMainFixtures)
_, err = c.Do("SELECT", dbAlt)
if err != nil {
t.Errorf("Couldn't select database %#v", dbAlt)
}
createKeyFixtures(t, c, dbAltFixtures)
expandedKeys, err := getKeysFromPatterns(c, keys, defaultCount)
if err != nil {
t.Errorf("Error getting keys from patterns: %#v", err)
}
expectedKeys := []dbKeyPair{
{db: dbMain, key: "dbMainNoPattern1"},
{db: dbMain, key: "dbMainSomePattern1"},
{db: dbMain, key: "dbMainSomePattern2"},
{db: dbAlt, key: "dbAltNoPattern1"},
{db: dbAlt, key: "dbAltSomePattern1"},
{db: dbAlt, key: "dbAltSomePattern2"},
}
sort.Slice(expectedKeys, func(i, j int) bool {
return (expectedKeys[i].db + expectedKeys[i].key) < (expectedKeys[j].db + expectedKeys[j].key)
})
sort.Slice(expandedKeys, func(i, j int) bool {
return (expandedKeys[i].db + expandedKeys[i].key) < (expandedKeys[j].db + expandedKeys[j].key)
})
if !reflect.DeepEqual(expectedKeys, expandedKeys) {
t.Errorf("When expanding keys:\nexpected: %#v\nactual: %#v", expectedKeys, expandedKeys)
}
got, err := getKeysFromPatterns(c, invalidKeys, defaultCount)
if err != nil {
t.Logf("Expected error - \"invalid DB\": %#v", err)
} else {
if len(got) != 0 {
t.Errorf("Error expected with invalid database %#v, got valid response: %#v", invalidKeys, got)
}
}
}
/*
func TestGetKeyInfo(t *testing.T) {
addr := os.Getenv("TEST_REDIS_URI")
db := dbNumStr
c, err := redis.DialURL(addr)
if err != nil {
t.Fatalf("Couldn't connect to %#v: %#v", addr, err)
}
_, err = c.Do("SELECT", db)
if err != nil {
t.Errorf("Couldn't select database %#v", db)
}
fixtures := []keyFixture{
{"SET", "key_info_test_string", []interface{}{"Woohoo!"}},
{"HSET", "key_info_test_hash", []interface{}{"hashkey1", "hashval1"}},
{"PFADD", "key_info_test_hll", []interface{}{"hllval1", "hllval2"}},
{"PFADD", "key_info_test_hll2", []interface{}{"hll2val_1", "hll2val_2", "hll2val_3"}},
{"LPUSH", "key_info_test_list", []interface{}{"listval1", "listval2", "listval3"}},
{"SADD", "key_info_test_set", []interface{}{"setval1", "setval2", "setval3", "setval4"}},
{"ZADD", "key_info_test_zset", []interface{}{
"1", "zsetval1",
"2", "zsetval2",
"3", "zsetval3",
"4", "zsetval4",
"5", "zsetval5",
}},
{"XADD", "key_info_test_stream", []interface{}{"*", "field1", "str1"}},
}
createKeyFixtures(t, c, fixtures)
defer func() {
deleteKeyFixtures(t, c, fixtures)
c.Close()
}()
expectedSizes := map[string]float64{
"key_info_test_string": 7,
"key_info_test_hash": 1,
"key_info_test_hll": 2,
"key_info_test_hll2": 3,
"key_info_test_list": 3,
"key_info_test_set": 4,
"key_info_test_zset": 5,
"key_info_test_stream": 1,
}
// Test all known types
for _, f := range fixtures {
keyType, err := redis.String(c.Do("TYPE", f.key))
if err != nil {
t.Fatalf("TYPE err: %s", err)
}
info, err := getKeyInfo(c, keyType, f.key, false)
if err != nil {
t.Fatalf("Error getting key info for %#v.", f.key)
}
expected := expectedSizes[f.key]
if info.size != expected {
t.Errorf("Wrong size for key: %#v. Expected: %#v; Actual: %#v", f.key, expected, info.size)
t.Logf("info: %#v", info)
}
}
absentKeyName := "absent_key"
// Test absent key returns the correct error
keyType, err := redis.String(c.Do("TYPE", absentKeyName))
if err != nil {
t.Fatalf("TYPE err: %s", err)
}
_, err = getKeyInfo(c, keyType, absentKeyName, false)
if !errors.Is(err, errKeyTypeNotFound) {
t.Errorf("Expected `errKeyTypeNotFound` for absent key. Got a different error, err: %#v", err)
}
}
*/
func TestKeySizeList(t *testing.T) {
s := dbNumStrFull + "=" + testKeysList[0]
e, _ := NewRedisExporter(
os.Getenv("TEST_REDIS_URI"),
Options{Namespace: "test", CheckSingleKeys: s},
)
setupTestKeys(t, os.Getenv("TEST_REDIS_URI"))
defer deleteTestKeys(t, os.Getenv("TEST_REDIS_URI"))
chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
found := false
for m := range chM {
if strings.Contains(m.Desc().String(), "test_key_size") {
found = true
}
}
if !found {
t.Errorf("didn't find the key")
}
}
func TestKeyValueInvalidDB(t *testing.T) {
e, _ := NewRedisExporter(
os.Getenv("TEST_REDIS_URI"),
Options{
Namespace: "test",
CheckSingleKeys: "999=" + url.QueryEscape(testKeys[0]),
},
)
chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
dontWant := map[string]bool{"test_key_size": false}
for m := range chM {
switch m.(type) {
case prometheus.Gauge:
for k := range dontWant {
if strings.Contains(m.Desc().String(), k) {
log.Println(m.Desc().String())
dontWant[k] = true
}
}
default:
log.Debugf("default: m: %#v", m)
}
}
for k, found := range dontWant {
if found {
t.Errorf("we found %s but it shouldn't be there", k)
}
}
}
func TestCheckKeys(t *testing.T) {
for _, tst := range []struct {
SingleCheckKey string
CheckKeys string
ExpectSuccess bool
}{
{"", "", true},
{"db1=key3", "", true},
{"check-key-01", "", true},
{"", "check-key-02", true},
{"wrong=wrong=1", "", false},
{"", "wrong=wrong=2", false},
} {
_, err := NewRedisExporter(os.Getenv("TEST_REDIS_URI"), Options{Namespace: "test", CheckSingleKeys: tst.SingleCheckKey, CheckKeys: tst.CheckKeys})
if tst.ExpectSuccess && err != nil {
t.Errorf("Expected success for test: %#v, got err: %s", tst, err)
return
}
if !tst.ExpectSuccess && err == nil {
t.Errorf("Expected failure for test: %#v, got no err", tst)
return
}
}
}
func TestCheckSingleKeyDefaultsTo0(t *testing.T) {
uri := os.Getenv("TEST_REDIS_URI")
e, _ := NewRedisExporter(uri, Options{Namespace: "test", CheckSingleKeys: "single", Registry: prometheus.NewRegistry()})
ts := httptest.NewServer(e)
defer ts.Close()
setupTestKeys(t, uri)
defer deleteTestKeys(t, uri)
body := downloadURL(t, ts.URL+"/metrics")
if !strings.Contains(body, `test_key_size{db="db0",key="single"} 0`) {
t.Errorf("Expected metric `test_key_size` with key=`single` and value 0 but got:\n%s", body)
}
}
func TestCheckKeysMultipleDBs(t *testing.T) {
uri := os.Getenv("TEST_REDIS_URI")
e, _ := NewRedisExporter(uri,
Options{Namespace: "test",
CheckSingleKeys: "single," +
dbNumStr + "=" + testKeys[0] + "," +
dbNumStr + "=" + testKeySingleString + "," +
altDBNumStr + "=" + TestKeysHllName + "," +
altDBNumStr + "=" + testKeySingleString + "," +
anotherAltDbNumStr + "=" + testKeys[0],
CheckKeys: dbNumStr + "=" + "test*",
CheckKeysBatchSize: 1000,
Registry: prometheus.NewRegistry(),
})
ts := httptest.NewServer(e)
defer ts.Close()
setupTestKeys(t, uri)
defer deleteTestKeys(t, uri)
body := downloadURL(t, ts.URL+"/metrics")
for _, k := range []string{
`test_key_size{db="db0",key="single"} 0`, // non-existent key
fmt.Sprintf(`test_key_size{db="db%s",key="%s"} 16`, dbNumStr, testKeySingleString),
fmt.Sprintf(`test_key_size{db="db%s",key="%s"} 3`, dbNumStr, TestKeysZSetName),
fmt.Sprintf(`test_key_size{db="db%s",key="%s"} 4`, dbNumStr, TestKeysHashName),
fmt.Sprintf(`test_key_size{db="db%s",key="%s"} 3`, altDBNumStr, TestKeysHllName),
fmt.Sprintf(`test_key_size{db="db%s",key="%s"} 16`, altDBNumStr, testKeySingleString),
fmt.Sprintf(`test_key_size{db="db%s",key="%s"} 7`, anotherAltDbNumStr, testKeys[0]),
fmt.Sprintf(`test_key_value{db="db%s",key="%s"} 1234.56`, dbNumStr, testKeys[0]),
fmt.Sprintf(`test_key_value{db="db%s",key="%s"} 1234.56`, anotherAltDbNumStr, testKeys[0]),
fmt.Sprintf(`key_memory_usage_bytes{db="db%s",key="%s"}`, dbNumStr, testKeySingleString),
fmt.Sprintf(`key_memory_usage_bytes{db="db%s",key="%s"}`, altDBNumStr, testKeySingleString),
fmt.Sprintf(`key_memory_usage_bytes{db="db%s",key="%s"}`, anotherAltDbNumStr, testKeys[0]),
} {
if !strings.Contains(body, k) {
t.Errorf("Expected metric: %s but got:\n%s", k, body)
}
}
}
func TestClusterGetKeyInfo(t *testing.T) {
clusterUri := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
if clusterUri == "" {
t.Skipf("Skipping TestClusterKeyValuesAndSizes, don't have env var TEST_REDIS_CLUSTER_MASTER_URI")
}
e, _ := NewRedisExporter(
clusterUri,
Options{
Namespace: "test",
CheckSingleKeys: strings.Join(AllTestKeys, ","),
Registry: prometheus.NewRegistry(),
IsCluster: true,
},
)
ts := httptest.NewServer(e)
defer ts.Close()
setupTestKeysCluster(t, clusterUri)
defer deleteTestKeysCluster(clusterUri)
body := downloadURL(t, ts.URL+"/metrics")
for _, want := range []string{
"key_value_as_string",
`test_key_size{db="db0",key="test-hll"} 3`,
} {
if !strings.Contains(body, want) {
t.Errorf("Expected metric: %s but got:\n%s", want, body)
}
}
}
func TestGetKeysCount(t *testing.T) {
addr := os.Getenv("TEST_REDIS_URI")
db := dbNumStr
c, err := redis.DialURL(addr)
if err != nil {
t.Fatalf("Couldn't connect to %#v: %#v", addr, err)
}
_, err = c.Do("SELECT", db)
if err != nil {
t.Errorf("Couldn't select database %#v", db)
}
fixtures := []keyFixture{
{"SET", "count_test:keys_count_test_string1", []interface{}{"Woohoo!"}},
{"SET", "count_test:keys_count_test_string2", []interface{}{"!oohooW"}},
{"LPUSH", "count_test:keys_count_test_list1", []interface{}{"listval1", "listval2", "listval3"}},
{"LPUSH", "count_test:keys_count_test_list2", []interface{}{"listval1", "listval2", "listval3"}},
{"LPUSH", "count_test:keys_count_test_list3", []interface{}{"listval1", "listval2", "listval3"}},
}
createKeyFixtures(t, c, fixtures)
defer func() {
deleteKeyFixtures(t, c, fixtures)
c.Close()
}()
expectedCount := map[string]int{
"count_test:keys_count_test_string*": 2,
"count_test:keys_count_test_list*": 3,
"count_test:*": 5,
}
for pattern, count := range expectedCount {
actualCount, err := getKeysCount(c, pattern, defaultCount)
if err != nil {
t.Errorf("Error getting count for pattern \"%#v\"", pattern)
}
if actualCount != count {
t.Errorf("Wrong count for pattern \"%#v\". Expected: %#v; Actual: %#v", pattern, count, actualCount)
}
}
got, err := getKeysCount(c, "pattern", invalidCount)
if err != nil {
t.Logf("Expected error - \"error retrieving keys\": %#v", err)
} else {
if got >= 0 {
t.Errorf("Error expected with invalidCount option \"%#v\", got valid response: %#v", invalidCount, got)
}
}
}