-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrunner_test.go
93 lines (83 loc) · 2.09 KB
/
runner_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
package differer
import (
"bytes"
"context"
"errors"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/jimen0/differer/scheduler"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
func TestRun(t *testing.T) {
tt := []struct {
name string
input string
runner *CloudRunner
exp *scheduler.Result
valid bool
}{
{
name: "valid",
runner: &CloudRunner{
Name: "test",
Service: "https://example.test",
Client: doFunc(func(req *http.Request) (*http.Response, error) {
var resp http.Response
resp.StatusCode = http.StatusOK
result := &scheduler.Result{Id: "foo", Value: "bbac"}
b, err := proto.Marshal(result)
require.Nil(t, err)
resp.Body = ioutil.NopCloser(bytes.NewReader(b))
return &resp, nil
}),
},
exp: &scheduler.Result{Id: "foo", Value: "bbac"},
valid: true,
},
{
name: "error response",
runner: &CloudRunner{
Name: "errorer",
Service: "https://example.test",
Client: doFunc(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("error")
}),
},
},
{
name: "unexpected status code",
runner: &CloudRunner{
Name: "internal errorer",
Service: "https://example.test",
Client: doFunc(func(req *http.Request) (*http.Response, error) {
var resp http.Response
resp.StatusCode = http.StatusInternalServerError
resp.Body = ioutil.NopCloser(strings.NewReader(``))
return &resp, nil
}),
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
res, err := tc.runner.Run(context.Background(), []byte(tc.input))
if !tc.valid {
require.NotNil(t, err)
return
}
require.Equal(t, tc.exp.Id, res.Id)
require.Equal(t, tc.exp.Value, res.Value)
require.Equal(t, tc.exp.Error, res.Error)
})
}
}
// doFunc is a testing helper that builds HTTP responses.
type doFunc func(req *http.Request) (*http.Response, error)
func (fn doFunc) Do(req *http.Request) (*http.Response, error) {
return fn(req)
}
// ensure doFunc implements clienter interface.
var _ clienter = (doFunc)(nil)