response_parser_test.go 3.5 KB

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