forked from arangodb-helper/arangodb-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter_test.go
74 lines (69 loc) · 2.56 KB
/
exporter_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
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package main
import (
"fmt"
_ "net/http/pprof"
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus"
)
// TestMetric tests the result of metricKey & newMetric for various inputs.
func TestMetric(t *testing.T) {
g1 := StatisticGroup{
Group: "g1",
Name: "g1-name",
Description: "Something g1",
}
tests := []struct {
Group StatisticGroup
Figure StatisticFigure
Postfix string
KeyResult string
CollectionPostfixes []string
}{
{g1, StatisticFigure{"g1", "f1", "f1-name", "descr-f1", FigureTypeAccumulated, "", nil}, "_pf", "g1-name_f1-name_pf", []string{""}},
{g1, StatisticFigure{"g1", "f1", "f1-name", "descr-f1", FigureTypeAccumulated, "tick", nil}, "_pf", "g1-name_f1-name_pf_tick", []string{""}},
{g1, StatisticFigure{"g1", "f2", "f2-name", "descr-f2", FigureTypeDistribution, "", []float64{0.1, 0.2}}, "_pf", "g1-name_f2-name_pf", []string{"_sum", "_count", "_bucket"}},
}
for i, test := range tests {
result := metricKey(test.Group, test.Figure, test.Postfix)
if result != test.KeyResult {
t.Errorf("metricKey for test %d failed: got '%s', expected '%s'", i, result, test.KeyResult)
}
colls := newMetric(test.Group, test.Figure)
if len(colls) != len(test.CollectionPostfixes) {
t.Errorf("newMetric for test %d returns unexpected #collectors: got %d, expected %d", i, len(colls), len(test.CollectionPostfixes))
} else {
for ci, c := range colls {
descrChan := make(chan *prometheus.Desc, 1)
c.Describe(descrChan)
d := <-descrChan
result := d.String()
expectedPostfix := test.CollectionPostfixes[ci]
if !strings.Contains(result, fmt.Sprintf("%s\", help", expectedPostfix)) {
t.Errorf("newMetric for test %d returns collector %d with wrong expectation. got '%s', expected it to contain '%s'", i, ci, result, expectedPostfix)
}
}
}
}
}