github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/flavorprofiles/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/loadbalancer/v2/flavorprofiles"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     9  
    10  	fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/loadbalancer/v2/testhelper"
    11  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    12  )
    13  
    14  func TestListFlavorProfiles(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  	HandleFlavorProfileListSuccessfully(t)
    18  
    19  	pages := 0
    20  	err := flavorprofiles.List(fake.ServiceClient(), flavorprofiles.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    21  		pages++
    22  
    23  		actual, err := flavorprofiles.ExtractFlavorProfiles(page)
    24  		if err != nil {
    25  			return false, err
    26  		}
    27  
    28  		if len(actual) != 2 {
    29  			t.Fatalf("Expected 2 flavors, got %d", len(actual))
    30  		}
    31  		th.CheckDeepEquals(t, FlavorProfileSingle, actual[0])
    32  		th.CheckDeepEquals(t, FlavorProfileAct, actual[1])
    33  
    34  		return true, nil
    35  	})
    36  
    37  	th.AssertNoErr(t, err)
    38  
    39  	if pages != 1 {
    40  		t.Errorf("Expected 1 page, saw %d", pages)
    41  	}
    42  }
    43  
    44  func TestListAllFlavorProfiles(t *testing.T) {
    45  	th.SetupHTTP()
    46  	defer th.TeardownHTTP()
    47  	HandleFlavorProfileListSuccessfully(t)
    48  
    49  	allPages, err := flavorprofiles.List(fake.ServiceClient(), flavorprofiles.ListOpts{}).AllPages(context.TODO())
    50  	th.AssertNoErr(t, err)
    51  	actual, err := flavorprofiles.ExtractFlavorProfiles(allPages)
    52  	th.AssertNoErr(t, err)
    53  	th.CheckDeepEquals(t, FlavorProfileSingle, actual[0])
    54  	th.CheckDeepEquals(t, FlavorProfileAct, actual[1])
    55  }
    56  
    57  func TestCreateFlavorProfile(t *testing.T) {
    58  	th.SetupHTTP()
    59  	defer th.TeardownHTTP()
    60  	HandleFlavorProfileCreationSuccessfully(t, SingleFlavorProfileBody)
    61  
    62  	actual, err := flavorprofiles.Create(context.TODO(), fake.ServiceClient(), flavorprofiles.CreateOpts{
    63  		Name:         "amphora-test",
    64  		ProviderName: "amphora",
    65  		FlavorData:   "{\"loadbalancer_topology\": \"ACTIVE_STANDBY\"}",
    66  	}).Extract()
    67  	th.AssertNoErr(t, err)
    68  
    69  	th.CheckDeepEquals(t, FlavorDb, *actual)
    70  }
    71  
    72  func TestRequiredCreateOpts(t *testing.T) {
    73  	res := flavorprofiles.Create(context.TODO(), fake.ServiceClient(), flavorprofiles.CreateOpts{})
    74  	if res.Err == nil {
    75  		t.Fatalf("Expected error, got none")
    76  	}
    77  }
    78  
    79  func TestGetFlavorProfiles(t *testing.T) {
    80  	th.SetupHTTP()
    81  	defer th.TeardownHTTP()
    82  	HandleFlavorProfileGetSuccessfully(t)
    83  
    84  	client := fake.ServiceClient()
    85  	actual, err := flavorprofiles.Get(context.TODO(), client, "dcd65be5-f117-4260-ab3d-b32cc5bd1272").Extract()
    86  	if err != nil {
    87  		t.Fatalf("Unexpected Get error: %v", err)
    88  	}
    89  
    90  	th.CheckDeepEquals(t, FlavorDb, *actual)
    91  }
    92  
    93  func TestDeleteFlavorProfile(t *testing.T) {
    94  	th.SetupHTTP()
    95  	defer th.TeardownHTTP()
    96  	HandleFlavorProfileDeletionSuccessfully(t)
    97  
    98  	res := flavorprofiles.Delete(context.TODO(), fake.ServiceClient(), "dcd65be5-f117-4260-ab3d-b32cc5bd1272")
    99  	th.AssertNoErr(t, res.Err)
   100  }
   101  
   102  func TestUpdateFlavorProfile(t *testing.T) {
   103  	th.SetupHTTP()
   104  	defer th.TeardownHTTP()
   105  	HandleFlavorProfileUpdateSuccessfully(t)
   106  
   107  	client := fake.ServiceClient()
   108  	actual, err := flavorprofiles.Update(context.TODO(), client, "dcd65be5-f117-4260-ab3d-b32cc5bd1272", flavorprofiles.UpdateOpts{
   109  		Name:         "amphora-test-updated",
   110  		ProviderName: "amphora",
   111  		FlavorData:   "{\"loadbalancer_topology\": \"SINGLE\"}",
   112  	}).Extract()
   113  	if err != nil {
   114  		t.Fatalf("Unexpected Update error: %v", err)
   115  	}
   116  
   117  	th.CheckDeepEquals(t, FlavorUpdated, *actual)
   118  }