PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/rackspace/gophercloud/acceptance/openstack/compute/v2/secgroup_test.go

https://gitlab.com/JamesClonk/machine
Go | 177 lines | 127 code | 49 blank | 1 comment | 5 complexity | 27ecce1591b5c21f7a498a66ab310fbc MD5 | raw file
  1. // +build acceptance compute secgroups
  2. package v2
  3. import (
  4. "testing"
  5. "github.com/rackspace/gophercloud"
  6. "github.com/rackspace/gophercloud/acceptance/tools"
  7. "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
  8. "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
  9. "github.com/rackspace/gophercloud/pagination"
  10. th "github.com/rackspace/gophercloud/testhelper"
  11. )
  12. func TestSecGroups(t *testing.T) {
  13. client, err := newClient()
  14. th.AssertNoErr(t, err)
  15. serverID, needsDeletion := findServer(t, client)
  16. groupID := createSecGroup(t, client)
  17. listSecGroups(t, client)
  18. newName := tools.RandomString("secgroup_", 5)
  19. updateSecGroup(t, client, groupID, newName)
  20. getSecGroup(t, client, groupID)
  21. addRemoveRules(t, client, groupID)
  22. addServerToSecGroup(t, client, serverID, newName)
  23. removeServerFromSecGroup(t, client, serverID, newName)
  24. if needsDeletion {
  25. servers.Delete(client, serverID)
  26. }
  27. deleteSecGroup(t, client, groupID)
  28. }
  29. func createSecGroup(t *testing.T, client *gophercloud.ServiceClient) string {
  30. opts := secgroups.CreateOpts{
  31. Name: tools.RandomString("secgroup_", 5),
  32. Description: "something",
  33. }
  34. group, err := secgroups.Create(client, opts).Extract()
  35. th.AssertNoErr(t, err)
  36. t.Logf("Created secgroup %s %s", group.ID, group.Name)
  37. return group.ID
  38. }
  39. func listSecGroups(t *testing.T, client *gophercloud.ServiceClient) {
  40. err := secgroups.List(client).EachPage(func(page pagination.Page) (bool, error) {
  41. secGrpList, err := secgroups.ExtractSecurityGroups(page)
  42. th.AssertNoErr(t, err)
  43. for _, sg := range secGrpList {
  44. t.Logf("Listing secgroup %s: Name [%s] Desc [%s] TenantID [%s]", sg.ID,
  45. sg.Name, sg.Description, sg.TenantID)
  46. }
  47. return true, nil
  48. })
  49. th.AssertNoErr(t, err)
  50. }
  51. func updateSecGroup(t *testing.T, client *gophercloud.ServiceClient, id, newName string) {
  52. opts := secgroups.UpdateOpts{
  53. Name: newName,
  54. Description: tools.RandomString("dec_", 10),
  55. }
  56. group, err := secgroups.Update(client, id, opts).Extract()
  57. th.AssertNoErr(t, err)
  58. t.Logf("Updated %s's name to %s", group.ID, group.Name)
  59. }
  60. func getSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) {
  61. group, err := secgroups.Get(client, id).Extract()
  62. th.AssertNoErr(t, err)
  63. t.Logf("Getting %s: %#v", id, group)
  64. }
  65. func addRemoveRules(t *testing.T, client *gophercloud.ServiceClient, id string) {
  66. opts := secgroups.CreateRuleOpts{
  67. ParentGroupID: id,
  68. FromPort: 22,
  69. ToPort: 22,
  70. IPProtocol: "TCP",
  71. CIDR: "0.0.0.0/0",
  72. }
  73. rule, err := secgroups.CreateRule(client, opts).Extract()
  74. th.AssertNoErr(t, err)
  75. t.Logf("Adding rule %s to group %s", rule.ID, id)
  76. err = secgroups.DeleteRule(client, rule.ID).ExtractErr()
  77. th.AssertNoErr(t, err)
  78. t.Logf("Deleted rule %s from group %s", rule.ID, id)
  79. }
  80. func findServer(t *testing.T, client *gophercloud.ServiceClient) (string, bool) {
  81. var serverID string
  82. var needsDeletion bool
  83. err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
  84. sList, err := servers.ExtractServers(page)
  85. th.AssertNoErr(t, err)
  86. for _, s := range sList {
  87. serverID = s.ID
  88. needsDeletion = false
  89. t.Logf("Found an existing server: ID [%s]", serverID)
  90. break
  91. }
  92. return true, nil
  93. })
  94. th.AssertNoErr(t, err)
  95. if serverID == "" {
  96. t.Log("No server found, creating one")
  97. choices, err := ComputeChoicesFromEnv()
  98. th.AssertNoErr(t, err)
  99. opts := &servers.CreateOpts{
  100. Name: tools.RandomString("secgroup_test_", 5),
  101. ImageRef: choices.ImageID,
  102. FlavorRef: choices.FlavorID,
  103. }
  104. s, err := servers.Create(client, opts).Extract()
  105. th.AssertNoErr(t, err)
  106. serverID = s.ID
  107. t.Logf("Created server %s, waiting for it to build", s.ID)
  108. err = servers.WaitForStatus(client, serverID, "ACTIVE", 300)
  109. th.AssertNoErr(t, err)
  110. needsDeletion = true
  111. }
  112. return serverID, needsDeletion
  113. }
  114. func addServerToSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) {
  115. err := secgroups.AddServerToGroup(client, serverID, groupName).ExtractErr()
  116. th.AssertNoErr(t, err)
  117. t.Logf("Adding group %s to server %s", groupName, serverID)
  118. }
  119. func removeServerFromSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) {
  120. err := secgroups.RemoveServerFromGroup(client, serverID, groupName).ExtractErr()
  121. th.AssertNoErr(t, err)
  122. t.Logf("Removing group %s from server %s", groupName, serverID)
  123. }
  124. func deleteSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) {
  125. err := secgroups.Delete(client, id).ExtractErr()
  126. th.AssertNoErr(t, err)
  127. t.Logf("Deleted group %s", id)
  128. }