Add a helper func to set a default metric config for unit tests (#1263)

* do not record for empty metric config

* Revert "do not record for empty metric config"

This reverts commit 539a5e4dbb.

* add a comment

* fix typo

* fix tests

* revert

* revert tests

* revert

* fix conflicts

* one more test file
This commit is contained in:
Yanwei Guo 2020-05-07 21:11:45 -07:00 committed by GitHub
parent 66f1d63f10
commit 19b1d7b64d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 3 deletions

View File

@ -23,6 +23,7 @@ import (
"go.opencensus.io/stats/view"
"knative.dev/pkg/metrics/metricstest"
_ "knative.dev/pkg/metrics/testing"
)
func TestNewStatsReporterErrors(t *testing.T) {

View File

@ -58,7 +58,7 @@ func TestClientMetrics(t *testing.T) {
metrics.Register(cp.NewLatencyMetric(), cp.NewResultMetric())
// Reset the metrics configuration to avoid leaked state from other tests.
setCurMetricsConfig(nil)
InitForTesting()
views := cp.DefaultViews()
if got, want := len(views), 2; got != want {

View File

@ -36,7 +36,7 @@ func TestMemStatsMetrics(t *testing.T) {
msp.Start(ctx, period)
// Reset the metrics configuration to avoid leaked state from other tests.
setCurMetricsConfig(nil)
InitForTesting()
views := msp.DefaultViews()
if got, want := len(views), 27; got != want {

26
metrics/testing.go Normal file
View File

@ -0,0 +1,26 @@
/*
Copyright 2020 The Knative 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 metrics
// InitForTesting initialize the necessary global variables for unit tests.
func InitForTesting() {
setCurMetricsConfig(&metricsConfig{
backendDestination: Prometheus,
component: "test",
domain: "test",
})
}

View File

@ -24,4 +24,5 @@ import (
func init() {
os.Setenv(metrics.DomainEnv, "knative.dev/testing")
metrics.InitForTesting()
}

View File

@ -48,7 +48,7 @@ func TestWorkqueueMetrics(t *testing.T) {
workqueue.SetProvider(wp)
// Reset the metrics configuration to avoid leaked state from other tests.
setCurMetricsConfig(nil)
InitForTesting()
views := wp.DefaultViews()
if got, want := len(views), 7; got != want {

View File

@ -22,6 +22,7 @@ import (
"knative.dev/pkg/metrics/metricskey"
"knative.dev/pkg/metrics/metricstest"
_ "knative.dev/pkg/metrics/testing"
)
func TestStatsReporter(t *testing.T) {

View File

@ -34,6 +34,7 @@ import (
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/metrics/metricstest"
_ "knative.dev/pkg/metrics/testing"
)
type fixedAdmissionController struct {

View File

@ -24,6 +24,7 @@ import (
admissionv1beta1 "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/metrics/metricstest"
_ "knative.dev/pkg/metrics/testing"
)
func TestWebhookStatsReporter(t *testing.T) {