-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds_test.go
388 lines (315 loc) · 9.67 KB
/
rds_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package main
import (
"bytes"
"encoding/json"
"fmt"
// "io/ioutil"
"net/http"
"os"
"testing"
"github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v1/subnets"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v1/vpcs"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/security/groups"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/rds/v3/instances"
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
fake "github.com/opentelekomcloud/gophertelekomcloud/testhelper/client"
)
// test service function with testhelper gophertelekomcloud and
// ProviderClient and main() with the MockMuxer on "http://127.0.0.1:50000/v3")
func Test_secgroupGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockRdsResponse(t)
sg, err := secgroupGet(fake.ServiceClient(), &groups.ListOpts{Name: "golang"})
th.AssertNoErr(t, err)
th.AssertEquals(t, "golang", sg.Name)
th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sg.ID)
}
func Test_subnetGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockRdsResponse(t)
sg, err := subnetGet(fake.ServiceClient(), &subnets.ListOpts{Name: "golang"})
th.AssertNoErr(t, err)
th.AssertEquals(t, "golang", sg.Name)
th.AssertEquals(t, "011fc878-5521-4654-a1ad-f5b0b5820302", sg.ID)
}
func Test_vpcGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockRdsResponse(t)
sg, err := vpcGet(fake.ServiceClient(), &vpcs.ListOpts{Name: "golang"})
th.AssertNoErr(t, err)
th.AssertEquals(t, "golang", sg.Name)
th.AssertEquals(t, "13551d6b-755d-4757-b956-536f674975c0", sg.ID)
}
func Test_rdsGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockRdsResponse(t)
sg, err := rdsGet(fake.ServiceClient(), "ed7cc6166ec24360a5ed5c5c9c2ed726in01")
th.AssertNoErr(t, err)
th.AssertEquals(t, "default", sg.Name)
th.AssertEquals(t, "ed7cc6166ec24360a5ed5c5c9c2ed726in01", sg.Id)
tools.PrintResource(t, sg)
}
func MockMuxer() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, ProviderGetResponse)
case "POST":
w.Header().Add("X-Subject-Token", "dG9rZW46IDEyMzQK")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_, _ = fmt.Fprint(w, ProviderPostResponse)
}
})
mux.HandleFunc("/v2.0/security-groups", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, SecurityGroupListResponse)
/* Debug output of the request
uri := r.URL.String()
fmt.Printf("Uri: %s\n", string(uri))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
fmt.Printf("Body: %s\n", body)
*/
}
})
mux.HandleFunc("/v1/bbfe8c41dd034a07bebd592bf03b4b0c/subnets", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, SubnetListResponse)
}
})
mux.HandleFunc("/v1/bbfe8c41dd034a07bebd592bf03b4b0c/vpcs", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, VpcListResponse)
}
})
mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, RdsGetResponse)
case "POST":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_, _ = fmt.Fprint(w, RdsCreateResponse)
}
})
mux.HandleFunc("/jobs", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, RdsJobResponse)
}
})
fmt.Println("Listening...")
var retries int = 3
for retries > 0 {
err := http.ListenAndServe("127.0.0.1:50000", mux)
if err != nil {
fmt.Println("Restart http server ... ", err)
retries -= 1
} else {
break
}
}
}
func MockRdsResponse(t *testing.T) {
th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
switch r.Method {
case "GET":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, RdsGetResponse)
case "POST":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_, _ = fmt.Fprint(w, RdsCreateResponse)
}
})
th.Mux.HandleFunc("/security-groups", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, SecurityGroupListResponse)
})
th.Mux.HandleFunc("/subnets", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, SubnetListResponse)
})
th.Mux.HandleFunc("/vpcs", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, VpcListResponse)
})
th.Mux.HandleFunc("/jobs", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, RdsJobResponse)
/* Example output http response
uri := r.URL.String()
fmt.Println(string(uri))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
fmt.Printf("%s", body)
*/
})
}
func Test_rdsCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
MockRdsResponse(t)
RdsCreateOpts := &instances.CreateRdsOpts{
Name: "default",
Port: "3306",
Password: "acc-test-password1!",
BackupStrategy: &instances.BackupStrategy{
StartTime: "08:15-09:15",
KeepDays: 12,
},
FlavorRef: "rds.mysql.s1.large.ha",
Region: "eu-de",
AvailabilityZone: "eu-de-01,eu-de-02",
VpcId: "490a4a08-ef4b-44c5-94be-3051ef9e4fce",
SubnetId: "0e2eda62-1d42-4d64-a9d1-4e9aa9cd994f",
SecurityGroupId: "2a1f7fc8-3307-42a7-aa6f-42c8b9b8f8c5",
Volume: &instances.Volume{
Type: "ULTRAHIGH",
Size: 100,
},
Datastore: &instances.Datastore{
Type: "MySQL",
Version: "5.6",
},
Ha: &instances.Ha{
Mode: "ha",
ReplicationMode: "semisync",
},
}
err := rdsCreate(fake.ServiceClient(), fake.ServiceClient(), fake.ServiceClient(), RdsCreateOpts)
th.AssertNoErr(t, err)
}
func Test_flags(t *testing.T) {
testCases := []struct {
name string
flags []string
expected int
}{
{"help_exit_0", []string{"-help"}, 0},
{"version_exit_0", []string{"-version"}, 0},
}
for _, tc := range testCases {
t.Run("test "+tc.name, func(t *testing.T) {
// prevent os.Exit exit here
// https://stackoverflow.com/questions/40615641/testing-os-exit-scenarios-in-go-with-coverage-information-coveralls-io-goverall
oldOsExit := osExit
defer func() {
osExit = oldOsExit
}()
var got int
tmpExit := func(code int) {
got = code
}
osExit = tmpExit
// flag.CommandLine = flag.NewFlagSet(tc.name, flag.ExitOnError)
os.Args = append([]string{tc.name}, tc.flags...)
err := os.Setenv("OS_AUTH_URL", "")
th.AssertNoErr(t, err)
getFlags(os.Args[1])
if got != tc.expected {
t.Errorf("Expected exit code: %d, got: %d", tc.expected, got)
}
})
}
}
func Test_getProvider(t *testing.T) {
go MockMuxer()
err := os.Setenv("OS_USERNAME", "test")
th.AssertNoErr(t, err)
err = os.Setenv("OS_USER_DOMAIN_NAME", "test")
th.AssertNoErr(t, err)
err = os.Setenv("OS_PASSWORD", "test")
th.AssertNoErr(t, err)
err = os.Setenv("OS_IDENTITY_API_VERSION", "3")
th.AssertNoErr(t, err)
err = os.Setenv("OS_AUTH_URL", "http://127.0.0.1:50000/v3")
th.AssertNoErr(t, err)
provider := getProvider()
defer getProvider()
p := &golangsdk.ProviderClient{
UserID: "91dca41cc55e4614aaca83b78af8ddc5",
}
th.CheckDeepEquals(t, p.UserID, provider.UserID)
fmt.Println("IdentityEndpoint: ", provider.IdentityEndpoint)
return
}
func PrettyJson(str string) (string, error) {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(str), "", " "); err != nil {
return "", err
}
return prettyJSON.String(), nil
}
func Test_main(t *testing.T) {
oldOsExit := osExit
defer func() {
osExit = oldOsExit
}()
var got int
tmpExit := func(code int) {
got = code
}
osExit = tmpExit
err := os.Setenv("OS_AUTH_URL", "")
th.AssertNoErr(t, err)
main()
if got != 0 {
t.Errorf("Expected exit code: %d", got)
}
}
func Test_Create(t *testing.T) {
go MockMuxer()
err := os.Setenv("OS_USERNAME", "test")
th.AssertNoErr(t, err)
err = os.Setenv("OS_USER_DOMAIN_NAME", "test")
th.AssertNoErr(t, err)
err = os.Setenv("OS_PASSWORD", "test")
th.AssertNoErr(t, err)
err = os.Setenv("OS_IDENTITY_API_VERSION", "3")
th.AssertNoErr(t, err)
err = os.Setenv("OS_AUTH_URL", "http://127.0.0.1:50000/v3")
th.AssertNoErr(t, err)
defer Create()
}