From b0d5700ec0216d1609cdbcb3841ef0d12672943d Mon Sep 17 00:00:00 2001 From: Marcin Budny Date: Fri, 2 Feb 2018 02:24:07 +0100 Subject: [PATCH] fixed version parsing for Windows and EnterpriseDB (#151) * fixed version parsing for Windows and EnterpriseDB * fixed formatting --- postgres_exporter.go | 2 +- postgres_exporter_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/postgres_exporter.go b/postgres_exporter.go index beb690fb..49d399ca 100644 --- a/postgres_exporter.go +++ b/postgres_exporter.go @@ -80,7 +80,7 @@ func (cu *ColumnUsage) UnmarshalYAML(unmarshal func(interface{}) error) error { } // Regex used to get the "short-version" from the postgres version field. -var versionRegex = regexp.MustCompile(`^\w+ ((\d+)(\.\d+)?(\.\d+)?)\s`) +var versionRegex = regexp.MustCompile(`^\w+ ((\d+)(\.\d+)?(\.\d+)?)`) var lowestSupportedVersion = semver.MustParse("9.1.0") // Parses the version of postgres into the short version string we can use to diff --git a/postgres_exporter_test.go b/postgres_exporter_test.go index f9190dc5..e5a50b09 100644 --- a/postgres_exporter_test.go +++ b/postgres_exporter_test.go @@ -145,6 +145,34 @@ func (s *FunctionalSuite) TestEnvironmentSettingWithDnsAndSecrets(c *C) { } } +func (s *FunctionalSuite) TestPostgresVersionParsing(c *C) { + type TestCase struct { + input string + expected string + } + + cases := []TestCase{ + { + input: "PostgreSQL 10.1 on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18) 6.3.0 20170516, 64-bit", + expected: "10.1.0", + }, + { + input: "PostgreSQL 9.5.4, compiled by Visual C++ build 1800, 64-bit", + expected: "9.5.4", + }, + { + input: "EnterpriseDB 9.6.5.10 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16), 64-bit", + expected: "9.6.5", + }, + } + + for _, cs := range cases { + ver, err := parseVersion(cs.input) + c.Assert(err, IsNil) + c.Assert(ver.String(), Equals, cs.expected) + } +} + func UnsetEnvironment(c *C, d string) { err := os.Unsetenv(d) c.Assert(err, IsNil)