blob: c0d023037d1570ca766e859e75fc08735dfdd49a [file] [log] [blame]
Jamie Hannafordc5afec42014-11-20 12:01:08 +01001// +build acceptance compute secgroups
2
3package v2
4
5import (
6 "testing"
7
Joe Topjian1c15e3f2016-08-08 10:48:38 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Jon Perritt27249f42016-02-18 10:35:59 -06009 "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) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060014 client, err := clients.NewComputeV2Client()
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 {
Joe Topjian66a046c2017-01-19 22:07:26 -070030 tools.PrintResource(t, secgroup)
Joe Topjian3d127372016-07-25 14:59:58 +000031 }
Jamie Hannafordc5afec42014-11-20 12:01:08 +010032}
33
Joe Topjian3d127372016-07-25 14:59:58 +000034func TestSecGroupsCreate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060035 client, err := clients.NewComputeV2Client()
Joe Topjian3d127372016-07-25 14:59:58 +000036 if err != nil {
37 t.Fatalf("Unable to create a compute client: %v", err)
38 }
39
Joe Topjian1c15e3f2016-08-08 10:48:38 -060040 securityGroup, err := CreateSecurityGroup(t, client)
Joe Topjian3d127372016-07-25 14:59:58 +000041 if err != nil {
42 t.Fatalf("Unable to create security group: %v", err)
43 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060044 defer DeleteSecurityGroup(t, client, securityGroup)
Joe Topjian3d127372016-07-25 14:59:58 +000045}
46
47func TestSecGroupsUpdate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060048 client, err := clients.NewComputeV2Client()
Joe Topjian3d127372016-07-25 14:59:58 +000049 if err != nil {
50 t.Fatalf("Unable to create a compute client: %v", err)
51 }
52
Joe Topjian1c15e3f2016-08-08 10:48:38 -060053 securityGroup, err := CreateSecurityGroup(t, client)
Joe Topjian3d127372016-07-25 14:59:58 +000054 if err != nil {
55 t.Fatalf("Unable to create security group: %v", err)
56 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060057 defer DeleteSecurityGroup(t, client, securityGroup)
Joe Topjian3d127372016-07-25 14:59:58 +000058
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) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060072 client, err := clients.NewComputeV2Client()
Joe Topjian3d127372016-07-25 14:59:58 +000073 if err != nil {
74 t.Fatalf("Unable to create a compute client: %v", err)
75 }
76
Joe Topjian1c15e3f2016-08-08 10:48:38 -060077 securityGroup, err := CreateSecurityGroup(t, client)
Joe Topjian3d127372016-07-25 14:59:58 +000078 if err != nil {
79 t.Fatalf("Unable to create security group: %v", err)
80 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060081 defer DeleteSecurityGroup(t, client, securityGroup)
Joe Topjian3d127372016-07-25 14:59:58 +000082
Joe Topjian1c15e3f2016-08-08 10:48:38 -060083 rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID)
Joe Topjian3d127372016-07-25 14:59:58 +000084 if err != nil {
85 t.Fatalf("Unable to create rule: %v", err)
86 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060087 defer DeleteSecurityGroupRule(t, client, rule)
Joe Topjian66a046c2017-01-19 22:07:26 -070088
89 newSecurityGroup, err := secgroups.Get(client, securityGroup.ID).Extract()
90 if err != nil {
91 t.Fatalf("Unable to obtain security group: %v", err)
92 }
93
94 tools.PrintResource(t, newSecurityGroup)
95
Joe Topjian3d127372016-07-25 14:59:58 +000096}
97
98func TestSecGroupsAddGroupToServer(t *testing.T) {
99 if testing.Short() {
100 t.Skip("Skipping test that requires server creation in short mode.")
101 }
102
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600103 client, err := clients.NewComputeV2Client()
Joe Topjian3d127372016-07-25 14:59:58 +0000104 if err != nil {
105 t.Fatalf("Unable to create a compute client: %v", err)
106 }
107
Joe Topjian66a046c2017-01-19 22:07:26 -0700108 server, err := CreateServer(t, client)
Joe Topjian3d127372016-07-25 14:59:58 +0000109 if err != nil {
110 t.Fatalf("Unable to create server: %v", err)
111 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600112 defer DeleteServer(t, client, server)
Joe Topjian3d127372016-07-25 14:59:58 +0000113
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600114 securityGroup, err := CreateSecurityGroup(t, client)
Joe Topjian3d127372016-07-25 14:59:58 +0000115 if err != nil {
116 t.Fatalf("Unable to create security group: %v", err)
117 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600118 defer DeleteSecurityGroup(t, client, securityGroup)
Joe Topjian3d127372016-07-25 14:59:58 +0000119
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600120 rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID)
Joe Topjian3d127372016-07-25 14:59:58 +0000121 if err != nil {
122 t.Fatalf("Unable to create rule: %v", err)
123 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600124 defer DeleteSecurityGroupRule(t, client, rule)
Joe Topjian3d127372016-07-25 14:59:58 +0000125
126 t.Logf("Adding group %s to server %s", securityGroup.ID, server.ID)
127 err = secgroups.AddServer(client, server.ID, securityGroup.Name).ExtractErr()
128 if err != nil && err.Error() != "EOF" {
129 t.Fatalf("Unable to add group %s to server %s: %s", securityGroup.ID, server.ID, err)
130 }
131
132 t.Logf("Removing group %s from server %s", securityGroup.ID, server.ID)
133 err = secgroups.RemoveServer(client, server.ID, securityGroup.Name).ExtractErr()
134 if err != nil && err.Error() != "EOF" {
135 t.Fatalf("Unable to remove group %s from server %s: %s", securityGroup.ID, server.ID, err)
136 }
137}