-
Notifications
You must be signed in to change notification settings - Fork 1
/
fn.go
102 lines (81 loc) · 2.69 KB
/
fn.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/response"
"github.com/stevendborrelli/function-unit-test/input/v1beta1"
)
type TestResult struct {
Description string `json:"description"`
Assertion string `json:"assert"`
Error string `json:"error,omitempty"`
}
type TestResults struct {
Passing []TestResult `json:"passing"`
Failing []TestResult `json:"failing"`
Error []TestResult `json:"errors,omitempty"`
}
type Function struct {
fnv1beta1.UnimplementedFunctionRunnerServiceServer
log logging.Logger
}
// RunFunction runs the Function.
func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequest) (*fnv1beta1.RunFunctionResponse, error) {
f.log.Info("Running function", "tag", req.GetMeta().GetTag())
rsp := response.To(req, response.DefaultTTL)
in := &v1beta1.Input{}
if err := request.GetInput(req, in); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
return rsp, nil
}
if len(in.TestCases) == 0 {
f.log.Info("No test cases supplied")
response.Fatal(rsp, errors.New("No test cases supplied"))
return rsp, nil
}
tr := TestResults{}
// Set up input variables for our tests
// TODO: we could filter resources here based on annotation, or hand it off to CEL to filter.
vars := RunFunctionRequestToCELVars(req)
for i, tc := range in.TestCases {
f.log.Info(fmt.Sprintf("running test case %d: %s", i, tc.Description))
res, err := Assert(tc.Assert, vars)
if err != nil {
tr.Error = append(tr.Error, TestResult{
Description: tc.Description,
Assertion: tc.Assert,
Error: err.Error()})
continue
}
if res {
tr.Passing = append(tr.Passing, TestResult{
Description: tc.Description,
Assertion: tc.Assert,
})
} else {
tr.Failing = append(tr.Failing, TestResult{
Description: tc.Description,
Assertion: tc.Assert,
})
}
}
response.Normalf(rsp, "I was run with input %q!", in.TestCases)
trJson, _ := json.Marshal(tr)
f.log.Info(string(trJson))
if in.ErrorOnFailedTest && len(tr.Failing) > 0 {
tf, _ := json.MarshalIndent(tr.Failing, "", " ")
response.Fatal(rsp, errors.Errorf("failing tests: %s", tf))
return rsp, errors.Errorf("failing tests: %s", tf)
}
if len(tr.Error) > 0 {
te, _ := json.MarshalIndent(tr.Error, "", " ")
dump, _ := json.MarshalIndent(req, "", " ")
response.Fatal(rsp, errors.Errorf("tests with errors: %s\ninput: %s", te, dump))
}
return rsp, nil
}