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 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud" |
| 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) { |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 14 | client, err := newClient() |
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 { |
| 30 | printSecurityGroup(t, &secgroup) |
| 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) { |
| 35 | client, err := newClient() |
| 36 | if err != nil { |
| 37 | t.Fatalf("Unable to create a compute client: %v", err) |
| 38 | } |
| 39 | |
| 40 | securityGroup, err := createSecurityGroup(t, client) |
| 41 | if err != nil { |
| 42 | t.Fatalf("Unable to create security group: %v", err) |
| 43 | } |
| 44 | defer deleteSecurityGroup(t, client, securityGroup) |
| 45 | } |
| 46 | |
| 47 | func TestSecGroupsUpdate(t *testing.T) { |
| 48 | client, err := newClient() |
| 49 | if err != nil { |
| 50 | t.Fatalf("Unable to create a compute client: %v", err) |
| 51 | } |
| 52 | |
| 53 | securityGroup, err := createSecurityGroup(t, client) |
| 54 | if err != nil { |
| 55 | t.Fatalf("Unable to create security group: %v", err) |
| 56 | } |
| 57 | defer deleteSecurityGroup(t, client, securityGroup) |
| 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) { |
| 72 | client, err := newClient() |
| 73 | if err != nil { |
| 74 | t.Fatalf("Unable to create a compute client: %v", err) |
| 75 | } |
| 76 | |
| 77 | securityGroup, err := createSecurityGroup(t, client) |
| 78 | if err != nil { |
| 79 | t.Fatalf("Unable to create security group: %v", err) |
| 80 | } |
| 81 | defer deleteSecurityGroup(t, client, securityGroup) |
| 82 | |
| 83 | rule, err := createSecurityGroupRule(t, client, securityGroup.ID) |
| 84 | if err != nil { |
| 85 | t.Fatalf("Unable to create rule: %v", err) |
| 86 | } |
| 87 | defer deleteSecurityGroupRule(t, client, rule) |
| 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 | |
| 95 | client, err := newClient() |
| 96 | if err != nil { |
| 97 | t.Fatalf("Unable to create a compute client: %v", err) |
| 98 | } |
| 99 | |
| 100 | choices, err := ComputeChoicesFromEnv() |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | |
| 105 | server, err := createServer(t, client, choices) |
| 106 | if err != nil { |
| 107 | t.Fatalf("Unable to create server: %v", err) |
| 108 | } |
| 109 | |
| 110 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 111 | t.Fatalf("Unable to wait for server: %v", err) |
| 112 | } |
| 113 | defer deleteServer(t, client, server) |
| 114 | |
| 115 | securityGroup, err := createSecurityGroup(t, client) |
| 116 | if err != nil { |
| 117 | t.Fatalf("Unable to create security group: %v", err) |
| 118 | } |
| 119 | defer deleteSecurityGroup(t, client, securityGroup) |
| 120 | |
| 121 | rule, err := createSecurityGroupRule(t, client, securityGroup.ID) |
| 122 | if err != nil { |
| 123 | t.Fatalf("Unable to create rule: %v", err) |
| 124 | } |
| 125 | defer deleteSecurityGroupRule(t, client, rule) |
| 126 | |
| 127 | t.Logf("Adding group %s to server %s", securityGroup.ID, server.ID) |
| 128 | err = secgroups.AddServer(client, server.ID, securityGroup.Name).ExtractErr() |
| 129 | if err != nil && err.Error() != "EOF" { |
| 130 | t.Fatalf("Unable to add group %s to server %s: %s", securityGroup.ID, server.ID, err) |
| 131 | } |
| 132 | |
| 133 | t.Logf("Removing group %s from server %s", securityGroup.ID, server.ID) |
| 134 | err = secgroups.RemoveServer(client, server.ID, securityGroup.Name).ExtractErr() |
| 135 | if err != nil && err.Error() != "EOF" { |
| 136 | t.Fatalf("Unable to remove group %s from server %s: %s", securityGroup.ID, server.ID, err) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func createSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (secgroups.SecurityGroup, error) { |
| 141 | createOpts := secgroups.CreateOpts{ |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 142 | Name: tools.RandomString("secgroup_", 5), |
| 143 | Description: "something", |
| 144 | } |
| 145 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 146 | securityGroup, err := secgroups.Create(client, createOpts).Extract() |
| 147 | if err != nil { |
| 148 | return *securityGroup, err |
Jamie Hannaford | 19460b2 | 2014-11-24 16:04:17 +0100 | [diff] [blame] | 149 | } |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 150 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 151 | t.Logf("Created security group: %s", securityGroup.ID) |
| 152 | return *securityGroup, nil |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 155 | func deleteSecurityGroup(t *testing.T, client *gophercloud.ServiceClient, securityGroup secgroups.SecurityGroup) { |
| 156 | err := secgroups.Delete(client, securityGroup.ID).ExtractErr() |
| 157 | if err != nil { |
| 158 | t.Fatalf("Unable to delete security group %s: %s", securityGroup.ID, err) |
| 159 | } |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 160 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 161 | t.Logf("Deleted security group: %s", securityGroup.ID) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 164 | func createSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, securityGroupID string) (secgroups.Rule, error) { |
| 165 | createOpts := secgroups.CreateRuleOpts{ |
| 166 | ParentGroupID: securityGroupID, |
| 167 | FromPort: tools.RandomInt(80, 89), |
| 168 | ToPort: tools.RandomInt(90, 99), |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 169 | IPProtocol: "TCP", |
| 170 | CIDR: "0.0.0.0/0", |
| 171 | } |
| 172 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 173 | rule, err := secgroups.CreateRule(client, createOpts).Extract() |
| 174 | if err != nil { |
| 175 | return *rule, err |
Joe Topjian | 2893f7b | 2016-01-24 17:14:36 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 178 | t.Logf("Created security group rule: %s", rule.ID) |
| 179 | return *rule, nil |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 182 | func deleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient, rule secgroups.Rule) { |
| 183 | err := secgroups.DeleteRule(client, rule.ID).ExtractErr() |
| 184 | if err != nil { |
| 185 | t.Fatalf("Unable to delete rule: %v", err) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 188 | t.Logf("Deleted security group rule: %s", rule.ID) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 191 | func printSecurityGroup(t *testing.T, securityGroup *secgroups.SecurityGroup) { |
| 192 | t.Logf("ID: %s", securityGroup.ID) |
| 193 | t.Logf("Name: %s", securityGroup.Name) |
| 194 | t.Logf("Description: %s", securityGroup.Description) |
| 195 | t.Logf("Tenant ID: %s", securityGroup.TenantID) |
| 196 | t.Logf("Rules:") |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 197 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 198 | for _, rule := range securityGroup.Rules { |
| 199 | t.Logf("\tID: %s", rule.ID) |
| 200 | t.Logf("\tFrom Port: %d", rule.FromPort) |
| 201 | t.Logf("\tTo Port: %d", rule.ToPort) |
| 202 | t.Logf("\tIP Protocol: %s", rule.IPProtocol) |
| 203 | t.Logf("\tIP Range: %s", rule.IPRange.CIDR) |
| 204 | t.Logf("\tParent Group ID: %s", rule.ParentGroupID) |
| 205 | t.Logf("\tGroup Tenant ID: %s", rule.Group.TenantID) |
| 206 | t.Logf("\tGroup Name: %s", rule.Group.Name) |
| 207 | } |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 208 | } |