model_parser_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package elasticsearch
  2. import (
  3. "github.com/grafana/grafana/pkg/tsdb"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. )
  9. func makeTime(hour int) string {
  10. //unixtime 1500000000 == 2017-07-14T02:40:00+00:00
  11. return strconv.Itoa((1500000000 + hour*60*60) * 1000)
  12. }
  13. func getIndexListByTime(pattern string, interval string, hour int) string {
  14. timeRange := &tsdb.TimeRange{
  15. From: makeTime(0),
  16. To: makeTime(hour),
  17. }
  18. return getIndexList(pattern, interval, timeRange)
  19. }
  20. func TestElasticsearchGetIndexList(t *testing.T) {
  21. Convey("Test Elasticsearch getIndex ", t, func() {
  22. Convey("Parse Interval Formats", func() {
  23. So(getIndexListByTime("[logstash-]YYYY.MM.DD", "Daily", 48),
  24. ShouldEqual, "logstash-2017.07.14,logstash-2017.07.15,logstash-2017.07.16")
  25. So(len(strings.Split(getIndexListByTime("[logstash-]YYYY.MM.DD.HH", "Hourly", 3), ",")),
  26. ShouldEqual, 4)
  27. So(getIndexListByTime("[logstash-]YYYY.W", "Weekly", 100),
  28. ShouldEqual, "logstash-2017.28,logstash-2017.29")
  29. So(getIndexListByTime("[logstash-]YYYY.MM", "Monthly", 700),
  30. ShouldEqual, "logstash-2017.07,logstash-2017.08")
  31. So(getIndexListByTime("[logstash-]YYYY", "Yearly", 10000),
  32. ShouldEqual, "logstash-2017,logstash-2018,logstash-2019")
  33. })
  34. Convey("No Interval", func() {
  35. index := getIndexListByTime("logstash-test", "", 1)
  36. So(index, ShouldEqual, "logstash-test")
  37. })
  38. })
  39. }