1/*
2Copyright 2019 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package testutil
18
19import (
20	"fmt"
21	"io"
22
23	"github.com/prometheus/client_golang/prometheus/testutil"
24
25	apimachineryversion "k8s.io/apimachinery/pkg/version"
26	"k8s.io/component-base/metrics"
27)
28
29// CollectAndCompare registers the provided Collector with a newly created
30// pedantic Registry. It then does the same as GatherAndCompare, gathering the
31// metrics from the pedantic Registry.
32func CollectAndCompare(c metrics.Collector, expected io.Reader, metricNames ...string) error {
33	lintProblems, err := testutil.CollectAndLint(c, metricNames...)
34	if err != nil {
35		return err
36	}
37	if err := getLintError(lintProblems); err != nil {
38		return err
39	}
40
41	return testutil.CollectAndCompare(c, expected, metricNames...)
42}
43
44// GatherAndCompare gathers all metrics from the provided Gatherer and compares
45// it to an expected output read from the provided Reader in the Prometheus text
46// exposition format. If any metricNames are provided, only metrics with those
47// names are compared.
48func GatherAndCompare(g metrics.Gatherer, expected io.Reader, metricNames ...string) error {
49	lintProblems, err := testutil.GatherAndLint(g, metricNames...)
50	if err != nil {
51		return err
52	}
53	if err := getLintError(lintProblems); err != nil {
54		return err
55	}
56
57	return testutil.GatherAndCompare(g, expected, metricNames...)
58}
59
60// CustomCollectAndCompare registers the provided StableCollector with a newly created
61// registry. It then does the same as GatherAndCompare, gathering the
62// metrics from the pedantic Registry.
63func CustomCollectAndCompare(c metrics.StableCollector, expected io.Reader, metricNames ...string) error {
64	registry := metrics.NewKubeRegistry()
65	registry.CustomMustRegister(c)
66
67	return GatherAndCompare(registry, expected, metricNames...)
68}
69
70// NewFakeKubeRegistry creates a fake `KubeRegistry` that takes the input version as `build in version`.
71// It should only be used in testing scenario especially for the deprecated metrics.
72// The input version format should be `major.minor.patch`, e.g. '1.18.0'.
73func NewFakeKubeRegistry(ver string) metrics.KubeRegistry {
74	backup := metrics.BuildVersion
75	defer func() {
76		metrics.BuildVersion = backup
77	}()
78
79	metrics.BuildVersion = func() apimachineryversion.Info {
80		return apimachineryversion.Info{
81			GitVersion: fmt.Sprintf("v%s-alpha+1.12345", ver),
82		}
83	}
84
85	return metrics.NewKubeRegistry()
86}
87