From e6a6aea422db60ecf2dfb5f6216de25c4e747bc9 Mon Sep 17 00:00:00 2001 From: Ken Fukuyama Date: Thu, 5 Apr 2018 23:29:11 +0900 Subject: [PATCH] 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 --- .../{tests => handlers}/alerthandler_test.go | 14 +++--- gateway/{tests => handlers}/cors_test.go | 6 +-- .../{tests => metrics}/add_metrics_test.go | 9 ++-- .../forward_request_test.go | 12 ++--- .../prometheus_test.go} | 45 +++++++++++++++++- gateway/tests/alexhostname_request.json | 24 ---------- gateway/tests/test_resolved_alert.json | 47 ------------------- gateway/tests/unmarshall_test.go | 45 ------------------ .../readconfig_test.go} | 24 +++++----- 9 files changed, 71 insertions(+), 155 deletions(-) rename gateway/{tests => handlers}/alerthandler_test.go (63%) rename gateway/{tests => handlers}/cors_test.go (86%) rename gateway/{tests => metrics}/add_metrics_test.go (91%) rename gateway/{tests => requests}/forward_request_test.go (87%) rename gateway/{tests/test_alert.json => requests/prometheus_test.go} (64%) delete mode 100644 gateway/tests/alexhostname_request.json delete mode 100644 gateway/tests/test_resolved_alert.json delete mode 100644 gateway/tests/unmarshall_test.go rename gateway/{tests/config_test.go => types/readconfig_test.go} (92%) diff --git a/gateway/tests/alerthandler_test.go b/gateway/handlers/alerthandler_test.go similarity index 63% rename from gateway/tests/alerthandler_test.go rename to gateway/handlers/alerthandler_test.go index da9ad36b..8ee71e0e 100644 --- a/gateway/tests/alerthandler_test.go +++ b/gateway/handlers/alerthandler_test.go @@ -1,17 +1,15 @@ // 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 tests +package handlers import ( "testing" - - "github.com/openfaas/faas/gateway/handlers" ) func TestScale1to5(t *testing.T) { minReplicas := uint64(1) - newReplicas := handlers.CalculateReplicas("firing", 1, handlers.DefaultMaxReplicas, minReplicas) + newReplicas := CalculateReplicas("firing", 1, DefaultMaxReplicas, minReplicas) if newReplicas != 5 { t.Log("Expected increment in blocks of 5 from 1 to 5") t.Fail() @@ -20,7 +18,7 @@ func TestScale1to5(t *testing.T) { func TestScale5to10(t *testing.T) { minReplicas := uint64(1) - newReplicas := handlers.CalculateReplicas("firing", 5, handlers.DefaultMaxReplicas, minReplicas) + newReplicas := CalculateReplicas("firing", 5, DefaultMaxReplicas, minReplicas) if newReplicas != 10 { t.Log("Expected increment in blocks of 5 from 5 to 10") t.Fail() @@ -29,7 +27,7 @@ func TestScale5to10(t *testing.T) { func TestScaleCeilingOf20Replicas_Noaction(t *testing.T) { minReplicas := uint64(1) - newReplicas := handlers.CalculateReplicas("firing", 20, handlers.DefaultMaxReplicas, minReplicas) + newReplicas := CalculateReplicas("firing", 20, DefaultMaxReplicas, minReplicas) if newReplicas != 20 { t.Log("Expected ceiling of 20 replicas") t.Fail() @@ -38,7 +36,7 @@ func TestScaleCeilingOf20Replicas_Noaction(t *testing.T) { func TestScaleCeilingOf20Replicas(t *testing.T) { minReplicas := uint64(1) - newReplicas := handlers.CalculateReplicas("firing", 19, handlers.DefaultMaxReplicas, minReplicas) + newReplicas := CalculateReplicas("firing", 19, DefaultMaxReplicas, minReplicas) if newReplicas != 20 { t.Log("Expected ceiling of 20 replicas") t.Fail() @@ -47,7 +45,7 @@ func TestScaleCeilingOf20Replicas(t *testing.T) { func TestBackingOff10to1(t *testing.T) { minReplicas := uint64(1) - newReplicas := handlers.CalculateReplicas("resolved", 10, handlers.DefaultMaxReplicas, minReplicas) + newReplicas := CalculateReplicas("resolved", 10, DefaultMaxReplicas, minReplicas) if newReplicas != 1 { t.Log("Expected backing off to 1 replica") t.Fail() diff --git a/gateway/tests/cors_test.go b/gateway/handlers/cors_test.go similarity index 86% rename from gateway/tests/cors_test.go rename to gateway/handlers/cors_test.go index 43aa113b..6d942aec 100644 --- a/gateway/tests/cors_test.go +++ b/gateway/handlers/cors_test.go @@ -1,11 +1,9 @@ -package tests +package handlers import ( "net/http" "net/http/httptest" "testing" - - "github.com/openfaas/faas/gateway/handlers" ) type customHandler struct { @@ -19,7 +17,7 @@ func Test_HeadersAdded(t *testing.T) { handler := customHandler{} host := "store.openfaas.com" - decorated := handlers.DecorateWithCORS(handler, host) + decorated := DecorateWithCORS(handler, host) request, _ := http.NewRequest(http.MethodGet, "/", nil) decorated.ServeHTTP(rr, request) diff --git a/gateway/tests/add_metrics_test.go b/gateway/metrics/add_metrics_test.go similarity index 91% rename from gateway/tests/add_metrics_test.go rename to gateway/metrics/add_metrics_test.go index 5899f82f..1454e3b7 100644 --- a/gateway/tests/add_metrics_test.go +++ b/gateway/metrics/add_metrics_test.go @@ -1,4 +1,4 @@ -package tests +package metrics import ( "encoding/json" @@ -7,16 +7,15 @@ import ( "net/http/httptest" "testing" - "github.com/openfaas/faas/gateway/metrics" "github.com/openfaas/faas/gateway/requests" ) type FakePrometheusQueryFetcher struct { } -func (q FakePrometheusQueryFetcher) Fetch(query string) (*metrics.VectorQueryResponse, error) { +func (q FakePrometheusQueryFetcher) Fetch(query string) (*VectorQueryResponse, error) { val := []byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"code":"200","function_name":"func_echoit"},"value":[1509267827.752,"1"]}]}}`) - queryRes := metrics.VectorQueryResponse{} + queryRes := VectorQueryResponse{} err := json.Unmarshal(val, &queryRes) return &queryRes, err } @@ -29,7 +28,7 @@ func Test_PrometheusMetrics_MixedInto_Services(t *testing.T) { functionsHandler := makeFunctionsHandler() fakeQuery := makeFakePrometheusQueryFetcher() - handler := metrics.AddMetricsHandler(functionsHandler, fakeQuery) + handler := AddMetricsHandler(functionsHandler, fakeQuery) rr := httptest.NewRecorder() request, _ := http.NewRequest(http.MethodGet, "/system/functions", nil) diff --git a/gateway/tests/forward_request_test.go b/gateway/requests/forward_request_test.go similarity index 87% rename from gateway/tests/forward_request_test.go rename to gateway/requests/forward_request_test.go index 453e9c18..e729185f 100644 --- a/gateway/tests/forward_request_test.go +++ b/gateway/requests/forward_request_test.go @@ -1,14 +1,12 @@ -package tests +package requests import ( "net/http" "testing" - - "github.com/openfaas/faas/gateway/requests" ) func TestFormattingOfURLWithPath_NoQuery(t *testing.T) { - req := requests.ForwardRequest{ + req := ForwardRequest{ RawQuery: "", RawPath: "/encode/utf8/", Method: http.MethodPost, @@ -23,7 +21,7 @@ func TestFormattingOfURLWithPath_NoQuery(t *testing.T) { } func TestFormattingOfURLAtRoot_NoQuery(t *testing.T) { - req := requests.ForwardRequest{ + req := ForwardRequest{ RawQuery: "", RawPath: "/", Method: http.MethodPost, @@ -45,7 +43,7 @@ func TestFormattingOfURLAtRoot_NoQuery(t *testing.T) { // } func TestUrlForFlask(t *testing.T) { - req := requests.ForwardRequest{ + req := ForwardRequest{ RawQuery: "query=uptime", RawPath: "/function/flask", Method: http.MethodPost, @@ -60,7 +58,7 @@ func TestUrlForFlask(t *testing.T) { } func TestFormattingOfURL_OneQuery(t *testing.T) { - req := requests.ForwardRequest{ + req := ForwardRequest{ RawQuery: "name=alex", RawPath: "/", Method: http.MethodPost, diff --git a/gateway/tests/test_alert.json b/gateway/requests/prometheus_test.go similarity index 64% rename from gateway/tests/test_alert.json rename to gateway/requests/prometheus_test.go index edc2a3b3..66e08ea6 100644 --- a/gateway/tests/test_alert.json +++ b/gateway/requests/prometheus_test.go @@ -1,4 +1,16 @@ -{ +// 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": [{ @@ -44,4 +56,33 @@ "externalURL": "http://f054879d97db:9093", "version": "3", "groupKey": 18195285354214864953 -} \ No newline at end of file +}`) + + 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") + } + +} diff --git a/gateway/tests/alexhostname_request.json b/gateway/tests/alexhostname_request.json deleted file mode 100644 index e7953654..00000000 --- a/gateway/tests/alexhostname_request.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "session": { - "sessionId": "SessionId.ea96e58d-dc16-43e1-b238-daac4541110c", - "application": { - "applicationId": "amzn1.ask.skill.72fb1025-aacc-4d05-a582-21344940c023" - }, - "attributes": {}, - "user": { - "userId": "amzn1.ask.account.AEN7KA5DBXAAWQPDUXTXFWBARZ5YZ6TNOQR5CUMV5LCCJTMBZVFP45SZVLGDD5GQBOM7QMELRS7LHG3F2FN2QQQMTBURDL5I4PQ33EHMNNGO4TXWG732Y6SDM2YZKHSPWIIWBH3GSE3Q3TTFAYN2Y66RHBKRANYCNMX2WORMASUGVRHUNBB4HZMJEC7HQDWUSXAOMP77WGJU4AY" - }, - "new": true - }, - "request": { - "type": "IntentRequest", - "requestId": "EdwRequestId.a934104e-3282-4620-b056-4aa4c5995503", - "locale": "en-GB", - "timestamp": "2016-12-07T15:50:01Z", - "intent": { - "name": "HostnameIntent", - "slots": {} - } - }, - "version": "1.0" -} \ No newline at end of file diff --git a/gateway/tests/test_resolved_alert.json b/gateway/tests/test_resolved_alert.json deleted file mode 100644 index 90e8cea8..00000000 --- a/gateway/tests/test_resolved_alert.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "receiver": "scale-up", - "status": "resolved", - "alerts": [{ - "status": "resolved", - "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": "2017-03-15T15:53:52.806Z", - "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 -} \ No newline at end of file diff --git a/gateway/tests/unmarshall_test.go b/gateway/tests/unmarshall_test.go deleted file mode 100644 index 01368e02..00000000 --- a/gateway/tests/unmarshall_test.go +++ /dev/null @@ -1,45 +0,0 @@ -// 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 tests - -import ( - "encoding/json" - "io/ioutil" - "testing" - - "github.com/openfaas/faas/gateway/requests" -) - -// TestUnmarshallAlert is an exploratory test from TDD'ing the struct to parse a Prometheus alert -func TestUnmarshallAlert(t *testing.T) { - file, _ := ioutil.ReadFile("./test_alert.json") - - var alert requests.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") - } - -} diff --git a/gateway/tests/config_test.go b/gateway/types/readconfig_test.go similarity index 92% rename from gateway/tests/config_test.go rename to gateway/types/readconfig_test.go index c5889556..d9d827b0 100644 --- a/gateway/tests/config_test.go +++ b/gateway/types/readconfig_test.go @@ -1,13 +1,11 @@ // 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 tests +package types import ( "testing" "time" - - "github.com/openfaas/faas/gateway/types" ) type EnvBucket struct { @@ -30,7 +28,7 @@ func (e EnvBucket) Setenv(key string, value string) { func TestRead_UseExternalProvider_Defaults(t *testing.T) { defaults := NewEnvBucket() - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) @@ -52,7 +50,7 @@ func TestRead_UseExternalProvider_Defaults(t *testing.T) { func TestRead_DirectFunctionsOverride(t *testing.T) { defaults := NewEnvBucket() - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} defaults.Setenv("direct_functions", "true") wantSuffix := "openfaas-fn.cluster.local.svc." defaults.Setenv("direct_functions_suffix", wantSuffix) @@ -72,7 +70,7 @@ func TestRead_DirectFunctionsOverride(t *testing.T) { func TestRead_EmptyTimeoutConfig(t *testing.T) { defaults := NewEnvBucket() - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) @@ -91,7 +89,7 @@ func TestRead_ReadAndWriteTimeoutConfig(t *testing.T) { defaults.Setenv("read_timeout", "10") defaults.Setenv("write_timeout", "60") - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) if (config.ReadTimeout) != time.Duration(10)*time.Second { @@ -109,7 +107,7 @@ func TestRead_ReadAndWriteTimeoutDurationConfig(t *testing.T) { defaults.Setenv("read_timeout", "20s") defaults.Setenv("write_timeout", "1m30s") - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) if (config.ReadTimeout) != time.Duration(20)*time.Second { @@ -124,7 +122,7 @@ func TestRead_ReadAndWriteTimeoutDurationConfig(t *testing.T) { func TestRead_UseNATSDefaultsToOff(t *testing.T) { defaults := NewEnvBucket() - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) @@ -138,7 +136,7 @@ func TestRead_UseNATS(t *testing.T) { defaults := NewEnvBucket() defaults.Setenv("faas_nats_address", "nats") defaults.Setenv("faas_nats_port", "6222") - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) @@ -153,7 +151,7 @@ func TestRead_UseNATSBadPort(t *testing.T) { defaults := NewEnvBucket() defaults.Setenv("faas_nats_address", "nats") defaults.Setenv("faas_nats_port", "6fff") - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) @@ -167,7 +165,7 @@ func TestRead_PrometheusNonDefaults(t *testing.T) { defaults := NewEnvBucket() defaults.Setenv("faas_prometheus_host", "prom1") defaults.Setenv("faas_prometheus_port", "9999") - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults) @@ -185,7 +183,7 @@ func TestRead_PrometheusNonDefaults(t *testing.T) { func TestRead_PrometheusDefaults(t *testing.T) { defaults := NewEnvBucket() - readConfig := types.ReadConfig{} + readConfig := ReadConfig{} config := readConfig.Read(defaults)