فهرست منبع

mysql: mysql tests should use a db server with UTC

To get rid of issues involving date/time when testing.
Also, makes it possible to run mysql integration tests for both
grafana config db and tsdb at the same time using
GRAFANA_TEST_DB=mysql go test ./pkg/...
Marcus Efraimsson 7 سال پیش
والد
کامیت
5c120c2c11

+ 3 - 0
docker/blocks/mysql_tests/Dockerfile

@@ -0,0 +1,3 @@
+FROM mysql:latest
+ADD setup.sql /docker-entrypoint-initdb.d
+CMD ["mysqld"]

+ 116 - 42
docker/blocks/mysql_tests/dashboard.json

@@ -7,14 +7,6 @@
       "type": "datasource",
       "type": "datasource",
       "pluginId": "mysql",
       "pluginId": "mysql",
       "pluginName": "MySQL"
       "pluginName": "MySQL"
-    },
-    {
-      "name": "DS_MSSQL_TEST",
-      "label": "MSSQL Test",
-      "description": "",
-      "type": "datasource",
-      "pluginId": "mssql",
-      "pluginName": "Microsoft SQL Server"
     }
     }
   ],
   ],
   "__requires": [
   "__requires": [
@@ -30,12 +22,6 @@
       "name": "Graph",
       "name": "Graph",
       "version": "5.0.0"
       "version": "5.0.0"
     },
     },
-    {
-      "type": "datasource",
-      "id": "mssql",
-      "name": "Microsoft SQL Server",
-      "version": "1.0.0"
-    },
     {
     {
       "type": "datasource",
       "type": "datasource",
       "id": "mysql",
       "id": "mysql",
@@ -114,7 +100,7 @@
   "gnetId": null,
   "gnetId": null,
   "graphTooltip": 0,
   "graphTooltip": 0,
   "id": null,
   "id": null,
-  "iteration": 1521715720483,
+  "iteration": 1523320712115,
   "links": [],
   "links": [],
   "panels": [
   "panels": [
     {
     {
@@ -349,7 +335,7 @@
         {
         {
           "alias": "Time",
           "alias": "Time",
           "dateFormat": "YYYY-MM-DD HH:mm:ss",
           "dateFormat": "YYYY-MM-DD HH:mm:ss",
-          "pattern": "time_sec",
+          "pattern": "time",
           "type": "date"
           "type": "date"
         },
         },
         {
         {
@@ -457,7 +443,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -536,7 +526,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -615,7 +609,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -694,7 +692,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -773,7 +775,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -852,7 +858,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -941,7 +951,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1034,7 +1048,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1123,7 +1141,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1204,7 +1226,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1293,7 +1319,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1374,7 +1404,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1463,7 +1497,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1544,7 +1582,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1634,14 +1676,18 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
       "bars": true,
       "bars": true,
       "dashLength": 10,
       "dashLength": 10,
       "dashes": false,
       "dashes": false,
-      "datasource": "${DS_MSSQL_TEST}",
+      "datasource": "${DS_MYSQL_TEST}",
       "fill": 1,
       "fill": 1,
       "gridPos": {
       "gridPos": {
         "h": 8,
         "h": 8,
@@ -1717,7 +1763,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1807,7 +1857,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1890,7 +1944,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -1980,7 +2038,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -2063,7 +2125,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -2153,7 +2219,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     },
     },
     {
     {
       "aliasColors": {},
       "aliasColors": {},
@@ -2236,7 +2306,11 @@
           "min": null,
           "min": null,
           "show": true
           "show": true
         }
         }
-      ]
+      ],
+      "yaxis": {
+        "align": false,
+        "alignLevel": null
+      }
     }
     }
   ],
   ],
   "refresh": false,
   "refresh": false,
@@ -2315,8 +2389,8 @@
     ]
     ]
   },
   },
   "time": {
   "time": {
-    "from": "2018-03-15T11:30:00.000Z",
-    "to": "2018-03-15T12:55:01.000Z"
+    "from": "2018-03-15T12:30:00.000Z",
+    "to": "2018-03-15T13:55:01.000Z"
   },
   },
   "timepicker": {
   "timepicker": {
     "refresh_intervals": [
     "refresh_intervals": [
@@ -2346,5 +2420,5 @@
   "timezone": "",
   "timezone": "",
   "title": "MySQL Data Source Test",
   "title": "MySQL Data Source Test",
   "uid": "Hmf8FDkmz",
   "uid": "Hmf8FDkmz",
-  "version": 9
+  "version": 12
 }
 }

+ 2 - 4
docker/blocks/mysql_tests/docker-compose.yaml

@@ -1,5 +1,6 @@
   mysqltests:
   mysqltests:
-    image: mysql:latest
+    build:
+      context: blocks/mysql_tests
     environment:
     environment:
       MYSQL_ROOT_PASSWORD: rootpass
       MYSQL_ROOT_PASSWORD: rootpass
       MYSQL_DATABASE: grafana_tests
       MYSQL_DATABASE: grafana_tests
@@ -7,7 +8,4 @@
       MYSQL_PASSWORD: password
       MYSQL_PASSWORD: password
     ports:
     ports:
       - "3306:3306"
       - "3306:3306"
-    volumes:
-      - /etc/localtime:/etc/localtime:ro
-      - /etc/timezone:/etc/timezone:ro
     tmpfs: /var/lib/mysql:rw
     tmpfs: /var/lib/mysql:rw

+ 2 - 0
docker/blocks/mysql_tests/setup.sql

@@ -0,0 +1,2 @@
+CREATE DATABASE grafana_ds_tests;
+GRANT ALL PRIVILEGES ON grafana_ds_tests.* TO 'grafana';

+ 10 - 1
pkg/services/sqlstore/sqlstore.go

@@ -258,7 +258,7 @@ func InitTestDB(t *testing.T) *xorm.Engine {
 	// x.ShowSQL()
 	// x.ShowSQL()
 
 
 	if err != nil {
 	if err != nil {
-		t.Fatalf("Failed to init in memory sqllite3 db %v", err)
+		t.Fatalf("Failed to init test database: %v", err)
 	}
 	}
 
 
 	sqlutil.CleanDB(x)
 	sqlutil.CleanDB(x)
@@ -269,3 +269,12 @@ func InitTestDB(t *testing.T) *xorm.Engine {
 
 
 	return x
 	return x
 }
 }
+
+func IsTestDbMySql() bool {
+	if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
+		return db == dbMySql
+	}
+
+	return false
+}
+

+ 23 - 15
pkg/tsdb/mysql/mysql_test.go

@@ -3,25 +3,35 @@ package mysql
 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/smartystreets/goconvey/convey"
 	. "github.com/smartystreets/goconvey/convey"
 )
 )
 
 
-// To run this test, remove the Skip from SkipConvey
-// and set up a MySQL db named grafana_tests and a user/password grafana/password
+// To run this test, set runMySqlTests=true
+// and set up a MySQL db named grafana_ds_tests and a user/password grafana/password
 // Use the docker/blocks/mysql_tests/docker-compose.yaml to spin up a
 // Use the docker/blocks/mysql_tests/docker-compose.yaml to spin up a
 // preconfigured MySQL server suitable for running these tests.
 // preconfigured MySQL 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 TestMySQL(t *testing.T) {
 func TestMySQL(t *testing.T) {
-	SkipConvey("MySQL", t, func() {
+	// change to true to run the MySQL tests
+	runMySqlTests := false
+	// runMySqlTests := true
+
+	if !(sqlstore.IsTestDbMySql() || runMySqlTests) {
+		t.Skip()
+	}
+
+	Convey("MySQL", t, func() {
 		x := InitMySQLTestDB(t)
 		x := InitMySQLTestDB(t)
 
 
 		endpoint := &MysqlQueryEndpoint{
 		endpoint := &MysqlQueryEndpoint{
@@ -35,7 +45,7 @@ func TestMySQL(t *testing.T) {
 		sess := x.NewSession()
 		sess := x.NewSession()
 		defer sess.Close()
 		defer sess.Close()
 
 
-		fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.Local)
+		fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC)
 
 
 		Convey("Given a table with different native data types", func() {
 		Convey("Given a table with different native data types", func() {
 			if exists, err := sess.IsTableExist("mysql_types"); err != nil || exists {
 			if exists, err := sess.IsTableExist("mysql_types"); err != nil || exists {
@@ -121,9 +131,8 @@ func TestMySQL(t *testing.T) {
 				So(column[7].(float64), ShouldEqual, 1.11)
 				So(column[7].(float64), ShouldEqual, 1.11)
 				So(column[8].(float64), ShouldEqual, 2.22)
 				So(column[8].(float64), ShouldEqual, 2.22)
 				So(*column[9].(*float32), ShouldEqual, 3.33)
 				So(*column[9].(*float32), ShouldEqual, 3.33)
-				_, offset := time.Now().Zone()
-				So(column[10].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now().Add(time.Duration(offset)*time.Second))
-				So(column[11].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now().Add(time.Duration(offset)*time.Second))
+				So(column[10].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now())
+				So(column[11].(time.Time), ShouldHappenWithin, time.Duration(10*time.Second), time.Now())
 				So(column[12].(string), ShouldEqual, "11:11:11")
 				So(column[12].(string), ShouldEqual, "11:11:11")
 				So(column[13].(int64), ShouldEqual, 2018)
 				So(column[13].(int64), ShouldEqual, 2018)
 				So(*column[14].(*[]byte), ShouldHaveSameTypeAs, []byte{1})
 				So(*column[14].(*[]byte), ShouldHaveSameTypeAs, []byte{1})
@@ -137,8 +146,7 @@ func TestMySQL(t *testing.T) {
 				So(column[22].(string), ShouldEqual, "longblob")
 				So(column[22].(string), ShouldEqual, "longblob")
 				So(column[23].(string), ShouldEqual, "val2")
 				So(column[23].(string), ShouldEqual, "val2")
 				So(column[24].(string), ShouldEqual, "a,b")
 				So(column[24].(string), ShouldEqual, "a,b")
-				So(column[25].(time.Time).Format("2006-01-02T00:00:00Z"), ShouldEqual, time.Now().Format("2006-01-02T00:00:00Z"))
-				So(column[26].(float64), ShouldEqual, float64(1514764861000))
+				So(column[25].(time.Time).Format("2006-01-02T00:00:00Z"), ShouldEqual, time.Now().UTC().Format("2006-01-02T00:00:00Z"))
 				So(column[27], ShouldEqual, nil)
 				So(column[27], ShouldEqual, nil)
 				So(column[28], ShouldEqual, nil)
 				So(column[28], ShouldEqual, nil)
 				So(column[29], ShouldEqual, "")
 				So(column[29], ShouldEqual, "")
@@ -647,16 +655,16 @@ func TestMySQL(t *testing.T) {
 }
 }
 
 
 func InitMySQLTestDB(t *testing.T) *xorm.Engine {
 func InitMySQLTestDB(t *testing.T) *xorm.Engine {
-	x, err := xorm.NewEngine(sqlutil.TestDB_Mysql.DriverName, sqlutil.TestDB_Mysql.ConnStr+"&parseTime=true")
-	x.DatabaseTZ = time.Local
-	x.TZLocation = time.Local
-
-	// x.ShowSQL()
-
+	x, err := xorm.NewEngine(sqlutil.TestDB_Mysql.DriverName, strings.Replace(sqlutil.TestDB_Mysql.ConnStr, "/grafana_tests", "/grafana_ds_tests", 1))
 	if err != nil {
 	if err != nil {
 		t.Fatalf("Failed to init mysql db %v", err)
 		t.Fatalf("Failed to init mysql db %v", err)
 	}
 	}
 
 
+	x.DatabaseTZ = time.UTC
+	x.TZLocation = time.UTC
+
+	// x.ShowSQL()
+
 	return x
 	return x
 }
 }