github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/flavorprofiles/testing/requests_test.go (about)

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