blob: 6247c260ac07a55820e857d9d9ec66d015b9d660 [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 Wilson5b32ca72014-10-22 08:11:37 -040028 t.Logf("Creating server.")
Ash Wilson88074d52014-10-21 10:04:17 -040029
Ash Wilson5b32ca72014-10-22 08:11:37 -040030 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
Ash Wilson88074d52014-10-21 10:04:17 -040031 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040032 t.Logf("Server created successfully.")
33
34 return s
Ash Wilson88074d52014-10-21 10:04:17 -040035}
36
37func logServer(t *testing.T, server *os.Server, index int) {
38 if index == -1 {
39 t.Logf(" id=[%s]", server.ID)
40 } else {
41 t.Logf("[%02d] id=[%s]", index, server.ID)
42 }
43 t.Logf(" name=[%s]", server.Name)
44 t.Logf(" tenant ID=[%s]", server.TenantID)
45 t.Logf(" user ID=[%s]", server.UserID)
46 t.Logf(" updated=[%s]", server.Updated)
47 t.Logf(" created=[%s]", server.Created)
48 t.Logf(" host ID=[%s]", server.HostID)
49 t.Logf(" access IPv4=[%s]", server.AccessIPv4)
50 t.Logf(" access IPv6=[%s]", server.AccessIPv6)
51 t.Logf(" image=[%v]", server.Image)
52 t.Logf(" flavor=[%v]", server.Flavor)
53 t.Logf(" addresses=[%v]", server.Addresses)
54 t.Logf(" metadata=[%v]", server.Metadata)
55 t.Logf(" links=[%v]", server.Links)
56 t.Logf(" keyname=[%s]", server.KeyName)
57 t.Logf(" admin password=[%s]", server.AdminPass)
58 t.Logf(" status=[%s]", server.Status)
59 t.Logf(" progress=[%d]", server.Progress)
60}
61
Ash Wilson5b32ca72014-10-22 08:11:37 -040062func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
63 t.Logf("> servers.Get")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040064
Ash Wilson5b32ca72014-10-22 08:11:37 -040065 details, err := servers.Get(client, server.ID).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040066 th.AssertNoErr(t, err)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040067 logServer(t, details, -1)
68}
69
Ash Wilson5b32ca72014-10-22 08:11:37 -040070func listServers(t *testing.T, client *gophercloud.ServiceClient) {
71 t.Logf("> servers.List")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040072
73 count := 0
Ash Wilson5b32ca72014-10-22 08:11:37 -040074 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -040075 count++
Ash Wilson5b32ca72014-10-22 08:11:37 -040076 t.Logf("--- Page %02d ---", count)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040077
Ash Wilson74b9bcf2014-10-21 14:03:40 -040078 s, err := servers.ExtractServers(page)
79 th.AssertNoErr(t, err)
80 for index, server := range s {
81 logServer(t, &server, index)
82 }
83
84 return true, nil
85 })
86 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -040087}
Ash Wilson86793322014-10-21 14:30:09 -040088
Ash Wilson5b32ca72014-10-22 08:11:37 -040089func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
90 t.Logf("> servers.ChangeAdminPassword")
Ash Wilson86793322014-10-21 14:30:09 -040091
Ash Wilson5b32ca72014-10-22 08:11:37 -040092 original := server.AdminPass
Ash Wilson86793322014-10-21 14:30:09 -040093
Ash Wilson88bbde32014-10-21 15:13:58 -040094 t.Logf("Changing server password.")
Ash Wilson5b32ca72014-10-22 08:11:37 -040095 err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).Extract()
Ash Wilson86793322014-10-21 14:30:09 -040096 th.AssertNoErr(t, err)
97
Ash Wilson5b32ca72014-10-22 08:11:37 -040098 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson86793322014-10-21 14:30:09 -040099 th.AssertNoErr(t, err)
Ash Wilson88bbde32014-10-21 15:13:58 -0400100 t.Logf("Password changed successfully.")
101}
102
Ash Wilson5b32ca72014-10-22 08:11:37 -0400103func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
104 t.Logf("> servers.Reboot")
Ash Wilson88bbde32014-10-21 15:13:58 -0400105
Ash Wilson5b32ca72014-10-22 08:11:37 -0400106 err := servers.Reboot(client, server.ID, os.HardReboot).Extract()
Ash Wilson88bbde32014-10-21 15:13:58 -0400107 th.AssertNoErr(t, err)
108
Ash Wilson5b32ca72014-10-22 08:11:37 -0400109 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson88bbde32014-10-21 15:13:58 -0400110 th.AssertNoErr(t, err)
111
Ash Wilson88bbde32014-10-21 15:13:58 -0400112 t.Logf("Server successfully rebooted.")
Ash Wilson86793322014-10-21 14:30:09 -0400113}
Ash Wilson90f87ba2014-10-21 18:36:18 -0400114
Ash Wilson5b32ca72014-10-22 08:11:37 -0400115func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
116 t.Logf("> servers.Rebuild")
Ash Wilson90f87ba2014-10-21 18:36:18 -0400117
118 options, err := optionsFromEnv()
119 th.AssertNoErr(t, err)
120
Ash Wilson90f87ba2014-10-21 18:36:18 -0400121 opts := os.RebuildOpts{
122 Name: tools.RandomString("RenamedGopher", 16),
Ash Wilson5b32ca72014-10-22 08:11:37 -0400123 AdminPass: tools.MakeNewPassword(server.AdminPass),
Ash Wilson90f87ba2014-10-21 18:36:18 -0400124 ImageID: options.imageID,
125 }
Ash Wilson5b32ca72014-10-22 08:11:37 -0400126 after, err := servers.Rebuild(client, server.ID, opts).Extract()
Ash Wilson90f87ba2014-10-21 18:36:18 -0400127 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400128 th.CheckEquals(t, after.ID, server.ID)
Ash Wilson90f87ba2014-10-21 18:36:18 -0400129
130 err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
131 th.AssertNoErr(t, err)
132
133 t.Logf("Server successfully rebuilt.")
134 logServer(t, after, -1)
135}
Ash Wilson5b32ca72014-10-22 08:11:37 -0400136
137func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
138 t.Logf("> servers.Delete")
139
140 err := servers.Delete(client, server.ID)
141 th.AssertNoErr(t, err)
142
143 t.Logf("Server deleted successfully.")
144}
145
146func TestServerOperations(t *testing.T) {
147 client, err := newClient()
148 th.AssertNoErr(t, err)
149
150 server := createServer(t, client)
151 defer deleteServer(t, client, server)
152
153 getServer(t, client, server)
154 listServers(t, client)
155 changeAdminPassword(t, client, server)
156 rebootServer(t, client, server)
157 rebuildServer(t, client, server)
158}