mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
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:
parent
5429df1e98
commit
e6a6aea422
@ -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()
|
@ -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)
|
||||
|
@ -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)
|
@ -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,
|
@ -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
|
||||
}`)
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
}
|
@ -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"
|
||||
}
|
@ -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
|
||||
}
|
@ -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")
|
||||
}
|
||||
|
||||
}
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user