Selaa lähdekoodia

test(mqe): basic test for response parser

bergquist 9 vuotta sitten
vanhempi
commit
c6ad0cc5ec

+ 0 - 6
pkg/tsdb/mqe/response_handler.go

@@ -1,6 +0,0 @@
-package mqe
-
-// wildcard as alias
-// add host to alias
-// add app to alias
-// regular alias

+ 18 - 0
pkg/tsdb/mqe/response_parser.go

@@ -0,0 +1,18 @@
+package mqe
+
+import (
+	"net/http"
+
+	"github.com/grafana/grafana/pkg/tsdb"
+)
+
+// wildcard as alias
+// add host to alias
+// add app to alias
+// regular alias
+
+type MQEResponseParser struct{}
+
+func (parser *MQEResponseParser) Parse(res *http.Response) (*tsdb.QueryResult, error) {
+	return nil, nil
+}

+ 140 - 0
pkg/tsdb/mqe/response_parser_test.go

@@ -0,0 +1,140 @@
+package mqe
+
+import (
+	"testing"
+
+	"time"
+
+	"net/http"
+	"strings"
+
+	"io/ioutil"
+
+	"github.com/grafana/grafana/pkg/components/simplejson"
+	. "github.com/smartystreets/goconvey/convey"
+)
+
+var (
+	dummieJson string
+)
+
+func TestMQEResponseParser(t *testing.T) {
+	Convey("MQE response parser", t, func() {
+		parser := &MQEResponseParser{}
+
+		Convey("Can parse response", func() {
+			response := &http.Response{
+				StatusCode: 200,
+				Body:       ioutil.NopCloser(strings.NewReader(dummieJson)),
+			}
+			_, err := parser.Parse(response)
+			So(err, ShouldBeNil)
+		})
+	})
+}
+
+type MQEResponse struct {
+	Success bool               `json:"success"`
+	Name    string             `json:"name"`
+	Body    []MQEResponseSerie `json:"body"`
+}
+
+type ResponseTimeRange struct {
+	Start      time.Time     `json:"start"`
+	End        time.Time     `json:"end"`
+	Resolution time.Duration `json:"Resolution"`
+}
+
+type MQEResponseSerie struct {
+	Query  string            `json:"query"`
+	Name   string            `json:"name"`
+	Type   string            `json:"type"`
+	Series []simplejson.Json `json:"series"`
+}
+
+func init() {
+	dummieJson = `{
+    "success": true,
+    "name": "select",
+    "body": [
+      {
+        "query": "os.disk.sda3.weighted_io_time",
+        "name": "os.disk.sda3.weighted_io_time",
+        "type": "series",
+        "series": [
+          {
+            "tagset": {
+              "app": "demoapp",
+              "host": "staples-lab-1"
+            },
+            "values": [1,2,3,4,5,6,7,8,9,10,11]
+          },
+          {
+            "tagset": {
+              "app": "demoapp",
+              "host": "staples-lab-2"
+            },
+            "values": [11,10,9,8,7,6,5,4,3,2,1]
+          }
+        ],
+        "timerange": {
+          "start": 1479287280000,
+          "end": 1479287580000,
+          "resolution": 30000
+        }
+      }
+    ],
+    "metadata": {
+      "description": {
+        "app": [
+          "demoapp"
+        ],
+        "host": [
+          "staples-lab-1",
+          "staples-lab-2"
+        ]
+      },
+      "notes": null,
+      "profile": [
+        {
+          "name": "Parsing Query",
+          "start": "2016-11-16T04:16:21.874354721-05:00",
+          "finish": "2016-11-16T04:16:21.874762291-05:00"
+        },
+        {
+          "name": "Cassandra GetAllTags",
+          "start": "2016-11-16T04:16:21.874907171-05:00",
+          "finish": "2016-11-16T04:16:21.876401922-05:00"
+        },
+        {
+          "name": "CachedMetricMetadataAPI_GetAllTags_Expired",
+          "start": "2016-11-16T04:16:21.874904751-05:00",
+          "finish": "2016-11-16T04:16:21.876407852-05:00"
+        },
+        {
+          "name": "CachedMetricMetadataAPI_GetAllTags",
+          "start": "2016-11-16T04:16:21.874899491-05:00",
+          "finish": "2016-11-16T04:16:21.876410382-05:00"
+        },
+        {
+          "name": "Blueflood FetchSingleTimeseries Resolution",
+          "description": "os.disk.sda3.weighted_io_time [app=demoapp,host=staples-lab-1]\n at 30s",
+          "start": "2016-11-16T04:16:21.876623312-05:00",
+          "finish": "2016-11-16T04:16:21.881763444-05:00"
+        },
+        {
+          "name": "Blueflood FetchSingleTimeseries Resolution",
+          "description": "os.disk.sda3.weighted_io_time [app=demoapp,host=staples-lab-2]\n at 30s",
+          "start": "2016-11-16T04:16:21.876642682-05:00",
+          "finish": "2016-11-16T04:16:21.881895914-05:00"
+        },
+        {
+          "name": "Blueflood FetchMultipleTimeseries",
+          "start": "2016-11-16T04:16:21.876418022-05:00",
+          "finish": "2016-11-16T04:16:21.881921474-05:00"
+        }
+      ]
+    }
+  }
+  `
+}

+ 0 - 3
pkg/tsdb/mqe/types.go

@@ -115,6 +115,3 @@ type TokenResponse struct {
 	Success bool
 	Success bool
 	Body    TokenBody
 	Body    TokenBody
 }
 }
-
-type MQEResponse struct {
-}

+ 4 - 18
pkg/tsdb/mqe/types_test.go

@@ -24,22 +24,12 @@ func TestWildcardExpansion(t *testing.T) {
 	to := now.UnixNano() / int64(time.Millisecond)
 	to := now.UnixNano() / int64(time.Millisecond)
 
 
 	Convey("Can expanding query", t, func() {
 	Convey("Can expanding query", t, func() {
-
 		Convey("Without wildcard series", func() {
 		Convey("Without wildcard series", func() {
 			query := &MQEQuery{
 			query := &MQEQuery{
 				Metrics: []MQEMetric{
 				Metrics: []MQEMetric{
-					MQEMetric{
-						Metric: "os.cpu.3.idle",
-						Alias:  "",
-					},
-					MQEMetric{
-						Metric: "os.cpu.2.idle",
-						Alias:  "",
-					},
-					MQEMetric{
-						Metric: "os.cpu.1.idle",
-						Alias:  "cpu",
-					},
+					MQEMetric{Metric: "os.cpu.3.idle", Alias: ""},
+					MQEMetric{Metric: "os.cpu.2.idle", Alias: ""},
+					MQEMetric{Metric: "os.cpu.1.idle", Alias: "cpu"},
 				},
 				},
 				Hosts:          []string{"staples-lab-1", "staples-lab-2"},
 				Hosts:          []string{"staples-lab-1", "staples-lab-2"},
 				Apps:           []string{"demoapp-1", "demoapp-2"},
 				Apps:           []string{"demoapp-1", "demoapp-2"},
@@ -59,10 +49,7 @@ func TestWildcardExpansion(t *testing.T) {
 		Convey("Containg wildcard series", func() {
 		Convey("Containg wildcard series", func() {
 			query := &MQEQuery{
 			query := &MQEQuery{
 				Metrics: []MQEMetric{
 				Metrics: []MQEMetric{
-					MQEMetric{
-						Metric: "os.cpu*",
-						Alias:  "cpu on core *",
-					},
+					MQEMetric{Metric: "os.cpu*", Alias: ""},
 				},
 				},
 				Hosts:          []string{"staples-lab-1"},
 				Hosts:          []string{"staples-lab-1"},
 				AddAppToAlias:  false,
 				AddAppToAlias:  false,
@@ -78,7 +65,6 @@ func TestWildcardExpansion(t *testing.T) {
 			So(expandeQueries[1], ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` where host in ('staples-lab-1') from %v to %v", from, to))
 			So(expandeQueries[1], ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` where host in ('staples-lab-1') from %v to %v", from, to))
 			So(expandeQueries[2], ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1') from %v to %v", from, to))
 			So(expandeQueries[2], ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1') from %v to %v", from, to))
 			So(expandeQueries[3], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1') from %v to %v", from, to))
 			So(expandeQueries[3], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1') from %v to %v", from, to))
-
 		})
 		})
 	})
 	})
 }
 }