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

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/flavors"
     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 TestListFlavors(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleFlavorListSuccessfully(t)
    17  
    18  	pages := 0
    19  	err := flavors.List(fake.ServiceClient(), flavors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    20  		pages++
    21  
    22  		actual, err := flavors.ExtractFlavors(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, FlavorBasic, actual[0])
    31  		th.CheckDeepEquals(t, FlavorAdvance, 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 TestListAllFlavors(t *testing.T) {
    44  	th.SetupHTTP()
    45  	defer th.TeardownHTTP()
    46  	HandleFlavorListSuccessfully(t)
    47  
    48  	allPages, err := flavors.List(fake.ServiceClient(), flavors.ListOpts{}).AllPages()
    49  	th.AssertNoErr(t, err)
    50  	actual, err := flavors.ExtractFlavors(allPages)
    51  	th.AssertNoErr(t, err)
    52  	th.CheckDeepEquals(t, FlavorBasic, actual[0])
    53  	th.CheckDeepEquals(t, FlavorAdvance, actual[1])
    54  }
    55  
    56  func TestCreateFlavor(t *testing.T) {
    57  	th.SetupHTTP()
    58  	defer th.TeardownHTTP()
    59  	HandleFlavorCreationSuccessfully(t, SingleFlavorBody)
    60  
    61  	actual, err := flavors.Create(fake.ServiceClient(), flavors.CreateOpts{
    62  		Name:            "Basic",
    63  		Description:     "A basic standalone Octavia load balancer.",
    64  		Enabled:         true,
    65  		FlavorProfileId: "9daa2768-74e7-4d13-bf5d-1b8e0dc239e1",
    66  	}).Extract()
    67  	th.AssertNoErr(t, err)
    68  
    69  	th.CheckDeepEquals(t, FlavorDb, *actual)
    70  }
    71  
    72  func TestRequiredCreateOpts(t *testing.T) {
    73  	res := flavors.Create(fake.ServiceClient(), flavors.CreateOpts{})
    74  	if res.Err == nil {
    75  		t.Fatalf("Expected error, got none")
    76  	}
    77  }
    78  
    79  func TestGetFlavor(t *testing.T) {
    80  	th.SetupHTTP()
    81  	defer th.TeardownHTTP()
    82  	HandleFlavorGetSuccessfully(t)
    83  
    84  	client := fake.ServiceClient()
    85  	actual, err := flavors.Get(client, "5548c807-e6e8-43d7-9ea4-b38d34dd74a0").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 TestDeleteFlavor(t *testing.T) {
    94  	th.SetupHTTP()
    95  	defer th.TeardownHTTP()
    96  	HandleFlavorDeletionSuccessfully(t)
    97  
    98  	res := flavors.Delete(fake.ServiceClient(), "5548c807-e6e8-43d7-9ea4-b38d34dd74a0")
    99  	th.AssertNoErr(t, res.Err)
   100  }
   101  
   102  func TestUpdateFlavor(t *testing.T) {
   103  	th.SetupHTTP()
   104  	defer th.TeardownHTTP()
   105  	HandleFlavorUpdateSuccessfully(t)
   106  
   107  	client := fake.ServiceClient()
   108  	actual, err := flavors.Update(client, "5548c807-e6e8-43d7-9ea4-b38d34dd74a0", flavors.UpdateOpts{
   109  		Name:        "Basic v2",
   110  		Description: "Rename flavor",
   111  		Enabled:     true,
   112  	}).Extract()
   113  	if err != nil {
   114  		t.Fatalf("Unexpected Update error: %v", err)
   115  	}
   116  
   117  	th.CheckDeepEquals(t, FlavorUpdated, *actual)
   118  }