postgres_exporter/postgres_exporter_test.go

152 lines
4.3 KiB
Go
Raw Normal View History

// +build !integration
package main
import (
. "gopkg.in/check.v1"
2017-02-24 14:08:36 +00:00
"testing"
"github.com/blang/semver"
2017-07-31 13:46:05 +00:00
"os"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
2017-02-24 14:08:36 +00:00
type FunctionalSuite struct {
}
var _ = Suite(&FunctionalSuite{})
func (s *FunctionalSuite) SetUpSuite(c *C) {
}
func (s *FunctionalSuite) TestSemanticVersionColumnDiscard(c *C) {
testMetricMap := map[string]map[string]ColumnMapping{
2017-02-24 14:08:36 +00:00
"test_namespace": {
"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
discardableMetric := testMetricMap["test_namespace"]["metric_which_discards"]
discardableMetric.supportedVersions = semver.MustParseRange(">0.0.1")
testMetricMap["test_namespace"]["metric_which_discards"] = discardableMetric
// 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
discardableMetric := testMetricMap["test_namespace"]["metric_which_discards"]
discardableMetric.supportedVersions = semver.MustParseRange(">0.0.1")
testMetricMap["test_namespace"]["metric_which_discards"] = discardableMetric
// 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,
)
}
2017-02-24 14:08:36 +00:00
}
2017-07-31 13:46:05 +00:00
2017-08-01 06:23:52 +00:00
// test read username and password from file
func (s *FunctionalSuite) TestEnvironmentSettingWithSecretsFiles(c *C) {
2017-07-31 13:46:05 +00:00
err := os.Setenv("DATA_SOURCE_USER_FILE", "./tests/username_file")
2017-08-01 06:23:52 +00:00
c.Assert(err, IsNil)
defer UnsetEnvironment(c, "DATA_SOURCE_USER_FILE")
2017-07-31 13:46:05 +00:00
err = os.Setenv("DATA_SOURCE_PASS_FILE", "./tests/userpass_file")
2017-08-01 06:23:52 +00:00
c.Assert(err, IsNil)
defer UnsetEnvironment(c, "DATA_SOURCE_PASS_FILE")
2017-07-31 13:46:05 +00:00
err = os.Setenv("DATA_SOURCE_URI", "localhost:5432/?sslmode=disable")
2017-08-01 06:23:52 +00:00
c.Assert(err, IsNil)
defer UnsetEnvironment(c, "DATA_SOURCE_URI")
2017-07-31 13:46:05 +00:00
var expected = "postgresql://custom_username:custom_password@localhost:5432/?sslmode=disable"
dsn := getDataSource()
if dsn != expected {
2017-08-01 06:23:52 +00:00
c.Errorf("Expected Username to be read from file. Found=%v, expected=%v", dsn, expected)
2017-07-31 13:46:05 +00:00
}
}
2017-08-01 06:23:52 +00:00
// test read DATA_SOURCE_NAME from environment
func (s *FunctionalSuite) TestEnvironmentSettingWithDns(c *C) {
2017-07-31 13:46:05 +00:00
envDsn := "postgresql://user:password@localhost:5432/?sslmode=enabled"
err := os.Setenv("DATA_SOURCE_NAME", envDsn)
2017-08-01 06:23:52 +00:00
c.Assert(err, IsNil)
defer UnsetEnvironment(c, "DATA_SOURCE_NAME")
2017-07-31 13:46:05 +00:00
dsn := getDataSource()
if dsn != envDsn {
2017-08-01 06:23:52 +00:00
c.Errorf("Expected Username to be read from file. Found=%v, expected=%v", dsn, envDsn)
2017-07-31 13:46:05 +00:00
}
}
2017-08-01 06:23:52 +00:00
// test DATA_SOURCE_NAME is used even if username and password environment variables are set
func (s *FunctionalSuite) TestEnvironmentSettingWithDnsAndSecrets(c *C) {
2017-07-31 13:46:05 +00:00
envDsn := "postgresql://userDsn:passwordDsn@localhost:55432/?sslmode=disabled"
err := os.Setenv("DATA_SOURCE_NAME", envDsn)
2017-08-01 06:23:52 +00:00
c.Assert(err, IsNil)
defer UnsetEnvironment(c, "DATA_SOURCE_NAME")
2017-07-31 13:46:05 +00:00
err = os.Setenv("DATA_SOURCE_USER_FILE", "./tests/username_file")
2017-08-01 06:23:52 +00:00
c.Assert(err, IsNil)
defer UnsetEnvironment(c, "DATA_SOURCE_USER_FILE")
2017-07-31 13:46:05 +00:00
err = os.Setenv("DATA_SOURCE_PASS", "envUserPass")
2017-08-01 06:23:52 +00:00
c.Assert(err, IsNil)
defer UnsetEnvironment(c, "DATA_SOURCE_PASS")
2017-07-31 13:46:05 +00:00
dsn := getDataSource()
if dsn != envDsn {
2017-08-01 06:23:52 +00:00
c.Errorf("Expected Username to be read from file. Found=%v, expected=%v", dsn, envDsn)
2017-07-31 13:46:05 +00:00
}
}
2017-08-01 06:23:52 +00:00
func UnsetEnvironment(c *C, d string) {
err := os.Unsetenv(d)
c.Assert(err, IsNil)
}