From 193923756dc73a13f87de6a5e8fcc0def0e59534 Mon Sep 17 00:00:00 2001 From: Andrei Ozerov Date: Thu, 19 Apr 2018 19:07:06 +0300 Subject: [PATCH] Resell V2 floatingips - add more tests (#62) Add tests with error cases. --- .../resell/v2/floatingips/testing/fixtures.go | 20 ++ .../v2/floatingips/testing/request_test.go | 246 ++++++++++++++++++ 2 files changed, 266 insertions(+) diff --git a/selvpcclient/resell/v2/floatingips/testing/fixtures.go b/selvpcclient/resell/v2/floatingips/testing/fixtures.go index 3a2f94b..58c0e1c 100644 --- a/selvpcclient/resell/v2/floatingips/testing/fixtures.go +++ b/selvpcclient/resell/v2/floatingips/testing/fixtures.go @@ -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 + } +} +` diff --git a/selvpcclient/resell/v2/floatingips/testing/request_test.go b/selvpcclient/resell/v2/floatingips/testing/request_test.go index f9ae485..7eb6556 100644 --- a/selvpcclient/resell/v2/floatingips/testing/request_test.go +++ b/selvpcclient/resell/v2/floatingips/testing/request_test.go @@ -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 @@ -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 @@ -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 @@ -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") + } +}