blob: ac1915f0601129c7161c41f58fb67633e4214f1a [file] [log] [blame]
Joe Topjian6aa8ba02016-07-24 16:12:30 +00001// +build acceptance compute servergroups
Joe Topjianc9fb21b2015-02-22 05:55:48 +00002
3package v2
4
5import (
6 "testing"
7
Joe Topjian1c15e3f2016-08-08 10:48:38 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Joe Topjian66a046c2017-01-19 22:07:26 -07009 "github.com/gophercloud/gophercloud/acceptance/tools"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
11 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Joe Topjianc9fb21b2015-02-22 05:55:48 +000012)
13
Joe Topjian6aa8ba02016-07-24 16:12:30 +000014func TestServergroupsList(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060015 client, err := clients.NewComputeV2Client()
Joe Topjian6aa8ba02016-07-24 16:12:30 +000016 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 Topjian66a046c2017-01-19 22:07:26 -070031 tools.PrintResource(t, serverGroup)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000032 }
33}
34
35func TestServergroupsCreate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060036 client, err := clients.NewComputeV2Client()
Joe Topjian6aa8ba02016-07-24 16:12:30 +000037 if err != nil {
38 t.Fatalf("Unable to create a compute client: %v", err)
39 }
40
Joe Topjian1c15e3f2016-08-08 10:48:38 -060041 serverGroup, err := CreateServerGroup(t, client, "anti-affinity")
Joe Topjian6aa8ba02016-07-24 16:12:30 +000042 if err != nil {
43 t.Fatalf("Unable to create server group: %v", err)
44 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060045 defer DeleteServerGroup(t, client, serverGroup)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000046
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 Topjian66a046c2017-01-19 22:07:26 -070052 tools.PrintResource(t, serverGroup)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000053}
54
55func TestServergroupsAffinityPolicy(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060056 client, err := clients.NewComputeV2Client()
Joe Topjian6aa8ba02016-07-24 16:12:30 +000057 if err != nil {
58 t.Fatalf("Unable to create a compute client: %v", err)
59 }
60
Joe Topjian1c15e3f2016-08-08 10:48:38 -060061 serverGroup, err := CreateServerGroup(t, client, "affinity")
Joe Topjian6aa8ba02016-07-24 16:12:30 +000062 if err != nil {
63 t.Fatalf("Unable to create server group: %v", err)
64 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060065 defer DeleteServerGroup(t, client, serverGroup)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000066
Joe Topjian66a046c2017-01-19 22:07:26 -070067 firstServer, err := CreateServerInServerGroup(t, client, serverGroup)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000068 if err != nil {
69 t.Fatalf("Unable to create server: %v", err)
70 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060071 defer DeleteServer(t, client, firstServer)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000072
73 firstServer, err = servers.Get(client, firstServer.ID).Extract()
74
Joe Topjian66a046c2017-01-19 22:07:26 -070075 secondServer, err := CreateServerInServerGroup(t, client, serverGroup)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000076 if err != nil {
77 t.Fatalf("Unable to create server: %v", err)
78 }
79
Joe Topjian1c15e3f2016-08-08 10:48:38 -060080 if err = WaitForComputeStatus(client, secondServer, "ACTIVE"); err != nil {
Joe Topjian6aa8ba02016-07-24 16:12:30 +000081 t.Fatalf("Unable to wait for server: %v", err)
82 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060083 defer DeleteServer(t, client, secondServer)
Joe Topjian6aa8ba02016-07-24 16:12:30 +000084
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}