-
Notifications
You must be signed in to change notification settings - Fork 8
/
dcs_test.go
211 lines (174 loc) · 4.53 KB
/
dcs_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
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
package eventmaster
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/wish/eventmaster/jh"
"github.com/pkg/errors"
)
func TestDCRoundtrip(t *testing.T) {
mds := &mockDataStore{}
store, err := GetTestEventStore(mds)
if err != nil {
t.Fatalf("creating event store: %v", err)
}
ts := httptest.NewServer(NewServer(store, "", ""))
name := "test"
{
dcs, err := store.GetDCs()
if err != nil {
t.Fatalf("get dcs: %v", err)
}
if got, want := len(dcs), 0; got != want {
t.Fatalf("number of dcs: got %v, want %v", got, want)
}
}
id, err := postDC(ts.URL, name)
if err != nil {
t.Fatalf("post dc: %v", err)
}
t.Logf("created dc: %v", id)
{
dcs, err := store.GetDCs()
if err != nil {
t.Fatalf("get dcs: %v", err)
}
if got, want := len(dcs), 1; got != want {
t.Fatalf("number of dcs: got %v, want %v", got, want)
}
}
{
dcs, err := getDCs(ts.URL, name)
if err != nil {
t.Fatalf("get dc: %v", err)
}
if got, want := len(dcs), 1; got != want {
t.Fatalf("number of dcs: got %v, want %v", got, want)
}
if got, want := dcs[0].ID, id; got != want {
t.Fatalf("dc id: got %v, want %v", got, want)
}
}
newName := "bar"
id2, err := updateDC(ts.URL, name, newName)
if err != nil {
t.Fatalf("patch: %v", err)
}
if got, want := id2, id; got != want {
t.Fatalf("id changed; got %v, want %v", got, want)
}
{
dcs, err := getDCs(ts.URL, name)
if err != nil {
t.Fatalf("get dc: %v", err)
}
if got, want := len(dcs), 1; got != want {
t.Fatalf("number of dcs: got %v, want %v", got, want)
}
if got, want := dcs[0].ID, id; got != want {
t.Fatalf("dc id: got %v, want %v", got, want)
}
}
}
func postDC(url, name string) (string, error) {
return dc(http.MethodPost, url, name, "")
}
func updateDC(url, name, new string) (string, error) {
return dc(http.MethodPut, url, new, name)
}
func dc(method, url, new, name string) (string, error) {
buf := &bytes.Buffer{}
dc := DC{Name: new}
if err := json.NewEncoder(buf).Encode(&dc); err != nil {
return "", errors.Wrap(err, "json encode")
}
url = url + "/v1/dc"
if name != "" {
url = fmt.Sprintf("%v/%v", url, name)
}
req, err := http.NewRequest(method, url, buf)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", errors.Wrap(err, "post dc")
}
buf.Reset()
io.Copy(buf, resp.Body)
resp.Body.Close()
var status int
switch method {
case http.MethodPost:
status = http.StatusCreated
case http.MethodPut:
status = http.StatusOK
default:
return "", errors.New("bad method")
}
if got, want := resp.StatusCode, status; got != want {
return "", jh.NewError(errors.Errorf("bad status: got %v, want %v, %v", got, want, buf.String()).Error(), got)
}
res := struct {
I string `json:"dc_id"`
}{}
if err := json.NewDecoder(buf).Decode(&res); err != nil {
return "", errors.Wrap(err, "json decode")
}
return res.I, nil
}
func getDCs(url, name string) ([]DC, error) {
resp, err := http.Get(url + "/v1/dc")
if err != nil {
return nil, errors.Wrap(err, "get dc")
}
buf := &bytes.Buffer{}
io.Copy(buf, resp.Body)
resp.Body.Close()
if got, want := resp.StatusCode, http.StatusOK; got != want {
return nil, errors.Errorf("bad status: got %v, want %v, %v", got, want, buf.String())
}
r := map[string][]DC{}
if err := json.NewDecoder(buf).Decode(&r); err != nil {
return nil, errors.Wrap(err, "json decode")
}
if _, ok := r["results"]; !ok {
return nil, errors.New("missing result key from response")
}
return r["results"], nil
}
func TestBadAddDC(t *testing.T) {
mds := &mockDataStore{}
store, err := GetTestEventStore(mds)
if err != nil {
t.Fatalf("creating event store: %v", err)
}
ts := httptest.NewServer(NewServer(store, "", ""))
name := ""
_, err = postDC(ts.URL, name)
if err == nil {
t.Fatalf("should fail to post dc with empty name: %v", err)
}
if got, want := err.(jh.Error).Status(), http.StatusBadRequest; got != want {
t.Fatalf("bad status: got %v, want %v", got, want)
}
}
func TestBadUpdateDC(t *testing.T) {
mds := &mockDataStore{}
store, err := GetTestEventStore(mds)
if err != nil {
t.Fatalf("creating event store: %v", err)
}
ts := httptest.NewServer(NewServer(store, "", ""))
if _, err := postDC(ts.URL, "foo"); err != nil {
t.Fatalf("initial post: %v", err)
}
_, err = updateDC(ts.URL, "foo", "")
if err == nil {
t.Fatalf("should fail to post dc with empty name: %v", err)
}
if got, want := err.(jh.Error).Status(), http.StatusBadRequest; got != want {
t.Fatalf("bad status: got %v, want %v", got, want)
}
}