|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/blang/semver" |
| 21 | + apimachineryversion "k8s.io/apimachinery/pkg/version" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +func TestGauge(t *testing.T) { |
| 26 | + v115 := semver.MustParse("1.15.0") |
| 27 | + v114 := semver.MustParse("1.14.0") |
| 28 | + var tests = []struct { |
| 29 | + desc string |
| 30 | + GaugeOpts |
| 31 | + registryVersion *semver.Version |
| 32 | + expectedMetricCount int |
| 33 | + expectedHelp string |
| 34 | + }{ |
| 35 | + { |
| 36 | + desc: "Test non deprecated", |
| 37 | + GaugeOpts: GaugeOpts{ |
| 38 | + Namespace: "namespace", |
| 39 | + Name: "metric_test_name", |
| 40 | + Subsystem: "subsystem", |
| 41 | + Help: "gauge help", |
| 42 | + }, |
| 43 | + registryVersion: &v115, |
| 44 | + expectedMetricCount: 1, |
| 45 | + expectedHelp: "[ALPHA] gauge help", |
| 46 | + }, |
| 47 | + { |
| 48 | + desc: "Test deprecated", |
| 49 | + GaugeOpts: GaugeOpts{ |
| 50 | + Namespace: "namespace", |
| 51 | + Name: "metric_test_name", |
| 52 | + Subsystem: "subsystem", |
| 53 | + Help: "gauge help", |
| 54 | + DeprecatedVersion: &v115, |
| 55 | + }, |
| 56 | + registryVersion: &v115, |
| 57 | + expectedMetricCount: 1, |
| 58 | + expectedHelp: "[ALPHA] (Deprecated since 1.15.0) gauge help", |
| 59 | + }, |
| 60 | + { |
| 61 | + desc: "Test hidden", |
| 62 | + GaugeOpts: GaugeOpts{ |
| 63 | + Namespace: "namespace", |
| 64 | + Name: "metric_test_name", |
| 65 | + Subsystem: "subsystem", |
| 66 | + Help: "gauge help", |
| 67 | + DeprecatedVersion: &v114, |
| 68 | + }, |
| 69 | + registryVersion: &v115, |
| 70 | + expectedMetricCount: 0, |
| 71 | + expectedHelp: "gauge help", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, test := range tests { |
| 76 | + t.Run(test.desc, func(t *testing.T) { |
| 77 | + registry := NewKubeRegistry(apimachineryversion.Info{ |
| 78 | + Major: "1", |
| 79 | + Minor: "15", |
| 80 | + GitVersion: "v1.15.0-alpha-1.12345", |
| 81 | + }) |
| 82 | + c := NewGauge(&test.GaugeOpts) |
| 83 | + registry.MustRegister(c) |
| 84 | + |
| 85 | + ms, err := registry.Gather() |
| 86 | + if len(ms) != test.expectedMetricCount { |
| 87 | + t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount) |
| 88 | + } |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("Gather failed %v", err) |
| 91 | + } |
| 92 | + for _, metric := range ms { |
| 93 | + if metric.GetHelp() != test.expectedHelp { |
| 94 | + t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // let's increment the counter and verify that the metric still works |
| 99 | + c.Set(100) |
| 100 | + c.Set(101) |
| 101 | + expected := 101 |
| 102 | + ms, err = registry.Gather() |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Gather failed %v", err) |
| 105 | + } |
| 106 | + for _, mf := range ms { |
| 107 | + for _, m := range mf.GetMetric() { |
| 108 | + if int(m.GetGauge().GetValue()) != expected { |
| 109 | + t.Errorf("Got %v, wanted %v as the count", m.GetGauge().GetValue(), expected) |
| 110 | + } |
| 111 | + t.Logf("%v\n", m.GetGauge().GetValue()) |
| 112 | + } |
| 113 | + } |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestGaugeVec(t *testing.T) { |
| 119 | + v115 := semver.MustParse("1.15.0") |
| 120 | + v114 := semver.MustParse("1.14.0") |
| 121 | + var tests = []struct { |
| 122 | + desc string |
| 123 | + GaugeOpts |
| 124 | + labels []string |
| 125 | + registryVersion *semver.Version |
| 126 | + expectedMetricCount int |
| 127 | + expectedHelp string |
| 128 | + }{ |
| 129 | + { |
| 130 | + desc: "Test non deprecated", |
| 131 | + GaugeOpts: GaugeOpts{ |
| 132 | + Namespace: "namespace", |
| 133 | + Name: "metric_test_name", |
| 134 | + Subsystem: "subsystem", |
| 135 | + Help: "gauge help", |
| 136 | + }, |
| 137 | + labels: []string{"label_a", "label_b"}, |
| 138 | + registryVersion: &v115, |
| 139 | + expectedMetricCount: 1, |
| 140 | + expectedHelp: "[ALPHA] gauge help", |
| 141 | + }, |
| 142 | + { |
| 143 | + desc: "Test deprecated", |
| 144 | + GaugeOpts: GaugeOpts{ |
| 145 | + Namespace: "namespace", |
| 146 | + Name: "metric_test_name", |
| 147 | + Subsystem: "subsystem", |
| 148 | + Help: "gauge help", |
| 149 | + DeprecatedVersion: &v115, |
| 150 | + }, |
| 151 | + labels: []string{"label_a", "label_b"}, |
| 152 | + registryVersion: &v115, |
| 153 | + expectedMetricCount: 1, |
| 154 | + expectedHelp: "[ALPHA] (Deprecated since 1.15.0) gauge help", |
| 155 | + }, |
| 156 | + { |
| 157 | + desc: "Test hidden", |
| 158 | + GaugeOpts: GaugeOpts{ |
| 159 | + Namespace: "namespace", |
| 160 | + Name: "metric_test_name", |
| 161 | + Subsystem: "subsystem", |
| 162 | + Help: "gauge help", |
| 163 | + DeprecatedVersion: &v114, |
| 164 | + }, |
| 165 | + labels: []string{"label_a", "label_b"}, |
| 166 | + registryVersion: &v115, |
| 167 | + expectedMetricCount: 0, |
| 168 | + expectedHelp: "gauge help", |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + for _, test := range tests { |
| 173 | + t.Run(test.desc, func(t *testing.T) { |
| 174 | + registry := NewKubeRegistry(apimachineryversion.Info{ |
| 175 | + Major: "1", |
| 176 | + Minor: "15", |
| 177 | + GitVersion: "v1.15.0-alpha-1.12345", |
| 178 | + }) |
| 179 | + c := NewGaugeVec(&test.GaugeOpts, test.labels) |
| 180 | + registry.MustRegister(c) |
| 181 | + c.WithLabelValues("1", "2").Set(1.0) |
| 182 | + ms, err := registry.Gather() |
| 183 | + |
| 184 | + if len(ms) != test.expectedMetricCount { |
| 185 | + t.Errorf("Got %v metrics, Want: %v metrics", len(ms), test.expectedMetricCount) |
| 186 | + } |
| 187 | + if err != nil { |
| 188 | + t.Fatalf("Gather failed %v", err) |
| 189 | + } |
| 190 | + for _, metric := range ms { |
| 191 | + if metric.GetHelp() != test.expectedHelp { |
| 192 | + t.Errorf("Got %s as help message, want %s", metric.GetHelp(), test.expectedHelp) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // let's increment the counter and verify that the metric still works |
| 197 | + c.WithLabelValues("1", "3").Set(1.0) |
| 198 | + c.WithLabelValues("2", "3").Set(1.0) |
| 199 | + ms, err = registry.Gather() |
| 200 | + if err != nil { |
| 201 | + t.Fatalf("Gather failed %v", err) |
| 202 | + } |
| 203 | + for _, mf := range ms { |
| 204 | + if len(mf.GetMetric()) != 3 { |
| 205 | + t.Errorf("Got %v metrics, wanted 2 as the count", len(mf.GetMetric())) |
| 206 | + } |
| 207 | + } |
| 208 | + }) |
| 209 | + } |
| 210 | +} |
0 commit comments