blob: 945854e3b27f30fe40cbd015a926f3e8d5bc5608 [file] [log] [blame]
Joe Topjianc9fb21b2015-02-22 05:55:48 +00001// +build acceptance compute servers
2
3package v2
4
5import (
Joe Topjian27a62432015-04-18 01:48:50 +00006 "fmt"
Joe Topjianc9fb21b2015-02-22 05:55:48 +00007 "testing"
8
9 "github.com/rackspace/gophercloud"
Joe Topjian27a62432015-04-18 01:48:50 +000010 "github.com/rackspace/gophercloud/acceptance/tools"
11 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/schedulerhints"
Joe Topjianc9fb21b2015-02-22 05:55:48 +000012 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/servergroups"
Joe Topjian27a62432015-04-18 01:48:50 +000013 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
14 th "github.com/rackspace/gophercloud/testhelper"
Joe Topjianc9fb21b2015-02-22 05:55:48 +000015)
16
17func createServerGroup(t *testing.T, computeClient *gophercloud.ServiceClient) (*servergroups.ServerGroup, error) {
18 sg, err := servergroups.Create(computeClient, &servergroups.CreateOpts{
19 Name: "test",
20 Policies: []string{"affinity"},
21 }).Extract()
22
23 if err != nil {
24 t.Fatalf("Unable to create server group: %v", err)
25 }
26
27 t.Logf("Created server group: %v", sg.ID)
28 t.Logf("It has policies: %v", sg.Policies)
29
30 return sg, nil
31}
32
33func getServerGroup(t *testing.T, computeClient *gophercloud.ServiceClient, sgID string) error {
34 sg, err := servergroups.Get(computeClient, sgID).Extract()
35 if err != nil {
36 t.Fatalf("Unable to get server group: %v", err)
37 }
38
39 t.Logf("Got server group: %v", sg.Name)
40
41 return nil
42}
43
Joe Topjian27a62432015-04-18 01:48:50 +000044func createServerInGroup(t *testing.T, computeClient *gophercloud.ServiceClient, choices *ComputeChoices, serverGroup *servergroups.ServerGroup) (*servers.Server, error) {
45 if testing.Short() {
46 t.Skip("Skipping test that requires server creation in short mode.")
47 }
48
49 name := tools.RandomString("ACPTTEST", 16)
50 t.Logf("Attempting to create server: %s\n", name)
51
52 pwd := tools.MakeNewPassword("")
53
54 serverCreateOpts := servers.CreateOpts{
55 Name: name,
56 FlavorRef: choices.FlavorID,
57 ImageRef: choices.ImageID,
58 AdminPass: pwd,
59 }
60 server, err := servers.Create(computeClient, schedulerhints.CreateOptsExt{
61 serverCreateOpts,
62 schedulerhints.SchedulerHints{
63 Group: serverGroup.ID,
64 },
65 }).Extract()
66 if err != nil {
67 t.Fatalf("Unable to create server: %v", err)
68 }
69
70 th.AssertEquals(t, pwd, server.AdminPass)
71
72 return server, err
73}
74
75func verifySchedulerWorked(t *testing.T, firstServer, secondServer *servers.Server) error {
76 t.Logf("First server hostID: %v", firstServer.HostID)
77 t.Logf("Second server hostID: %v", secondServer.HostID)
78 if firstServer.HostID == secondServer.HostID {
79 return nil
80 }
81
82 return fmt.Errorf("%s and %s were not scheduled on the same host.", firstServer.ID, secondServer.ID)
83}
84
Joe Topjianc9fb21b2015-02-22 05:55:48 +000085func TestServerGroups(t *testing.T) {
Joe Topjian27a62432015-04-18 01:48:50 +000086 choices, err := ComputeChoicesFromEnv()
87 if err != nil {
88 t.Fatal(err)
89 }
90
Joe Topjianc9fb21b2015-02-22 05:55:48 +000091 computeClient, err := newClient()
92 if err != nil {
93 t.Fatalf("Unable to create a compute client: %v", err)
94 }
95
96 sg, err := createServerGroup(t, computeClient)
97 if err != nil {
98 t.Fatalf("Unable to create server group: %v", err)
99 }
100 defer func() {
101 servergroups.Delete(computeClient, sg.ID)
Joe Topjian27a62432015-04-18 01:48:50 +0000102 t.Logf("Server Group deleted.")
Joe Topjianc9fb21b2015-02-22 05:55:48 +0000103 }()
104
105 err = getServerGroup(t, computeClient, sg.ID)
106 if err != nil {
107 t.Fatalf("Unable to get server group: %v", err)
108 }
Joe Topjian27a62432015-04-18 01:48:50 +0000109
110 firstServer, err := createServerInGroup(t, computeClient, choices, sg)
111 if err != nil {
112 t.Fatalf("Unable to create server: %v", err)
113 }
114 defer func() {
115 servers.Delete(computeClient, firstServer.ID)
116 t.Logf("Server deleted.")
117 }()
118
119 if err = waitForStatus(computeClient, firstServer, "ACTIVE"); err != nil {
120 t.Fatalf("Unable to wait for server: %v", err)
121 }
122
123 firstServer, err = servers.Get(computeClient, firstServer.ID).Extract()
124
125 secondServer, err := createServerInGroup(t, computeClient, choices, sg)
126 if err != nil {
127 t.Fatalf("Unable to create server: %v", err)
128 }
129 defer func() {
130 servers.Delete(computeClient, secondServer.ID)
131 t.Logf("Server deleted.")
132 }()
133
134 if err = waitForStatus(computeClient, secondServer, "ACTIVE"); err != nil {
135 t.Fatalf("Unable to wait for server: %v", err)
136 }
137
138 secondServer, err = servers.Get(computeClient, secondServer.ID).Extract()
139
140 if err = verifySchedulerWorked(t, firstServer, secondServer); err != nil {
141 t.Fatalf("Scheduling did not work: %v", err)
142 }
Joe Topjianc9fb21b2015-02-22 05:55:48 +0000143}