-
Notifications
You must be signed in to change notification settings - Fork 28
/
client_test.go
170 lines (157 loc) · 3.51 KB
/
client_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
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
package aiven
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_Init(t *testing.T) {
var c Client = Client{}
c.Init()
}
func TestCheckRetry(t *testing.T) {
type option struct {
name string // test name
code int // response status code
expected bool // expected result
body string
method string
}
opts := []*option{
{
name: "Retries 408",
code: http.StatusRequestTimeout,
expected: true,
},
{
name: "Retries 429",
code: http.StatusTooManyRequests,
expected: true,
},
{
name: "Retries 404",
code: http.StatusNotFound,
body: "Service 'test-foo' does not exist",
method: "POST",
expected: true,
},
{
name: "Does not retry 404 with different error message",
code: http.StatusNotFound,
body: "User 'test-foo' does not exist",
method: "POST",
expected: false,
},
{
name: "Retries deletion 417",
code: http.StatusExpectationFailed,
method: "DELETE",
expected: true,
},
}
doRetry := []int{500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511}
for _, code := range doRetry {
opts = append(opts, &option{
name: fmt.Sprintf("Tests code %d, should retry", code),
code: code,
expected: true,
})
}
// Except 408 and 429
doNotRetry := []int{
100, 101, 102, 103,
200, 201, 202, 203, 204, 205, 206, 207, 208, 226,
300, 301, 302, 303, 304, 305, 306, 307, 308,
400, 401, 402, 403, 404, 405, 406, 407, 409, 410, 411, 412, 413, 414,
415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 431, 451,
}
for _, code := range doNotRetry {
opts = append(opts, &option{
name: fmt.Sprintf("Tests code %d, should not retry", code),
code: code,
expected: false,
})
}
ctx := context.Background()
for _, opt := range opts {
t.Run(opt.name, func(t *testing.T) {
rsp := &http.Response{
Status: fmt.Sprintf("%d", opt.code),
StatusCode: opt.code,
Body: io.NopCloser(strings.NewReader(opt.body)),
Request: &http.Request{Method: opt.method},
}
retry, err := checkRetry(ctx, rsp, nil)
assert.Equal(t, opt.expected, retry)
assert.Nil(t, err)
})
}
}
func TestIsServiceLagError(t *testing.T) {
cases := []struct {
body string
method string
expected bool
}{
{
body: "Service lol does not exist",
method: "POST",
expected: true,
},
{
// Invalid body
body: "User lol does not exist",
method: "POST",
expected: false,
},
{
// Invalid method
body: "Service lol does not exist",
method: "GET",
expected: false,
},
}
for i, opt := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
assert.Equal(t, opt.expected, isServiceLagError(opt.method, opt.body))
})
}
}
func TestIsUserError(t *testing.T) {
cases := []struct {
body string
method string
expected bool
}{
{
body: "User avnadmin with component main not found",
method: "",
expected: true,
},
{
body: "User root with component main not found",
method: "",
expected: true,
},
{
// Invalid user
body: "User lol with component main not found",
method: "",
expected: false,
},
{
// Invalid body
body: "User root with component",
method: "",
expected: false,
},
}
for i, opt := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
assert.Equal(t, opt.expected, isUserError(opt.method, opt.body))
})
}
}