PageRenderTime 239ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2/ports_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 349 lines | 259 code | 60 blank | 30 comment | 107 complexity | a23ad2835263ac733a38e29155c43fae MD5 | raw file
  1. // +build acceptance networking
  2. package v2
  3. import (
  4. "testing"
  5. "github.com/gophercloud/gophercloud/acceptance/clients"
  6. extensions "github.com/gophercloud/gophercloud/acceptance/openstack/networking/v2/extensions"
  7. "github.com/gophercloud/gophercloud/acceptance/tools"
  8. "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
  9. )
  10. func TestPortsList(t *testing.T) {
  11. client, err := clients.NewNetworkV2Client()
  12. if err != nil {
  13. t.Fatalf("Unable to create a network client: %v", err)
  14. }
  15. allPages, err := ports.List(client, nil).AllPages()
  16. if err != nil {
  17. t.Fatalf("Unable to list ports: %v", err)
  18. }
  19. allPorts, err := ports.ExtractPorts(allPages)
  20. if err != nil {
  21. t.Fatalf("Unable to extract ports: %v", err)
  22. }
  23. for _, port := range allPorts {
  24. tools.PrintResource(t, port)
  25. }
  26. }
  27. func TestPortsCRUD(t *testing.T) {
  28. client, err := clients.NewNetworkV2Client()
  29. if err != nil {
  30. t.Fatalf("Unable to create a network client: %v", err)
  31. }
  32. // Create Network
  33. network, err := CreateNetwork(t, client)
  34. if err != nil {
  35. t.Fatalf("Unable to create network: %v", err)
  36. }
  37. defer DeleteNetwork(t, client, network.ID)
  38. // Create Subnet
  39. subnet, err := CreateSubnet(t, client, network.ID)
  40. if err != nil {
  41. t.Fatalf("Unable to create subnet: %v", err)
  42. }
  43. defer DeleteSubnet(t, client, subnet.ID)
  44. // Create port
  45. port, err := CreatePort(t, client, network.ID, subnet.ID)
  46. if err != nil {
  47. t.Fatalf("Unable to create port: %v", err)
  48. }
  49. defer DeletePort(t, client, port.ID)
  50. if len(port.SecurityGroups) != 1 {
  51. t.Logf("WARNING: Port did not have a default security group applied")
  52. }
  53. tools.PrintResource(t, port)
  54. // Update port
  55. newPortName := tools.RandomString("TESTACC-", 8)
  56. updateOpts := ports.UpdateOpts{
  57. Name: newPortName,
  58. }
  59. newPort, err := ports.Update(client, port.ID, updateOpts).Extract()
  60. if err != nil {
  61. t.Fatalf("Could not update port: %v", err)
  62. }
  63. tools.PrintResource(t, newPort)
  64. }
  65. func TestPortsRemoveSecurityGroups(t *testing.T) {
  66. client, err := clients.NewNetworkV2Client()
  67. if err != nil {
  68. t.Fatalf("Unable to create a network client: %v", err)
  69. }
  70. // Create Network
  71. network, err := CreateNetwork(t, client)
  72. if err != nil {
  73. t.Fatalf("Unable to create network: %v", err)
  74. }
  75. defer DeleteNetwork(t, client, network.ID)
  76. // Create Subnet
  77. subnet, err := CreateSubnet(t, client, network.ID)
  78. if err != nil {
  79. t.Fatalf("Unable to create subnet: %v", err)
  80. }
  81. defer DeleteSubnet(t, client, subnet.ID)
  82. // Create port
  83. port, err := CreatePort(t, client, network.ID, subnet.ID)
  84. if err != nil {
  85. t.Fatalf("Unable to create port: %v", err)
  86. }
  87. defer DeletePort(t, client, port.ID)
  88. tools.PrintResource(t, port)
  89. // Create a Security Group
  90. group, err := extensions.CreateSecurityGroup(t, client)
  91. if err != nil {
  92. t.Fatalf("Unable to create security group: %v", err)
  93. }
  94. defer extensions.DeleteSecurityGroup(t, client, group.ID)
  95. // Add the group to the port
  96. updateOpts := ports.UpdateOpts{
  97. SecurityGroups: &[]string{group.ID},
  98. }
  99. newPort, err := ports.Update(client, port.ID, updateOpts).Extract()
  100. if err != nil {
  101. t.Fatalf("Could not update port: %v", err)
  102. }
  103. // Remove the group
  104. updateOpts = ports.UpdateOpts{
  105. SecurityGroups: &[]string{},
  106. }
  107. newPort, err = ports.Update(client, port.ID, updateOpts).Extract()
  108. if err != nil {
  109. t.Fatalf("Could not update port: %v", err)
  110. }
  111. tools.PrintResource(t, newPort)
  112. if len(newPort.SecurityGroups) > 0 {
  113. t.Fatalf("Unable to remove security group from port")
  114. }
  115. }
  116. func TestPortsDontAlterSecurityGroups(t *testing.T) {
  117. client, err := clients.NewNetworkV2Client()
  118. if err != nil {
  119. t.Fatalf("Unable to create a network client: %v", err)
  120. }
  121. // Create Network
  122. network, err := CreateNetwork(t, client)
  123. if err != nil {
  124. t.Fatalf("Unable to create network: %v", err)
  125. }
  126. defer DeleteNetwork(t, client, network.ID)
  127. // Create Subnet
  128. subnet, err := CreateSubnet(t, client, network.ID)
  129. if err != nil {
  130. t.Fatalf("Unable to create subnet: %v", err)
  131. }
  132. defer DeleteSubnet(t, client, subnet.ID)
  133. // Create a Security Group
  134. group, err := extensions.CreateSecurityGroup(t, client)
  135. if err != nil {
  136. t.Fatalf("Unable to create security group: %v", err)
  137. }
  138. defer extensions.DeleteSecurityGroup(t, client, group.ID)
  139. // Create port
  140. port, err := CreatePort(t, client, network.ID, subnet.ID)
  141. if err != nil {
  142. t.Fatalf("Unable to create port: %v", err)
  143. }
  144. defer DeletePort(t, client, port.ID)
  145. tools.PrintResource(t, port)
  146. // Add the group to the port
  147. updateOpts := ports.UpdateOpts{
  148. SecurityGroups: &[]string{group.ID},
  149. }
  150. newPort, err := ports.Update(client, port.ID, updateOpts).Extract()
  151. if err != nil {
  152. t.Fatalf("Could not update port: %v", err)
  153. }
  154. // Update the port again
  155. updateOpts = ports.UpdateOpts{
  156. Name: "some_port",
  157. }
  158. newPort, err = ports.Update(client, port.ID, updateOpts).Extract()
  159. if err != nil {
  160. t.Fatalf("Could not update port: %v", err)
  161. }
  162. tools.PrintResource(t, newPort)
  163. if len(newPort.SecurityGroups) == 0 {
  164. t.Fatalf("Port had security group updated")
  165. }
  166. }
  167. func TestPortsWithNoSecurityGroup(t *testing.T) {
  168. client, err := clients.NewNetworkV2Client()
  169. if err != nil {
  170. t.Fatalf("Unable to create a network client: %v", err)
  171. }
  172. // Create Network
  173. network, err := CreateNetwork(t, client)
  174. if err != nil {
  175. t.Fatalf("Unable to create network: %v", err)
  176. }
  177. defer DeleteNetwork(t, client, network.ID)
  178. // Create Subnet
  179. subnet, err := CreateSubnet(t, client, network.ID)
  180. if err != nil {
  181. t.Fatalf("Unable to create subnet: %v", err)
  182. }
  183. defer DeleteSubnet(t, client, subnet.ID)
  184. // Create port
  185. port, err := CreatePortWithNoSecurityGroup(t, client, network.ID, subnet.ID)
  186. if err != nil {
  187. t.Fatalf("Unable to create port: %v", err)
  188. }
  189. defer DeletePort(t, client, port.ID)
  190. tools.PrintResource(t, port)
  191. if len(port.SecurityGroups) != 0 {
  192. t.Fatalf("Port was created with security groups")
  193. }
  194. }
  195. func TestPortsRemoveAddressPair(t *testing.T) {
  196. client, err := clients.NewNetworkV2Client()
  197. if err != nil {
  198. t.Fatalf("Unable to create a network client: %v", err)
  199. }
  200. // Create Network
  201. network, err := CreateNetwork(t, client)
  202. if err != nil {
  203. t.Fatalf("Unable to create network: %v", err)
  204. }
  205. defer DeleteNetwork(t, client, network.ID)
  206. // Create Subnet
  207. subnet, err := CreateSubnet(t, client, network.ID)
  208. if err != nil {
  209. t.Fatalf("Unable to create subnet: %v", err)
  210. }
  211. defer DeleteSubnet(t, client, subnet.ID)
  212. // Create port
  213. port, err := CreatePort(t, client, network.ID, subnet.ID)
  214. if err != nil {
  215. t.Fatalf("Unable to create port: %v", err)
  216. }
  217. defer DeletePort(t, client, port.ID)
  218. tools.PrintResource(t, port)
  219. // Add an address pair to the port
  220. updateOpts := ports.UpdateOpts{
  221. AllowedAddressPairs: &[]ports.AddressPair{
  222. ports.AddressPair{IPAddress: "192.168.255.10", MACAddress: "aa:bb:cc:dd:ee:ff"},
  223. },
  224. }
  225. newPort, err := ports.Update(client, port.ID, updateOpts).Extract()
  226. if err != nil {
  227. t.Fatalf("Could not update port: %v", err)
  228. }
  229. // Remove the address pair
  230. updateOpts = ports.UpdateOpts{
  231. AllowedAddressPairs: &[]ports.AddressPair{},
  232. }
  233. newPort, err = ports.Update(client, port.ID, updateOpts).Extract()
  234. if err != nil {
  235. t.Fatalf("Could not update port: %v", err)
  236. }
  237. tools.PrintResource(t, newPort)
  238. if len(newPort.AllowedAddressPairs) > 0 {
  239. t.Fatalf("Unable to remove the address pair")
  240. }
  241. }
  242. func TestPortsDontUpdateAllowedAddressPairs(t *testing.T) {
  243. client, err := clients.NewNetworkV2Client()
  244. if err != nil {
  245. t.Fatalf("Unable to create a network client: %v", err)
  246. }
  247. // Create Network
  248. network, err := CreateNetwork(t, client)
  249. if err != nil {
  250. t.Fatalf("Unable to create network: %v", err)
  251. }
  252. defer DeleteNetwork(t, client, network.ID)
  253. // Create Subnet
  254. subnet, err := CreateSubnet(t, client, network.ID)
  255. if err != nil {
  256. t.Fatalf("Unable to create subnet: %v", err)
  257. }
  258. defer DeleteSubnet(t, client, subnet.ID)
  259. // Create port
  260. port, err := CreatePort(t, client, network.ID, subnet.ID)
  261. if err != nil {
  262. t.Fatalf("Unable to create port: %v", err)
  263. }
  264. defer DeletePort(t, client, port.ID)
  265. tools.PrintResource(t, port)
  266. // Add an address pair to the port
  267. updateOpts := ports.UpdateOpts{
  268. AllowedAddressPairs: &[]ports.AddressPair{
  269. ports.AddressPair{IPAddress: "192.168.255.10", MACAddress: "aa:bb:cc:dd:ee:ff"},
  270. },
  271. }
  272. newPort, err := ports.Update(client, port.ID, updateOpts).Extract()
  273. if err != nil {
  274. t.Fatalf("Could not update port: %v", err)
  275. }
  276. tools.PrintResource(t, newPort)
  277. // Remove the address pair
  278. updateOpts = ports.UpdateOpts{
  279. Name: "some_port",
  280. }
  281. newPort, err = ports.Update(client, port.ID, updateOpts).Extract()
  282. if err != nil {
  283. t.Fatalf("Could not update port: %v", err)
  284. }
  285. tools.PrintResource(t, newPort)
  286. if len(newPort.AllowedAddressPairs) == 0 {
  287. t.Fatalf("Address Pairs were removed")
  288. }
  289. }