Skip to content

Commit

Permalink
Merge 1a788d0 into e3cf7dc
Browse files Browse the repository at this point in the history
  • Loading branch information
ozerovandrei committed Sep 26, 2018
2 parents e3cf7dc + 1a788d0 commit cf10761
Show file tree
Hide file tree
Showing 27 changed files with 1,149 additions and 417 deletions.
3 changes: 2 additions & 1 deletion selvpcclient/resell/v2/capabilities/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package capabilities

import (
"context"
"net/http"
"strings"

"github.com/selectel/go-selvpcclient/selvpcclient"
Expand All @@ -12,7 +13,7 @@ const resourceURL = "capabilities"
// Get returns the domain capabilities.
func Get(ctx context.Context, client *selvpcclient.ServiceClient) (*Capabilities, *selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL}, "/")
responseResult, err := client.DoRequest(ctx, "GET", url, nil)
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
Expand Down
66 changes: 42 additions & 24 deletions selvpcclient/resell/v2/capabilities/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,41 @@ func TestGetCapabilities(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/capabilities",
TestGetCapabilitiesRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/capabilities",
RawResponse: TestGetCapabilitiesRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
capabilities, _, err := capabilities.Get(ctx, testEnv.Client)
c, _, err := capabilities.Get(ctx, testEnv.Client)
if err != nil {
t.Fatal(err)
}

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if capabilities == nil {
if c == nil {
t.Fatal("didn't get capabilities")
}
if len(capabilities.Licenses) != 2 {
t.Errorf("expected 2 licenses, but got %d", len(capabilities.Licenses))
if len(c.Licenses) != 2 {
t.Errorf("expected 2 licenses, but got %d", len(c.Licenses))
}
if len(capabilities.Regions) != 3 {
t.Errorf("expected 3 regions, but got %d", len(capabilities.Regions))
if len(c.Regions) != 3 {
t.Errorf("expected 3 regions, but got %d", len(c.Regions))
}
if len(capabilities.Resources) != 16 {
t.Errorf("expected 16 resources, but got %d", len(capabilities.Resources))
if len(c.Resources) != 16 {
t.Errorf("expected 16 resources, but got %d", len(c.Resources))
}
if len(capabilities.Subnets) != 1 {
t.Errorf("expected 1 subnets, but got %d", len(capabilities.Subnets))
if len(c.Subnets) != 1 {
t.Errorf("expected 1 subnets, but got %d", len(c.Subnets))
}
if len(capabilities.Traffic.Granularities) != 3 {
t.Errorf("expected 3 traffic granularities, but got %d", len(capabilities.Traffic.Granularities))
if len(c.Traffic.Granularities) != 3 {
t.Errorf("expected 3 traffic granularities, but got %d", len(c.Traffic.Granularities))
}
}

Expand All @@ -53,16 +59,22 @@ func TestGetCapabilitiesHTTPError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/capabilities",
TestGetCapabilitiesRaw, http.MethodGet, http.StatusBadGateway, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/capabilities",
RawResponse: TestGetCapabilitiesRaw,
Method: http.MethodGet,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

ctx := context.Background()
capabilities, httpResponse, err := capabilities.Get(ctx, testEnv.Client)
c, httpResponse, err := capabilities.Get(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if capabilities != nil {
if c != nil {
t.Fatal("expected no capabilities from the Get method")
}
if err == nil {
Expand All @@ -81,9 +93,9 @@ func TestGetCapabilitiesTimeoutError(t *testing.T) {
testEnv.NewTestResellV2Client()

ctx := context.Background()
capabilities, _, err := capabilities.Get(ctx, testEnv.Client)
c, _, err := capabilities.Get(ctx, testEnv.Client)

if capabilities != nil {
if c != nil {
t.Fatal("expected no capabilities from the Get method")
}
if err == nil {
Expand All @@ -97,16 +109,22 @@ func TestGetCapabilitiesUnmarshalError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/capabilities",
TestGetCapabilitiesInvalidRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/capabilities",
RawResponse: TestGetCapabilitiesInvalidRaw,
Method: http.MethodGet,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

ctx := context.Background()
capabilities, _, err := capabilities.Get(ctx, testEnv.Client)
c, _, err := capabilities.Get(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if capabilities != nil {
if c != nil {
t.Fatal("expected no capabilities from the Get method")
}
if err == nil {
Expand Down
9 changes: 5 additions & 4 deletions selvpcclient/resell/v2/floatingips/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"net/http"
"strings"

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

responseResult, err := client.DoRequest(ctx, "GET", url, nil)
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -75,7 +76,7 @@ func Create(ctx context.Context, client *selvpcclient.ServiceClient, projectID s
}

url := strings.Join([]string{client.Endpoint, resourceURL, "projects", projectID}, "/")
responseResult, err := client.DoRequest(ctx, "POST", url, bytes.NewReader(requestBody))
responseResult, err := client.DoRequest(ctx, http.MethodPost, url, bytes.NewReader(requestBody))
if err != nil {
return nil, nil, err
}
Expand All @@ -98,7 +99,7 @@ func Create(ctx context.Context, client *selvpcclient.ServiceClient, projectID s
// Delete deletes a single floating ip by its id.
func Delete(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/")
responseResult, err := client.DoRequest(ctx, "DELETE", url, nil)
responseResult, err := client.DoRequest(ctx, http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
Expand Down
127 changes: 97 additions & 30 deletions selvpcclient/resell/v2/floatingips/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ func TestGetFloatingIP(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
TestGetFloatingIPResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
RawResponse: TestGetFloatingIPResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
actual, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
Expand All @@ -41,9 +47,14 @@ func TestGetFloatingIPHTTPError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
TestGetFloatingIPResponseRaw, http.MethodGet, http.StatusBadGateway,
&endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
RawResponse: TestGetFloatingIPResponseRaw,
Method: http.MethodGet,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

ctx := context.Background()
floatingIP, httpResponse, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
Expand Down Expand Up @@ -86,9 +97,14 @@ func TestGetFloatingIPUnmarshalError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
TestSingleFloatingIPInvalidResponseRaw, http.MethodGet, http.StatusOK,
&endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
RawResponse: TestSingleFloatingIPInvalidResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
floatingIP, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
Expand All @@ -110,8 +126,14 @@ func TestListFloatingIPs(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestListFloatingIPsResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips",
RawResponse: TestListFloatingIPsResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
actual, _, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
Expand Down Expand Up @@ -140,8 +162,14 @@ func TestListFloatingIPsSingle(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestListFloatingIPsSingleResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips",
RawResponse: TestListFloatingIPsSingleResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
actual, _, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
Expand All @@ -165,9 +193,14 @@ func TestListFloatingIPsHTTPError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestListFloatingIPsResponseRaw, http.MethodGet, http.StatusBadGateway,
&endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips",
RawResponse: TestListFloatingIPsResponseRaw,
Method: http.MethodGet,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

ctx := context.Background()
allFloatingIPs, httpResponse, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
Expand Down Expand Up @@ -210,9 +243,14 @@ func TestListFloatingIPsUnmarshalError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestManyFloatingIPsInvalidResponseRaw, http.MethodGet, http.StatusOK,
&endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips",
RawResponse: TestManyFloatingIPsInvalidResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
allFloatingIPs, _, err := floatingips.List(ctx, testEnv.Client, floatingips.ListOpts{})
Expand All @@ -234,9 +272,15 @@ func TestCreateFloatingIPs(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
TestCreateFloatingIPResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost, http.StatusOK,
&endpointCalled, t)
testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
RawResponse: TestCreateFloatingIPResponseRaw,
RawRequest: TestCreateFloatingIPOptsRaw,
Method: http.MethodPost,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
createOpts := TestCreateFloatingIPOpts
Expand All @@ -261,9 +305,15 @@ func TestCreateFloatingIPsHTTPError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
TestCreateFloatingIPResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost,
http.StatusBadRequest, &endpointCalled, t)
testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
RawResponse: TestCreateFloatingIPResponseRaw,
RawRequest: TestCreateFloatingIPOptsRaw,
Method: http.MethodPost,
Status: http.StatusBadRequest,
CallFlag: &endpointCalled,
})

ctx := context.Background()
createOpts := TestCreateFloatingIPOpts
Expand Down Expand Up @@ -308,8 +358,15 @@ func TestCreateFloatingIPsUnmarshalError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
TestManyFloatingIPsInvalidResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost, http.StatusOK, &endpointCalled, t)
testutils.HandleReqWithBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317",
RawResponse: TestManyFloatingIPsInvalidResponseRaw,
RawRequest: TestCreateFloatingIPOptsRaw,
Method: http.MethodPost,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
createOpts := TestCreateFloatingIPOpts
Expand All @@ -332,8 +389,13 @@ func TestDeleteFloatingIP(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
"", http.MethodDelete, http.StatusOK, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
Method: http.MethodDelete,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
_, err := floatingips.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")
Expand All @@ -351,8 +413,13 @@ func TestDeleteFloatingIPHTTPError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
"", http.MethodDelete, http.StatusBadGateway, &endpointCalled, t)
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd",
Method: http.MethodDelete,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

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

0 comments on commit cf10761

Please sign in to comment.