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