Skip to content

Commit 8fc014d

Browse files
committedApr 27, 2018
Added test
1 parent 4fd8d86 commit 8fc014d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
 

‎Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ update-vendor:
113113
@$(PULSAR) go flatten -V $(VENDORDIR) $(VENDORDIR)
114114
@${MAKE} -B -s clean
115115

116+
.PHONY: run-tests
117+
run-tests: $(GOBUILDDIR)
118+
GOPATH=$(GOBUILDDIR) go test $(REPOPATH)
119+
116120
docker: build
117121
for arch in $(ARCHS); do \
118122
docker build --build-arg=GOARCH=$$arch -t $(DOCKERIMAGE)-$$arch . ;\

‎exporter_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package main
24+
25+
import (
26+
"fmt"
27+
_ "net/http/pprof"
28+
"strings"
29+
"testing"
30+
31+
"github.com/prometheus/client_golang/prometheus"
32+
)
33+
34+
// TestMetric tests the result of metricKey & newMetric for various inputs.
35+
func TestMetric(t *testing.T) {
36+
g1 := StatisticGroup{
37+
Group: "g1",
38+
Name: "g1-name",
39+
Description: "Something g1",
40+
}
41+
tests := []struct {
42+
Group StatisticGroup
43+
Figure StatisticFigure
44+
Postfix string
45+
KeyResult string
46+
CollectionPostfixes []string
47+
}{
48+
{g1, StatisticFigure{"g1", "f1", "f1-name", "descr-f1", FigureTypeAccumulated, "", nil}, "_pf", "g1-name_f1-name_pf", []string{""}},
49+
{g1, StatisticFigure{"g1", "f1", "f1-name", "descr-f1", FigureTypeAccumulated, "tick", nil}, "_pf", "g1-name_f1-name_pf_tick", []string{""}},
50+
{g1, StatisticFigure{"g1", "f2", "f2-name", "descr-f2", FigureTypeDistribution, "", []float64{0.1, 0.2}}, "_pf", "g1-name_f2-name_pf", []string{"_sum", "_count", "_bucket"}},
51+
}
52+
53+
for i, test := range tests {
54+
result := metricKey(test.Group, test.Figure, test.Postfix)
55+
if result != test.KeyResult {
56+
t.Errorf("metricKey for test %d failed: got '%s', expected '%s'", i, result, test.KeyResult)
57+
}
58+
colls := newMetric(test.Group, test.Figure)
59+
if len(colls) != len(test.CollectionPostfixes) {
60+
t.Errorf("newMetric for test %d returns unexpected #collectors: got %d, expected %d", i, len(colls), len(test.CollectionPostfixes))
61+
} else {
62+
for ci, c := range colls {
63+
descrChan := make(chan *prometheus.Desc, 1)
64+
c.Describe(descrChan)
65+
d := <-descrChan
66+
result := d.String()
67+
expectedPostfix := test.CollectionPostfixes[ci]
68+
if !strings.Contains(result, fmt.Sprintf("%s\", help", expectedPostfix)) {
69+
t.Errorf("newMetric for test %d returns collector %d with wrong expectation. got '%s', expected it to contain '%s'", i, ci, result, expectedPostfix)
70+
}
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)