From 3ead0f9aff77e748f30ff3941b987ce0359a2037 Mon Sep 17 00:00:00 2001 From: Will Rouesnel Date: Tue, 29 Nov 2016 02:12:51 +1100 Subject: [PATCH] Add a test case to detect regression on issue fixed by PR #39. Related issue is #38. Time to start expanding functional test coverage since the integration tests missed this. --- Makefile | 2 +- postgres_exporter.go | 3 ++ postgres_exporter_test.go | 87 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 postgres_exporter_test.go diff --git a/Makefile b/Makefile index 0d603062..7781485a 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ vet: go vet . test: - go test -v . + go test -v -cover . test-integration: postgres_exporter postgres_exporter_integration_test tests/test-smoke ./postgres_exporter ./postgres_exporter_integration_test diff --git a/postgres_exporter.go b/postgres_exporter.go index 4940b54b..6e68f108 100644 --- a/postgres_exporter.go +++ b/postgres_exporter.go @@ -508,6 +508,9 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu // Force to discard if not compatible. if columnMapping.supportedVersions != nil { if !columnMapping.supportedVersions(pgVersion) { + // It's very useful to be able to see what columns are being + // rejected. + log.Debugln(columnName, "is being forced to discard due to version incompatibility.") thisMap[columnName] = MetricMap{ discard: true, conversion: func(in interface{}) (float64, bool) { diff --git a/postgres_exporter_test.go b/postgres_exporter_test.go new file mode 100644 index 00000000..6af757d3 --- /dev/null +++ b/postgres_exporter_test.go @@ -0,0 +1,87 @@ +// +build !integration + +package main + +import ( + "testing" + . "gopkg.in/check.v1" + + "github.com/blang/semver" +) + +// Hook up gocheck into the "go test" runner. +func Test(t *testing.T) { TestingT(t) } + +type FunctionalSuite struct{ + e *Exporter +} + +var _ = Suite(&FunctionalSuite{}) + +func (s *FunctionalSuite) SetUpSuite(c *C) { + +} + +func (s *FunctionalSuite) TestSemanticVersionColumnDiscard(c *C) { + testMetricMap := map[string]map[string]ColumnMapping{ + "test_namespace" : map[string]ColumnMapping{ + "metric_which_stays" : {COUNTER, "This metric should not be eliminated", nil, nil}, + "metric_which_discards" : {COUNTER, "This metric should be forced to DISCARD", nil, nil}, + }, + } + + { + // No metrics should be eliminated + resultMap := makeDescMap(semver.MustParse("0.0.1"), testMetricMap) + c.Check( + resultMap["test_namespace"].columnMappings["metric_which_stays"].discard, + Equals, + false, + ) + c.Check( + resultMap["test_namespace"].columnMappings["metric_which_discards"].discard, + Equals, + false, + ) + } + + { + // Update the map so the discard metric should be eliminated + discardable_metric := testMetricMap["test_namespace"]["metric_which_discards"] + discardable_metric.supportedVersions = semver.MustParseRange(">0.0.1") + testMetricMap["test_namespace"]["metric_which_discards"] = discardable_metric + + // Discard metric should be discarded + resultMap := makeDescMap(semver.MustParse("0.0.1"), testMetricMap) + c.Check( + resultMap["test_namespace"].columnMappings["metric_which_stays"].discard, + Equals, + false, + ) + c.Check( + resultMap["test_namespace"].columnMappings["metric_which_discards"].discard, + Equals, + true, + ) + } + + { + // Update the map so the discard metric should be kept but has a version + discardable_metric := testMetricMap["test_namespace"]["metric_which_discards"] + discardable_metric.supportedVersions = semver.MustParseRange(">0.0.1") + testMetricMap["test_namespace"]["metric_which_discards"] = discardable_metric + + // Discard metric should be discarded + resultMap := makeDescMap(semver.MustParse("0.0.2"), testMetricMap) + c.Check( + resultMap["test_namespace"].columnMappings["metric_which_stays"].discard, + Equals, + false, + ) + c.Check( + resultMap["test_namespace"].columnMappings["metric_which_discards"].discard, + Equals, + false, + ) + } +} \ No newline at end of file