This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
131 lines (109 loc) · 2.5 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/alexellis/faas/gateway/requests"
)
func request(path, method string, data interface{}) ([]byte, *http.Response, error) {
res, err := json.Marshal(data)
if err != nil {
return nil, nil, err
}
input := bytes.NewBuffer(res)
gatewayAddr := os.Getenv("FAAS_GATEWAY_ADDR")
if gatewayAddr == "" {
gatewayAddr = "localhost"
}
request, err := http.NewRequest(method, "http://"+gatewayAddr+":8080"+path, input)
if err != nil {
return nil, nil, err
}
request.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
return body, resp, nil
}
func TestBefore(t *testing.T) {
deleteReq := requests.DeleteFunctionRequest{
FunctionName: "nodeinfo",
}
_, _, err := request("/system/functions", "DELETE", deleteReq)
if err != nil {
t.Log(err)
}
}
func TestDeploy(t *testing.T) {
deploy := requests.CreateFunctionRequest{
Image: "functions/nodeinfo:latest",
Service: "nodeinfo",
Network: "func_functions",
EnvProcess: "",
}
_, res, err := request("/system/functions", "POST", deploy)
if err != nil {
t.Log(err)
t.Fail()
}
if res != nil && res.StatusCode != http.StatusCreated {
t.Logf("got %d, wanted %d", res.StatusCode, http.StatusCreated)
t.Fail()
}
}
func TestList(t *testing.T) {
data, res, err := request("/system/functions", "GET", nil)
if err != nil {
t.Log(err)
t.Fail()
}
if res.StatusCode != http.StatusOK {
t.Logf("got %d, wanted %d", res.StatusCode, http.StatusOK)
t.Fail()
}
functions := []requests.Function{}
err = json.Unmarshal(data, &functions)
if err != nil {
t.Log(err)
t.Fail()
}
if len(functions) == 0 {
t.Log("List functions got: 0, want: > 0")
t.Fail()
}
}
func TestInvoke(t *testing.T) {
_, res, err := request("/function/nodeinfo", "POST", nil)
if err != nil {
t.Log(err)
t.Fail()
}
if res.StatusCode != http.StatusOK {
t.Logf("got %d, wanted %d", res.StatusCode, http.StatusOK)
t.Fail()
}
}
func TestDelete(t *testing.T) {
deleteReq := requests.DeleteFunctionRequest{
FunctionName: "nodeinfo",
}
_, res, err := request("/system/functions", "DELETE", deleteReq)
if err != nil {
t.Log(err)
t.Fail()
}
if res.StatusCode != http.StatusOK {
t.Logf("got %d, wanted %d", res.StatusCode, http.StatusOK)
t.Fail()
}
}