mirror of
https://github.com/openfaas/faas.git
synced 2025-06-21 00:06:38 +00:00
Add tests around error handing done in #196
Signed-off-by: Alex Young <alex@heuris.io>
This commit is contained in:
@ -30,6 +30,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
|
|||||||
request := requests.CreateFunctionRequest{}
|
request := requests.CreateFunctionRequest{}
|
||||||
err := json.Unmarshal(body, &request)
|
err := json.Unmarshal(body, &request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("Error parsing request:", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -43,7 +44,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
|
|||||||
if len(request.RegistryAuth) > 0 {
|
if len(request.RegistryAuth) > 0 {
|
||||||
auth, err := BuildEncodedAuthConfig(request.RegistryAuth, request.Image)
|
auth, err := BuildEncodedAuthConfig(request.RegistryAuth, request.Image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error while building registry auth configuration", err)
|
log.Println("Error building registry auth configuration:", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("Invalid registry auth"))
|
w.Write([]byte("Invalid registry auth"))
|
||||||
return
|
return
|
||||||
@ -54,7 +55,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
|
|||||||
|
|
||||||
response, err := c.ServiceCreate(context.Background(), spec, options)
|
response, err := c.ServiceCreate(context.Background(), spec, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println("Error creating service:", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("Deployment error: " + err.Error()))
|
w.Write([]byte("Deployment error: " + err.Error()))
|
||||||
return
|
return
|
||||||
|
@ -4,13 +4,42 @@
|
|||||||
package inttests
|
package inttests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreate_ValidJson(t *testing.T) {
|
type PostFunctionRequest struct {
|
||||||
reqBody := `{}`
|
Image string `json:"image"`
|
||||||
_, code, err := fireRequest("http://localhost:8080/system/functions", http.MethodPost, reqBody)
|
EnvProcess string `json:"envProcess"`
|
||||||
|
Network string `json:"network"`
|
||||||
|
Service string `json:"service"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteFunctionRequest struct {
|
||||||
|
FunctionName string `json:"functionName"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func createFunction(request PostFunctionRequest) (string, int, error) {
|
||||||
|
marshalled, _ := json.Marshal(request)
|
||||||
|
return fireRequest("http://localhost:8080/system/functions", http.MethodPost, string(marshalled))
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteFunction(name string) (string, int, error) {
|
||||||
|
marshalled, _ := json.Marshal(DeleteFunctionRequest{name})
|
||||||
|
return fireRequest("http://localhost:8080/system/functions", http.MethodDelete, string(marshalled))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreate_ValidRequest(t *testing.T) {
|
||||||
|
request := PostFunctionRequest{
|
||||||
|
"functions/resizer",
|
||||||
|
"",
|
||||||
|
"func_functions",
|
||||||
|
"test_resizer",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, code, err := createFunction(request)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
@ -18,11 +47,70 @@ func TestCreate_ValidJson(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if code != http.StatusOK {
|
if code != http.StatusOK {
|
||||||
t.Errorf("Got HTTP code: %d, want %d\n", code, http.StatusBadRequest)
|
t.Errorf("Got HTTP code: %d, want %d\n", code, http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteFunction("test_resizer")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreate_InvalidImage(t *testing.T) {
|
||||||
|
request := PostFunctionRequest{
|
||||||
|
"a b c",
|
||||||
|
"",
|
||||||
|
"func_functions",
|
||||||
|
"test_resizer",
|
||||||
|
}
|
||||||
|
|
||||||
|
body, code, err := createFunction(request)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedErrorCode := http.StatusBadRequest
|
||||||
|
if code != expectedErrorCode {
|
||||||
|
t.Errorf("Got HTTP code: %d, want %d\n", code, expectedErrorCode)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedErrorSlice := "is not a valid repository/tag"
|
||||||
|
if !strings.Contains(body, expectedErrorSlice) {
|
||||||
|
t.Errorf("Error message %s does not contain: %s\n", body, expectedErrorSlice)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateBadFunctionNotJson(t *testing.T) {
|
func TestCreate_InvalidNetwork(t *testing.T) {
|
||||||
|
request := PostFunctionRequest{
|
||||||
|
"functions/resizer",
|
||||||
|
"",
|
||||||
|
"non_existent_network",
|
||||||
|
"test_resizer",
|
||||||
|
}
|
||||||
|
|
||||||
|
body, code, err := createFunction(request)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedErrorCode := http.StatusBadRequest
|
||||||
|
if code != expectedErrorCode {
|
||||||
|
t.Errorf("Got HTTP code: %d, want %d\n", code, expectedErrorCode)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedErrorSlice := "network non_existent_network not found"
|
||||||
|
if !strings.Contains(body, expectedErrorSlice) {
|
||||||
|
t.Errorf("Error message %s does not contain: %s\n", body, expectedErrorSlice)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreate_InvalidJson(t *testing.T) {
|
||||||
reqBody := `not json`
|
reqBody := `not json`
|
||||||
_, code, err := fireRequest("http://localhost:8080/system/functions", http.MethodPost, reqBody)
|
_, code, err := fireRequest("http://localhost:8080/system/functions", http.MethodPost, reqBody)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user