github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/portsbinding/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 "time" 6 7 fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" 8 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsbinding" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" 10 th "github.com/gophercloud/gophercloud/testhelper" 11 ) 12 13 func TestList(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 HandleListSuccessfully(t) 18 19 type PortWithExt struct { 20 ports.Port 21 portsbinding.PortsBindingExt 22 } 23 var actual []PortWithExt 24 25 expected := []PortWithExt{ 26 { 27 Port: ports.Port{ 28 Status: "ACTIVE", 29 Name: "", 30 AdminStateUp: true, 31 NetworkID: "70c1db1f-b701-45bd-96e0-a313ee3430b3", 32 TenantID: "", 33 DeviceOwner: "network:router_gateway", 34 MACAddress: "fa:16:3e:58:42:ed", 35 FixedIPs: []ports.IP{ 36 { 37 SubnetID: "008ba151-0b8c-4a67-98b5-0d2b87666062", 38 IPAddress: "172.24.4.2", 39 }, 40 }, 41 ID: "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b", 42 SecurityGroups: []string{}, 43 DeviceID: "9ae135f4-b6e0-4dad-9e91-3c223e385824", 44 CreatedAt: time.Date(2019, time.June, 30, 4, 15, 37, 0, time.UTC), 45 UpdatedAt: time.Date(2019, time.June, 30, 5, 18, 49, 0, time.UTC), 46 }, 47 PortsBindingExt: portsbinding.PortsBindingExt{ 48 VNICType: "normal", 49 HostID: "devstack", 50 }, 51 }, 52 } 53 54 allPages, err := ports.List(fake.ServiceClient(), ports.ListOpts{}).AllPages() 55 th.AssertNoErr(t, err) 56 57 err = ports.ExtractPortsInto(allPages, &actual) 58 th.AssertNoErr(t, err) 59 60 th.CheckDeepEquals(t, expected, actual) 61 } 62 63 func TestGet(t *testing.T) { 64 th.SetupHTTP() 65 defer th.TeardownHTTP() 66 67 HandleGet(t) 68 69 var s struct { 70 ports.Port 71 portsbinding.PortsBindingExt 72 } 73 74 err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").ExtractInto(&s) 75 th.AssertNoErr(t, err) 76 77 th.AssertEquals(t, s.Status, "ACTIVE") 78 th.AssertEquals(t, s.Name, "") 79 th.AssertEquals(t, s.AdminStateUp, true) 80 th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 81 th.AssertEquals(t, s.TenantID, "7e02058126cc4950b75f9970368ba177") 82 th.AssertEquals(t, s.DeviceOwner, "network:router_interface") 83 th.AssertEquals(t, s.MACAddress, "fa:16:3e:23:fd:d7") 84 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 85 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.1"}, 86 }) 87 th.AssertEquals(t, s.ID, "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2") 88 th.AssertDeepEquals(t, s.SecurityGroups, []string{}) 89 th.AssertEquals(t, s.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e") 90 91 th.AssertEquals(t, s.HostID, "devstack") 92 th.AssertEquals(t, s.VNICType, "normal") 93 th.AssertEquals(t, s.VIFType, "ovs") 94 th.AssertDeepEquals(t, s.VIFDetails, map[string]interface{}{"port_filter": true, "ovs_hybrid_plug": true}) 95 } 96 97 func TestCreate(t *testing.T) { 98 th.SetupHTTP() 99 defer th.TeardownHTTP() 100 101 HandleCreate(t) 102 103 var s struct { 104 ports.Port 105 portsbinding.PortsBindingExt 106 } 107 108 asu := true 109 portCreateOpts := ports.CreateOpts{ 110 Name: "private-port", 111 AdminStateUp: &asu, 112 NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", 113 FixedIPs: []ports.IP{ 114 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 115 }, 116 SecurityGroups: &[]string{"foo"}, 117 } 118 119 createOpts := portsbinding.CreateOptsExt{ 120 CreateOptsBuilder: portCreateOpts, 121 HostID: "HOST1", 122 VNICType: "normal", 123 } 124 125 err := ports.Create(fake.ServiceClient(), createOpts).ExtractInto(&s) 126 th.AssertNoErr(t, err) 127 128 th.AssertEquals(t, s.Status, "DOWN") 129 th.AssertEquals(t, s.Name, "private-port") 130 th.AssertEquals(t, s.AdminStateUp, true) 131 th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 132 th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 133 th.AssertEquals(t, s.DeviceOwner, "") 134 th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0") 135 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 136 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 137 }) 138 th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 139 th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 140 th.AssertEquals(t, s.HostID, "HOST1") 141 th.AssertEquals(t, s.VNICType, "normal") 142 } 143 144 func TestRequiredCreateOpts(t *testing.T) { 145 res := ports.Create(fake.ServiceClient(), portsbinding.CreateOptsExt{CreateOptsBuilder: ports.CreateOpts{}}) 146 if res.Err == nil { 147 t.Fatalf("Expected error, got none") 148 } 149 } 150 151 func TestUpdate(t *testing.T) { 152 th.SetupHTTP() 153 defer th.TeardownHTTP() 154 155 HandleUpdate(t) 156 157 var s struct { 158 ports.Port 159 portsbinding.PortsBindingExt 160 } 161 162 name := "new_port_name" 163 portUpdateOpts := ports.UpdateOpts{ 164 Name: &name, 165 FixedIPs: []ports.IP{ 166 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 167 }, 168 SecurityGroups: &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}, 169 } 170 171 hostID := "HOST1" 172 updateOpts := portsbinding.UpdateOptsExt{ 173 UpdateOptsBuilder: portUpdateOpts, 174 HostID: &hostID, 175 VNICType: "normal", 176 } 177 178 err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&s) 179 th.AssertNoErr(t, err) 180 181 th.AssertEquals(t, s.Name, "new_port_name") 182 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 183 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 184 }) 185 th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 186 th.AssertEquals(t, s.HostID, "HOST1") 187 th.AssertEquals(t, s.VNICType, "normal") 188 }