blob: da06c35809f256a242869e13502a68eca6e1e0c5 [file] [log] [blame]
Jamie Hannafordc5afec42014-11-20 12:01:08 +01001// +build acceptance compute secgroups
2
3package v2
4
5import (
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/acceptance/tools"
10 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
11 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
12 "github.com/gophercloud/gophercloud/pagination"
13 th "github.com/gophercloud/gophercloud/testhelper"
Jamie Hannafordc5afec42014-11-20 12:01:08 +010014)
15
16func 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 Hannafordaea96c62014-11-25 12:05:04 +010044func createSecGroup(t *testing.T, client *gophercloud.ServiceClient) string {
Jamie Hannafordc5afec42014-11-20 12:01:08 +010045 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
58func 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 Hannafordaea96c62014-11-25 12:05:04 +010074func updateSecGroup(t *testing.T, client *gophercloud.ServiceClient, id, newName string) {
Jamie Hannaford19460b22014-11-24 16:04:17 +010075 opts := secgroups.UpdateOpts{
76 Name: newName,
77 Description: tools.RandomString("dec_", 10),
78 }
Jamie Hannafordc5afec42014-11-20 12:01:08 +010079 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 Hannafordaea96c62014-11-25 12:05:04 +010085func getSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannafordc5afec42014-11-20 12:01:08 +010086 group, err := secgroups.Get(client, id).Extract()
87 th.AssertNoErr(t, err)
88
Jamie Hannafordaea96c62014-11-25 12:05:04 +010089 t.Logf("Getting %s: %#v", id, group)
Jamie Hannafordc5afec42014-11-20 12:01:08 +010090}
91
Jamie Hannafordaea96c62014-11-25 12:05:04 +010092func addRemoveRules(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannaford04abbc72014-11-21 11:27:57 +010093 opts := secgroups.CreateRuleOpts{
Jamie Hannafordc5afec42014-11-20 12:01:08 +010094 ParentGroupID: id,
95 FromPort: 22,
96 ToPort: 22,
97 IPProtocol: "TCP",
98 CIDR: "0.0.0.0/0",
99 }
100
Jamie Hannaford04abbc72014-11-21 11:27:57 +0100101 rule, err := secgroups.CreateRule(client, opts).Extract()
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100102 th.AssertNoErr(t, err)
103
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100104 t.Logf("Adding rule %s to group %s", rule.ID, id)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100105
106 err = secgroups.DeleteRule(client, rule.ID).ExtractErr()
107 th.AssertNoErr(t, err)
108
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100109 t.Logf("Deleted rule %s from group %s", rule.ID, id)
Joe Topjian2893f7b2016-01-24 17:14:36 +0000110
111 icmpOpts := secgroups.CreateRuleOpts{
112 ParentGroupID: id,
113 FromPort: 0,
114 ToPort: 0,
115 IPProtocol: "ICMP",
116 CIDR: "0.0.0.0/0",
117 }
118
119 icmpRule, err := secgroups.CreateRule(client, icmpOpts).Extract()
120 th.AssertNoErr(t, err)
121
122 t.Logf("Adding ICMP rule %s to group %s", icmpRule.ID, id)
123
124 err = secgroups.DeleteRule(client, icmpRule.ID).ExtractErr()
125 th.AssertNoErr(t, err)
126
127 t.Logf("Deleted ICMP rule %s from group %s", icmpRule.ID, id)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100128}
129
130func findServer(t *testing.T, client *gophercloud.ServiceClient) (string, bool) {
131 var serverID string
132 var needsDeletion bool
133
134 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
135 sList, err := servers.ExtractServers(page)
136 th.AssertNoErr(t, err)
137
138 for _, s := range sList {
139 serverID = s.ID
140 needsDeletion = false
141
142 t.Logf("Found an existing server: ID [%s]", serverID)
143 break
144 }
145
146 return true, nil
147 })
148 th.AssertNoErr(t, err)
149
150 if serverID == "" {
151 t.Log("No server found, creating one")
152
153 choices, err := ComputeChoicesFromEnv()
154 th.AssertNoErr(t, err)
155
156 opts := &servers.CreateOpts{
157 Name: tools.RandomString("secgroup_test_", 5),
158 ImageRef: choices.ImageID,
159 FlavorRef: choices.FlavorID,
160 }
161
162 s, err := servers.Create(client, opts).Extract()
163 th.AssertNoErr(t, err)
164 serverID = s.ID
165
166 t.Logf("Created server %s, waiting for it to build", s.ID)
167 err = servers.WaitForStatus(client, serverID, "ACTIVE", 300)
168 th.AssertNoErr(t, err)
169
170 needsDeletion = true
171 }
172
173 return serverID, needsDeletion
174}
175
176func addServerToSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) {
177 err := secgroups.AddServerToGroup(client, serverID, groupName).ExtractErr()
178 th.AssertNoErr(t, err)
179
180 t.Logf("Adding group %s to server %s", groupName, serverID)
181}
182
183func removeServerFromSecGroup(t *testing.T, client *gophercloud.ServiceClient, serverID, groupName string) {
184 err := secgroups.RemoveServerFromGroup(client, serverID, groupName).ExtractErr()
185 th.AssertNoErr(t, err)
186
187 t.Logf("Removing group %s from server %s", groupName, serverID)
188}
189
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100190func deleteSecGroup(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100191 err := secgroups.Delete(client, id).ExtractErr()
192 th.AssertNoErr(t, err)
193
Jamie Hannafordaea96c62014-11-25 12:05:04 +0100194 t.Logf("Deleted group %s", id)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100195}