blob: 62fb3417430b0ce35980489531fdd6461a980ffe [file] [log] [blame]
Ash Wilson88074d52014-10-21 10:04:17 -04001// +build acceptance
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/acceptance/tools"
10 os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Ash Wilson74b9bcf2014-10-21 14:03:40 -040011 "github.com/rackspace/gophercloud/pagination"
Ash Wilson88074d52014-10-21 10:04:17 -040012 "github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func createServer(t *testing.T, client *gophercloud.ServiceClient) *os.Server {
17 options, err := optionsFromEnv()
18 th.AssertNoErr(t, err)
19
Ash Wilson7e571412014-10-21 14:29:55 -040020 name := tools.RandomString("Gophercloud-", 8)
21 t.Logf("Creating server [%s].", name)
22 s, err := servers.Create(client, &os.CreateOpts{
23 Name: name,
Ash Wilson88074d52014-10-21 10:04:17 -040024 ImageRef: options.imageID,
25 FlavorRef: options.flavorID,
Ash Wilson7e571412014-10-21 14:29:55 -040026 }).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040027 th.AssertNoErr(t, err)
Ash Wilson7e571412014-10-21 14:29:55 -040028 t.Logf("Server created successfully.")
Ash Wilson88074d52014-10-21 10:04:17 -040029 return s
30}
31
32func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
Ash Wilson7e571412014-10-21 14:29:55 -040033 t.Logf("Deleting server [%s].", server.ID)
Ash Wilson88074d52014-10-21 10:04:17 -040034 err := servers.Delete(client, server.ID)
35 th.AssertNoErr(t, err)
Ash Wilson7e571412014-10-21 14:29:55 -040036 t.Logf("Server deleted successfully.")
Ash Wilson88074d52014-10-21 10:04:17 -040037}
38
39func logServer(t *testing.T, server *os.Server, index int) {
40 if index == -1 {
41 t.Logf(" id=[%s]", server.ID)
42 } else {
43 t.Logf("[%02d] id=[%s]", index, server.ID)
44 }
45 t.Logf(" name=[%s]", server.Name)
46 t.Logf(" tenant ID=[%s]", server.TenantID)
47 t.Logf(" user ID=[%s]", server.UserID)
48 t.Logf(" updated=[%s]", server.Updated)
49 t.Logf(" created=[%s]", server.Created)
50 t.Logf(" host ID=[%s]", server.HostID)
51 t.Logf(" access IPv4=[%s]", server.AccessIPv4)
52 t.Logf(" access IPv6=[%s]", server.AccessIPv6)
53 t.Logf(" image=[%v]", server.Image)
54 t.Logf(" flavor=[%v]", server.Flavor)
55 t.Logf(" addresses=[%v]", server.Addresses)
56 t.Logf(" metadata=[%v]", server.Metadata)
57 t.Logf(" links=[%v]", server.Links)
58 t.Logf(" keyname=[%s]", server.KeyName)
59 t.Logf(" admin password=[%s]", server.AdminPass)
60 t.Logf(" status=[%s]", server.Status)
61 t.Logf(" progress=[%d]", server.Progress)
62}
63
64func TestCreateServer(t *testing.T) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -040065 t.Parallel()
66
Ash Wilson88074d52014-10-21 10:04:17 -040067 client, err := newClient()
68 th.AssertNoErr(t, err)
69
Ash Wilson88074d52014-10-21 10:04:17 -040070 s := createServer(t, client)
Ash Wilson7e571412014-10-21 14:29:55 -040071 defer deleteServer(t, client, s)
Ash Wilson88074d52014-10-21 10:04:17 -040072
73 t.Logf("Waiting for server to become active ...")
74 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
75 th.AssertNoErr(t, err)
76
Ash Wilson7e571412014-10-21 14:29:55 -040077 t.Logf("Server launched.")
Ash Wilson88074d52014-10-21 10:04:17 -040078 logServer(t, s, -1)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040079
Ash Wilson7e571412014-10-21 14:29:55 -040080 t.Logf("Getting additional server details.")
81 details, err := servers.Get(client, s.ID).Extract()
Ash Wilson74b9bcf2014-10-21 14:03:40 -040082 logServer(t, details, -1)
83}
84
85func TestListServers(t *testing.T) {
86 t.Parallel()
87
88 client, err := newClient()
89 th.AssertNoErr(t, err)
90
91 count := 0
92 err = servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
93 count++
94 t.Logf("-- Page %02d --", count)
95
Ash Wilson74b9bcf2014-10-21 14:03:40 -040096 s, err := servers.ExtractServers(page)
97 th.AssertNoErr(t, err)
98 for index, server := range s {
99 logServer(t, &server, index)
100 }
101
102 return true, nil
103 })
104 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -0400105}
Ash Wilson86793322014-10-21 14:30:09 -0400106
107func TestChangeAdminPassword(t *testing.T) {
108 t.Parallel()
109
110 client, err := newClient()
111 th.AssertNoErr(t, err)
112
113 s := createServer(t, client)
114 defer deleteServer(t, client, s)
115 original := s.AdminPass
116
117 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
118 th.AssertNoErr(t, err)
119
Ash Wilson88bbde32014-10-21 15:13:58 -0400120 t.Logf("Changing server password.")
Ash Wilson86793322014-10-21 14:30:09 -0400121 err = servers.ChangeAdminPassword(client, s.ID, tools.MakeNewPassword(original)).Extract()
122 th.AssertNoErr(t, err)
123
124 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
125 th.AssertNoErr(t, err)
Ash Wilson88bbde32014-10-21 15:13:58 -0400126 t.Logf("Password changed successfully.")
127}
128
129func TestReboot(t *testing.T) {
130 t.Parallel()
131
132 client, err := newClient()
133 th.AssertNoErr(t, err)
134
135 s := createServer(t, client)
136 defer deleteServer(t, client, s)
137
138 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
139 th.AssertNoErr(t, err)
140
141 t.Logf("Rebooting server.")
142 err = servers.Reboot(client, s.ID, os.HardReboot).Extract()
143 th.AssertNoErr(t, err)
144
145 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
146 th.AssertNoErr(t, err)
147 t.Logf("Server successfully rebooted.")
Ash Wilson86793322014-10-21 14:30:09 -0400148}
Ash Wilson90f87ba2014-10-21 18:36:18 -0400149
150func TestRebuild(t *testing.T) {
151 t.Parallel()
152
153 options, err := optionsFromEnv()
154 th.AssertNoErr(t, err)
155
156 client, err := newClient()
157 th.AssertNoErr(t, err)
158
159 s := createServer(t, client)
160 defer deleteServer(t, client, s)
161
162 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
163 th.AssertNoErr(t, err)
164
165 t.Logf("Rebuilding server.")
166 opts := os.RebuildOpts{
167 Name: tools.RandomString("RenamedGopher", 16),
168 AdminPass: tools.MakeNewPassword(s.AdminPass),
169 ImageID: options.imageID,
170 }
171 after, err := servers.Rebuild(client, s.ID, opts).Extract()
172 th.AssertNoErr(t, err)
173
174 err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
175 th.AssertNoErr(t, err)
176
177 t.Logf("Server successfully rebuilt.")
178 logServer(t, after, -1)
179}