time-grain_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package azuremonitor
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. )
  6. func TestTimeGrain(t *testing.T) {
  7. Convey("TimeGrain", t, func() {
  8. tgc := &TimeGrain{}
  9. Convey("create ISO 8601 Duration", func() {
  10. Convey("when given a time unit smaller than a day", func() {
  11. minuteKbnDuration := tgc.createISO8601Duration(1, "m")
  12. hourKbnDuration := tgc.createISO8601Duration(2, "h")
  13. minuteDuration := tgc.createISO8601Duration(1, "minute")
  14. hourDuration := tgc.createISO8601Duration(2, "hour")
  15. Convey("should convert it to a time duration", func() {
  16. So(minuteKbnDuration, ShouldEqual, "PT1M")
  17. So(hourKbnDuration, ShouldEqual, "PT2H")
  18. So(minuteDuration, ShouldEqual, "PT1M")
  19. So(hourDuration, ShouldEqual, "PT2H")
  20. })
  21. })
  22. Convey("when given the day time unit", func() {
  23. kbnDuration := tgc.createISO8601Duration(1, "d")
  24. duration := tgc.createISO8601Duration(2, "day")
  25. Convey("should convert it to a date duration", func() {
  26. So(kbnDuration, ShouldEqual, "P1D")
  27. So(duration, ShouldEqual, "P2D")
  28. })
  29. })
  30. })
  31. Convey("create ISO 8601 Duration from Grafana interval in milliseconds", func() {
  32. Convey("and interval is less than a minute", func() {
  33. durationMS, err := tgc.createISO8601DurationFromIntervalMS(100)
  34. So(err, ShouldBeNil)
  35. durationS, err := tgc.createISO8601DurationFromIntervalMS(59999)
  36. So(err, ShouldBeNil)
  37. Convey("should be rounded up to a minute as is the minimum interval for Azure Monitor", func() {
  38. So(durationMS, ShouldEqual, "PT1M")
  39. So(durationS, ShouldEqual, "PT1M")
  40. })
  41. })
  42. Convey("and interval is more than a minute", func() {
  43. intervals := map[string]int64{
  44. "10m": 600000,
  45. "2d": 172800000,
  46. }
  47. durationM, err := tgc.createISO8601DurationFromIntervalMS(intervals["10m"])
  48. So(err, ShouldBeNil)
  49. durationD, err := tgc.createISO8601DurationFromIntervalMS(intervals["2d"])
  50. So(err, ShouldBeNil)
  51. Convey("should be rounded up to a minute as is the minimum interval for Azure Monitor", func() {
  52. So(durationM, ShouldEqual, "PT10M")
  53. So(durationD, ShouldEqual, "P2D")
  54. })
  55. })
  56. })
  57. })
  58. }