PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 158 lines | 119 code | 30 blank | 9 comment | 47 complexity | ab0e71966edd0534034c5ace64c44b38 MD5 | raw file
  1. // +build acceptance networking
  2. package v2
  3. import (
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "github.com/gophercloud/gophercloud/acceptance/clients"
  8. "github.com/gophercloud/gophercloud/acceptance/tools"
  9. "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
  10. )
  11. func TestSubnetsList(t *testing.T) {
  12. client, err := clients.NewNetworkV2Client()
  13. if err != nil {
  14. t.Fatalf("Unable to create a network client: %v", err)
  15. }
  16. allPages, err := subnets.List(client, nil).AllPages()
  17. if err != nil {
  18. t.Fatalf("Unable to list subnets: %v", err)
  19. }
  20. allSubnets, err := subnets.ExtractSubnets(allPages)
  21. if err != nil {
  22. t.Fatalf("Unable to extract subnets: %v", err)
  23. }
  24. for _, subnet := range allSubnets {
  25. tools.PrintResource(t, subnet)
  26. }
  27. }
  28. func TestSubnetCRUD(t *testing.T) {
  29. client, err := clients.NewNetworkV2Client()
  30. if err != nil {
  31. t.Fatalf("Unable to create a network client: %v", err)
  32. }
  33. // Create Network
  34. network, err := CreateNetwork(t, client)
  35. if err != nil {
  36. t.Fatalf("Unable to create network: %v", err)
  37. }
  38. defer DeleteNetwork(t, client, network.ID)
  39. // Create Subnet
  40. subnet, err := CreateSubnet(t, client, network.ID)
  41. if err != nil {
  42. t.Fatalf("Unable to create subnet: %v", err)
  43. }
  44. defer DeleteSubnet(t, client, subnet.ID)
  45. tools.PrintResource(t, subnet)
  46. // Update Subnet
  47. newSubnetName := tools.RandomString("TESTACC-", 8)
  48. updateOpts := subnets.UpdateOpts{
  49. Name: newSubnetName,
  50. }
  51. _, err = subnets.Update(client, subnet.ID, updateOpts).Extract()
  52. if err != nil {
  53. t.Fatalf("Unable to update subnet: %v", err)
  54. }
  55. // Get subnet
  56. newSubnet, err := subnets.Get(client, subnet.ID).Extract()
  57. if err != nil {
  58. t.Fatalf("Unable to get subnet: %v", err)
  59. }
  60. tools.PrintResource(t, newSubnet)
  61. }
  62. func TestSubnetsDefaultGateway(t *testing.T) {
  63. client, err := clients.NewNetworkV2Client()
  64. if err != nil {
  65. t.Fatalf("Unable to create a network client: %v", err)
  66. }
  67. // Create Network
  68. network, err := CreateNetwork(t, client)
  69. if err != nil {
  70. t.Fatalf("Unable to create network: %v", err)
  71. }
  72. defer DeleteNetwork(t, client, network.ID)
  73. // Create Subnet
  74. subnet, err := CreateSubnetWithDefaultGateway(t, client, network.ID)
  75. if err != nil {
  76. t.Fatalf("Unable to create subnet: %v", err)
  77. }
  78. defer DeleteSubnet(t, client, subnet.ID)
  79. tools.PrintResource(t, subnet)
  80. if subnet.GatewayIP == "" {
  81. t.Fatalf("A default gateway was not created.")
  82. }
  83. var noGateway = ""
  84. updateOpts := subnets.UpdateOpts{
  85. GatewayIP: &noGateway,
  86. }
  87. newSubnet, err := subnets.Update(client, subnet.ID, updateOpts).Extract()
  88. if err != nil {
  89. t.Fatalf("Unable to update subnet")
  90. }
  91. if newSubnet.GatewayIP != "" {
  92. t.Fatalf("Gateway was not updated correctly")
  93. }
  94. }
  95. func TestSubnetsNoGateway(t *testing.T) {
  96. client, err := clients.NewNetworkV2Client()
  97. if err != nil {
  98. t.Fatalf("Unable to create a network client: %v", err)
  99. }
  100. // Create Network
  101. network, err := CreateNetwork(t, client)
  102. if err != nil {
  103. t.Fatalf("Unable to create network: %v", err)
  104. }
  105. defer DeleteNetwork(t, client, network.ID)
  106. // Create Subnet
  107. subnet, err := CreateSubnetWithNoGateway(t, client, network.ID)
  108. if err != nil {
  109. t.Fatalf("Unable to create subnet: %v", err)
  110. }
  111. defer DeleteSubnet(t, client, subnet.ID)
  112. tools.PrintResource(t, subnet)
  113. if subnet.GatewayIP != "" {
  114. t.Fatalf("A gateway exists when it shouldn't.")
  115. }
  116. subnetParts := strings.Split(subnet.CIDR, ".")
  117. newGateway := fmt.Sprintf("%s.%s.%s.1", subnetParts[0], subnetParts[1], subnetParts[2])
  118. updateOpts := subnets.UpdateOpts{
  119. GatewayIP: &newGateway,
  120. }
  121. newSubnet, err := subnets.Update(client, subnet.ID, updateOpts).Extract()
  122. if err != nil {
  123. t.Fatalf("Unable to update subnet")
  124. }
  125. if newSubnet.GatewayIP == "" {
  126. t.Fatalf("Gateway was not updated correctly")
  127. }
  128. }