forked from openfaas/cron-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
103 lines (85 loc) · 4.56 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
// Copyright (c) OpenFaaS Author(s) 2021. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"testing"
"github.com/openfaas/connector-sdk/types"
cfunction "github.com/openfaas/cron-connector/types"
ptypes "github.com/openfaas/faas-provider/types"
)
func TestgetNewAndDeleteFuncs(t *testing.T) {
newCronFunctions := make(cfunction.CronFunctions, 3)
defaultReq := ptypes.FunctionStatus{}
newCronFunctions[0] = cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_unchanged", Namespace: "openfaas-fn", Schedule: "* * * * *"}
newCronFunctions[1] = cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_to_add", Namespace: "openfaas-fn", Schedule: "* * * * *"}
newCronFunctions[2] = cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_to_update", Namespace: "openfaas-fn", Schedule: "*/5 * * * *"}
oldFuncs := make(cfunction.ScheduledFunctions, 3)
oldFuncs[0] = cfunction.ScheduledFunction{Function: cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_unchanged", Namespace: "openfaas-fn", Schedule: "* * * * *"}, ID: 0}
oldFuncs[1] = cfunction.ScheduledFunction{Function: cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_to_delete", Namespace: "openfaas-fn", Schedule: "* * * * *"}, ID: 0}
oldFuncs[2] = cfunction.ScheduledFunction{Function: cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_to_update", Namespace: "openfaas-fn", Schedule: "* * * * *"}, ID: 0}
addFuncs, deleteFuncs := getNewAndDeleteFuncs(newCronFunctions, oldFuncs, "openfaas-fn")
if !deleteFuncs.Contains(&oldFuncs[1].Function) {
t.Error("function was not deleted")
}
if !addFuncs.Contains(&newCronFunctions[1]) {
t.Error("function was not added")
}
if !deleteFuncs.Contains(&oldFuncs[2].Function) && !addFuncs.Contains(&newCronFunctions[2]) {
t.Error("function will not be updated")
}
if addFuncs.Contains(&newCronFunctions[0]) || deleteFuncs.Contains(&newCronFunctions[0]) {
t.Error("function should be left as it is")
}
}
func TestNamespaceFuncs(t *testing.T) {
newCronFunctions := make(cfunction.CronFunctions, 3)
defaultReq := ptypes.FunctionStatus{}
newCronFunctions[0] = cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_one", Namespace: "openfaas-fn", Schedule: "* * * * *"}
newCronFunctions[1] = cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_one", Namespace: "custom", Schedule: "* * * * *"}
newCronFunctions[2] = cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_to_update", Namespace: "openfaas-fn", Schedule: "*/5 * * * *"}
oldFuncs := make(cfunction.ScheduledFunctions, 3)
oldFuncs[0] = cfunction.ScheduledFunction{Function: cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_one", Namespace: "openfaas-fn", Schedule: "* * * * *"}, ID: 0}
oldFuncs[1] = cfunction.ScheduledFunction{Function: cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_to_delete", Namespace: "openfaas-fn", Schedule: "* * * * *"}, ID: 0}
oldFuncs[2] = cfunction.ScheduledFunction{Function: cfunction.CronFunction{FuncData: defaultReq, Name: "test_function_to_update", Namespace: "openfaas-fn", Schedule: "* * * * *"}, ID: 0}
addFuncs, deleteFuncs := getNewAndDeleteFuncs(newCronFunctions, oldFuncs, "openfaas-fn")
if !deleteFuncs.Contains(&oldFuncs[1].Function) {
t.Error("function was not deleted")
}
if !addFuncs.Contains(&newCronFunctions[1]) {
t.Error("function was not added")
}
if !deleteFuncs.Contains(&oldFuncs[2].Function) && !addFuncs.Contains(&newCronFunctions[2]) {
t.Error("function will not be updated")
}
if addFuncs.Contains(&newCronFunctions[0]) || deleteFuncs.Contains(&newCronFunctions[0]) {
t.Error("function should be left as it is")
}
}
func TestGatewayRoute_Async(t *testing.T) {
testscases := []struct {
GatewayURL string
ExpectedGatewayURL string
AsyncFunctionInvocation bool
}{
{
GatewayURL: "http://localhost:8080",
AsyncFunctionInvocation: true,
ExpectedGatewayURL: "http://localhost:8080/async-function",
},
{
GatewayURL: "http://localhost:8080",
AsyncFunctionInvocation: false,
ExpectedGatewayURL: "http://localhost:8080/function",
},
}
for _, test := range testscases {
config := &types.ControllerConfig{
GatewayURL: test.GatewayURL,
AsyncFunctionInvocation: test.AsyncFunctionInvocation,
}
val := gatewayRoute(config)
if val != test.ExpectedGatewayURL {
t.Errorf("expected: %s, got: %s", test.ExpectedGatewayURL, val)
}
}
}