SourceStatsReporter UT fix (#667)

* source stats reporter... should be used by well-behaved sources

* public fields

* moving serialization/deserialization of metrics and logging maps to pkg

* nits

* adding UT

* same order

* updates

* sock-puppet

* unregistration problem?

* removing base64 encoding

* removing TODO

* removing config changes... done in another PR

* to properly address the comment

* changing importer to source

* moving stats_reporter to new source folder

* fixing UT
This commit is contained in:
Ignacio Cano 2019-09-13 09:40:34 -07:00 committed by Knative Prow Robot
parent 2b5e8011b8
commit 1bea34570b
1 changed files with 11 additions and 12 deletions

View File

@ -24,16 +24,8 @@ import (
"knative.dev/pkg/metrics/metricstest"
)
// unregister, ehm, unregisters the metrics that were registered, by
// virtue of StatsReporter creation.
// Since golang executes test iterations within the same process, the stats reporter
// returns an error if the metric is already registered and the test panics.
func unregister() {
metricstest.Unregister("event_count")
}
func TestStatsReporter(t *testing.T) {
t.Skip("Fails in PROW but not locally, possibly due to the webhook stats_reporter test. Needs further investigation.")
setup()
args := &ReportArgs{
Namespace: "testns",
@ -47,9 +39,6 @@ func TestStatsReporter(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create a new reporter: %v", err)
}
// Without this `go test ... -count=X`, where X > 1, fails, since
// we get an error about view already being registered.
defer unregister()
wantTags := map[string]string{
metricskey.LabelNamespaceName: "testns",
@ -77,3 +66,13 @@ func expectSuccess(t *testing.T, f func() error) {
t.Errorf("Reporter expected success but got error: %v", err)
}
}
func setup() {
resetMetrics()
}
func resetMetrics() {
// OpenCensus metrics carry global state that need to be reset between unit tests.
metricstest.Unregister("event_count")
register()
}