blob: 8a2e7f7972bfa876adcfc1b3bfbdf8ead394f11a [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"
Jamie Hannafordc5afec42014-11-20 12:01:08 +010011)
12
Joe Topjian3d127372016-07-25 14:59:58 +000013func TestSecGroupsList(t *testing.T) {
Jamie Hannafordc5afec42014-11-20 12:01:08 +010014 client, err := newClient()
Joe Topjian3d127372016-07-25 14:59:58 +000015 if err != nil {
16 t.Fatalf("Unable to create a compute client: %v", err)
Jamie Hannafordc5afec42014-11-20 12:01:08 +010017 }
18
Joe Topjian3d127372016-07-25 14:59:58 +000019 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 Hannafordc5afec42014-11-20 12:01:08 +010032}
33
Joe Topjian3d127372016-07-25 14:59:58 +000034func 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
47func 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
71func 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
90func 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
140func createSecurityGroup(t *testing.T, client *gophercloud.ServiceClient) (secgroups.SecurityGroup, error) {
141 createOpts := secgroups.CreateOpts{
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100142 Name: tools.RandomString("secgroup_", 5),
143 Description: "something",
144 }
145
Joe Topjian3d127372016-07-25 14:59:58 +0000146 securityGroup, err := secgroups.Create(client, createOpts).Extract()
147 if err != nil {
148 return *securityGroup, err
Jamie Hannaford19460b22014-11-24 16:04:17 +0100149 }
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100150
Joe Topjian3d127372016-07-25 14:59:58 +0000151 t.Logf("Created security group: %s", securityGroup.ID)
152 return *securityGroup, nil
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100153}
154
Joe Topjian3d127372016-07-25 14:59:58 +0000155func 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 Hannafordc5afec42014-11-20 12:01:08 +0100160
Joe Topjian3d127372016-07-25 14:59:58 +0000161 t.Logf("Deleted security group: %s", securityGroup.ID)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100162}
163
Joe Topjian3d127372016-07-25 14:59:58 +0000164func 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 Hannafordc5afec42014-11-20 12:01:08 +0100169 IPProtocol: "TCP",
170 CIDR: "0.0.0.0/0",
171 }
172
Joe Topjian3d127372016-07-25 14:59:58 +0000173 rule, err := secgroups.CreateRule(client, createOpts).Extract()
174 if err != nil {
175 return *rule, err
Joe Topjian2893f7b2016-01-24 17:14:36 +0000176 }
177
Joe Topjian3d127372016-07-25 14:59:58 +0000178 t.Logf("Created security group rule: %s", rule.ID)
179 return *rule, nil
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100180}
181
Joe Topjian3d127372016-07-25 14:59:58 +0000182func 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 Hannafordc5afec42014-11-20 12:01:08 +0100186 }
187
Joe Topjian3d127372016-07-25 14:59:58 +0000188 t.Logf("Deleted security group rule: %s", rule.ID)
Jamie Hannafordc5afec42014-11-20 12:01:08 +0100189}
190
Joe Topjian3d127372016-07-25 14:59:58 +0000191func 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 Hannafordc5afec42014-11-20 12:01:08 +0100197
Joe Topjian3d127372016-07-25 14:59:58 +0000198 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 Hannafordc5afec42014-11-20 12:01:08 +0100208}