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 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 13 | func TestSecGroupsList(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 14 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 15 | if err != nil { |
| 16 | t.Fatalf("Unable to create a compute client: %v", err) |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 17 | } |
| 18 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 19 | 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 Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame^] | 30 | tools.PrintResource(t, secgroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 31 | } |
Jamie Hannaford | c5afec4 | 2014-11-20 12:01:08 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 34 | func TestSecGroupsCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 35 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 36 | if err != nil { |
| 37 | t.Fatalf("Unable to create a compute client: %v", err) |
| 38 | } |
| 39 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 40 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 41 | if err != nil { |
| 42 | t.Fatalf("Unable to create security group: %v", err) |
| 43 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 44 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func TestSecGroupsUpdate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 48 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 49 | if err != nil { |
| 50 | t.Fatalf("Unable to create a compute client: %v", err) |
| 51 | } |
| 52 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 53 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 54 | if err != nil { |
| 55 | t.Fatalf("Unable to create security group: %v", err) |
| 56 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 57 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 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 | |
| 71 | func TestSecGroupsRuleCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 72 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 73 | if err != nil { |
| 74 | t.Fatalf("Unable to create a compute client: %v", err) |
| 75 | } |
| 76 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 77 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 78 | if err != nil { |
| 79 | t.Fatalf("Unable to create security group: %v", err) |
| 80 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 81 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 82 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 83 | rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 84 | if err != nil { |
| 85 | t.Fatalf("Unable to create rule: %v", err) |
| 86 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 87 | defer DeleteSecurityGroupRule(t, client, rule) |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame^] | 88 | |
| 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 Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func TestSecGroupsAddGroupToServer(t *testing.T) { |
| 99 | if testing.Short() { |
| 100 | t.Skip("Skipping test that requires server creation in short mode.") |
| 101 | } |
| 102 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 103 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 104 | if err != nil { |
| 105 | t.Fatalf("Unable to create a compute client: %v", err) |
| 106 | } |
| 107 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame^] | 108 | server, err := CreateServer(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 109 | if err != nil { |
| 110 | t.Fatalf("Unable to create server: %v", err) |
| 111 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 112 | defer DeleteServer(t, client, server) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 113 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 114 | securityGroup, err := CreateSecurityGroup(t, client) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 115 | if err != nil { |
| 116 | t.Fatalf("Unable to create security group: %v", err) |
| 117 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 118 | defer DeleteSecurityGroup(t, client, securityGroup) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 119 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 120 | rule, err := CreateSecurityGroupRule(t, client, securityGroup.ID) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 121 | if err != nil { |
| 122 | t.Fatalf("Unable to create rule: %v", err) |
| 123 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 124 | defer DeleteSecurityGroupRule(t, client, rule) |
Joe Topjian | 3d12737 | 2016-07-25 14:59:58 +0000 | [diff] [blame] | 125 | |
| 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 | } |