PageRenderTime 813ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/compute/v2/secgroup_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 137 lines | 110 code | 26 blank | 1 comment | 49 complexity | d44e10874879483cd29e6dfead952600 MD5 | raw file
  1. // +build acceptance compute secgroups
  2. package v2
  3. import (
  4. "testing"
  5. "github.com/gophercloud/gophercloud/acceptance/clients"
  6. "github.com/gophercloud/gophercloud/acceptance/tools"
  7. "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
  8. )
  9. func TestSecGroupsList(t *testing.T) {
  10. client, err := clients.NewComputeV2Client()
  11. if err != nil {
  12. t.Fatalf("Unable to create a compute client: %v", err)
  13. }
  14. allPages, err := secgroups.List(client).AllPages()
  15. if err != nil {
  16. t.Fatalf("Unable to retrieve security groups: %v", err)
  17. }
  18. allSecGroups, err := secgroups.ExtractSecurityGroups(allPages)
  19. if err != nil {
  20. t.Fatalf("Unable to extract security groups: %v", err)
  21. }
  22. for _, secgroup := range allSecGroups {
  23. tools.PrintResource(t, secgroup)
  24. }
  25. }
  26. func TestSecGroupsCreate(t *testing.T) {
  27. client, err := clients.NewComputeV2Client()
  28. if err != nil {
  29. t.Fatalf("Unable to create a compute client: %v", err)
  30. }
  31. securityGroup, err := CreateSecurityGroup(t, client)
  32. if err != nil {
  33. t.Fatalf("Unable to create security group: %v", err)
  34. }
  35. defer DeleteSecurityGroup(t, client, securityGroup)
  36. }
  37. func TestSecGroupsUpdate(t *testing.T) {
  38. client, err := clients.NewComputeV2Client()
  39. if err != nil {
  40. t.Fatalf("Unable to create a compute client: %v", err)
  41. }
  42. securityGroup, err := CreateSecurityGroup(t, client)
  43. if err != nil {
  44. t.Fatalf("Unable to create security group: %v", err)
  45. }
  46. defer DeleteSecurityGroup(t, client, securityGroup)
  47. updateOpts := secgroups.UpdateOpts{
  48. Name: tools.RandomString("secgroup_", 4),
  49. Description: tools.RandomString("dec_", 10),
  50. }
  51. updatedSecurityGroup, err := secgroups.Update(client, securityGroup.ID, updateOpts).Extract()
  52. if err != nil {
  53. t.Fatalf("Unable to update security group: %v", err)
  54. }
  55. t.Logf("Updated %s's name to %s", updatedSecurityGroup.ID, updatedSecurityGroup.Name)
  56. }
  57. func TestSecGroupsRuleCreate(t *testing.T) {
  58. client, err := clients.NewComputeV2Client()
  59. if err != nil {
  60. t.Fatalf("Unable to create a compute client: %v", err)
  61. }
  62. securityGroup, err := CreateSecurityGroup(t, client)
  63. if err != nil {
  64. t.Fatalf("Unable to create security group: %v", err)
  65. }
  66. defer DeleteSecurityGroup(t, client, securityGroup)
  67. rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID)
  68. if err != nil {
  69. t.Fatalf("Unable to create rule: %v", err)
  70. }
  71. defer DeleteSecurityGroupRule(t, client, rule)
  72. newSecurityGroup, err := secgroups.Get(client, securityGroup.ID).Extract()
  73. if err != nil {
  74. t.Fatalf("Unable to obtain security group: %v", err)
  75. }
  76. tools.PrintResource(t, newSecurityGroup)
  77. }
  78. func TestSecGroupsAddGroupToServer(t *testing.T) {
  79. if testing.Short() {
  80. t.Skip("Skipping test that requires server creation in short mode.")
  81. }
  82. client, err := clients.NewComputeV2Client()
  83. if err != nil {
  84. t.Fatalf("Unable to create a compute client: %v", err)
  85. }
  86. server, err := CreateServer(t, client)
  87. if err != nil {
  88. t.Fatalf("Unable to create server: %v", err)
  89. }
  90. defer DeleteServer(t, client, server)
  91. securityGroup, err := CreateSecurityGroup(t, client)
  92. if err != nil {
  93. t.Fatalf("Unable to create security group: %v", err)
  94. }
  95. defer DeleteSecurityGroup(t, client, securityGroup)
  96. rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID)
  97. if err != nil {
  98. t.Fatalf("Unable to create rule: %v", err)
  99. }
  100. defer DeleteSecurityGroupRule(t, client, rule)
  101. t.Logf("Adding group %s to server %s", securityGroup.ID, server.ID)
  102. err = secgroups.AddServer(client, server.ID, securityGroup.Name).ExtractErr()
  103. if err != nil && err.Error() != "EOF" {
  104. t.Fatalf("Unable to add group %s to server %s: %s", securityGroup.ID, server.ID, err)
  105. }
  106. t.Logf("Removing group %s from server %s", securityGroup.ID, server.ID)
  107. err = secgroups.RemoveServer(client, server.ID, securityGroup.Name).ExtractErr()
  108. if err != nil && err.Error() != "EOF" {
  109. t.Fatalf("Unable to remove group %s from server %s: %s", securityGroup.ID, server.ID, err)
  110. }
  111. }