|
1 | 1 | package exporter |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strconv" |
4 | 5 | "strings" |
5 | 6 | "testing" |
| 7 | + "time" |
6 | 8 |
|
7 | 9 | "github.com/prometheus/client_golang/prometheus" |
8 | 10 | ) |
9 | 11 |
|
| 12 | +func TestDurationFieldToTimestamp(t *testing.T) { |
| 13 | + nowTs := time.Now().Unix() |
| 14 | + for _, tst := range []struct { |
| 15 | + in string |
| 16 | + expectedOk bool |
| 17 | + expectedVal int64 |
| 18 | + }{ |
| 19 | + { |
| 20 | + in: "123", |
| 21 | + expectedOk: true, |
| 22 | + expectedVal: nowTs - 123, |
| 23 | + }, |
| 24 | + { |
| 25 | + in: "0", |
| 26 | + expectedOk: true, |
| 27 | + expectedVal: nowTs - 0, |
| 28 | + }, |
| 29 | + { |
| 30 | + in: "abc", |
| 31 | + expectedOk: false, |
| 32 | + }, |
| 33 | + } { |
| 34 | + res, err := durationFieldToTimestamp(tst.in) |
| 35 | + if err == nil && !tst.expectedOk { |
| 36 | + t.Fatalf("expected not ok, but got no error, input: [%s]", tst.in) |
| 37 | + } else if err != nil && tst.expectedOk { |
| 38 | + t.Fatalf("expected ok, but got error: %s, input: [%s]", err, tst.in) |
| 39 | + } |
| 40 | + if tst.expectedOk { |
| 41 | + resInt64, err := strconv.ParseInt(res, 10, 64) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("ParseInt( %s ) err: %s", res, err) |
| 44 | + } |
| 45 | + if resInt64 != tst.expectedVal { |
| 46 | + t.Fatalf("expected %d, but got: %d", tst.expectedVal, resInt64) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
10 | 52 | func TestParseClientListString(t *testing.T) { |
11 | | - tsts := map[string][]string{ |
12 | | - "id=11 addr=127.0.0.1:63508 fd=8 name= age=6321 idle=6320 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=setex": []string{"", "6321", "6320", "N", "0", "0", "setex", "127.0.0.1", "63508"}, |
13 | | - "id=14 addr=127.0.0.1:64958 fd=9 name=foo age=5 idle=0 flags=N db=1 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client": []string{"foo", "5", "0", "N", "1", "0", "client", "127.0.0.1", "64958"}, |
| 53 | + convertDurationToTimestampString := func(duration string) string { |
| 54 | + ts, err := durationFieldToTimestamp(duration) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + return ts |
| 59 | + } |
| 60 | + |
| 61 | + tsts := []struct { |
| 62 | + in string |
| 63 | + expectedOk bool |
| 64 | + expectedLbls []string |
| 65 | + }{ |
| 66 | + { |
| 67 | + in: "id=11 addr=127.0.0.1:63508 fd=8 name= age=6321 idle=6320 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=setex", |
| 68 | + expectedOk: true, |
| 69 | + expectedLbls: []string{"", convertDurationToTimestampString("6321"), convertDurationToTimestampString("6320"), "N", "0", "0", "setex", "127.0.0.1", "63508"}, |
| 70 | + }, { |
| 71 | + in: "id=14 addr=127.0.0.1:64958 fd=9 name=foo age=5 idle=0 flags=N db=1 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client", |
| 72 | + expectedOk: true, |
| 73 | + expectedLbls: []string{"foo", convertDurationToTimestampString("5"), convertDurationToTimestampString("0"), "N", "1", "0", "client", "127.0.0.1", "64958"}, |
| 74 | + }, { |
| 75 | + in: "id=14 addr=127.0.0.1:64958 fd=9 name=foo age=ABCDE idle=0 flags=N db=1 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client", |
| 76 | + expectedOk: false, |
| 77 | + }, { |
| 78 | + in: "id=14 addr=127.0.0.1:64958 fd=9 name=foo age=5 idle=NOPE flags=N db=1 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client", |
| 79 | + expectedOk: false, |
| 80 | + }, { |
| 81 | + in: "", |
| 82 | + expectedOk: false, |
| 83 | + }, |
14 | 84 | } |
15 | 85 |
|
16 | | - for k, v := range tsts { |
17 | | - lbls, ok := parseClientListString(k) |
| 86 | + for _, tst := range tsts { |
| 87 | + lbls, ok := parseClientListString(tst.in) |
| 88 | + if !tst.expectedOk { |
| 89 | + if ok { |
| 90 | + t.Errorf("expected NOT ok, but got ok, input: %s", tst.in) |
| 91 | + } |
| 92 | + continue |
| 93 | + } |
18 | 94 | mismatch := false |
19 | 95 | for idx, l := range lbls { |
20 | | - if l != v[idx] { |
| 96 | + if l != tst.expectedLbls[idx] { |
21 | 97 | mismatch = true |
22 | 98 | break |
23 | 99 | } |
24 | 100 | } |
25 | | - if !ok || mismatch { |
26 | | - t.Errorf("TestParseClientListString( %s ) error. Given: %s Wanted: %s", k, lbls, v) |
| 101 | + if mismatch { |
| 102 | + t.Errorf("TestParseClientListString( %s ) error. Given: %s Wanted: %s", tst.in, lbls, tst.expectedLbls) |
27 | 103 | } |
28 | 104 | } |
29 | 105 | } |
|
0 commit comments