blob: 0ee47ea1f6942b89c5b2dfdb3d4f40c250e2d081 [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
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/acceptance/tools"
10 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/schedulerhints"
11 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
12 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Joe Topjianc9fb21b2015-02-22 05:55:48 +000013)
14
Joe Topjian6aa8ba02016-07-24 16:12:30 +000015func TestServergroupsList(t *testing.T) {
16 client, err := newClient()
17 if err != nil {
18 t.Fatalf("Unable to create a compute client: %v", err)
19 }
20
21 allPages, err := servergroups.List(client).AllPages()
22 if err != nil {
23 t.Fatalf("Unable to list server groups: %v", err)
24 }
25
26 allServerGroups, err := servergroups.ExtractServerGroups(allPages)
27 if err != nil {
28 t.Fatalf("Unable to extract server groups: %v", err)
29 }
30
31 for _, serverGroup := range allServerGroups {
32 printServerGroup(t, &serverGroup)
33 }
34}
35
36func TestServergroupsCreate(t *testing.T) {
37 client, err := newClient()
38 if err != nil {
39 t.Fatalf("Unable to create a compute client: %v", err)
40 }
41
42 serverGroup, err := createServerGroup(t, client, "anti-affinity")
43 if err != nil {
44 t.Fatalf("Unable to create server group: %v", err)
45 }
46 defer deleteServerGroup(t, client, serverGroup)
47
48 serverGroup, err = servergroups.Get(client, serverGroup.ID).Extract()
49 if err != nil {
50 t.Fatalf("Unable to get server group: %v", err)
51 }
52
53 printServerGroup(t, serverGroup)
54}
55
56func TestServergroupsAffinityPolicy(t *testing.T) {
57 client, err := newClient()
58 if err != nil {
59 t.Fatalf("Unable to create a compute client: %v", err)
60 }
61
62 choices, err := ComputeChoicesFromEnv()
63 if err != nil {
64 t.Fatal(err)
65 }
66
67 serverGroup, err := createServerGroup(t, client, "affinity")
68 if err != nil {
69 t.Fatalf("Unable to create server group: %v", err)
70 }
71 defer deleteServerGroup(t, client, serverGroup)
72
73 firstServer, err := createServerInServerGroup(t, client, choices, serverGroup)
74 if err != nil {
75 t.Fatalf("Unable to create server: %v", err)
76 }
77
78 if err = waitForStatus(client, firstServer, "ACTIVE"); err != nil {
79 t.Fatalf("Unable to wait for server: %v", err)
80 }
81 defer deleteServer(t, client, firstServer)
82
83 firstServer, err = servers.Get(client, firstServer.ID).Extract()
84
85 secondServer, err := createServerInServerGroup(t, client, choices, serverGroup)
86 if err != nil {
87 t.Fatalf("Unable to create server: %v", err)
88 }
89
90 if err = waitForStatus(client, secondServer, "ACTIVE"); err != nil {
91 t.Fatalf("Unable to wait for server: %v", err)
92 }
93 defer deleteServer(t, client, secondServer)
94
95 secondServer, err = servers.Get(client, secondServer.ID).Extract()
96
97 if firstServer.HostID != secondServer.HostID {
98 t.Fatalf("%s and %s were not scheduled on the same host.", firstServer.ID, secondServer.ID)
99 }
100}
101
102func createServerGroup(t *testing.T, client *gophercloud.ServiceClient, policy string) (*servergroups.ServerGroup, error) {
103 sg, err := servergroups.Create(client, &servergroups.CreateOpts{
Joe Topjianc9fb21b2015-02-22 05:55:48 +0000104 Name: "test",
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000105 Policies: []string{policy},
Joe Topjianc9fb21b2015-02-22 05:55:48 +0000106 }).Extract()
107
108 if err != nil {
109 t.Fatalf("Unable to create server group: %v", err)
110 }
111
Joe Topjianc9fb21b2015-02-22 05:55:48 +0000112 return sg, nil
113}
114
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000115func createServerInServerGroup(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices, serverGroup *servergroups.ServerGroup) (*servers.Server, error) {
Joe Topjian27a62432015-04-18 01:48:50 +0000116 if testing.Short() {
117 t.Skip("Skipping test that requires server creation in short mode.")
118 }
119
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000120 networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName)
121 if err != nil {
122 t.Fatalf("Failed to obtain network ID: %v", err)
123 }
124
Joe Topjian27a62432015-04-18 01:48:50 +0000125 name := tools.RandomString("ACPTTEST", 16)
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000126 t.Logf("Attempting to create server: %s", name)
Joe Topjian27a62432015-04-18 01:48:50 +0000127
128 pwd := tools.MakeNewPassword("")
129
130 serverCreateOpts := servers.CreateOpts{
131 Name: name,
132 FlavorRef: choices.FlavorID,
133 ImageRef: choices.ImageID,
134 AdminPass: pwd,
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000135 Networks: []servers.Network{
136 servers.Network{UUID: networkID},
137 },
Joe Topjian27a62432015-04-18 01:48:50 +0000138 }
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000139
140 schedulerHintsOpts := schedulerhints.CreateOptsExt{
Joe Topjian27a62432015-04-18 01:48:50 +0000141 serverCreateOpts,
142 schedulerhints.SchedulerHints{
143 Group: serverGroup.ID,
144 },
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000145 }
146 server, err := servers.Create(client, schedulerHintsOpts).Extract()
Joe Topjian27a62432015-04-18 01:48:50 +0000147 if err != nil {
148 t.Fatalf("Unable to create server: %v", err)
149 }
150
Joe Topjian27a62432015-04-18 01:48:50 +0000151 return server, err
152}
153
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000154func deleteServerGroup(t *testing.T, client *gophercloud.ServiceClient, serverGroup *servergroups.ServerGroup) {
155 err := servergroups.Delete(client, serverGroup.ID).ExtractErr()
156 if err != nil {
157 t.Fatalf("Unable to delete server group %s: %v", serverGroup.ID, err)
Joe Topjian27a62432015-04-18 01:48:50 +0000158 }
159
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000160 t.Logf("Deleted server group %s", serverGroup.ID)
Joe Topjian27a62432015-04-18 01:48:50 +0000161}
162
Joe Topjian6aa8ba02016-07-24 16:12:30 +0000163func printServerGroup(t *testing.T, serverGroup *servergroups.ServerGroup) {
164 t.Logf("ID: %s", serverGroup.ID)
165 t.Logf("Name: %s", serverGroup.Name)
166 t.Logf("Policies: %#v", serverGroup.Policies)
167 t.Logf("Members: %#v", serverGroup.Members)
168 t.Logf("Metadata: %#v", serverGroup.Metadata)
Joe Topjianc9fb21b2015-02-22 05:55:48 +0000169}