Skip to content

Commit be5ed20

Browse files
authored
Refactor hardcoded methods, emptyFmt and testutils (#91)
* Refactor testutils handlers Use single structure with parameters for each of the testutils handler. Update all unit tests for the Resell API. * Resell V2: refactor capabilities package Remove shadowing of capabilities package in unit test and use http.MethodGet instead of hardcoded value. * Resell V2: refactor tokens package Use http.MethodPost instead of hardcoded value. * Refactor testing client Use http package constants instead of hardcoded method names. Fix emptyFmt errors. * Refactor floatingips package Remove hardcoded method names and use http constants. * Refactor Resell V2 licenses package Remove shadowing of package name in unit tests and remove hardcoded method names. * Refactor Resell V2 subnets package Remove hardcoded method names and use http constants. * Refactor Resell V2 vrrpsubnets package Remove hardcoded method names and use http constants. Remove hardcoded IPv4 type in the testing fixtures. * Refactor Resell V2 traffic package Remove hardcoded method names and use http constants. Remove package name shadowing in tests. * Refactor Resell V2 quotas package Remove hardcoded method names and use http constants. Fix emptyFmt error in request_opts. * Refactor Resell V2 roles package Remove hardcoded method names and use http constants. * Refactor Resell V2 projects package Remove hardcoded method names and use http constants. * Refactor Resell V2 users package Remove hardcoded method names and use http constants. * Refactor common selvpc package Remove shadowing of net/url package, fix emptyFmt error and rephrase DoRequest inline comment. * Refactor testutils handler opts Pass handler options by pointer to prevent copying of big structure in tests. * Fix UnmarshallError tests Change status codes in UnmarshallError to StatusOK.
1 parent e3cf7dc commit be5ed20

File tree

27 files changed

+1149
-417
lines changed

27 files changed

+1149
-417
lines changed

selvpcclient/resell/v2/capabilities/requests.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package capabilities
22

33
import (
44
"context"
5+
"net/http"
56
"strings"
67

78
"github.com/selectel/go-selvpcclient/selvpcclient"
@@ -12,7 +13,7 @@ const resourceURL = "capabilities"
1213
// Get returns the domain capabilities.
1314
func Get(ctx context.Context, client *selvpcclient.ServiceClient) (*Capabilities, *selvpcclient.ResponseResult, error) {
1415
url := strings.Join([]string{client.Endpoint, resourceURL}, "/")
15-
responseResult, err := client.DoRequest(ctx, "GET", url, nil)
16+
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
1617
if err != nil {
1718
return nil, nil, err
1819
}

selvpcclient/resell/v2/capabilities/testing/requests_test.go

+42-24
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,41 @@ func TestGetCapabilities(t *testing.T) {
1515
testEnv := testutils.SetupTestEnv()
1616
defer testEnv.TearDownTestEnv()
1717
testEnv.NewTestResellV2Client()
18-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/capabilities",
19-
TestGetCapabilitiesRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
18+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
19+
Mux: testEnv.Mux,
20+
URL: "/resell/v2/capabilities",
21+
RawResponse: TestGetCapabilitiesRaw,
22+
Method: http.MethodGet,
23+
Status: http.StatusOK,
24+
CallFlag: &endpointCalled,
25+
})
2026

2127
ctx := context.Background()
22-
capabilities, _, err := capabilities.Get(ctx, testEnv.Client)
28+
c, _, err := capabilities.Get(ctx, testEnv.Client)
2329
if err != nil {
2430
t.Fatal(err)
2531
}
2632

2733
if !endpointCalled {
2834
t.Fatal("endpoint wasn't called")
2935
}
30-
if capabilities == nil {
36+
if c == nil {
3137
t.Fatal("didn't get capabilities")
3238
}
33-
if len(capabilities.Licenses) != 2 {
34-
t.Errorf("expected 2 licenses, but got %d", len(capabilities.Licenses))
39+
if len(c.Licenses) != 2 {
40+
t.Errorf("expected 2 licenses, but got %d", len(c.Licenses))
3541
}
36-
if len(capabilities.Regions) != 3 {
37-
t.Errorf("expected 3 regions, but got %d", len(capabilities.Regions))
42+
if len(c.Regions) != 3 {
43+
t.Errorf("expected 3 regions, but got %d", len(c.Regions))
3844
}
39-
if len(capabilities.Resources) != 16 {
40-
t.Errorf("expected 16 resources, but got %d", len(capabilities.Resources))
45+
if len(c.Resources) != 16 {
46+
t.Errorf("expected 16 resources, but got %d", len(c.Resources))
4147
}
42-
if len(capabilities.Subnets) != 1 {
43-
t.Errorf("expected 1 subnets, but got %d", len(capabilities.Subnets))
48+
if len(c.Subnets) != 1 {
49+
t.Errorf("expected 1 subnets, but got %d", len(c.Subnets))
4450
}
45-
if len(capabilities.Traffic.Granularities) != 3 {
46-
t.Errorf("expected 3 traffic granularities, but got %d", len(capabilities.Traffic.Granularities))
51+
if len(c.Traffic.Granularities) != 3 {
52+
t.Errorf("expected 3 traffic granularities, but got %d", len(c.Traffic.Granularities))
4753
}
4854
}
4955

@@ -53,16 +59,22 @@ func TestGetCapabilitiesHTTPError(t *testing.T) {
5359
testEnv := testutils.SetupTestEnv()
5460
defer testEnv.TearDownTestEnv()
5561
testEnv.NewTestResellV2Client()
56-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/capabilities",
57-
TestGetCapabilitiesRaw, http.MethodGet, http.StatusBadGateway, &endpointCalled, t)
62+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
63+
Mux: testEnv.Mux,
64+
URL: "/resell/v2/capabilities",
65+
RawResponse: TestGetCapabilitiesRaw,
66+
Method: http.MethodGet,
67+
Status: http.StatusBadGateway,
68+
CallFlag: &endpointCalled,
69+
})
5870

5971
ctx := context.Background()
60-
capabilities, httpResponse, err := capabilities.Get(ctx, testEnv.Client)
72+
c, httpResponse, err := capabilities.Get(ctx, testEnv.Client)
6173

6274
if !endpointCalled {
6375
t.Fatal("endpoint wasn't called")
6476
}
65-
if capabilities != nil {
77+
if c != nil {
6678
t.Fatal("expected no capabilities from the Get method")
6779
}
6880
if err == nil {
@@ -81,9 +93,9 @@ func TestGetCapabilitiesTimeoutError(t *testing.T) {
8193
testEnv.NewTestResellV2Client()
8294

8395
ctx := context.Background()
84-
capabilities, _, err := capabilities.Get(ctx, testEnv.Client)
96+
c, _, err := capabilities.Get(ctx, testEnv.Client)
8597

86-
if capabilities != nil {
98+
if c != nil {
8799
t.Fatal("expected no capabilities from the Get method")
88100
}
89101
if err == nil {
@@ -97,16 +109,22 @@ func TestGetCapabilitiesUnmarshalError(t *testing.T) {
97109
testEnv := testutils.SetupTestEnv()
98110
defer testEnv.TearDownTestEnv()
99111
testEnv.NewTestResellV2Client()
100-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/capabilities",
101-
TestGetCapabilitiesInvalidRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
112+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
113+
Mux: testEnv.Mux,
114+
URL: "/resell/v2/capabilities",
115+
RawResponse: TestGetCapabilitiesInvalidRaw,
116+
Method: http.MethodGet,
117+
Status: http.StatusOK,
118+
CallFlag: &endpointCalled,
119+
})
102120

103121
ctx := context.Background()
104-
capabilities, _, err := capabilities.Get(ctx, testEnv.Client)
122+
c, _, err := capabilities.Get(ctx, testEnv.Client)
105123

106124
if !endpointCalled {
107125
t.Fatal("endpoint wasn't called")
108126
}
109-
if capabilities != nil {
127+
if c != nil {
110128
t.Fatal("expected no capabilities from the Get method")
111129
}
112130
if err == nil {

selvpcclient/resell/v2/floatingips/requests.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"net/http"
78
"strings"
89

910
"github.com/selectel/go-selvpcclient/selvpcclient"
@@ -14,7 +15,7 @@ const resourceURL = "floatingips"
1415
// Get returns a single floating ip by its id.
1516
func Get(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*FloatingIP, *selvpcclient.ResponseResult, error) {
1617
url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/")
17-
responseResult, err := client.DoRequest(ctx, "GET", url, nil)
18+
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
1819
if err != nil {
1920
return nil, nil, err
2021
}
@@ -46,7 +47,7 @@ func List(ctx context.Context, client *selvpcclient.ServiceClient, opts ListOpts
4647
url = strings.Join([]string{url, queryParams}, "?")
4748
}
4849

49-
responseResult, err := client.DoRequest(ctx, "GET", url, nil)
50+
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
5051
if err != nil {
5152
return nil, nil, err
5253
}
@@ -75,7 +76,7 @@ func Create(ctx context.Context, client *selvpcclient.ServiceClient, projectID s
7576
}
7677

7778
url := strings.Join([]string{client.Endpoint, resourceURL, "projects", projectID}, "/")
78-
responseResult, err := client.DoRequest(ctx, "POST", url, bytes.NewReader(requestBody))
79+
responseResult, err := client.DoRequest(ctx, http.MethodPost, url, bytes.NewReader(requestBody))
7980
if err != nil {
8081
return nil, nil, err
8182
}
@@ -98,7 +99,7 @@ func Create(ctx context.Context, client *selvpcclient.ServiceClient, projectID s
9899
// Delete deletes a single floating ip by its id.
99100
func Delete(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*selvpcclient.ResponseResult, error) {
100101
url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/")
101-
responseResult, err := client.DoRequest(ctx, "DELETE", url, nil)
102+
responseResult, err := client.DoRequest(ctx, http.MethodDelete, url, nil)
102103
if err != nil {
103104
return nil, err
104105
}

selvpcclient/resell/v2/floatingips/testing/requests_test.go

+97-30
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ func TestGetFloatingIP(t *testing.T) {
1616
testEnv := testutils.SetupTestEnv()
1717
defer testEnv.TearDownTestEnv()
1818
testEnv.NewTestResellV2Client()
19-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
20-
TestGetFloatingIPResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
19+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
20+
Mux: testEnv.Mux,
21+
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
22+
RawResponse: TestGetFloatingIPResponseRaw,
23+
Method: http.MethodGet,
24+
Status: http.StatusOK,
25+
CallFlag: &endpointCalled,
26+
})
2127

2228
ctx := context.Background()
2329
actual, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
@@ -41,9 +47,14 @@ func TestGetFloatingIPHTTPError(t *testing.T) {
4147
testEnv := testutils.SetupTestEnv()
4248
defer testEnv.TearDownTestEnv()
4349
testEnv.NewTestResellV2Client()
44-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
45-
TestGetFloatingIPResponseRaw, http.MethodGet, http.StatusBadGateway,
46-
&endpointCalled, t)
50+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
51+
Mux: testEnv.Mux,
52+
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
53+
RawResponse: TestGetFloatingIPResponseRaw,
54+
Method: http.MethodGet,
55+
Status: http.StatusBadGateway,
56+
CallFlag: &endpointCalled,
57+
})
4758

4859
ctx := context.Background()
4960
floatingIP, httpResponse, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
@@ -86,9 +97,14 @@ func TestGetFloatingIPUnmarshalError(t *testing.T) {
8697
testEnv := testutils.SetupTestEnv()
8798
defer testEnv.TearDownTestEnv()
8899
testEnv.NewTestResellV2Client()
89-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
90-
TestSingleFloatingIPInvalidResponseRaw, http.MethodGet, http.StatusOK,
91-
&endpointCalled, t)
100+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
101+
Mux: testEnv.Mux,
102+
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
103+
RawResponse: TestSingleFloatingIPInvalidResponseRaw,
104+
Method: http.MethodGet,
105+
Status: http.StatusOK,
106+
CallFlag: &endpointCalled,
107+
})
92108

93109
ctx := context.Background()
94110
floatingIP, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
@@ -110,8 +126,14 @@ func TestListFloatingIPs(t *testing.T) {
110126
testEnv := testutils.SetupTestEnv()
111127
defer testEnv.TearDownTestEnv()
112128
testEnv.NewTestResellV2Client()
113-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
114-
TestListFloatingIPsResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
129+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
130+
Mux: testEnv.Mux,
131+
URL: "/resell/v2/floatingips",
132+
RawResponse: TestListFloatingIPsResponseRaw,
133+
Method: http.MethodGet,
134+
Status: http.StatusOK,
135+
CallFlag: &endpointCalled,
136+
})
115137

116138
ctx := context.Background()
117139
actual, _, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
@@ -140,8 +162,14 @@ func TestListFloatingIPsSingle(t *testing.T) {
140162
testEnv := testutils.SetupTestEnv()
141163
defer testEnv.TearDownTestEnv()
142164
testEnv.NewTestResellV2Client()
143-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
144-
TestListFloatingIPsSingleResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
165+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
166+
Mux: testEnv.Mux,
167+
URL: "/resell/v2/floatingips",
168+
RawResponse: TestListFloatingIPsSingleResponseRaw,
169+
Method: http.MethodGet,
170+
Status: http.StatusOK,
171+
CallFlag: &endpointCalled,
172+
})
145173

146174
ctx := context.Background()
147175
actual, _, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
@@ -165,9 +193,14 @@ func TestListFloatingIPsHTTPError(t *testing.T) {
165193
testEnv := testutils.SetupTestEnv()
166194
defer testEnv.TearDownTestEnv()
167195
testEnv.NewTestResellV2Client()
168-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
169-
TestListFloatingIPsResponseRaw, http.MethodGet, http.StatusBadGateway,
170-
&endpointCalled, t)
196+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
197+
Mux: testEnv.Mux,
198+
URL: "/resell/v2/floatingips",
199+
RawResponse: TestListFloatingIPsResponseRaw,
200+
Method: http.MethodGet,
201+
Status: http.StatusBadGateway,
202+
CallFlag: &endpointCalled,
203+
})
171204

172205
ctx := context.Background()
173206
allFloatingIPs, httpResponse, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
@@ -210,9 +243,14 @@ func TestListFloatingIPsUnmarshalError(t *testing.T) {
210243
testEnv := testutils.SetupTestEnv()
211244
defer testEnv.TearDownTestEnv()
212245
testEnv.NewTestResellV2Client()
213-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
214-
TestManyFloatingIPsInvalidResponseRaw, http.MethodGet, http.StatusOK,
215-
&endpointCalled, t)
246+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
247+
Mux: testEnv.Mux,
248+
URL: "/resell/v2/floatingips",
249+
RawResponse: TestManyFloatingIPsInvalidResponseRaw,
250+
Method: http.MethodGet,
251+
Status: http.StatusOK,
252+
CallFlag: &endpointCalled,
253+
})
216254

217255
ctx := context.Background()
218256
allFloatingIPs, _, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
@@ -234,9 +272,15 @@ func TestCreateFloatingIPs(t *testing.T) {
234272
testEnv := testutils.SetupTestEnv()
235273
defer testEnv.TearDownTestEnv()
236274
testEnv.NewTestResellV2Client()
237-
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
238-
TestCreateFloatingIPResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost, http.StatusOK,
239-
&endpointCalled, t)
275+
testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{
276+
Mux: testEnv.Mux,
277+
URL: "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
278+
RawResponse: TestCreateFloatingIPResponseRaw,
279+
RawRequest: TestCreateFloatingIPOptsRaw,
280+
Method: http.MethodPost,
281+
Status: http.StatusOK,
282+
CallFlag: &endpointCalled,
283+
})
240284

241285
ctx := context.Background()
242286
createOpts := TestCreateFloatingIPOpts
@@ -261,9 +305,15 @@ func TestCreateFloatingIPsHTTPError(t *testing.T) {
261305
testEnv := testutils.SetupTestEnv()
262306
defer testEnv.TearDownTestEnv()
263307
testEnv.NewTestResellV2Client()
264-
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
265-
TestCreateFloatingIPResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost,
266-
http.StatusBadRequest, &endpointCalled, t)
308+
testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{
309+
Mux: testEnv.Mux,
310+
URL: "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
311+
RawResponse: TestCreateFloatingIPResponseRaw,
312+
RawRequest: TestCreateFloatingIPOptsRaw,
313+
Method: http.MethodPost,
314+
Status: http.StatusBadRequest,
315+
CallFlag: &endpointCalled,
316+
})
267317

268318
ctx := context.Background()
269319
createOpts := TestCreateFloatingIPOpts
@@ -308,8 +358,15 @@ func TestCreateFloatingIPsUnmarshalError(t *testing.T) {
308358
testEnv := testutils.SetupTestEnv()
309359
defer testEnv.TearDownTestEnv()
310360
testEnv.NewTestResellV2Client()
311-
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
312-
TestManyFloatingIPsInvalidResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)
361+
testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{
362+
Mux: testEnv.Mux,
363+
URL: "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
364+
RawResponse: TestManyFloatingIPsInvalidResponseRaw,
365+
RawRequest: TestCreateFloatingIPOptsRaw,
366+
Method: http.MethodPost,
367+
Status: http.StatusOK,
368+
CallFlag: &endpointCalled,
369+
})
313370

314371
ctx := context.Background()
315372
createOpts := TestCreateFloatingIPOpts
@@ -332,8 +389,13 @@ func TestDeleteFloatingIP(t *testing.T) {
332389
testEnv := testutils.SetupTestEnv()
333390
defer testEnv.TearDownTestEnv()
334391
testEnv.NewTestResellV2Client()
335-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
336-
"", http.MethodDelete, http.StatusOK, &endpointCalled, t)
392+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
393+
Mux: testEnv.Mux,
394+
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
395+
Method: http.MethodDelete,
396+
Status: http.StatusOK,
397+
CallFlag: &endpointCalled,
398+
})
337399

338400
ctx := context.Background()
339401
_, err := floatingips.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
@@ -351,8 +413,13 @@ func TestDeleteFloatingIPHTTPError(t *testing.T) {
351413
testEnv := testutils.SetupTestEnv()
352414
defer testEnv.TearDownTestEnv()
353415
testEnv.NewTestResellV2Client()
354-
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
355-
"", http.MethodDelete, http.StatusBadGateway, &endpointCalled, t)
416+
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
417+
Mux: testEnv.Mux,
418+
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
419+
Method: http.MethodDelete,
420+
Status: http.StatusBadGateway,
421+
CallFlag: &endpointCalled,
422+
})
356423

357424
ctx := context.Background()
358425
httpResponse, err := floatingips.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")

0 commit comments

Comments
 (0)