|
| 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