github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/baremetal/v1/ports/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/ports" 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 TestListDetailPorts(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 HandlePortListDetailSuccessfully(t) 17 18 pages := 0 19 err := ports.ListDetail(client.ServiceClient(), ports.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 20 pages++ 21 22 actual, err := ports.ExtractPorts(page) 23 if err != nil { 24 return false, err 25 } 26 27 if len(actual) != 2 { 28 t.Fatalf("Expected 2 ports, got %d", len(actual)) 29 } 30 th.CheckDeepEquals(t, PortBar, actual[0]) 31 th.CheckDeepEquals(t, PortFoo, 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 TestListPorts(t *testing.T) { 44 th.SetupHTTP() 45 defer th.TeardownHTTP() 46 HandlePortListSuccessfully(t) 47 48 pages := 0 49 err := ports.List(client.ServiceClient(), ports.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 50 pages++ 51 52 actual, err := ports.ExtractPorts(page) 53 if err != nil { 54 return false, err 55 } 56 57 if len(actual) != 2 { 58 t.Fatalf("Expected 2 ports, got %d", len(actual)) 59 } 60 th.AssertEquals(t, "3abe3f36-9708-4e9f-b07e-0f898061d3a7", actual[0].UUID) 61 th.AssertEquals(t, "f2845e11-dbd4-4728-a8c0-30d19f48924a", actual[1].UUID) 62 63 return true, nil 64 }) 65 66 th.AssertNoErr(t, err) 67 68 if pages != 1 { 69 t.Errorf("Expected 1 page, saw %d", pages) 70 } 71 } 72 73 func TestListOpts(t *testing.T) { 74 // Detail cannot take Fields 75 opts := ports.ListOpts{ 76 Fields: []string{"uuid", "address"}, 77 } 78 79 _, err := opts.ToPortListDetailQuery() 80 th.AssertEquals(t, err.Error(), "fields is not a valid option when getting a detailed listing of ports") 81 82 // Regular ListOpts can 83 query, err := opts.ToPortListQuery() 84 th.AssertEquals(t, "?fields=uuid%2Caddress", query) 85 th.AssertNoErr(t, err) 86 } 87 88 func TestCreatePort(t *testing.T) { 89 th.SetupHTTP() 90 defer th.TeardownHTTP() 91 HandlePortCreationSuccessfully(t, SinglePortBody) 92 93 iTrue := true 94 actual, err := ports.Create(context.TODO(), client.ServiceClient(), ports.CreateOpts{ 95 NodeUUID: "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086", 96 Address: "52:54:00:4d:87:e6", 97 PXEEnabled: &iTrue, 98 }).Extract() 99 th.AssertNoErr(t, err) 100 101 th.CheckDeepEquals(t, PortFoo, *actual) 102 } 103 104 func TestDeletePort(t *testing.T) { 105 th.SetupHTTP() 106 defer th.TeardownHTTP() 107 HandlePortDeletionSuccessfully(t) 108 109 res := ports.Delete(context.TODO(), client.ServiceClient(), "3abe3f36-9708-4e9f-b07e-0f898061d3a7") 110 th.AssertNoErr(t, res.Err) 111 } 112 113 func TestGetPort(t *testing.T) { 114 th.SetupHTTP() 115 defer th.TeardownHTTP() 116 HandlePortGetSuccessfully(t) 117 118 c := client.ServiceClient() 119 actual, err := ports.Get(context.TODO(), c, "f2845e11-dbd4-4728-a8c0-30d19f48924a").Extract() 120 if err != nil { 121 t.Fatalf("Unexpected Get error: %v", err) 122 } 123 124 th.CheckDeepEquals(t, PortFoo, *actual) 125 } 126 127 func TestUpdatePort(t *testing.T) { 128 th.SetupHTTP() 129 defer th.TeardownHTTP() 130 HandlePortUpdateSuccessfully(t, SinglePortBody) 131 132 c := client.ServiceClient() 133 actual, err := ports.Update(context.TODO(), c, "f2845e11-dbd4-4728-a8c0-30d19f48924a", ports.UpdateOpts{ 134 ports.UpdateOperation{ 135 Op: ports.ReplaceOp, 136 Path: "/address", 137 Value: "22:22:22:22:22:22", 138 }, 139 }).Extract() 140 if err != nil { 141 t.Fatalf("Unexpected Update error: %v", err) 142 } 143 144 th.CheckDeepEquals(t, PortFoo, *actual) 145 }