bergquist 8 лет назад
Родитель
Сommit
7f3a7ea128

+ 67 - 9
pkg/services/provisioning/dashboard/file_reader_test.go

@@ -4,7 +4,9 @@ import (
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/services/alerting"
+	"os"
 	"testing"
+	"time"
 
 	"github.com/grafana/grafana/pkg/log"
 	. "github.com/smartystreets/goconvey/convey"
@@ -13,11 +15,15 @@ import (
 var (
 	defaultDashboards string = "./test-dashboards/folder-one"
 	brokenDashboards  string = "./test-dashboards/broken-dashboards"
+	oneDashboard      string = "./test-dashboards/one-dashboard"
+
+	fakeRepo *fakeDashboardRepo
 )
 
 func TestDashboardFileReader(t *testing.T) {
 	Convey("Reading dashboards from disk", t, func() {
 		bus.ClearBusHandlers()
+		fakeRepo = &fakeDashboardRepo{}
 
 		bus.AddHandler("test", mockGetDashboardQuery)
 		bus.AddHandler("test", mockValidateDashboardAlertsCommand)
@@ -25,21 +31,60 @@ func TestDashboardFileReader(t *testing.T) {
 		bus.AddHandler("test", mockUpdateDashboardAlertsCommand)
 		logger := log.New("test.logger")
 
+		cfg := &DashboardsAsConfig{
+			Name:    "Default",
+			Type:    "file",
+			OrgId:   1,
+			Folder:  "",
+			Options: map[string]interface{}{},
+		}
+
 		Convey("Can read default dashboard", func() {
-			cfg := &DashboardsAsConfig{
-				Name:   "Default",
-				Type:   "file",
-				OrgId:  1,
-				Folder: "",
-				Options: map[string]interface{}{
-					"folder": defaultDashboards,
-				},
-			}
+			cfg.Options["folder"] = defaultDashboards
+
+			reader, err := NewDashboardFilereader(cfg, logger)
+			So(err, ShouldBeNil)
+
+			err = reader.walkFolder()
+			So(err, ShouldBeNil)
+
+			So(len(fakeRepo.inserted), ShouldEqual, 2)
+		})
+
+		Convey("Should not update dashboards when db is newer", func() {
+			cfg.Options["folder"] = oneDashboard
+
+			fakeRepo.getDashboard = append(fakeRepo.getDashboard, &models.Dashboard{
+				Updated: time.Now().Add(time.Hour),
+				Slug:    "grafana",
+			})
+
+			reader, err := NewDashboardFilereader(cfg, logger)
+			So(err, ShouldBeNil)
+
+			err = reader.walkFolder()
+			So(err, ShouldBeNil)
+
+			So(len(fakeRepo.inserted), ShouldEqual, 0)
+		})
+
+		Convey("Can read default dashboard and replace old version in database", func() {
+			cfg.Options["folder"] = oneDashboard
+
+			stat, _ := os.Stat(oneDashboard + "/dashboard1.json")
+
+			fakeRepo.getDashboard = append(fakeRepo.getDashboard, &models.Dashboard{
+				Updated: stat.ModTime().AddDate(0, 0, -1),
+				Slug:    "grafana",
+			})
+
 			reader, err := NewDashboardFilereader(cfg, logger)
 			So(err, ShouldBeNil)
 
 			err = reader.walkFolder()
 			So(err, ShouldBeNil)
+
+			So(len(fakeRepo.inserted), ShouldEqual, 1)
 		})
 
 		Convey("Invalid configuration should return error", func() {
@@ -71,7 +116,19 @@ func TestDashboardFileReader(t *testing.T) {
 	})
 }
 
+type fakeDashboardRepo struct {
+	inserted     []*models.SaveDashboardCommand
+	getDashboard []*models.Dashboard
+}
+
 func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error {
+	for _, d := range fakeRepo.getDashboard {
+		if d.Slug == cmd.Slug {
+			cmd.Result = d
+			return nil
+		}
+	}
+
 	return models.ErrDashboardNotFound
 }
 
@@ -80,6 +137,7 @@ func mockValidateDashboardAlertsCommand(cmd *alerting.ValidateDashboardAlertsCom
 }
 
 func mockSaveDashboardCommand(cmd *models.SaveDashboardCommand) error {
+	fakeRepo.inserted = append(fakeRepo.inserted, cmd)
 	return nil
 }
 

+ 173 - 0
pkg/services/provisioning/dashboard/test-dashboards/one-dashboard/dashboard1.json

@@ -0,0 +1,173 @@
+{
+    "title": "Grafana",
+    "tags": [],
+    "style": "dark",
+    "timezone": "browser",
+    "editable": true,
+    "rows": [
+      {
+        "title": "New row",
+        "height": "150px",
+        "collapse": false,
+        "editable": true,
+        "panels": [
+          {
+            "id": 1,
+            "span": 12,
+            "editable": true,
+            "type": "text",
+            "mode": "html",
+            "content": "<div class=\"text-center\" style=\"padding-top: 15px\">\n<img src=\"img/logo_transparent_200x.png\"> \n</div>",
+            "style": {},
+            "title": "Welcome to"
+          }
+        ]
+      },
+      {
+        "title": "Welcome to Grafana",
+        "height": "210px",
+        "collapse": false,
+        "editable": true,
+        "panels": [
+          {
+            "id": 2,
+            "span": 6,
+            "type": "text",
+            "mode": "html",
+            "content": "<br/>\n\n<div class=\"row-fluid\">\n  <div class=\"span6\">\n    <ul>\n      <li>\n        <a href=\"http://grafana.org/docs#configuration\" target=\"_blank\">Configuration</a>\n      </li>\n      <li>\n        <a href=\"http://grafana.org/docs/troubleshooting\" target=\"_blank\">Troubleshooting</a>\n      </li>\n      <li>\n        <a href=\"http://grafana.org/docs/support\" target=\"_blank\">Support</a>\n      </li>\n      <li>\n        <a href=\"http://grafana.org/docs/features/intro\" target=\"_blank\">Getting started</a>  (Must read!)\n      </li>\n    </ul>\n  </div>\n  <div class=\"span6\">\n    <ul>\n      <li>\n        <a href=\"http://grafana.org/docs/features/graphing\" target=\"_blank\">Graphing</a>\n      </li>\n      <li>\n        <a href=\"http://grafana.org/docs/features/annotations\" target=\"_blank\">Annotations</a>\n      </li>\n      <li>\n        <a href=\"http://grafana.org/docs/features/graphite\" target=\"_blank\">Graphite</a>\n      </li>\n      <li>\n        <a href=\"http://grafana.org/docs/features/influxdb\" target=\"_blank\">InfluxDB</a>\n      </li>\n      <li>\n        <a href=\"http://grafana.org/docs/features/opentsdb\" target=\"_blank\">OpenTSDB</a>\n      </li>\n    </ul>\n  </div>\n</div>",
+            "style": {},
+            "title": "Documentation Links"
+          },
+          {
+            "id": 3,
+            "span": 6,
+            "type": "text",
+            "mode": "html",
+            "content": "<br/>\n\n<div class=\"row-fluid\">\n  <div class=\"span12\">\n    <ul>\n      <li>Ctrl+S saves the current dashboard</li>\n      <li>Ctrl+F Opens the dashboard finder</li>\n      <li>Ctrl+H Hide/show row controls</li>\n      <li>Click and drag graph title to move panel</li>\n      <li>Hit Escape to exit graph when in fullscreen or edit mode</li>\n      <li>Click the colored icon in the legend to change series color</li>\n      <li>Ctrl or Shift + Click legend name to hide other series</li>\n    </ul>\n  </div>\n</div>\n",
+            "style": {},
+            "title": "Tips & Shortcuts"
+          }
+        ]
+      },
+      {
+        "title": "test",
+        "height": "250px",
+        "editable": true,
+        "collapse": false,
+        "panels": [
+          {
+            "id": 4,
+            "span": 12,
+            "type": "graph",
+            "x-axis": true,
+            "y-axis": true,
+            "scale": 1,
+            "y_formats": [
+              "short",
+              "short"
+            ],
+            "grid": {
+              "max": null,
+              "min": null,
+              "leftMax": null,
+              "rightMax": null,
+              "leftMin": null,
+              "rightMin": null,
+              "threshold1": null,
+              "threshold2": null,
+              "threshold1Color": "rgba(216, 200, 27, 0.27)",
+              "threshold2Color": "rgba(234, 112, 112, 0.22)"
+            },
+            "resolution": 100,
+            "lines": true,
+            "fill": 1,
+            "linewidth": 2,
+            "dashes": false,
+            "dashLength": 10,
+            "spaceLength": 10,
+            "points": false,
+            "pointradius": 5,
+            "bars": false,
+            "stack": true,
+            "spyable": true,
+            "options": false,
+            "legend": {
+              "show": true,
+              "values": false,
+              "min": false,
+              "max": false,
+              "current": false,
+              "total": false,
+              "avg": false
+            },
+            "interactive": true,
+            "legend_counts": true,
+            "timezone": "browser",
+            "percentage": false,
+            "nullPointMode": "connected",
+            "steppedLine": false,
+            "tooltip": {
+              "value_type": "cumulative",
+              "query_as_alias": true
+            },
+            "targets": [
+              {
+                "target": "randomWalk('random walk')",
+                "function": "mean",
+                "column": "value"
+              }
+            ],
+            "aliasColors": {},
+            "aliasYAxis": {},
+            "title": "First Graph (click title to edit)",
+            "datasource": "graphite",
+            "renderer": "flot",
+            "annotate": {
+              "enable": false
+            }
+          }
+        ]
+      }
+    ],
+    "nav": [
+      {
+        "type": "timepicker",
+        "collapse": false,
+        "enable": true,
+        "status": "Stable",
+        "time_options": [
+          "5m",
+          "15m",
+          "1h",
+          "6h",
+          "12h",
+          "24h",
+          "2d",
+          "7d",
+          "30d"
+        ],
+        "refresh_intervals": [
+          "5s",
+          "10s",
+          "30s",
+          "1m",
+          "5m",
+          "15m",
+          "30m",
+          "1h",
+          "2h",
+          "1d"
+        ],
+        "now": true
+      }
+    ],
+    "time": {
+      "from": "now-6h",
+      "to": "now"
+    },
+    "templating": {
+      "list": []
+    },
+    "version": 5
+  }
+