-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_test.go
168 lines (135 loc) · 4.51 KB
/
function_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//go:build unit_test
// +build unit_test
package evervault_test
import (
"fmt"
"testing"
"github.com/evervault/evervault-go"
"github.com/stretchr/testify/assert"
)
func TestGetFunctionRunToken(t *testing.T) {
t.Parallel()
server := startMockHTTPServer("", "")
defer server.Close()
testClient := mockedClient(t, server)
res, err := testClient.CreateFunctionRunToken("test_function", "test_payload")
if err != nil {
t.Errorf("Failed to create run token, got %s", err)
return
}
if res.Token != "test_token" {
t.Errorf("Expected encrypted string, got %s", res)
}
}
func TestRunFunctionSuccess(t *testing.T) {
t.Parallel()
message := "Hello from a Function! It seems you have 4 letters in your name"
id := "func_run_65bc5168cb8b"
functionResponsePayload := fmt.Sprintf(`
{
"status": "success",
"result": { "message": "%s" },
"id": "%s"
}`, message, id)
server := startMockHTTPServer(functionResponsePayload, "")
defer server.Close()
testClient := mockedClient(t, server)
payload := map[string]any{"name": "john", "age": 30}
res, err := testClient.RunFunction("test_function", payload)
if err != nil {
t.Errorf("Failed to run Function, got %s", err)
return
}
assert.Equal(t, "success", res.Status)
assert.Equal(t, id, res.ID)
assert.Equal(t, message, res.Result["message"])
}
func TestRunFunctionFailure(t *testing.T) {
t.Parallel()
message := "Uh oh!"
stack := "Error: Uh oh!..."
id := "func_run_65bc5168cb8b"
functionResponsePayload := fmt.Sprintf(`
{
"status": "failure",
"error": { "message": "%s", "stack": "%s" },
"id": "%s"
}`, message, stack, id)
server := startMockHTTPServer(functionResponsePayload, "")
defer server.Close()
testClient := mockedClient(t, server)
payload := map[string]any{"name": "john", "age": 30}
_, err := testClient.RunFunction("test_function", payload)
if runtimeError, ok := err.(evervault.FunctionRuntimeError); !ok {
t.Error("Expected FunctionRuntimeError, got", err)
} else {
assert.Equal(t, message, runtimeError.ErrorBody.Message)
assert.Equal(t, stack, runtimeError.ErrorBody.Stack)
assert.Equal(t, id, runtimeError.ID)
}
}
func TestRunFunctionTimeout(t *testing.T) {
t.Parallel()
message := "Function execution exceeded the allotted time and has timed out. Please review your code to ensure it finishes within the time limit set in function.toml."
functionResponsePayload := fmt.Sprintf(`
{
"status": 408,
"code": "functions/request-timeout",
"title": "Function Request Timeout",
"detail": "%s"
}`, message)
server := startMockHTTPServer(functionResponsePayload, "")
defer server.Close()
testClient := mockedClient(t, server)
payload := map[string]any{"name": "john", "age": 30}
_, err := testClient.RunFunction("test_function", payload)
if functionTimeoutError, ok := err.(evervault.FunctionTimeoutError); !ok {
t.Error("Expected FunctionTimeoutError, got", err)
} else {
assert.Equal(t, message, functionTimeoutError.Message)
}
}
func TestRunFunctionNotReady(t *testing.T) {
t.Parallel()
message := "The Function is not ready to be invoked yet. This can occur when it hasn't been executed recently. Please try again shortly."
functionResponsePayload := fmt.Sprintf(`
{
"status": 409,
"code": "functions/function-not-ready",
"title": "Function Not Ready",
"detail": "%s"
}`, message)
server := startMockHTTPServer(functionResponsePayload, "")
defer server.Close()
testClient := mockedClient(t, server)
payload := map[string]any{"name": "john", "age": 30}
_, err := testClient.RunFunction("test_function", payload)
if functionNotReadyError, ok := err.(evervault.FunctionNotReadyError); !ok {
t.Error("Expected FunctionNotReadyError, got", err)
} else {
assert.Equal(t, message, functionNotReadyError.Message)
}
}
func TestRunFunctionUnauthorized(t *testing.T) {
t.Parallel()
message := "The request cannot be authenticated. The request does not contain valid credentials. Please retry with a valid API key."
code := "unauthorized"
functionResponsePayload := fmt.Sprintf(`
{
"status": 409,
"code": "%s",
"title": "Unauthorized",
"detail": "%s"
}`, code, message)
server := startMockHTTPServer(functionResponsePayload, "")
defer server.Close()
testClient := mockedClient(t, server)
payload := map[string]any{"name": "john", "age": 30}
_, err := testClient.RunFunction("test_function", payload)
if evervaultError, ok := err.(evervault.APIError); !ok {
t.Error("Expected Evervault Error, got", err)
} else {
assert.Equal(t, code, evervaultError.Code)
assert.Equal(t, message, evervaultError.Message)
}
}