|
| 1 | +// Copyright 2020 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package testutil |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/prometheus/client_golang/prometheus" |
| 20 | +) |
| 21 | + |
| 22 | +func TestCollectAndLintGood(t *testing.T) { |
| 23 | + cnt := prometheus.NewCounterVec( |
| 24 | + prometheus.CounterOpts{ |
| 25 | + Name: "some_total", |
| 26 | + Help: "A value that represents a counter.", |
| 27 | + ConstLabels: prometheus.Labels{ |
| 28 | + "label1": "value1", |
| 29 | + }, |
| 30 | + }, |
| 31 | + []string{"foo"}, |
| 32 | + ) |
| 33 | + cnt.WithLabelValues("bar") |
| 34 | + cnt.WithLabelValues("baz") |
| 35 | + |
| 36 | + problems, err := CollectAndLint(cnt) |
| 37 | + if err != nil { |
| 38 | + t.Error("Unexpected error:", err) |
| 39 | + } |
| 40 | + if len(problems) > 0 { |
| 41 | + t.Error("Unexpected lint problems:", problems) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestCollectAndLintBad(t *testing.T) { |
| 46 | + cnt := prometheus.NewCounterVec( |
| 47 | + prometheus.CounterOpts{ |
| 48 | + Name: "someThing_ms", |
| 49 | + Help: "A value that represents a counter.", |
| 50 | + ConstLabels: prometheus.Labels{ |
| 51 | + "label1": "value1", |
| 52 | + }, |
| 53 | + }, |
| 54 | + []string{"fooBar"}, |
| 55 | + ) |
| 56 | + cnt.WithLabelValues("bar") |
| 57 | + cnt.WithLabelValues("baz") |
| 58 | + |
| 59 | + problems, err := CollectAndLint(cnt) |
| 60 | + if err != nil { |
| 61 | + t.Error("Unexpected error:", err) |
| 62 | + } |
| 63 | + if len(problems) < 5 { |
| 64 | + // The exact nature of the lint problems found is tested within |
| 65 | + // the promlint package itself. Here we only want to make sure |
| 66 | + // that the collector successfully hit the linter and got enough |
| 67 | + // problems flagged. |
| 68 | + t.Error("Not enough lint problems found.") |
| 69 | + } |
| 70 | +} |
0 commit comments