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