Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 1 | // +build acceptance compute secgroups |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 13 | func TestSecGroupsList(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 14 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 15 | if err != nil { |
| 16 | t.Fatalf("Unable to create a compute client: %v", err) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 17 | } |
| 18 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 19 | allPages, err := secgroups.List(client).AllPages() |
| 20 | if err != nil { |
| 21 | t.Fatalf("Unable to retrieve security groups: %v", err) |
| 22 | } |
| 23 | |
| 24 | allSecGroups, err := secgroups.ExtractSecurityGroups(allPages) |
| 25 | if err != nil { |
| 26 | t.Fatalf("Unable to extract security groups: %v", err) |
| 27 | } |
| 28 | |
| 29 | for _, secgroup := range allSecGroups { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 30 | PrintSecurityGroup(t, &secgroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 31 | } |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 34 | func TestSecGroupsCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 35 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 36 | if err != nil { |
| 37 | t.Fatalf("Unable to create a compute client: %v", err) |
| 38 | } |
| 39 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 40 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 41 | if err != nil { |
| 42 | t.Fatalf("Unable to create security group: %v", err) |
| 43 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 44 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func TestSecGroupsUpdate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 48 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 49 | if err != nil { |
| 50 | t.Fatalf("Unable to create a compute client: %v", err) |
| 51 | } |
| 52 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 53 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 54 | if err != nil { |
| 55 | t.Fatalf("Unable to create security group: %v", err) |
| 56 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 57 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 58 | |
| 59 | updateOpts := secgroups.UpdateOpts{ |
| 60 | Name: tools.RandomString("secgroup_", 4), |
| 61 | Description: tools.RandomString("dec_", 10), |
| 62 | } |
| 63 | updatedSecurityGroup, err := secgroups.Update(client, securityGroup.ID, updateOpts).Extract() |
| 64 | if err != nil { |
| 65 | t.Fatalf("Unable to update security group: %v", err) |
| 66 | } |
| 67 | |
| 68 | t.Logf("Updated %s's name to %s", updatedSecurityGroup.ID, updatedSecurityGroup.Name) |
| 69 | } |
| 70 | |
| 71 | func TestSecGroupsRuleCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 72 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 73 | if err != nil { |
| 74 | t.Fatalf("Unable to create a compute client: %v", err) |
| 75 | } |
| 76 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 77 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 78 | if err != nil { |
| 79 | t.Fatalf("Unable to create security group: %v", err) |
| 80 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 81 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 82 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 83 | rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 84 | if err != nil { |
| 85 | t.Fatalf("Unable to create rule: %v", err) |
| 86 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 87 | defer DeleteSecurityGroupRule(t, client, rule) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func TestSecGroupsAddGroupToServer(t *testing.T) { |
| 91 | if testing.Short() { |
| 92 | t.Skip("Skipping test that requires server creation in short mode.") |
| 93 | } |
| 94 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 95 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 96 | if err != nil { |
| 97 | t.Fatalf("Unable to create a compute client: %v", err) |
| 98 | } |
| 99 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 100 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 105 | server, err := CreateServer(t, client, choices) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 106 | if err != nil { |
| 107 | t.Fatalf("Unable to create server: %v", err) |
| 108 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 109 | defer DeleteServer(t, client, server) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 110 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 111 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 112 | if err != nil { |
| 113 | t.Fatalf("Unable to create security group: %v", err) |
| 114 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 115 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 116 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 117 | rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 118 | if err != nil { |
| 119 | t.Fatalf("Unable to create rule: %v", err) |
| 120 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 121 | defer DeleteSecurityGroupRule(t, client, rule) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 122 | |
| 123 | t.Logf("Adding group %s to server %s", securityGroup.ID, server.ID) |
| 124 | err = secgroups.AddServer(client, server.ID, securityGroup.Name).ExtractErr() |
| 125 | if err != nil && err.Error() != "EOF" { |
| 126 | t.Fatalf("Unable to add group %s to server %s: %s", securityGroup.ID, server.ID, err) |
| 127 | } |
| 128 | |
| 129 | t.Logf("Removing group %s from server %s", securityGroup.ID, server.ID) |
| 130 | err = secgroups.RemoveServer(client, server.ID, securityGroup.Name).ExtractErr() |
| 131 | if err != nil && err.Error() != "EOF" { |
| 132 | t.Fatalf("Unable to remove group %s from server %s: %s", securityGroup.ID, server.ID, err) |
| 133 | } |
| 134 | } |