-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker_test.go
96 lines (88 loc) · 2.3 KB
/
checker_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
package healthy
import (
"context"
errs "errors"
"testing"
"time"
"github.com/hellofresh/health-go/v5"
"github.com/music-tribe/errors"
"github.com/stretchr/testify/assert"
)
func TestNewChecker(t *testing.T) {
type args struct {
name string
checkFunc health.CheckFunc
}
tests := []struct {
name string
args args
wantName string
wantCheckErrMsg string
}{
{
name: "when the name is not added, it should default to health-check",
args: args{
name: "",
checkFunc: NewMockChecker(nil),
},
wantName: defaultName,
wantCheckErrMsg: "",
},
{
name: "when the name is added, it should return this name",
args: args{
name: "hello",
checkFunc: NewMockChecker(nil),
},
wantName: "hello",
wantCheckErrMsg: "",
},
{
name: "when the checkFunc is missing, it should default to the stub checkFunc",
args: args{
name: "hello",
checkFunc: nil,
},
wantName: "hello",
wantCheckErrMsg: stubCheckerErrorMessage,
},
{
name: "when the checkFunc is added, it should return any errors from that checkFunc",
args: args{
name: "hello",
checkFunc: NewMockChecker(errors.NewCloudError(400, "oops")),
},
wantName: "hello",
wantCheckErrMsg: "oops",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewChecker(tt.args.name, tt.args.checkFunc)
if got.Name != tt.wantName {
t.Errorf("wanted name to be %s but got %s\n", tt.wantName, got.Name)
}
if err := got.CheckFunc(context.Background()); err != nil {
msg := err.Error()
ce := new(errors.CloudError)
if errs.As(err, &ce) {
msg = ce.Message
}
if msg != tt.wantCheckErrMsg {
t.Errorf("wanted err msg to be %s but got %s\n", tt.wantName, msg)
}
}
})
}
}
func TestNewCheckWithTimeout(t *testing.T) {
t.Parallel()
t.Run("When NewChecker is called we should default Timeout to 2 seconds", func(t *testing.T) {
c := NewChecker("hello", NewMockChecker(nil))
assert.Equal(t, 2*time.Second, c.Timeout)
})
t.Run("When NewCheckerWithTimeout is called we should set Timeout to timeout passed in", func(t *testing.T) {
c := NewCheckerWithTimeout("hello", NewMockChecker(nil), 5*time.Second)
assert.Equal(t, 5*time.Second, c.Timeout)
})
}