Change:
What would you like to be : Once a metric be deprecated, we should update the unit test accordingly if there is any. We need to work out a way for a unit test freely select codebase version, and also should make sure the unit test won't fail in the subsequent release. Why is this needed: Deprecated also needs to be protected. /sig instrumentation /assign

Discussion:
kr.CustomMustRegister(cs...) } <ins> // BuildVersion is a helper function that can be easily mocked. var BuildVersion = version.Get </ins> func newKubeRegistry(v apimachineryversion.Info) *kubeRegistry { r := &kubeRegistry{ PromRegistry:     prometheus.NewRegistry(),

// NewKubeRegistry creates a new vanilla Registry without any Collectors // pre-registered. func NewKubeRegistry() KubeRegistry { <del> r := newKubeRegistry(version.Get()) </del> <ins> r := newKubeRegistry(BuildVersion()) </ins> return r }

<del> load("@io_bazel_rules_go//go:def.bzl", "go_library") </del> <ins> load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") </ins> go_library( name = "go_default_library",

importpath = "k8s.io/component-base/metrics/testutil", visibility = ["//visibility:public"], deps = [ <ins> "//staging/src/k8s.io/apimachinery/pkg/version:go_default_library", </ins> "//staging/src/k8s.io/component-base/metrics:go_default_library", "//vendor/github.com/prometheus/client_golang/prometheus/testutil:go_default_library", "//vendor/github.com/prometheus/common/expfmt:go_default_library",

tags = ["automanaged"], visibility = ["//visibility:public"], ) <ins> go_test( name = "go_default_test", srcs = ["testutil_test.go"], embed = [":go_default_library"], deps = ["//staging/src/k8s.io/component-base/metrics:go_default_library"], ) </ins>

package testutil import ( <ins> "fmt" </ins> "io" "github.com/prometheus/client_golang/prometheus/testutil" <ins> apimachineryversion "k8s.io/apimachinery/pkg/version" </ins> "k8s.io/component-base/metrics" )

return GatherAndCompare(registry, expected, metricNames...) } <ins> // NewFakeKubeRegistry creates a fake `KubeRegistry` that takes the input version as `build in version`. // It should only be used in testing scenario especially for the deprecated metrics. // The input version format should be `major.minor.patch`, e.g. '1.18.0'. func NewFakeKubeRegistry(ver string) metrics.KubeRegistry { backup := metrics.BuildVersion defer func() { metrics.BuildVersion = backup }() metrics.BuildVersion = func() apimachineryversion.Info { return apimachineryversion.Info{ GitVersion: fmt.Sprintf("v%s-alpha+1.12345", ver), } } return metrics.NewKubeRegistry() } </ins>

<ins> /* Copyright 2020 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 testutil import ( "strings" "testing" "k8s.io/component-base/metrics" ) func TestNewFakeKubeRegistry(t *testing.T) { registryVersion := "1.18.0" counter := metrics.NewCounter( &metrics.CounterOpts{ Name: "test_counter_name", Help: "counter help", }, ) deprecatedCounter := metrics.NewCounter( &metrics.CounterOpts{ Name:              "test_deprecated_counter", Help:              "counter help", DeprecatedVersion: "1.18.0", }, ) hiddenCounter := metrics.NewCounter( &metrics.CounterOpts{ Name:              "test_hidden_counter", Help:              "counter help", DeprecatedVersion: "1.17.0", }, ) var tests = []struct { name     string metric   *metrics.Counter expected string }{ { name:   "normal", metric: counter, expected: ` # HELP test_counter_name [ALPHA] counter help # TYPE test_counter_name counter test_counter_name 0 `, }, { name:   "deprecated", metric: deprecatedCounter, expected: ` # HELP test_deprecated_counter [ALPHA] (Deprecated since 1.18.0) counter help # TYPE test_deprecated_counter counter test_deprecated_counter 0 `, }, { name:     "hidden", metric:   hiddenCounter, expected: ``, }, } for _, test := range tests { tc := test t.Run(tc.name, func(t *testing.T) { registry := NewFakeKubeRegistry(registryVersion) registry.MustRegister(tc.metric) if err := GatherAndCompare(registry, strings.NewReader(tc.expected), tc.metric.FQName()); err != nil { t.Fatal(err) } }) } } </ins>