-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathwait_test.go
More file actions
217 lines (202 loc) · 5.76 KB
/
wait_test.go
File metadata and controls
217 lines (202 loc) · 5.76 KB
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
package wait
import (
"context"
"errors"
"net/http"
"testing"
"testing/synctest"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
alb "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
)
const (
testRegion = "eu01"
testName = "testlb"
)
var (
testProject = uuid.NewString()
)
type response struct {
loadbalancer *alb.LoadBalancer
err error
}
type mockSettings struct {
n int
responses []response
}
func newAPIMock(settings *mockSettings) alb.DefaultAPI {
return &alb.DefaultAPIServiceMock{
GetLoadBalancerExecuteMock: utils.Ptr(func(_ alb.ApiGetLoadBalancerRequest) (*alb.LoadBalancer, error) {
resp := settings.responses[settings.n]
settings.n++
settings.n %= len(settings.responses)
return resp.loadbalancer, resp.err
}),
}
}
func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) {
tests := []struct {
name string
responses []response
want *alb.LoadBalancer
wantErr bool
}{
{
"create succeeded immediately",
[]response{
{
loadbalancer: &alb.LoadBalancer{
Status: utils.Ptr(LOADBALANCERSTATUS_READY),
},
err: nil,
},
},
&alb.LoadBalancer{
Status: utils.Ptr(LOADBALANCERSTATUS_READY),
},
false,
},
{
"create succeeded delayed",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_READY)}, nil},
},
&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_READY)},
false,
},
{
"create failed delayed",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_ERROR)}, nil},
},
&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_ERROR)},
true,
},
{
"timeout",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
},
nil,
true,
},
{
"broken state",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr("bogus")}, nil},
},
&alb.LoadBalancer{Status: utils.Ptr("bogus")},
false,
},
// no special update tests needed as the states are the same
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ctx := context.Background()
client := newAPIMock(&mockSettings{
responses: tt.responses,
})
handler := CreateOrUpdateLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
got, err := handler.WaitWithContext(ctx)
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error response. want %v but got %qe ", tt.wantErr, err)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("differing loadbalancer %s", diff)
}
})
})
}
}
func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
tests := []struct {
name string
responses []response
wantErr bool
}{
{
"Delete with '404' succeeded immediately",
[]response{
{nil, oapierror.NewError(http.StatusNotFound, "not found")},
},
false,
},
{
"Delete with '404' delayed",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{nil, oapierror.NewError(http.StatusNotFound, "not found")},
},
false,
},
{
"Delete with 'gone' succeeded immediately",
[]response{
{nil, oapierror.NewError(http.StatusGone, "gone")},
},
false,
},
{
"Delete with 'gone' delayed",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{nil, oapierror.NewError(http.StatusGone, "not found")},
},
false,
},
{
"Delete with error delayed",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_ERROR)}, oapierror.NewError(http.StatusInternalServerError, "kapow")},
},
true,
},
{
"Cannot delete",
[]response{
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_PENDING)}, nil},
{&alb.LoadBalancer{Status: utils.Ptr(LOADBALANCERSTATUS_ERROR)}, oapierror.NewError(http.StatusOK, "ok")},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ctx := context.Background()
client := newAPIMock(&mockSettings{
responses: tt.responses,
})
handler := DeleteLoadbalancerWaitHandler(ctx, client, testProject, testRegion, testName)
_, err := handler.WaitWithContext(ctx)
if tt.wantErr != (err != nil) {
t.Fatalf("wrong error result. want err: %v got %v", tt.wantErr, err)
}
if tt.wantErr {
var apiErr *oapierror.GenericOpenAPIError
if !errors.As(err, &apiErr) {
t.Fatalf("expected openapi error, got %v", err)
}
}
})
})
}
}