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 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/acceptance/tools" |
| 10 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups" |
| 11 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 12 | "github.com/rackspace/gophercloud/pagination" |
| 13 | th "github.com/rackspace/gophercloud/testhelper" |
| 14 | ) |
| 15 | |
| 16 | func TestSecGroups(t *testing.T) { |
| 17 | client, err := newClient() |
| 18 | th.AssertNoErr(t, err) |
| 19 | |
| 20 | serverID, needsDeletion := findServer(t, client) |
| 21 | |
| 22 | groupID := createSecGroup(t, client) |
| 23 | |
| 24 | listSecGroups(t, client) |
| 25 | |
| 26 | newName := tools.RandomString("secgroup_", 5) |
| 27 | updateSecGroup(t, client, groupID, newName) |
| 28 | |
| 29 | getSecGroup(t, client, groupID) |
| 30 | |
| 31 | addRemoveRules(t, client, groupID) |
| 32 | |
| 33 | addServerToSecGroup(t, client, serverID, newName) |
| 34 | |
| 35 | removeServerFromSecGroup(t, client, serverID, newName) |
| 36 | |
| 37 | if needsDeletion { |
| 38 | servers.Delete(client, serverID) |
| 39 | } |
| 40 | |
| 41 | deleteSecGroup(t, client, groupID) |
| 42 | } |
| 43 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 44 | func createSecGroup(t *testing.T, client *gophercloud.ServiceClient) string { |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 45 | opts := secgroups.CreateOpts{ |
| 46 | Name: tools.RandomString("secgroup_", 5), |
| 47 | Description: "something", |
| 48 | } |
| 49 | |
| 50 | group, err := secgroups.Create(client, opts).Extract() |
| 51 | th.AssertNoErr(t, err) |
| 52 | |
| 53 | t.Logf("Created secgroup %s %s", group.ID, group.Name) |
| 54 | |
| 55 | return group.ID |
| 56 | } |
| 57 | |
| 58 | func listSecGroups(t *testing.T, client *gophercloud.ServiceClient) { |
| 59 | err := secgroups.List(client).EachPage(func(page pagination.Page) (bool, error) { |
| 60 | secGrpList, err := secgroups.ExtractSecurityGroups(page) |
| 61 | th.AssertNoErr(t, err) |
| 62 | |
| 63 | for _, sg := range secGrpList { |
| 64 | t.Logf("Listing secgroup %s: Name [%s] Desc [%s] TenantID [%s]", sg.ID, |
| 65 | sg.Name, sg.Description, sg.TenantID) |
| 66 | } |
| 67 | |
| 68 | return true, nil |
| 69 | }) |
| 70 | |
| 71 | th.AssertNoErr(t, err) |
| 72 | } |
| 73 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 74 | func updateSecGroup(t *testing.T, client *gophercloud.ServiceClient, id, newName string) { |
Jamie Hannaford | 19460b2 | 2014-11-24 16:04:17 +0100 | [diff] [blame] | 75 | opts := secgroups.UpdateOpts{ |
| 76 | Name: newName, |
| 77 | Description: tools.RandomString("dec_", 10), |
| 78 | } |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 79 | group, err := secgroups.Update(client, id, opts).Extract() |
| 80 | th.AssertNoErr(t, err) |
| 81 | |
| 82 | t.Logf("Updated %s's name to %s", group.ID, group.Name) |
| 83 | } |
| 84 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 85 | func getSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) { |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 86 | group, err := secgroups.Get(client, id).Extract() |
| 87 | th.AssertNoErr(t, err) |
| 88 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 89 | t.Logf("Getting %s: %#v", id, group) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 92 | func addRemoveRules(t *testing.T, client *gophercloud.ServiceClient, id string) { |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 93 | opts := secgroups.CreateRuleOpts{ |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 94 | ParentGroupID: id, |
| 95 | FromPort: 22, |
| 96 | ToPort: 22, |
| 97 | IPProtocol: "TCP", |
| 98 | CIDR: "0.0.0.0/0", |
| 99 | } |
| 100 | |
Jamie Hannaford | 04abbc7 | 2014-11-21 11:27:57 +0100 | [diff] [blame] | 101 | rule, err := secgroups.CreateRule(client, opts).Extract() |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 102 | th.AssertNoErr(t, err) |
| 103 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 104 | t.Logf("Adding rule %s to group %s", rule.ID, id) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 105 | |
| 106 | err = secgroups.DeleteRule(client, rule.ID).ExtractErr() |
| 107 | th.AssertNoErr(t, err) |
| 108 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 109 | t.Logf("Deleted rule %s from group %s", rule.ID, id) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func findServer(t *testing.T, client *gophercloud.ServiceClient) (string, bool) { |
| 113 | var serverID string |
| 114 | var needsDeletion bool |
| 115 | |
| 116 | err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
| 117 | sList, err := servers.ExtractServers(page) |
| 118 | th.AssertNoErr(t, err) |
| 119 | |
| 120 | for _, s := range sList { |
| 121 | serverID = s.ID |
| 122 | needsDeletion = false |
| 123 | |
| 124 | t.Logf("Found an existing server: ID [%s]", serverID) |
| 125 | break |
| 126 | } |
| 127 | |
| 128 | return true, nil |
| 129 | }) |
| 130 | th.AssertNoErr(t, err) |
| 131 | |
| 132 | if serverID == "" { |
| 133 | t.Log("No server found, creating one") |
| 134 | |
| 135 | choices, err := ComputeChoicesFromEnv() |
| 136 | th.AssertNoErr(t, err) |
| 137 | |
| 138 | opts := &servers.CreateOpts{ |
| 139 | Name: tools.RandomString("secgroup_test_", 5), |
| 140 | ImageRef: choices.ImageID, |
| 141 | FlavorRef: choices.FlavorID, |
| 142 | } |
| 143 | |
| 144 | s, err := servers.Create(client, opts).Extract() |
| 145 | th.AssertNoErr(t, err) |
| 146 | serverID = s.ID |
| 147 | |
| 148 | t.Logf("Created server %s, waiting for it to build", s.ID) |
| 149 | err = servers.WaitForStatus(client, serverID, "ACTIVE", 300) |
| 150 | th.AssertNoErr(t, err) |
| 151 | |
| 152 | needsDeletion = true |
| 153 | } |
| 154 | |
| 155 | return serverID, needsDeletion |
| 156 | } |
| 157 | |
| 158 | func addServerToSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) { |
| 159 | err := secgroups.AddServerToGroup(client, serverID, groupName).ExtractErr() |
| 160 | th.AssertNoErr(t, err) |
| 161 | |
| 162 | t.Logf("Adding group %s to server %s", groupName, serverID) |
| 163 | } |
| 164 | |
| 165 | func removeServerFromSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) { |
| 166 | err := secgroups.RemoveServerFromGroup(client, serverID, groupName).ExtractErr() |
| 167 | th.AssertNoErr(t, err) |
| 168 | |
| 169 | t.Logf("Removing group %s from server %s", groupName, serverID) |
| 170 | } |
| 171 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 172 | func deleteSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) { |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 173 | err := secgroups.Delete(client, id).ExtractErr() |
| 174 | th.AssertNoErr(t, err) |
| 175 | |
Jamie Hannaford | aea96c6 | 2014-11-25 12:05:04 +0100 | [diff] [blame] | 176 | t.Logf("Deleted group %s", id) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 177 | } |