123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- /*
- Copyright 2019 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package main
- import (
- "fmt"
- "reflect"
- "testing"
- "k8s.io/component-base/metrics"
- )
- const fakeFilename = "testdata/metric.go"
- func TestSkipMetrics(t *testing.T) {
- for _, test := range []struct {
- testName string
- src string
- }{
- {
- testName: "Skip alpha metric with local variable",
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var name = "metric"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: name,
- StabilityLevel: metrics.ALPHA,
- },
- )
- `},
- {
- testName: "Skip alpha metric created via function call",
- src: `
- package test
- import "k8s.io/component-base/metrics"
- func getName() string {
- return "metric"
- }
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: getName(),
- StabilityLevel: metrics.ALPHA,
- },
- )
- `},
- {
- testName: "Skip metric without stability set",
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: "metric",
- },
- )
- `},
- {
- testName: "Skip functions of similar signature (not imported from framework path) with import rename",
- src: `
- package test
- import metrics "k8s.io/fake/path"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Skip functions of similar signature (not imported from framework path)",
- src: `
- package test
- import "k8s.io/fake/path/metrics"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Skip . package import of non metric framework",
- src: `
- package test
- import . "k8s.io/fake/path"
- var _ = NewCounter(
- &CounterOpts{
- StabilityLevel: STABLE,
- },
- )
- `},
- } {
- t.Run(test.testName, func(t *testing.T) {
- metrics, errors := searchFileForStableMetrics(fakeFilename, test.src)
- if len(metrics) != 0 {
- t.Errorf("Didn't expect any stable metrics found, got: %d", len(metrics))
- }
- if len(errors) != 0 {
- t.Errorf("Didn't expect any errors found, got: %s", errors)
- }
- })
- }
- }
- func TestStableMetric(t *testing.T) {
- for _, test := range []struct {
- testName string
- src string
- metric metric
- }{
- {
- testName: "Counter",
- metric: metric{
- Name: "metric",
- Namespace: "namespace",
- Subsystem: "subsystem",
- StabilityLevel: "STABLE",
- DeprecatedVersion: "1.16",
- Help: "help",
- Type: counterMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: "metric",
- Subsystem: "subsystem",
- Namespace: "namespace",
- Help: "help",
- DeprecatedVersion: "1.16",
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "CounterVec",
- metric: metric{
- Name: "metric",
- Namespace: "namespace",
- Subsystem: "subsystem",
- Labels: []string{"label-1"},
- StabilityLevel: "STABLE",
- DeprecatedVersion: "1.16",
- Help: "help",
- Type: counterMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounterVec(
- &metrics.CounterOpts{
- Name: "metric",
- Namespace: "namespace",
- Subsystem: "subsystem",
- Help: "help",
- DeprecatedVersion: "1.16",
- StabilityLevel: metrics.STABLE,
- },
- []string{"label-1"},
- )
- `},
- {
- testName: "Gauge",
- metric: metric{
- Name: "gauge",
- Namespace: "namespace",
- Subsystem: "subsystem",
- StabilityLevel: "STABLE",
- DeprecatedVersion: "1.16",
- Help: "help",
- Type: gaugeMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewGauge(
- &metrics.GaugeOpts{
- Name: "gauge",
- Namespace: "namespace",
- Subsystem: "subsystem",
- Help: "help",
- DeprecatedVersion: "1.16",
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "GaugeVec",
- metric: metric{
- Name: "gauge",
- Namespace: "namespace",
- Subsystem: "subsystem",
- StabilityLevel: "STABLE",
- DeprecatedVersion: "1.16",
- Help: "help",
- Type: gaugeMetricType,
- Labels: []string{"label-1", "label-2"},
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewGaugeVec(
- &metrics.GaugeOpts{
- Name: "gauge",
- Namespace: "namespace",
- Subsystem: "subsystem",
- Help: "help",
- DeprecatedVersion: "1.16",
- StabilityLevel: metrics.STABLE,
- },
- []string{"label-2", "label-1"},
- )
- `},
- {
- testName: "Histogram",
- metric: metric{
- Name: "histogram",
- Namespace: "namespace",
- Subsystem: "subsystem",
- DeprecatedVersion: "1.16",
- StabilityLevel: "STABLE",
- Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
- Help: "help",
- Type: histogramMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewHistogram(
- &metrics.HistogramOpts{
- Name: "histogram",
- Namespace: "namespace",
- Subsystem: "subsystem",
- StabilityLevel: metrics.STABLE,
- Help: "help",
- DeprecatedVersion: "1.16",
- Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
- },
- )
- `},
- {
- testName: "HistogramVec",
- metric: metric{
- Name: "histogram",
- Namespace: "namespace",
- Subsystem: "subsystem",
- DeprecatedVersion: "1.16",
- StabilityLevel: "STABLE",
- Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
- Help: "help",
- Type: histogramMetricType,
- Labels: []string{"label-1", "label-2"},
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewHistogramVec(
- &metrics.HistogramOpts{
- Name: "histogram",
- Namespace: "namespace",
- Subsystem: "subsystem",
- StabilityLevel: metrics.STABLE,
- Help: "help",
- DeprecatedVersion: "1.16",
- Buckets: []float64{0.001, 0.01, 0.1, 1, 10, 100},
- },
- []string{"label-2", "label-1"},
- )
- `},
- {
- testName: "Custom import",
- metric: metric{
- Name: "metric",
- StabilityLevel: "STABLE",
- Type: counterMetricType,
- },
- src: `
- package test
- import custom "k8s.io/component-base/metrics"
- var _ = custom.NewCounter(
- &custom.CounterOpts{
- Name: "metric",
- StabilityLevel: custom.STABLE,
- },
- )
- `},
- {
- testName: "Const",
- metric: metric{
- Name: "metric",
- StabilityLevel: "STABLE",
- Type: counterMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- const name = "metric"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: name,
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Variable",
- metric: metric{
- Name: "metric",
- StabilityLevel: "STABLE",
- Type: counterMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var name = "metric"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: name,
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Multiple consts in block",
- metric: metric{
- Name: "metric",
- StabilityLevel: "STABLE",
- Type: counterMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- const (
- unrelated1 = "unrelated1"
- name = "metric"
- unrelated2 = "unrelated2"
- )
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: name,
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Multiple variables in Block",
- metric: metric{
- Name: "metric",
- StabilityLevel: "STABLE",
- Type: counterMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var (
- unrelated1 = "unrelated1"
- name = "metric"
- _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: name,
- StabilityLevel: metrics.STABLE,
- },
- )
- )
- `},
- {
- testName: "Histogram with linear buckets",
- metric: metric{
- Name: "histogram",
- StabilityLevel: "STABLE",
- Buckets: metrics.LinearBuckets(1, 1, 3),
- Type: histogramMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewHistogram(
- &metrics.HistogramOpts{
- Name: "histogram",
- StabilityLevel: metrics.STABLE,
- Buckets: metrics.LinearBuckets(1, 1, 3),
- },
- )
- `},
- {
- testName: "Histogram with exponential buckets",
- metric: metric{
- Name: "histogram",
- StabilityLevel: "STABLE",
- Buckets: metrics.ExponentialBuckets(1, 2, 3),
- Type: histogramMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewHistogram(
- &metrics.HistogramOpts{
- Name: "histogram",
- StabilityLevel: metrics.STABLE,
- Buckets: metrics.ExponentialBuckets(1, 2, 3),
- },
- )
- `},
- {
- testName: "Histogram with default buckets",
- metric: metric{
- Name: "histogram",
- StabilityLevel: "STABLE",
- Buckets: metrics.DefBuckets,
- Type: histogramMetricType,
- },
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewHistogram(
- &metrics.HistogramOpts{
- Name: "histogram",
- StabilityLevel: metrics.STABLE,
- Buckets: metrics.DefBuckets,
- },
- )
- `},
- } {
- t.Run(test.testName, func(t *testing.T) {
- metrics, errors := searchFileForStableMetrics(fakeFilename, test.src)
- if len(errors) != 0 {
- t.Errorf("Unexpected errors: %s", errors)
- }
- if len(metrics) != 1 {
- t.Fatalf("Unexpected number of metrics: got %d, want 1", len(metrics))
- }
- if test.metric.Labels == nil {
- test.metric.Labels = []string{}
- }
- if !reflect.DeepEqual(metrics[0], test.metric) {
- t.Errorf("metric:\ngot %v\nwant %v", metrics[0], test.metric)
- }
- })
- }
- }
- func TestIncorrectStableMetricDeclarations(t *testing.T) {
- for _, test := range []struct {
- testName string
- src string
- err error
- }{
- {
- testName: "Fail on stable summary metric (Summary is DEPRECATED)",
- err: fmt.Errorf("testdata/metric.go:4:9: Stable summary metric is not supported"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewSummary(
- &metrics.SummaryOpts{
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Fail on stable metric with attribute set to unknown variable",
- err: fmt.Errorf("testdata/metric.go:6:4: Metric attribute was not correctly set. Please use only global consts in same file"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: unknownVariable,
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Fail on stable metric with attribute set to local function return",
- err: fmt.Errorf("testdata/metric.go:9:4: Non string attribute it not supported"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- func getName() string {
- return "metric"
- }
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: getName(),
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Fail on stable metric with attribute set to imported function return",
- err: fmt.Errorf("testdata/metric.go:7:4: Non string attribute it not supported"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- import "k8s.io/kubernetes/utils"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- Name: utils.getMetricName(),
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "Fail on metric with stability set to function return",
- err: fmt.Errorf("testdata/metric.go:9:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- func getMetricStability() metrics.StabilityLevel {
- return metrics.STABLE
- }
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- StabilityLevel: getMetricsStability(),
- },
- )
- `},
- {
- testName: "error for passing stability as string",
- err: fmt.Errorf("testdata/metric.go:6:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- StabilityLevel: "stable",
- },
- )
- `},
- {
- testName: "error for passing stability as unknown const",
- err: fmt.Errorf("testdata/metric.go:6:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- StabilityLevel: metrics.UNKNOWN,
- },
- )
- `},
- {
- testName: "error for passing stability as variable",
- err: fmt.Errorf("testdata/metric.go:7:20: StabilityLevel should be passed STABLE, ALPHA or removed"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var stable = metrics.STABLE
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- StabilityLevel: stable,
- },
- )
- `},
- {
- testName: "error for stable metric created via function call",
- err: fmt.Errorf("testdata/metric.go:6:10: Opts for STABLE metric was not directly passed to new metric function"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounter(getStableCounterOpts())
- func getStableCounterOpts() *metrics.CounterOpts {
- return &metrics.CounterOpts{
- StabilityLevel: metrics.STABLE,
- }
- }
- `},
- {
- testName: "error . package import of metric framework",
- err: fmt.Errorf(`testdata/metric.go:3:8: Importing using "." is not supported`),
- src: `
- package test
- import . "k8s.io/component-base/metrics"
- var _ = NewCounter(
- &CounterOpts{
- StabilityLevel: STABLE,
- },
- )
- `},
- {
- testName: "error stable metric opts passed to local function",
- err: fmt.Errorf("testdata/metric.go:4:9: Opts for STABLE metric was not directly passed to new metric function"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = RegisterMetric(
- &metrics.CounterOpts{
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "error stable metric opts passed to imported function",
- err: fmt.Errorf("testdata/metric.go:4:9: Opts for STABLE metric was not directly passed to new metric function"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = test.RegisterMetric(
- &metrics.CounterOpts{
- StabilityLevel: metrics.STABLE,
- },
- )
- `},
- {
- testName: "error stable metric opts passed to imported function",
- err: fmt.Errorf("testdata/metric.go:6:4: Positional arguments are not supported"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var _ = metrics.NewCounter(
- &metrics.CounterOpts{
- "counter",
- },
- )
- `},
- {
- testName: "error stable historgram with unknown prometheus bucket variable",
- err: fmt.Errorf("testdata/metric.go:9:13: Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- import "github.com/prometheus/client_golang/prometheus"
- var _ = metrics.NewHistogram(
- &metrics.HistogramOpts{
- Name: "histogram",
- StabilityLevel: metrics.STABLE,
- Buckets: prometheus.FakeBuckets,
- },
- )
- `},
- {
- testName: "error stable historgram with unknown bucket variable",
- err: fmt.Errorf("testdata/metric.go:9:13: Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- var buckets = []float64{1, 2, 3}
- var _ = metrics.NewHistogram(
- &metrics.HistogramOpts{
- Name: "histogram",
- StabilityLevel: metrics.STABLE,
- Buckets: buckets,
- },
- )
- `},
- {
- testName: "error stable historgram with unknown bucket variable from unknown library",
- err: fmt.Errorf("testdata/metric.go:9:13: Buckets should be set to list of floats, result from function call of prometheus.LinearBuckets or prometheus.ExponentialBuckets"),
- src: `
- package test
- import "k8s.io/component-base/metrics"
- import "github.com/fake_prometheus/prometheus"
- var _ = metrics.NewHistogram(
- &metrics.HistogramOpts{
- Name: "histogram",
- StabilityLevel: metrics.STABLE,
- Buckets: prometheus.DefBuckets,
- },
- )
- `},
- } {
- t.Run(test.testName, func(t *testing.T) {
- _, errors := searchFileForStableMetrics(fakeFilename, test.src)
- if len(errors) != 1 {
- t.Fatalf("Unexpected number of errors, got %d, want 1", len(errors))
- }
- if !reflect.DeepEqual(errors[0], test.err) {
- t.Errorf("error:\ngot %v\nwant %v", errors[0], test.err)
- }
- })
- }
- }
|