2016-11-16 16:09:20 +00:00
|
|
|
// These are specialized integration tests. We only build them when we're doing
|
|
|
|
// a lot of additional work to keep the external docker environment they require
|
|
|
|
// working.
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "gopkg.in/check.v1"
|
|
|
|
|
|
|
|
"database/sql"
|
2016-11-17 15:26:33 +00:00
|
|
|
"fmt"
|
2017-03-27 21:34:22 +00:00
|
|
|
|
2017-02-24 14:08:36 +00:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-11-16 16:09:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
|
|
|
func Test(t *testing.T) { TestingT(t) }
|
|
|
|
|
2017-02-24 14:08:36 +00:00
|
|
|
type IntegrationSuite struct {
|
2016-11-16 16:09:20 +00:00
|
|
|
e *Exporter
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Suite(&IntegrationSuite{})
|
|
|
|
|
|
|
|
func (s *IntegrationSuite) SetUpSuite(c *C) {
|
|
|
|
dsn := os.Getenv("DATA_SOURCE_NAME")
|
|
|
|
c.Assert(dsn, Not(Equals), "")
|
|
|
|
|
2016-11-17 16:52:58 +00:00
|
|
|
exporter := NewExporter(dsn, "")
|
2016-11-16 16:09:20 +00:00
|
|
|
c.Assert(exporter, NotNil)
|
|
|
|
// Assign the exporter to the suite
|
|
|
|
s.e = exporter
|
|
|
|
|
|
|
|
prometheus.MustRegister(exporter)
|
|
|
|
}
|
|
|
|
|
2017-06-06 13:26:32 +00:00
|
|
|
// TODO: it would be nice if cu didn't mostly just recreate the scrape function
|
2016-11-16 16:09:20 +00:00
|
|
|
func (s *IntegrationSuite) TestAllNamespacesReturnResults(c *C) {
|
|
|
|
// Setup a dummy channel to consume metrics
|
|
|
|
ch := make(chan prometheus.Metric, 100)
|
|
|
|
go func() {
|
2017-02-24 14:08:36 +00:00
|
|
|
for range ch {
|
|
|
|
}
|
2016-11-16 16:09:20 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Open a database connection
|
|
|
|
db, err := sql.Open("postgres", s.e.dsn)
|
|
|
|
c.Assert(db, NotNil)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer db.Close()
|
|
|
|
|
2016-11-17 15:26:33 +00:00
|
|
|
// Do a version update
|
|
|
|
err = s.e.checkMapVersions(ch, db)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2017-03-27 21:34:22 +00:00
|
|
|
err = querySettings(ch, db)
|
|
|
|
if !c.Check(err, Equals, nil) {
|
|
|
|
fmt.Println("## ERRORS FOUND")
|
|
|
|
fmt.Println(err)
|
2016-11-17 15:26:33 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:09:20 +00:00
|
|
|
// This should never happen in our test cases.
|
2016-11-17 15:26:33 +00:00
|
|
|
errMap := queryNamespaceMappings(ch, db, s.e.metricMap, s.e.queryOverrides)
|
|
|
|
if !c.Check(len(errMap), Equals, 0) {
|
|
|
|
fmt.Println("## NAMESPACE ERRORS FOUND")
|
|
|
|
for namespace, err := range errMap {
|
|
|
|
fmt.Println(namespace, ":", err)
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 14:08:36 +00:00
|
|
|
}
|
2017-08-03 12:20:03 +00:00
|
|
|
|
2017-08-03 14:01:05 +00:00
|
|
|
// TestInvalidDsnDoesntCrash tests that specifying an invalid DSN doesn't crash
|
|
|
|
// the exporter. Related to https://github.com/wrouesnel/postgres_exporter/issues/93
|
|
|
|
// although not a replication of the scenario.
|
2017-08-03 12:20:03 +00:00
|
|
|
func (s *IntegrationSuite) TestInvalidDsnDoesntCrash(c *C) {
|
|
|
|
// Setup a dummy channel to consume metrics
|
|
|
|
ch := make(chan prometheus.Metric, 100)
|
|
|
|
go func() {
|
|
|
|
for range ch {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-03 14:01:05 +00:00
|
|
|
// Send a bad DSN
|
|
|
|
exporter := NewExporter("invalid dsn", *queriesPath)
|
|
|
|
c.Assert(exporter, NotNil)
|
|
|
|
exporter.scrape(ch)
|
|
|
|
|
|
|
|
// Send a DSN to a non-listening port.
|
|
|
|
exporter = NewExporter("postgresql://nothing:nothing@127.0.0.1:1/nothing", *queriesPath)
|
|
|
|
c.Assert(exporter, NotNil)
|
2017-08-03 12:20:03 +00:00
|
|
|
exporter.scrape(ch)
|
2017-08-03 14:01:05 +00:00
|
|
|
}
|