-
Notifications
You must be signed in to change notification settings - Fork 20
/
testfn.go
228 lines (200 loc) · 5.69 KB
/
testfn.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/fnproject/cli/client"
functions "github.com/funcy/functions_go"
"github.com/onsi/gomega"
"github.com/urfave/cli"
)
type testStruct struct {
Tests []fftest `yaml:"tests,omitempty" json:"tests,omitempty"`
}
func testfn() cli.Command {
cmd := testcmd{RoutesApi: functions.NewRoutesApi()}
return cli.Command{
Name: "test",
Usage: "run functions test if present",
Flags: cmd.flags(),
Action: cmd.test,
}
}
type testcmd struct {
*functions.RoutesApi
build bool
remote string
}
func (t *testcmd) flags() []cli.Flag {
return []cli.Flag{
// cli.BoolFlag{
// Name: "b",
// Usage: "build before test",
// Destination: &t.build,
// },
cli.StringFlag{
Name: "remote",
Usage: "run tests by calling the function on Oracle Functions daemon on `appname`",
Destination: &t.remote,
},
}
}
func (t *testcmd) test(c *cli.Context) error {
gomega.RegisterFailHandler(func(message string, callerSkip ...int) {
fmt.Println("In gomega FailHandler:", message)
})
// First, build it
err := c.App.Command("build").Run(c)
if err != nil {
return err
}
ff, err := loadFuncfile()
if err != nil {
return err
}
var tests []fftest
// Look for test.json file too
tfile := "test.json"
if exists(tfile) {
f, err := os.Open(tfile)
if err != nil {
return fmt.Errorf("could not open %s for parsing. Error: %v", tfile, err)
}
ts := &testStruct{}
err = json.NewDecoder(f).Decode(ts)
if err != nil {
fmt.Println("Invalid tests.json file:", err)
return err
}
tests = ts.Tests
} else {
tests = ff.Tests
}
if len(tests) == 0 {
return errors.New("no tests found for this function")
}
fmt.Printf("Running %v tests...", len(tests))
target := ff.ImageName()
runtest := runlocaltest
if t.remote != "" {
if ff.Path == "" {
return errors.New("execution of tests on remote server demand that this function has a `path`.")
}
if err := resetBasePath(t.Configuration); err != nil {
return fmt.Errorf("error setting endpoint: %v", err)
}
baseURL, err := url.Parse(t.Configuration.BasePath)
if err != nil {
return fmt.Errorf("error parsing base path: %v", err)
}
u, err := url.Parse("../")
u.Path = path.Join(u.Path, "r", t.remote, ff.Path)
target = baseURL.ResolveReference(u).String()
runtest = runremotetest
}
errorCount := 0
fmt.Println("running tests on", ff.ImageName(), ":")
for i, tt := range tests {
fmt.Printf("\nTest %v\n", i+1)
start := time.Now()
var err error
err = runtest(target, tt.Input, tt.Output, tt.Err, tt.Env)
if err != nil {
fmt.Print("FAILED")
errorCount += 1
scanner := bufio.NewScanner(strings.NewReader(err.Error()))
for scanner.Scan() {
fmt.Println("\t\t", scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading test result:", err)
break
}
} else {
fmt.Print("PASSED")
}
fmt.Println(" - ", tt.Name, " (", time.Since(start), ")")
}
fmt.Printf("\n%v tests passed, %v tests failed.\n", len(tests)-errorCount, errorCount)
if errorCount > 0 {
return errors.New("tests failed, errors found")
}
return nil
}
func runlocaltest(target string, in *inputMap, expectedOut *outputMap, expectedErr *string, env map[string]string) error {
inBytes, _ := json.Marshal(in.Body)
stdin := &bytes.Buffer{}
if in != nil {
stdin = bytes.NewBuffer(inBytes)
}
expectedB, _ := json.Marshal(expectedOut.Body)
expectedString := string(expectedB)
// TODO: use the same run as `fn run` so we don't have to dupe all the config and env vars that get passed in
var stdout, stderr bytes.Buffer
var restrictedEnv []string
for k, v := range env {
oldv := os.Getenv(k)
defer func(oldk, oldv string) {
os.Setenv(oldk, oldv)
}(k, oldv)
os.Setenv(k, v)
restrictedEnv = append(restrictedEnv, k)
}
ff := &funcfile{Name: target}
if err := runff(ff, stdin, &stdout, &stderr, "", restrictedEnv, nil, DefaultFormat, 1); err != nil {
return fmt.Errorf("%v\nstdout:%s\nstderr:%s\n", err, stdout.String(), stderr.String())
}
out := stdout.String()
if expectedOut == nil && out != "" {
return fmt.Errorf("unexpected output found: %s", out)
}
if gomega.Expect(out).To(gomega.MatchJSON(expectedString)) {
// PASS!
return nil
}
// don't think we should test error output, it's just for logging
// err := stderr.String()
// if expectedErr == nil && err != "" {
// return fmt.Errorf("unexpected error output found: %s", err)
// } else if expectedErr != nil && *expectedErr != err {
// return fmt.Errorf("mismatched error output found.\nexpected (%d bytes):\n%s\ngot (%d bytes):\n%s\n", len(*expectedErr), *expectedErr, len(err), err)
// }
return fmt.Errorf("mismatched output found.\nexpected:\n%s\ngot:\n%s\n", expectedString, out)
}
func runremotetest(target string, in *inputMap, expectedOut *outputMap, expectedErr *string, env map[string]string) error {
inBytes, _ := json.Marshal(in)
stdin := &bytes.Buffer{}
if in != nil {
stdin = bytes.NewBuffer(inBytes)
}
expectedString, _ := json.Marshal(expectedOut.Body)
var stdout bytes.Buffer
var restrictedEnv []string
for k, v := range env {
oldv := os.Getenv(k)
defer func(oldk, oldv string) {
os.Setenv(oldk, oldv)
}(k, oldv)
os.Setenv(k, v)
restrictedEnv = append(restrictedEnv, k)
}
if err := client.CallFN(target, stdin, &stdout, "", restrictedEnv, false); err != nil {
return fmt.Errorf("%v\nstdout:%s\n", err, stdout.String())
}
out := stdout.String()
if expectedOut == nil && out != "" {
return fmt.Errorf("unexpected output found: %s", out)
}
if gomega.Expect(out).To(gomega.MatchJSON(expectedString)) {
// PASS!
return nil
}
return nil
}