github.com/gophercloud/gophercloud@v1.11.0/openstack/baremetal/v1/allocations/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gophercloud/gophercloud/openstack/baremetal/v1/allocations"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  	th "github.com/gophercloud/gophercloud/testhelper"
     9  	"github.com/gophercloud/gophercloud/testhelper/client"
    10  )
    11  
    12  func TestListAllocations(t *testing.T) {
    13  	th.SetupHTTP()
    14  	defer th.TeardownHTTP()
    15  	HandleAllocationListSuccessfully(t)
    16  
    17  	pages := 0
    18  	err := allocations.List(client.ServiceClient(), allocations.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    19  		pages++
    20  
    21  		actual, err := allocations.ExtractAllocations(page)
    22  		if err != nil {
    23  			return false, err
    24  		}
    25  
    26  		if len(actual) != 2 {
    27  			t.Fatalf("Expected 2 allocations, got %d", len(actual))
    28  		}
    29  		th.AssertEquals(t, "5344a3e2-978a-444e-990a-cbf47c62ef88", actual[0].UUID)
    30  		th.AssertEquals(t, "eff80f47-75f0-4d41-b1aa-cf07c201adac", actual[1].UUID)
    31  
    32  		return true, nil
    33  	})
    34  
    35  	th.AssertNoErr(t, err)
    36  
    37  	if pages != 1 {
    38  		t.Errorf("Expected 1 page, saw %d", pages)
    39  	}
    40  }
    41  
    42  func TestCreateAllocation(t *testing.T) {
    43  	th.SetupHTTP()
    44  	defer th.TeardownHTTP()
    45  	HandleAllocationCreationSuccessfully(t, SingleAllocationBody)
    46  
    47  	actual, err := allocations.Create(client.ServiceClient(), allocations.CreateOpts{
    48  		Name:           "allocation-1",
    49  		ResourceClass:  "baremetal",
    50  		CandidateNodes: []string{"344a3e2-978a-444e-990a-cbf47c62ef88"},
    51  		Traits:         []string{"foo"},
    52  	}).Extract()
    53  	th.AssertNoErr(t, err)
    54  
    55  	th.CheckDeepEquals(t, Allocation1, *actual)
    56  }
    57  
    58  func TestDeleteAllocation(t *testing.T) {
    59  	th.SetupHTTP()
    60  	defer th.TeardownHTTP()
    61  	HandleAllocationDeletionSuccessfully(t)
    62  
    63  	res := allocations.Delete(client.ServiceClient(), "344a3e2-978a-444e-990a-cbf47c62ef88")
    64  	th.AssertNoErr(t, res.Err)
    65  }
    66  
    67  func TestGetAllocation(t *testing.T) {
    68  	th.SetupHTTP()
    69  	defer th.TeardownHTTP()
    70  	HandleAllocationGetSuccessfully(t)
    71  
    72  	c := client.ServiceClient()
    73  	actual, err := allocations.Get(c, "344a3e2-978a-444e-990a-cbf47c62ef88").Extract()
    74  	if err != nil {
    75  		t.Fatalf("Unexpected Get error: %v", err)
    76  	}
    77  
    78  	th.CheckDeepEquals(t, Allocation1, *actual)
    79  }