|
| 1 | +// Copyright 2025 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 version |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/google/go-cmp/cmp" |
| 22 | + |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +var defaultLabels = []string{"branch", "goarch", "goos", "goversion", "revision", "tags", "version"} |
| 27 | + |
| 28 | +func TestGoVersionCollector(t *testing.T) { |
| 29 | + reg := prometheus.NewPedanticRegistry() |
| 30 | + reg.MustRegister(NewCollector( |
| 31 | + "foo"), |
| 32 | + ) |
| 33 | + result, err := reg.Gather() |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + if _, err := json.Marshal(result); err != nil { |
| 39 | + t.Errorf("json marshalling should not fail, %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + got := []string{} |
| 43 | + |
| 44 | + for _, r := range result { |
| 45 | + got = append(got, r.GetName()) |
| 46 | + fmt.Println("foo") |
| 47 | + m := r.GetMetric() |
| 48 | + |
| 49 | + if len(m) != 1 { |
| 50 | + t.Errorf("expected 1 metric, but got %d", len(m)) |
| 51 | + } |
| 52 | + |
| 53 | + lk := []string{} |
| 54 | + for _, lp := range m[0].GetLabel() { |
| 55 | + lk = append(lk, lp.GetName()) |
| 56 | + } |
| 57 | + |
| 58 | + if diff := cmp.Diff(lk, defaultLabels); diff != "" { |
| 59 | + t.Errorf("missmatch (-want +got):\n%s", diff) |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + if diff := cmp.Diff(got, []string{"foo_build_info"}); diff != "" { |
| 65 | + t.Errorf("missmatch (-want +got):\n%s", diff) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestGoVersionCollectorWithLabels(t *testing.T) { |
| 70 | + reg := prometheus.NewPedanticRegistry() |
| 71 | + |
| 72 | + labels := prometheus.Labels{ |
| 73 | + "z-mylabel": "myvalue", |
| 74 | + } |
| 75 | + reg.MustRegister(NewCollector( |
| 76 | + "foo", WithExtraConstLabels(labels)), |
| 77 | + ) |
| 78 | + result, err := reg.Gather() |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + if _, err := json.Marshal(result); err != nil { |
| 84 | + t.Errorf("json marshalling should not fail, %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + got := []string{} |
| 88 | + |
| 89 | + for _, r := range result { |
| 90 | + got = append(got, r.GetName()) |
| 91 | + fmt.Println("foo") |
| 92 | + m := r.GetMetric() |
| 93 | + |
| 94 | + if len(m) != 1 { |
| 95 | + t.Errorf("expected 1 metric, but got %d", len(m)) |
| 96 | + } |
| 97 | + |
| 98 | + lk := []string{} |
| 99 | + for _, lp := range m[0].GetLabel() { |
| 100 | + lk = append(lk, lp.GetName()) |
| 101 | + } |
| 102 | + |
| 103 | + labels := append(defaultLabels, "z-mylabel") |
| 104 | + |
| 105 | + if diff := cmp.Diff(lk, labels); diff != "" { |
| 106 | + t.Errorf("missmatch (-want +got):\n%s", diff) |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + if diff := cmp.Diff(got, []string{"foo_build_info"}); diff != "" { |
| 112 | + t.Errorf("missmatch (-want +got):\n%s", diff) |
| 113 | + } |
| 114 | +} |
0 commit comments