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 | |
| 44 | func createSecGroup(t *testing.T, client *gophercloud.ServiceClient) string { |
| 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 | |
| 74 | func updateSecGroup(t *testing.T, client *gophercloud.ServiceClient, id, newName string) { |
| 75 | opts := secgroups.UpdateOpts{Name: newName} |
| 76 | group, err := secgroups.Update(client, id, opts).Extract() |
| 77 | th.AssertNoErr(t, err) |
| 78 | |
| 79 | t.Logf("Updated %s's name to %s", group.ID, group.Name) |
| 80 | } |
| 81 | |
| 82 | func getSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 83 | group, err := secgroups.Get(client, id).Extract() |
| 84 | th.AssertNoErr(t, err) |
| 85 | |
| 86 | t.Logf("Getting %s: %#v", id, group) |
| 87 | } |
| 88 | |
| 89 | func addRemoveRules(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 90 | opts := secgroups.AddRuleOpts{ |
| 91 | ParentGroupID: id, |
| 92 | FromPort: 22, |
| 93 | ToPort: 22, |
| 94 | IPProtocol: "TCP", |
| 95 | CIDR: "0.0.0.0/0", |
| 96 | } |
| 97 | |
| 98 | rule, err := secgroups.AddRule(client, opts).Extract() |
| 99 | th.AssertNoErr(t, err) |
| 100 | |
| 101 | t.Logf("Adding rule %s to group %s", rule.ID, id) |
| 102 | |
| 103 | err = secgroups.DeleteRule(client, rule.ID).ExtractErr() |
| 104 | th.AssertNoErr(t, err) |
| 105 | |
| 106 | t.Logf("Deleted rule %s from group %s", rule.ID, id) |
| 107 | } |
| 108 | |
| 109 | func findServer(t *testing.T, client *gophercloud.ServiceClient) (string, bool) { |
| 110 | var serverID string |
| 111 | var needsDeletion bool |
| 112 | |
| 113 | err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
| 114 | sList, err := servers.ExtractServers(page) |
| 115 | th.AssertNoErr(t, err) |
| 116 | |
| 117 | for _, s := range sList { |
| 118 | serverID = s.ID |
| 119 | needsDeletion = false |
| 120 | |
| 121 | t.Logf("Found an existing server: ID [%s]", serverID) |
| 122 | break |
| 123 | } |
| 124 | |
| 125 | return true, nil |
| 126 | }) |
| 127 | th.AssertNoErr(t, err) |
| 128 | |
| 129 | if serverID == "" { |
| 130 | t.Log("No server found, creating one") |
| 131 | |
| 132 | choices, err := ComputeChoicesFromEnv() |
| 133 | th.AssertNoErr(t, err) |
| 134 | |
| 135 | opts := &servers.CreateOpts{ |
| 136 | Name: tools.RandomString("secgroup_test_", 5), |
| 137 | ImageRef: choices.ImageID, |
| 138 | FlavorRef: choices.FlavorID, |
| 139 | } |
| 140 | |
| 141 | s, err := servers.Create(client, opts).Extract() |
| 142 | th.AssertNoErr(t, err) |
| 143 | serverID = s.ID |
| 144 | |
| 145 | t.Logf("Created server %s, waiting for it to build", s.ID) |
| 146 | err = servers.WaitForStatus(client, serverID, "ACTIVE", 300) |
| 147 | th.AssertNoErr(t, err) |
| 148 | |
| 149 | needsDeletion = true |
| 150 | } |
| 151 | |
| 152 | return serverID, needsDeletion |
| 153 | } |
| 154 | |
| 155 | func addServerToSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) { |
| 156 | err := secgroups.AddServerToGroup(client, serverID, groupName).ExtractErr() |
| 157 | th.AssertNoErr(t, err) |
| 158 | |
| 159 | t.Logf("Adding group %s to server %s", groupName, serverID) |
| 160 | } |
| 161 | |
| 162 | func removeServerFromSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) { |
| 163 | err := secgroups.RemoveServerFromGroup(client, serverID, groupName).ExtractErr() |
| 164 | th.AssertNoErr(t, err) |
| 165 | |
| 166 | t.Logf("Removing group %s from server %s", groupName, serverID) |
| 167 | } |
| 168 | |
| 169 | func deleteSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 170 | err := secgroups.Delete(client, id).ExtractErr() |
| 171 | th.AssertNoErr(t, err) |
| 172 | |
| 173 | t.Logf("Deleted group %s", id) |
| 174 | } |