Skip to content

Commit

Permalink
Resell V2 floatingips - add more tests (#62)
Browse files Browse the repository at this point in the history
Add tests with error cases.
  • Loading branch information
ozerovandrei committed Apr 19, 2018
1 parent 75553ed commit 1939237
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 0 deletions.
20 changes: 20 additions & 0 deletions selvpcclient/resell/v2/floatingips/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,23 @@ var TestCreateFloatingIPResponse = []*floatingips.FloatingIP{
Status: "DOWN",
},
}

// TestManyFloatingIPsInvalidResponseRaw represents a raw invalid response with several floating ips.
const TestManyFloatingIPsInvalidResponseRaw = `
{
"floatingips": [
{
"id": 123
}
]
}
`

// TestSingleFloatingIPInvalidResponseRaw represents a raw invalid response with a single floating ip.
const TestSingleFloatingIPInvalidResponseRaw = `
{
"floatingip": {
"id": 123
}
}
`
246 changes: 246 additions & 0 deletions selvpcclient/resell/v2/floatingips/testing/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,75 @@ func TestGetFloatingIP(t *testing.T) {
}
}

func TestGetFloatingIPHTTPError(t *testing.T) {
endpointCalled := false

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)

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

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if floatingIP != nil {
t.Fatal("expected no floating ip from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadGateway, httpResponse.StatusCode)
}
}

func TestGetFloatingIPTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
floatingIP, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")

if floatingIP != nil {
t.Fatal("expected no floating ip from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
}

func TestGetFloatingIPUnmarshalError(t *testing.T) {
endpointCalled := false

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)

ctx := context.Background()
floatingIP, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if floatingIP != nil {
t.Fatal("expected no floating ip from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestListFloatingIPs(t *testing.T) {
endpointCalled := false

Expand Down Expand Up @@ -90,6 +159,75 @@ func TestListFloatingIPsSingle(t *testing.T) {
}
}

func TestListFloatingIPsHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestListFloatingIPsResponseRaw, http.MethodGet, http.StatusBadGateway,
&endpointCalled, t)

ctx := context.Background()
allFloatingIPs, httpResponse, err := floatingips.List(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if allFloatingIPs != nil {
t.Fatal("expected no floating ips from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadGateway, httpResponse.StatusCode)
}
}

func TestListFloatingIPsTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
allFloatingIPs, _, err := floatingips.List(ctx, testEnv.Client)

if allFloatingIPs != nil {
t.Fatal("expected no floating ips from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestListFloatingIPsUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips",
TestManyFloatingIPsInvalidResponseRaw, http.MethodGet, http.StatusOK,
&endpointCalled, t)

ctx := context.Background()
allFloatingIPs, _, err := floatingips.List(ctx, testEnv.Client)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if allFloatingIPs != nil {
t.Fatal("expected no floating ips from the List method")
}
if err == nil {
t.Fatal("expected error from the List method")
}
}

func TestCreateFloatingIPs(t *testing.T) {
endpointCalled := false

Expand Down Expand Up @@ -117,6 +255,77 @@ func TestCreateFloatingIPs(t *testing.T) {
}
}

func TestCreateFloatingIPsHTTPError(t *testing.T) {
endpointCalled := false

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)

ctx := context.Background()
createOpts := TestCreateFloatingIPOpts
floatingIPs, httpResponse, err := floatingips.Create(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317", createOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if floatingIPs != nil {
t.Fatal("expected no floating ips from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
if httpResponse.StatusCode != http.StatusBadRequest {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestCreateFloatingIPsTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
createOpts := TestCreateFloatingIPOpts
floatingIPs, _, err := floatingips.Create(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317", createOpts)

if floatingIPs != nil {
t.Fatal("expected no floating ips from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
}

func TestCreateFloatingIPsUnmarshalError(t *testing.T) {
endpointCalled := false

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)

ctx := context.Background()
createOpts := TestCreateFloatingIPOpts
floatingIPs, _, err := floatingips.Create(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317", createOpts)

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if floatingIPs != nil {
t.Fatal("expected no floating ips from the Create method")
}
if err == nil {
t.Fatal("expected error from the Create method")
}
}

func TestDeleteFloatingIP(t *testing.T) {
endpointCalled := false

Expand All @@ -135,3 +344,40 @@ func TestDeleteFloatingIP(t *testing.T) {
t.Fatal("endpoint wasn't called")
}
}

func TestDeleteFloatingIPHTTPError(t *testing.T) {
endpointCalled := false

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)

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

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if err == nil {
t.Fatal("expected error from the Delete method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode)
}
}

func TestDeleteFloatingIPTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

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

if err == nil {
t.Fatal("expected error from the Delete method")
}
}

0 comments on commit 1939237

Please sign in to comment.