response_parser_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package mqe
  2. import (
  3. "testing"
  4. "net/http"
  5. "strings"
  6. "io/ioutil"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. var (
  10. dummieJson string
  11. )
  12. func TestMQEResponseParser(t *testing.T) {
  13. Convey("MQE response parser", t, func() {
  14. parser := NewResponseParser()
  15. Convey("Can parse response", func() {
  16. response := &http.Response{
  17. StatusCode: 200,
  18. Body: ioutil.NopCloser(strings.NewReader(dummieJson)),
  19. }
  20. res, err := parser.Parse(response)
  21. So(err, ShouldBeNil)
  22. So(len(res.Series), ShouldEqual, 2)
  23. So(len(res.Series[0].Points), ShouldEqual, 14)
  24. startTime := 1479287280000
  25. for i := 0; i < 11; i++ {
  26. So(res.Series[0].Points[i][0].Float64, ShouldEqual, i+1)
  27. So(res.Series[0].Points[i][1].Float64, ShouldEqual, startTime+(i*30000))
  28. }
  29. })
  30. })
  31. }
  32. func init() {
  33. dummieJson = `{
  34. "success": true,
  35. "name": "select",
  36. "body": [
  37. {
  38. "query": "os.disk.sda3.weighted_io_time",
  39. "name": "os.disk.sda3.weighted_io_time",
  40. "type": "series",
  41. "series": [
  42. {
  43. "tagset": {
  44. "app": "demoapp",
  45. "host": "staples-lab-1"
  46. },
  47. "values": [1,2,3,4,5,6,7,8,9,10,11, null, null, null]
  48. },
  49. {
  50. "tagset": {
  51. "app": "demoapp",
  52. "host": "staples-lab-2"
  53. },
  54. "values": [11,10,9,8,7,6,5,4,3,2,1]
  55. }
  56. ],
  57. "timerange": {
  58. "start": 1479287280000,
  59. "end": 1479287580000,
  60. "resolution": 30000
  61. }
  62. }
  63. ],
  64. "metadata": {
  65. "description": {
  66. "app": [
  67. "demoapp"
  68. ],
  69. "host": [
  70. "staples-lab-1",
  71. "staples-lab-2"
  72. ]
  73. },
  74. "notes": null,
  75. "profile": [
  76. {
  77. "name": "Parsing Query",
  78. "start": "2016-11-16T04:16:21.874354721-05:00",
  79. "finish": "2016-11-16T04:16:21.874762291-05:00"
  80. },
  81. {
  82. "name": "Cassandra GetAllTags",
  83. "start": "2016-11-16T04:16:21.874907171-05:00",
  84. "finish": "2016-11-16T04:16:21.876401922-05:00"
  85. },
  86. {
  87. "name": "CachedMetricMetadataAPI_GetAllTags_Expired",
  88. "start": "2016-11-16T04:16:21.874904751-05:00",
  89. "finish": "2016-11-16T04:16:21.876407852-05:00"
  90. },
  91. {
  92. "name": "CachedMetricMetadataAPI_GetAllTags",
  93. "start": "2016-11-16T04:16:21.874899491-05:00",
  94. "finish": "2016-11-16T04:16:21.876410382-05:00"
  95. },
  96. {
  97. "name": "Blueflood FetchSingleTimeseries Resolution",
  98. "description": "os.disk.sda3.weighted_io_time [app=demoapp,host=staples-lab-1]\n at 30s",
  99. "start": "2016-11-16T04:16:21.876623312-05:00",
  100. "finish": "2016-11-16T04:16:21.881763444-05:00"
  101. },
  102. {
  103. "name": "Blueflood FetchSingleTimeseries Resolution",
  104. "description": "os.disk.sda3.weighted_io_time [app=demoapp,host=staples-lab-2]\n at 30s",
  105. "start": "2016-11-16T04:16:21.876642682-05:00",
  106. "finish": "2016-11-16T04:16:21.881895914-05:00"
  107. },
  108. {
  109. "name": "Blueflood FetchMultipleTimeseries",
  110. "start": "2016-11-16T04:16:21.876418022-05:00",
  111. "finish": "2016-11-16T04:16:21.881921474-05:00"
  112. }
  113. ]
  114. }
  115. }
  116. `
  117. }