Пример #1
0
func TestImageUploaderFactory(t *testing.T) {
	Convey("Can create image uploader for ", t, func() {
		Convey("S3ImageUploader", func() {
			var err error
			setting.NewConfigContext(&setting.CommandLineArgs{
				HomePath: "../../../",
			})

			setting.ImageUploadProvider = "s3"

			s3sec, err := setting.Cfg.GetSection("external_image_storage.s3")
			s3sec.NewKey("bucket_url", "https://foo.bar.baz.s3-us-east-2.amazonaws.com")
			s3sec.NewKey("access_key", "access_key")
			s3sec.NewKey("secret_key", "secret_key")

			uploader, err := NewImageUploader()

			So(err, ShouldBeNil)
			original, ok := uploader.(*S3Uploader)

			So(ok, ShouldBeTrue)
			So(original.region, ShouldEqual, "us-east-2")
			So(original.bucket, ShouldEqual, "foo.bar.baz")
			So(original.accessKey, ShouldEqual, "access_key")
			So(original.secretKey, ShouldEqual, "secret_key")
		})

		Convey("Webdav uploader", func() {
			var err error

			setting.NewConfigContext(&setting.CommandLineArgs{
				HomePath: "../../../",
			})

			setting.ImageUploadProvider = "webdav"

			webdavSec, err := setting.Cfg.GetSection("external_image_storage.webdav")
			webdavSec.NewKey("url", "webdavUrl")
			webdavSec.NewKey("username", "username")
			webdavSec.NewKey("password", "password")

			uploader, err := NewImageUploader()

			So(err, ShouldBeNil)
			original, ok := uploader.(*WebdavUploader)

			So(ok, ShouldBeTrue)
			So(original.url, ShouldEqual, "webdavUrl")
			So(original.username, ShouldEqual, "username")
			So(original.password, ShouldEqual, "password")
		})
	})
}
Пример #2
0
func initRuntime() {
	setting.NewConfigContext(&setting.CommandLineArgs{
		Config:   *configFile,
		HomePath: *homePath,
		Args:     flag.Args(),
	})

	log.Info("Starting Grafana")
	log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
	setting.LogConfigurationInfo()

	sqlstore.NewEngine()
	sqlstore.EnsureAdminUser()
}
Пример #3
0
func TestUploadToS3(t *testing.T) {
	SkipConvey("[Integration test] for external_image_store.webdav", t, func() {
		setting.NewConfigContext(&setting.CommandLineArgs{
			HomePath: "../../../",
		})

		s3Uploader, _ := NewImageUploader()

		path, err := s3Uploader.Upload("../../../public/img/logo_transparent_400x.png")

		So(err, ShouldBeNil)
		So(path, ShouldNotEqual, "")
	})
}
Пример #4
0
func initRuntime() {
	err := setting.NewConfigContext(&setting.CommandLineArgs{
		Config:   *configFile,
		HomePath: *homePath,
		Args:     flag.Args(),
	})

	if err != nil {
		log.Fatal(3, err.Error())
	}

	logger := log.New("main")
	logger.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))

	setting.LogConfigurationInfo()
}
Пример #5
0
func TestInterval(t *testing.T) {
	Convey("Default interval ", t, func() {
		setting.NewConfigContext(&setting.CommandLineArgs{
			HomePath: "../../",
		})

		Convey("for 5min", func() {
			tr := NewTimeRange("5m", "now")

			interval := CalculateInterval(tr)
			So(interval.Text, ShouldEqual, "200ms")
		})

		Convey("for 15min", func() {
			tr := NewTimeRange("15m", "now")

			interval := CalculateInterval(tr)
			So(interval.Text, ShouldEqual, "500ms")
		})

		Convey("for 30min", func() {
			tr := NewTimeRange("30m", "now")

			interval := CalculateInterval(tr)
			So(interval.Text, ShouldEqual, "1s")
		})

		Convey("for 1h", func() {
			tr := NewTimeRange("1h", "now")

			interval := CalculateInterval(tr)
			So(interval.Text, ShouldEqual, "2s")
		})

		Convey("Round interval", func() {
			So(roundInterval(time.Millisecond*30), ShouldEqual, time.Millisecond*20)
			So(roundInterval(time.Millisecond*45), ShouldEqual, time.Millisecond*50)
		})

		Convey("Format value", func() {
			So(formatDuration(time.Second*61), ShouldEqual, "1m")
			So(formatDuration(time.Millisecond*30), ShouldEqual, "30ms")
			So(formatDuration(time.Hour*23), ShouldEqual, "23h")
			So(formatDuration(time.Hour*24*367), ShouldEqual, "1y")
		})
	})
}
Пример #6
0
func initRuntime() {
	exportDatabaseServiceEnv()

	err := setting.NewConfigContext(&setting.CommandLineArgs{
		Config:   *configFile,
		HomePath: *homePath,
		Args:     flag.Args(),
	})

	if err != nil {
		log.Fatal(3, err.Error())
	}

	log.Info("Starting Grafana")
	log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0))
	setting.LogConfigurationInfo()

	sqlstore.NewEngine()
	sqlstore.EnsureAdminUser()
}
Пример #7
0
func runDbCommand(command func(commandLine CommandLine) error) func(context *cli.Context) {
	return func(context *cli.Context) {

		flag.Parse()
		setting.NewConfigContext(&setting.CommandLineArgs{
			Config:   *configFile,
			HomePath: *homePath,
			Args:     flag.Args(),
		})

		sqlstore.NewEngine()

		cmd := &contextCommandLine{context}
		if err := command(cmd); err != nil {
			logger.Errorf("\n%s: ", color.RedString("Error"))
			logger.Errorf("%s\n\n", err)

			cmd.ShowHelp()
			os.Exit(1)
		} else {
			logger.Info("\n\n")
		}
	}
}
Пример #8
0
func TestAlertRuleExtraction(t *testing.T) {

	Convey("Parsing alert rules  from dashboard json", t, func() {

		RegisterCondition("query", func(model *simplejson.Json, index int) (Condition, error) {
			return &FakeCondition{}, nil
		})

		setting.NewConfigContext(&setting.CommandLineArgs{
			HomePath: "../../../",
		})

		// mock data
		defaultDs := &m.DataSource{Id: 12, OrgId: 1, Name: "I am default", IsDefault: true}
		graphite2Ds := &m.DataSource{Id: 15, OrgId: 1, Name: "graphite2"}
		influxDBDs := &m.DataSource{Id: 16, OrgId: 1, Name: "InfluxDB"}

		bus.AddHandler("test", func(query *m.GetDataSourcesQuery) error {
			query.Result = []*m.DataSource{defaultDs, graphite2Ds}
			return nil
		})

		bus.AddHandler("test", func(query *m.GetDataSourceByNameQuery) error {
			if query.Name == defaultDs.Name {
				query.Result = defaultDs
			}
			if query.Name == graphite2Ds.Name {
				query.Result = graphite2Ds
			}
			if query.Name == influxDBDs.Name {
				query.Result = influxDBDs
			}
			return nil
		})

		json := `
      {
        "id": 57,
        "title": "Graphite 4",
        "originalTitle": "Graphite 4",
        "tags": ["graphite"],
        "rows": [
        {
          "panels": [
          {
            "title": "Active desktop users",
            "editable": true,
            "type": "graph",
            "id": 3,
            "targets": [
            {
              "refId": "A",
              "target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
            }
            ],
            "datasource": null,
            "alert": {
              "name": "name1",
              "message": "desc1",
              "handler": 1,
              "frequency": "60s",
              "conditions": [
              {
                "type": "query",
                "query": {"params": ["A", "5m", "now"]},
                "reducer": {"type": "avg", "params": []},
                "evaluator": {"type": ">", "params": [100]}
              }
              ]
            }
          },
          {
            "title": "Active mobile users",
            "id": 4,
            "targets": [
              {"refId": "A", "target": ""},
              {"refId": "B", "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
            ],
            "datasource": "graphite2",
            "alert": {
              "name": "name2",
              "message": "desc2",
              "handler": 0,
              "frequency": "60s",
              "severity": "warning",
              "conditions": [
              {
                "type": "query",
                "query":  {"params": ["B", "5m", "now"]},
                "reducer": {"type": "avg", "params": []},
                "evaluator": {"type": ">", "params": [100]}
              }
              ]
            }
          }
          ]
        }
      ]
      }`

		Convey("Parsing and validating dashboard containing graphite alerts", func() {

			dashJson, err := simplejson.NewJson([]byte(json))
			So(err, ShouldBeNil)

			dash := m.NewDashboardFromJson(dashJson)
			extractor := NewDashAlertExtractor(dash, 1)

			alerts, err := extractor.GetAlerts()

			Convey("Get rules without error", func() {
				So(err, ShouldBeNil)
			})

			Convey("all properties have been set", func() {
				So(len(alerts), ShouldEqual, 2)

				for _, v := range alerts {
					So(v.DashboardId, ShouldEqual, 57)
					So(v.Name, ShouldNotBeEmpty)
					So(v.Message, ShouldNotBeEmpty)

					settings := simplejson.NewFromAny(v.Settings)
					So(settings.Get("interval").MustString(""), ShouldEqual, "")
				}

				Convey("should extract handler property", func() {
					So(alerts[0].Handler, ShouldEqual, 1)
					So(alerts[1].Handler, ShouldEqual, 0)
				})

				Convey("should extract frequency in seconds", func() {
					So(alerts[0].Frequency, ShouldEqual, 60)
					So(alerts[1].Frequency, ShouldEqual, 60)
				})

				Convey("should extract panel idc", func() {
					So(alerts[0].PanelId, ShouldEqual, 3)
					So(alerts[1].PanelId, ShouldEqual, 4)
				})

				Convey("should extract name and desc", func() {
					So(alerts[0].Name, ShouldEqual, "name1")
					So(alerts[0].Message, ShouldEqual, "desc1")
					So(alerts[1].Name, ShouldEqual, "name2")
					So(alerts[1].Message, ShouldEqual, "desc2")
				})

				Convey("should set datasourceId", func() {
					condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
					query := condition.Get("query")
					So(query.Get("datasourceId").MustInt64(), ShouldEqual, 12)
				})

				Convey("should copy query model to condition", func() {
					condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
					model := condition.Get("query").Get("model")
					So(model.Get("target").MustString(), ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)")
				})
			})
		})

		Convey("Parse and validate dashboard containing influxdb alert", func() {

			json2 := `{
				  "id": 4,
				  "title": "Influxdb",
				  "tags": [
				    "apa"
				  ],
				  "style": "dark",
				  "timezone": "browser",
				  "editable": true,
				  "hideControls": false,
				  "sharedCrosshair": false,
				  "rows": [
				    {
				      "collapse": false,
				      "editable": true,
				      "height": "450px",
				      "panels": [
				        {
				          "alert": {
				            "conditions": [
				              {
				                "evaluator": {
				                  "params": [
				                    10
				                  ],
				                  "type": "gt"
				                },
				                "query": {
				                  "params": [
				                    "B",
				                    "5m",
				                    "now"
				                  ]
				                },
				                "reducer": {
				                  "params": [],
				                  "type": "avg"
				                },
				                "type": "query"
				              }
				            ],
				            "frequency": "3s",
				            "handler": 1,
				            "name": "Influxdb",
				            "noDataState": "no_data",
				            "notifications": [
				              {
				                "id": 6
				              }
				            ]
				          },
				          "alerting": {},
				          "aliasColors": {
				            "logins.count.count": "#890F02"
				          },
				          "bars": false,
				          "datasource": "InfluxDB",
				          "editable": true,
				          "error": false,
				          "fill": 1,
				          "grid": {},
				          "id": 1,
				          "interval": ">10s",
				          "isNew": true,
				          "legend": {
				            "avg": false,
				            "current": false,
				            "max": false,
				            "min": false,
				            "show": true,
				            "total": false,
				            "values": false
				          },
				          "lines": true,
				          "linewidth": 2,
				          "links": [],
				          "nullPointMode": "connected",
				          "percentage": false,
				          "pointradius": 5,
				          "points": false,
				          "renderer": "flot",
				          "seriesOverrides": [],
				          "span": 10,
				          "stack": false,
				          "steppedLine": false,
				          "targets": [
				            {
				              "dsType": "influxdb",
				              "groupBy": [
				                {
				                  "params": [
				                    "$interval"
				                  ],
				                  "type": "time"
				                },
				                {
				                  "params": [
				                    "datacenter"
				                  ],
				                  "type": "tag"
				                },
				                {
				                  "params": [
				                    "none"
				                  ],
				                  "type": "fill"
				                }
				              ],
				              "hide": false,
				              "measurement": "logins.count",
				              "policy": "default",
				              "query": "SELECT 8 * count(\"value\") FROM \"logins.count\" WHERE $timeFilter GROUP BY time($interval), \"datacenter\" fill(none)",
				              "rawQuery": true,
				              "refId": "B",
				              "resultFormat": "time_series",
				              "select": [
				                [
				                  {
				                    "params": [
				                      "value"
				                    ],
				                    "type": "field"
				                  },
				                  {
				                    "params": [],
				                    "type": "count"
				                  }
				                ]
				              ],
				              "tags": []
				            },
				            {
				              "dsType": "influxdb",
				              "groupBy": [
				                {
				                  "params": [
				                    "$interval"
				                  ],
				                  "type": "time"
				                },
				                {
				                  "params": [
				                    "null"
				                  ],
				                  "type": "fill"
				                }
				              ],
				              "hide": true,
				              "measurement": "cpu",
				              "policy": "default",
				              "refId": "A",
				              "resultFormat": "time_series",
				              "select": [
				                [
				                  {
				                    "params": [
				                      "value"
				                    ],
				                    "type": "field"
				                  },
				                  {
				                    "params": [],
				                    "type": "mean"
				                  }
				                ],
				                [
				                  {
				                    "params": [
				                      "value"
				                    ],
				                    "type": "field"
				                  },
				                  {
				                    "params": [],
				                    "type": "sum"
				                  }
				                ]
				              ],
				              "tags": []
				            }
				          ],
				          "thresholds": [
				            {
				              "colorMode": "critical",
				              "fill": true,
				              "line": true,
				              "op": "gt",
				              "value": 10
				            }
				          ],
				          "timeFrom": null,
				          "timeShift": null,
				          "title": "Panel Title",
				          "tooltip": {
				            "msResolution": false,
				            "ordering": "alphabetical",
				            "shared": true,
				            "sort": 0,
				            "value_type": "cumulative"
				          },
				          "type": "graph",
				          "xaxis": {
				            "mode": "time",
				            "name": null,
				            "show": true,
				            "values": []
				          },
				          "yaxes": [
				            {
				              "format": "short",
				              "logBase": 1,
				              "max": null,
				              "min": null,
				              "show": true
				            },
				            {
				              "format": "short",
				              "logBase": 1,
				              "max": null,
				              "min": null,
				              "show": true
				            }
				          ]
				        },
				        {
				          "editable": true,
				          "error": false,
				          "id": 2,
				          "isNew": true,
				          "limit": 10,
				          "links": [],
				          "show": "current",
				          "span": 2,
				          "stateFilter": [
				            "alerting"
				          ],
				          "title": "Alert status",
				          "type": "alertlist"
				        }
				      ],
				      "title": "Row"
				    }
				  ],
				  "time": {
				    "from": "now-5m",
				    "to": "now"
				  },
				  "timepicker": {
				    "now": true,
				    "refresh_intervals": [
				      "5s",
				      "10s",
				      "30s",
				      "1m",
				      "5m",
				      "15m",
				      "30m",
				      "1h",
				      "2h",
				      "1d"
				    ],
				    "time_options": [
				      "5m",
				      "15m",
				      "1h",
				      "6h",
				      "12h",
				      "24h",
				      "2d",
				      "7d",
				      "30d"
				    ]
				  },
				  "templating": {
				    "list": []
				  },
				  "annotations": {
				    "list": []
				  },
				  "schemaVersion": 13,
				  "version": 120,
				  "links": [],
				  "gnetId": null
				}`

			dashJson, err := simplejson.NewJson([]byte(json2))
			So(err, ShouldBeNil)
			dash := m.NewDashboardFromJson(dashJson)
			extractor := NewDashAlertExtractor(dash, 1)

			alerts, err := extractor.GetAlerts()

			Convey("Get rules without error", func() {
				So(err, ShouldBeNil)
			})

			Convey("should be able to read interval", func() {
				So(len(alerts), ShouldEqual, 1)

				for _, alert := range alerts {
					So(alert.DashboardId, ShouldEqual, 4)

					conditions := alert.Settings.Get("conditions").MustArray()
					cond := simplejson.NewFromAny(conditions[0])

					So(cond.Get("query").Get("model").Get("interval").MustString(), ShouldEqual, ">10s")
				}
			})
		})
	})
}
Пример #9
0
func TestInfluxdbResponseParser(t *testing.T) {
	Convey("Influxdb response parser", t, func() {
		Convey("Response parser", func() {
			parser := &ResponseParser{}

			setting.NewConfigContext(&setting.CommandLineArgs{
				HomePath: "../../../",
			})

			response := &Response{
				Results: []Result{
					Result{
						Series: []Row{
							{
								Name:    "cpu",
								Columns: []string{"time", "mean", "sum"},
								Tags:    map[string]string{"datacenter": "America"},
								Values: [][]interface{}{
									{json.Number("111"), json.Number("222"), json.Number("333")},
									{json.Number("111"), json.Number("222"), json.Number("333")},
									{json.Number("111"), json.Number("null"), json.Number("333")},
								},
							},
						},
					},
				},
			}

			query := &Query{}

			result := parser.Parse(response, query)

			Convey("can parse all series", func() {
				So(len(result.Series), ShouldEqual, 2)
			})

			Convey("can parse all points", func() {
				So(len(result.Series[0].Points), ShouldEqual, 3)
				So(len(result.Series[1].Points), ShouldEqual, 3)
			})

			Convey("can parse multi row result", func() {
				So(result.Series[0].Points[1][0].Float64, ShouldEqual, float64(222))
				So(result.Series[1].Points[1][0].Float64, ShouldEqual, float64(333))
			})

			Convey("can parse null points", func() {
				So(result.Series[0].Points[2][0].Valid, ShouldBeFalse)
			})

			Convey("can format serie names", func() {
				So(result.Series[0].Name, ShouldEqual, "cpu.mean { datacenter: America }")
				So(result.Series[1].Name, ShouldEqual, "cpu.sum { datacenter: America }")
			})
		})

		Convey("Response parser with alias", func() {
			parser := &ResponseParser{}

			response := &Response{
				Results: []Result{
					Result{
						Series: []Row{
							{
								Name:    "cpu.upc",
								Columns: []string{"time", "mean", "sum"},
								Tags:    map[string]string{"datacenter": "America"},
								Values: [][]interface{}{
									{json.Number("111"), json.Number("222"), json.Number("333")},
								},
							},
						},
					},
				},
			}

			Convey("$ alias", func() {
				Convey("simple alias", func() {
					query := &Query{Alias: "serie alias"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "serie alias")
				})

				Convey("measurement alias", func() {
					query := &Query{Alias: "alias $m $measurement", Measurement: "10m"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias 10m 10m")
				})

				Convey("column alias", func() {
					query := &Query{Alias: "alias $col", Measurement: "10m"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias mean")
					So(result.Series[1].Name, ShouldEqual, "alias sum")
				})

				Convey("tag alias", func() {
					query := &Query{Alias: "alias $tag_datacenter"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias America")
				})

				Convey("segment alias", func() {
					query := &Query{Alias: "alias $1"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias upc")
				})

				Convey("segment position out of bound", func() {
					query := &Query{Alias: "alias $5"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias $5")
				})
			})

			Convey("[[]] alias", func() {
				Convey("simple alias", func() {
					query := &Query{Alias: "serie alias"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "serie alias")
				})

				Convey("measurement alias", func() {
					query := &Query{Alias: "alias [[m]] [[measurement]]", Measurement: "10m"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias 10m 10m")
				})

				Convey("column alias", func() {
					query := &Query{Alias: "alias [[col]]", Measurement: "10m"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias mean")
					So(result.Series[1].Name, ShouldEqual, "alias sum")
				})

				Convey("tag alias", func() {
					query := &Query{Alias: "alias [[tag_datacenter]]"}
					result := parser.Parse(response, query)

					So(result.Series[0].Name, ShouldEqual, "alias America")
				})
			})
		})
	})
}
Пример #10
0
func TestGraphitePublisher(t *testing.T) {

	setting.CustomInitPath = "conf/does_not_exist.ini"

	Convey("Test graphite prefix replacement", t, func() {
		var err error
		err = setting.NewConfigContext(&setting.CommandLineArgs{
			HomePath: "../../",
		})

		So(err, ShouldBeNil)

		sec, err := setting.Cfg.NewSection("metrics.graphite")
		sec.NewKey("prefix", "prod.grafana.%(instance_name)s.")
		sec.NewKey("address", "localhost:2001")

		So(err, ShouldBeNil)

		setting.InstanceName = "hostname.with.dots.com"
		publisher, err := CreateGraphitePublisher()

		So(err, ShouldBeNil)
		So(publisher, ShouldNotBeNil)

		So(publisher.prefix, ShouldEqual, "prod.grafana.hostname_with_dots_com.")
		So(publisher.address, ShouldEqual, "localhost:2001")
	})

	Convey("Test graphite publisher default prefix", t, func() {
		var err error
		err = setting.NewConfigContext(&setting.CommandLineArgs{
			HomePath: "../../",
		})

		So(err, ShouldBeNil)

		sec, err := setting.Cfg.NewSection("metrics.graphite")
		sec.NewKey("address", "localhost:2001")

		So(err, ShouldBeNil)

		setting.InstanceName = "hostname.with.dots.com"
		publisher, err := CreateGraphitePublisher()

		So(err, ShouldBeNil)
		So(publisher, ShouldNotBeNil)

		So(publisher.prefix, ShouldEqual, "prod.grafana.hostname_with_dots_com.")
		So(publisher.address, ShouldEqual, "localhost:2001")
	})

	Convey("Test graphite publisher default values", t, func() {
		var err error
		err = setting.NewConfigContext(&setting.CommandLineArgs{
			HomePath: "../../",
		})

		So(err, ShouldBeNil)

		_, err = setting.Cfg.NewSection("metrics.graphite")

		publisher, err := CreateGraphitePublisher()

		So(err, ShouldBeNil)
		So(publisher, ShouldBeNil)
	})
}