mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 02:21:50 +08:00
postgres: support running multiple postgres integration tests
Makes it possible to run mysql integration tests for both grafana config db and tsdb at the same time using GRAFANA_TEST_DB=postgres go test ./pkg/...
This commit is contained in:
3
docker/blocks/postgres_tests/Dockerfile
Normal file
3
docker/blocks/postgres_tests/Dockerfile
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
FROM postgres:latest
|
||||||
|
ADD setup.sql /docker-entrypoint-initdb.d
|
||||||
|
CMD ["postgres"]
|
@ -100,7 +100,7 @@
|
|||||||
"gnetId": null,
|
"gnetId": null,
|
||||||
"graphTooltip": 0,
|
"graphTooltip": 0,
|
||||||
"id": null,
|
"id": null,
|
||||||
"iteration": 1521725946837,
|
"iteration": 1523320929325,
|
||||||
"links": [],
|
"links": [],
|
||||||
"panels": [
|
"panels": [
|
||||||
{
|
{
|
||||||
@ -443,7 +443,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -522,7 +526,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -601,7 +609,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -680,7 +692,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -759,7 +775,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -838,7 +858,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -927,7 +951,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1008,7 +1036,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1097,7 +1129,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1178,7 +1214,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1267,7 +1307,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1348,7 +1392,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1437,7 +1485,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1518,7 +1570,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1608,7 +1664,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1691,7 +1751,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1781,7 +1845,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1864,7 +1932,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -1954,7 +2026,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -2037,7 +2113,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -2127,7 +2207,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"aliasColors": {},
|
"aliasColors": {},
|
||||||
@ -2210,7 +2294,11 @@
|
|||||||
"min": null,
|
"min": null,
|
||||||
"show": true
|
"show": true
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"yaxis": {
|
||||||
|
"align": false,
|
||||||
|
"alignLevel": null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"refresh": false,
|
"refresh": false,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
postgrestest:
|
postgrestest:
|
||||||
image: postgres:latest
|
build:
|
||||||
|
context: blocks/postgres_tests
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: grafanatest
|
POSTGRES_USER: grafanatest
|
||||||
POSTGRES_PASSWORD: grafanatest
|
POSTGRES_PASSWORD: grafanatest
|
||||||
|
3
docker/blocks/postgres_tests/setup.sql
Normal file
3
docker/blocks/postgres_tests/setup.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
CREATE DATABASE grafanadstest;
|
||||||
|
REVOKE CONNECT ON DATABASE grafanadstest FROM PUBLIC;
|
||||||
|
GRANT CONNECT ON DATABASE grafanadstest TO grafanatest;
|
@ -278,3 +278,10 @@ func IsTestDbMySql() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsTestDbPostgres() bool {
|
||||||
|
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
|
||||||
|
return db == dbPostgres
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -3,26 +3,36 @@ package postgres
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
|
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
|
||||||
"github.com/grafana/grafana/pkg/tsdb"
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
// To run this test, remove the Skip from SkipConvey
|
// To run this test, set runMySqlTests=true
|
||||||
// and set up a PostgreSQL db named grafanatest and a user/password grafanatest/grafanatest!
|
// and set up a PostgreSQL db named grafanadstest and a user/password grafanatest/grafanatest!
|
||||||
// Use the docker/blocks/postgres_tests/docker-compose.yaml to spin up a
|
// Use the docker/blocks/postgres_tests/docker-compose.yaml to spin up a
|
||||||
// preconfigured Postgres server suitable for running these tests.
|
// preconfigured Postgres server suitable for running these tests.
|
||||||
// Thers's also a dashboard.json in same directory that you can import to Grafana
|
// Thers's also a dashboard.json in same directory that you can import to Grafana
|
||||||
// once you've created a datasource for the test server/database.
|
// once you've created a datasource for the test server/database.
|
||||||
func TestPostgres(t *testing.T) {
|
func TestPostgres(t *testing.T) {
|
||||||
SkipConvey("PostgreSQL", t, func() {
|
// change to true to run the MySQL tests
|
||||||
|
runPostgresTests := false
|
||||||
|
// runPostgresTests := true
|
||||||
|
|
||||||
|
if !(sqlstore.IsTestDbPostgres() || runPostgresTests) {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
|
||||||
|
Convey("PostgreSQL", t, func() {
|
||||||
x := InitPostgresTestDB(t)
|
x := InitPostgresTestDB(t)
|
||||||
|
|
||||||
endpoint := &PostgresQueryEndpoint{
|
endpoint := &PostgresQueryEndpoint{
|
||||||
@ -157,8 +167,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err = sess.InsertMulti(series)
|
_, err = sess.InsertMulti(series)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
}
|
|
||||||
|
|
||||||
Convey("When doing a metric query using timeGroup", func() {
|
Convey("When doing a metric query using timeGroup", func() {
|
||||||
query := &tsdb.TsdbQuery{
|
query := &tsdb.TsdbQuery{
|
||||||
@ -451,7 +460,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
|
|
||||||
So(len(queryResult.Series), ShouldEqual, 1)
|
So(len(queryResult.Series), ShouldEqual, 1)
|
||||||
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(tInitial.UnixNano()/1e6))
|
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(tInitial.UnixNano()/1e6))
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When doing a metric query using epoch (int32 nullable) as time column should return metric with time in milliseconds", func() {
|
Convey("When doing a metric query using epoch (int32 nullable) as time column should return metric with time in milliseconds", func() {
|
||||||
query := &tsdb.TsdbQuery{
|
query := &tsdb.TsdbQuery{
|
||||||
@ -473,7 +482,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
|
|
||||||
So(len(queryResult.Series), ShouldEqual, 1)
|
So(len(queryResult.Series), ShouldEqual, 1)
|
||||||
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(tInitial.UnixNano()/1e6))
|
So(queryResult.Series[0].Points[0][1].Float64, ShouldEqual, float64(tInitial.UnixNano()/1e6))
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When doing a metric query using epoch (float32) as time column should return metric with time in milliseconds", func() {
|
Convey("When doing a metric query using epoch (float32) as time column should return metric with time in milliseconds", func() {
|
||||||
query := &tsdb.TsdbQuery{
|
query := &tsdb.TsdbQuery{
|
||||||
@ -486,7 +495,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
RefId: "A",
|
RefId: "A",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := endpoint.Query(nil, nil, query)
|
resp, err := endpoint.Query(nil, nil, query)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@ -508,7 +517,7 @@ func TestPostgres(t *testing.T) {
|
|||||||
RefId: "A",
|
RefId: "A",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := endpoint.Query(nil, nil, query)
|
resp, err := endpoint.Query(nil, nil, query)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@ -826,16 +835,16 @@ func TestPostgres(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitPostgresTestDB(t *testing.T) *xorm.Engine {
|
func InitPostgresTestDB(t *testing.T) *xorm.Engine {
|
||||||
x, err := xorm.NewEngine(sqlutil.TestDB_Postgres.DriverName, sqlutil.TestDB_Postgres.ConnStr)
|
x, err := xorm.NewEngine(sqlutil.TestDB_Postgres.DriverName, strings.Replace(sqlutil.TestDB_Postgres.ConnStr, "dbname=grafanatest", "dbname=grafanadstest", 1))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to init postgres db %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
x.DatabaseTZ = time.UTC
|
x.DatabaseTZ = time.UTC
|
||||||
x.TZLocation = time.UTC
|
x.TZLocation = time.UTC
|
||||||
|
|
||||||
// x.ShowSQL()
|
// x.ShowSQL()
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to init postgres db %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user