Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 1 | // +build acceptance compute servergroups |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 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" |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" |
| 11 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 12 | ) |
| 13 | |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 14 | func TestServergroupsList(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 15 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 16 | if err != nil { |
| 17 | t.Fatalf("Unable to create a compute client: %v", err) |
| 18 | } |
| 19 | |
| 20 | allPages, err := servergroups.List(client).AllPages() |
| 21 | if err != nil { |
| 22 | t.Fatalf("Unable to list server groups: %v", err) |
| 23 | } |
| 24 | |
| 25 | allServerGroups, err := servergroups.ExtractServerGroups(allPages) |
| 26 | if err != nil { |
| 27 | t.Fatalf("Unable to extract server groups: %v", err) |
| 28 | } |
| 29 | |
| 30 | for _, serverGroup := range allServerGroups { |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 31 | tools.PrintResource(t, serverGroup) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestServergroupsCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 36 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 37 | if err != nil { |
| 38 | t.Fatalf("Unable to create a compute client: %v", err) |
| 39 | } |
| 40 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 41 | serverGroup, err := CreateServerGroup(t, client, "anti-affinity") |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 42 | if err != nil { |
| 43 | t.Fatalf("Unable to create server group: %v", err) |
| 44 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 45 | defer DeleteServerGroup(t, client, serverGroup) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 46 | |
| 47 | serverGroup, err = servergroups.Get(client, serverGroup.ID).Extract() |
| 48 | if err != nil { |
| 49 | t.Fatalf("Unable to get server group: %v", err) |
| 50 | } |
| 51 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 52 | tools.PrintResource(t, serverGroup) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func TestServergroupsAffinityPolicy(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 56 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 57 | if err != nil { |
| 58 | t.Fatalf("Unable to create a compute client: %v", err) |
| 59 | } |
| 60 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 61 | serverGroup, err := CreateServerGroup(t, client, "affinity") |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 62 | if err != nil { |
| 63 | t.Fatalf("Unable to create server group: %v", err) |
| 64 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 65 | defer DeleteServerGroup(t, client, serverGroup) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 66 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 67 | firstServer, err := CreateServerInServerGroup(t, client, serverGroup) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 68 | if err != nil { |
| 69 | t.Fatalf("Unable to create server: %v", err) |
| 70 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 71 | defer DeleteServer(t, client, firstServer) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 72 | |
| 73 | firstServer, err = servers.Get(client, firstServer.ID).Extract() |
| 74 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 75 | secondServer, err := CreateServerInServerGroup(t, client, serverGroup) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 76 | if err != nil { |
| 77 | t.Fatalf("Unable to create server: %v", err) |
| 78 | } |
| 79 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 80 | if err = WaitForComputeStatus(client, secondServer, "ACTIVE"); err != nil { |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 81 | t.Fatalf("Unable to wait for server: %v", err) |
| 82 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 83 | defer DeleteServer(t, client, secondServer) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 84 | |
| 85 | secondServer, err = servers.Get(client, secondServer.ID).Extract() |
| 86 | |
| 87 | if firstServer.HostID != secondServer.HostID { |
| 88 | t.Fatalf("%s and %s were not scheduled on the same host.", firstServer.ID, secondServer.ID) |
| 89 | } |
| 90 | } |