Moved unit test files inside same directory as test target

The unit tests were inside the `gateway/tests` directory which had no
effect to the coverage for `go test`. Therefore, moved the tests inside
the same directory as the test target.

Signed-off-by: Ken Fukuyama <kenfdev@gmail.com>
This commit is contained in:
Ken Fukuyama
2018-04-05 23:29:11 +09:00
committed by Alex Ellis
parent 5429df1e98
commit e6a6aea422
9 changed files with 71 additions and 155 deletions

View File

@ -0,0 +1,73 @@
package requests
import (
"net/http"
"testing"
)
func TestFormattingOfURLWithPath_NoQuery(t *testing.T) {
req := ForwardRequest{
RawQuery: "",
RawPath: "/encode/utf8/",
Method: http.MethodPost,
}
url := req.ToURL("markdown", 8080)
want := "http://markdown:8080/encode/utf8/"
if url != want {
t.Logf("Got: %s, want: %s", url, want)
t.Fail()
}
}
func TestFormattingOfURLAtRoot_NoQuery(t *testing.T) {
req := ForwardRequest{
RawQuery: "",
RawPath: "/",
Method: http.MethodPost,
}
url := req.ToURL("markdown", 8080)
want := "http://markdown:8080/"
if url != want {
t.Logf("Got: %s, want: %s", url, want)
t.Fail()
}
}
// experimental test
// func TestMyURL(t *testing.T) {
// v, _ := url.Parse("http://markdown/site?query=test")
// t.Logf("RequestURI %s", v.RequestURI())
// t.Logf("extra %s", v.Path)
// }
func TestUrlForFlask(t *testing.T) {
req := ForwardRequest{
RawQuery: "query=uptime",
RawPath: "/function/flask",
Method: http.MethodPost,
}
url := req.ToURL("flask", 8080)
want := "http://flask:8080/function/flask?query=uptime"
if url != want {
t.Logf("Got: %s, want: %s", url, want)
t.Fail()
}
}
func TestFormattingOfURL_OneQuery(t *testing.T) {
req := ForwardRequest{
RawQuery: "name=alex",
RawPath: "/",
Method: http.MethodPost,
}
url := req.ToURL("flask", 8080)
want := "http://flask:8080/?name=alex"
if url != want {
t.Logf("Got: %s, want: %s", url, want)
t.Fail()
}
}

View File

@ -0,0 +1,88 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package requests
import (
"encoding/json"
"testing"
)
// TestUnmarshallAlert is an exploratory test from TDD'ing the struct to parse a Prometheus alert
func TestUnmarshallAlert(t *testing.T) {
file := []byte(`{
"receiver": "scale-up",
"status": "firing",
"alerts": [{
"status": "firing",
"labels": {
"alertname": "APIHighInvocationRate",
"code": "200",
"function_name": "func_nodeinfo",
"instance": "gateway:8080",
"job": "gateway",
"monitor": "faas-monitor",
"service": "gateway",
"severity": "major",
"value": "8.998200359928017"
},
"annotations": {
"description": "High invocation total on gateway:8080",
"summary": "High invocation total on gateway:8080"
},
"startsAt": "2017-03-15T15:52:57.805Z",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://4156cb797423:9090/graph?g0.expr=rate%28gateway_function_invocation_total%5B10s%5D%29+%3E+5\u0026g0.tab=0"
}],
"groupLabels": {
"alertname": "APIHighInvocationRate",
"service": "gateway"
},
"commonLabels": {
"alertname": "APIHighInvocationRate",
"code": "200",
"function_name": "func_nodeinfo",
"instance": "gateway:8080",
"job": "gateway",
"monitor": "faas-monitor",
"service": "gateway",
"severity": "major",
"value": "8.998200359928017"
},
"commonAnnotations": {
"description": "High invocation total on gateway:8080",
"summary": "High invocation total on gateway:8080"
},
"externalURL": "http://f054879d97db:9093",
"version": "3",
"groupKey": 18195285354214864953
}`)
var alert PrometheusAlert
err := json.Unmarshal(file, &alert)
if err != nil {
t.Fatal(err)
}
if (len(alert.Status)) == 0 {
t.Fatal("No status read")
}
if (len(alert.Receiver)) == 0 {
t.Fatal("No status read")
}
if (len(alert.Alerts)) == 0 {
t.Fatal("No alerts read")
}
if (len(alert.Alerts[0].Labels.AlertName)) == 0 {
t.Fatal("No alerts name")
}
if (len(alert.Alerts[0].Labels.FunctionName)) == 0 {
t.Fatal("No function name read")
}
}