Merge pull request #84919 from RainbowMango/pr_functionality_for_customcollector_testing
provide functionality for custom collector testing
This commit is contained in:
		| @@ -18,6 +18,7 @@ package metrics | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/blang/semver" | ||||
| @@ -156,3 +157,16 @@ func (d *Desc) initializeDeprecatedDesc() { | ||||
| 	d.markDeprecated() | ||||
| 	d.initialize() | ||||
| } | ||||
|  | ||||
| // GetRawDesc will returns a new *Desc with original parameters provided to NewDesc(). | ||||
| // | ||||
| // It will be useful in testing scenario that the same Desc be registered to different registry. | ||||
| //   1. Desc `D` is registered to registry 'A' in TestA (Note: `D` maybe created) | ||||
| //   2. Desc `D` is registered to registry 'B' in TestB (Note: since 'D' has been created once, thus will be ignored by registry 'B') | ||||
| func (d *Desc) GetRawDesc() *Desc { | ||||
| 	// remove stability from help if any | ||||
| 	stabilityStr := fmt.Sprintf("[%v] ", d.stabilityLevel) | ||||
| 	rawHelp := strings.Replace(d.help, stabilityStr, "", -1) | ||||
|  | ||||
| 	return NewDesc(d.fqName, rawHelp, d.variableLabels, d.constLabels, d.stabilityLevel, d.deprecatedVersion) | ||||
| } | ||||
|   | ||||
| @@ -38,3 +38,13 @@ func CollectAndCompare(c metrics.Collector, expected io.Reader, metricNames ...s | ||||
| func GatherAndCompare(g metrics.Gatherer, expected io.Reader, metricNames ...string) error { | ||||
| 	return testutil.GatherAndCompare(g, expected, metricNames...) | ||||
| } | ||||
|  | ||||
| // CustomCollectAndCompare registers the provided StableCollector with a newly created | ||||
| // registry. It then does the same as GatherAndCompare, gathering the | ||||
| // metrics from the pedantic Registry. | ||||
| func CustomCollectAndCompare(c metrics.StableCollector, expected io.Reader, metricNames ...string) error { | ||||
| 	registry := metrics.NewKubeRegistry() | ||||
| 	registry.CustomMustRegister(c) | ||||
|  | ||||
| 	return GatherAndCompare(registry, expected, metricNames...) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot