secgroup_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // +build acceptance compute secgroups
  2. package v2
  3. import (
  4. "testing"
  5. "devel.mephi.ru/iacherepanov/openstack-gophercloud/acceptance/clients"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/acceptance/tools"
  7. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/compute/v2/extensions/secgroups"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/compute/v2/servers"
  9. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  10. )
  11. func TestSecGroupsList(t *testing.T) {
  12. client, err := clients.NewComputeV2Client()
  13. th.AssertNoErr(t, err)
  14. allPages, err := secgroups.List(client).AllPages()
  15. th.AssertNoErr(t, err)
  16. allSecGroups, err := secgroups.ExtractSecurityGroups(allPages)
  17. th.AssertNoErr(t, err)
  18. var found bool
  19. for _, secgroup := range allSecGroups {
  20. tools.PrintResource(t, secgroup)
  21. if secgroup.Name == "default" {
  22. found = true
  23. }
  24. }
  25. th.AssertEquals(t, found, true)
  26. }
  27. func TestSecGroupsCRUD(t *testing.T) {
  28. client, err := clients.NewComputeV2Client()
  29. th.AssertNoErr(t, err)
  30. securityGroup, err := CreateSecurityGroup(t, client)
  31. th.AssertNoErr(t, err)
  32. defer DeleteSecurityGroup(t, client, securityGroup.ID)
  33. tools.PrintResource(t, securityGroup)
  34. newName := tools.RandomString("secgroup_", 4)
  35. updateOpts := secgroups.UpdateOpts{
  36. Name: newName,
  37. Description: tools.RandomString("dec_", 10),
  38. }
  39. updatedSecurityGroup, err := secgroups.Update(client, securityGroup.ID, updateOpts).Extract()
  40. th.AssertNoErr(t, err)
  41. tools.PrintResource(t, updatedSecurityGroup)
  42. t.Logf("Updated %s's name to %s", updatedSecurityGroup.ID, updatedSecurityGroup.Name)
  43. th.AssertEquals(t, updatedSecurityGroup.Name, newName)
  44. }
  45. func TestSecGroupsRuleCreate(t *testing.T) {
  46. client, err := clients.NewComputeV2Client()
  47. th.AssertNoErr(t, err)
  48. securityGroup, err := CreateSecurityGroup(t, client)
  49. th.AssertNoErr(t, err)
  50. defer DeleteSecurityGroup(t, client, securityGroup.ID)
  51. tools.PrintResource(t, securityGroup)
  52. rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID)
  53. th.AssertNoErr(t, err)
  54. defer DeleteSecurityGroupRule(t, client, rule.ID)
  55. tools.PrintResource(t, rule)
  56. newSecurityGroup, err := secgroups.Get(client, securityGroup.ID).Extract()
  57. th.AssertNoErr(t, err)
  58. tools.PrintResource(t, newSecurityGroup)
  59. th.AssertEquals(t, len(newSecurityGroup.Rules), 1)
  60. }
  61. func TestSecGroupsAddGroupToServer(t *testing.T) {
  62. clients.RequireLong(t)
  63. client, err := clients.NewComputeV2Client()
  64. th.AssertNoErr(t, err)
  65. server, err := CreateServer(t, client)
  66. th.AssertNoErr(t, err)
  67. defer DeleteServer(t, client, server)
  68. securityGroup, err := CreateSecurityGroup(t, client)
  69. th.AssertNoErr(t, err)
  70. defer DeleteSecurityGroup(t, client, securityGroup.ID)
  71. rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID)
  72. th.AssertNoErr(t, err)
  73. defer DeleteSecurityGroupRule(t, client, rule.ID)
  74. t.Logf("Adding group %s to server %s", securityGroup.ID, server.ID)
  75. err = secgroups.AddServer(client, server.ID, securityGroup.Name).ExtractErr()
  76. th.AssertNoErr(t, err)
  77. server, err = servers.Get(client, server.ID).Extract()
  78. th.AssertNoErr(t, err)
  79. tools.PrintResource(t, server)
  80. var found bool
  81. for _, sg := range server.SecurityGroups {
  82. if sg["name"] == securityGroup.Name {
  83. found = true
  84. }
  85. }
  86. th.AssertEquals(t, found, true)
  87. t.Logf("Removing group %s from server %s", securityGroup.ID, server.ID)
  88. err = secgroups.RemoveServer(client, server.ID, securityGroup.Name).ExtractErr()
  89. th.AssertNoErr(t, err)
  90. server, err = servers.Get(client, server.ID).Extract()
  91. th.AssertNoErr(t, err)
  92. found = false
  93. tools.PrintResource(t, server)
  94. for _, sg := range server.SecurityGroups {
  95. if sg["name"] == securityGroup.Name {
  96. found = true
  97. }
  98. }
  99. th.AssertEquals(t, found, false)
  100. }