blob: c3465cd4537af6647e947d7fd604a6080f76f94e [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 {
Jon Perritt8135c6f2014-10-23 12:29:14 -050017 if testing.Short(){
18 t.Skip("Skipping test that requires server creation in short mode.")
19 }
20
Ash Wilson88074d52014-10-21 10:04:17 -040021 options, err := optionsFromEnv()
22 th.AssertNoErr(t, err)
23
Ash Wilson7e571412014-10-21 14:29:55 -040024 name := tools.RandomString("Gophercloud-", 8)
25 t.Logf("Creating server [%s].", name)
26 s, err := servers.Create(client, &os.CreateOpts{
27 Name: name,
Ash Wilson88074d52014-10-21 10:04:17 -040028 ImageRef: options.imageID,
29 FlavorRef: options.flavorID,
Ash Wilson7e571412014-10-21 14:29:55 -040030 }).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040031 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040032 t.Logf("Creating server.")
Ash Wilson88074d52014-10-21 10:04:17 -040033
Ash Wilson5b32ca72014-10-22 08:11:37 -040034 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
Ash Wilson88074d52014-10-21 10:04:17 -040035 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040036 t.Logf("Server created successfully.")
37
38 return s
Ash Wilson88074d52014-10-21 10:04:17 -040039}
40
41func logServer(t *testing.T, server *os.Server, index int) {
42 if index == -1 {
43 t.Logf(" id=[%s]", server.ID)
44 } else {
45 t.Logf("[%02d] id=[%s]", index, server.ID)
46 }
47 t.Logf(" name=[%s]", server.Name)
48 t.Logf(" tenant ID=[%s]", server.TenantID)
49 t.Logf(" user ID=[%s]", server.UserID)
50 t.Logf(" updated=[%s]", server.Updated)
51 t.Logf(" created=[%s]", server.Created)
52 t.Logf(" host ID=[%s]", server.HostID)
53 t.Logf(" access IPv4=[%s]", server.AccessIPv4)
54 t.Logf(" access IPv6=[%s]", server.AccessIPv6)
55 t.Logf(" image=[%v]", server.Image)
56 t.Logf(" flavor=[%v]", server.Flavor)
57 t.Logf(" addresses=[%v]", server.Addresses)
58 t.Logf(" metadata=[%v]", server.Metadata)
59 t.Logf(" links=[%v]", server.Links)
60 t.Logf(" keyname=[%s]", server.KeyName)
61 t.Logf(" admin password=[%s]", server.AdminPass)
62 t.Logf(" status=[%s]", server.Status)
63 t.Logf(" progress=[%d]", server.Progress)
64}
65
Ash Wilson5b32ca72014-10-22 08:11:37 -040066func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
67 t.Logf("> servers.Get")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040068
Ash Wilson5b32ca72014-10-22 08:11:37 -040069 details, err := servers.Get(client, server.ID).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040070 th.AssertNoErr(t, err)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040071 logServer(t, details, -1)
72}
73
Ash Wilson5b32ca72014-10-22 08:11:37 -040074func listServers(t *testing.T, client *gophercloud.ServiceClient) {
75 t.Logf("> servers.List")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040076
77 count := 0
Ash Wilson5b32ca72014-10-22 08:11:37 -040078 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -040079 count++
Ash Wilson5b32ca72014-10-22 08:11:37 -040080 t.Logf("--- Page %02d ---", count)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040081
Ash Wilson74b9bcf2014-10-21 14:03:40 -040082 s, err := servers.ExtractServers(page)
83 th.AssertNoErr(t, err)
84 for index, server := range s {
85 logServer(t, &server, index)
86 }
87
88 return true, nil
89 })
90 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -040091}
Ash Wilson86793322014-10-21 14:30:09 -040092
Ash Wilson5b32ca72014-10-22 08:11:37 -040093func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
94 t.Logf("> servers.ChangeAdminPassword")
Ash Wilson86793322014-10-21 14:30:09 -040095
Ash Wilson5b32ca72014-10-22 08:11:37 -040096 original := server.AdminPass
Ash Wilson86793322014-10-21 14:30:09 -040097
Ash Wilson88bbde32014-10-21 15:13:58 -040098 t.Logf("Changing server password.")
Ash Wilson5b32ca72014-10-22 08:11:37 -040099 err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).Extract()
Ash Wilson86793322014-10-21 14:30:09 -0400100 th.AssertNoErr(t, err)
101
Ash Wilson5b32ca72014-10-22 08:11:37 -0400102 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson86793322014-10-21 14:30:09 -0400103 th.AssertNoErr(t, err)
Ash Wilson88bbde32014-10-21 15:13:58 -0400104 t.Logf("Password changed successfully.")
105}
106
Ash Wilson5b32ca72014-10-22 08:11:37 -0400107func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
108 t.Logf("> servers.Reboot")
Ash Wilson88bbde32014-10-21 15:13:58 -0400109
Ash Wilson5b32ca72014-10-22 08:11:37 -0400110 err := servers.Reboot(client, server.ID, os.HardReboot).Extract()
Ash Wilson88bbde32014-10-21 15:13:58 -0400111 th.AssertNoErr(t, err)
112
Ash Wilson5b32ca72014-10-22 08:11:37 -0400113 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson88bbde32014-10-21 15:13:58 -0400114 th.AssertNoErr(t, err)
115
Ash Wilson88bbde32014-10-21 15:13:58 -0400116 t.Logf("Server successfully rebooted.")
Ash Wilson86793322014-10-21 14:30:09 -0400117}
Ash Wilson90f87ba2014-10-21 18:36:18 -0400118
Ash Wilson5b32ca72014-10-22 08:11:37 -0400119func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
120 t.Logf("> servers.Rebuild")
Ash Wilson90f87ba2014-10-21 18:36:18 -0400121
122 options, err := optionsFromEnv()
123 th.AssertNoErr(t, err)
124
Ash Wilson90f87ba2014-10-21 18:36:18 -0400125 opts := os.RebuildOpts{
126 Name: tools.RandomString("RenamedGopher", 16),
Ash Wilson5b32ca72014-10-22 08:11:37 -0400127 AdminPass: tools.MakeNewPassword(server.AdminPass),
Ash Wilson90f87ba2014-10-21 18:36:18 -0400128 ImageID: options.imageID,
129 }
Ash Wilson5b32ca72014-10-22 08:11:37 -0400130 after, err := servers.Rebuild(client, server.ID, opts).Extract()
Ash Wilson90f87ba2014-10-21 18:36:18 -0400131 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400132 th.CheckEquals(t, after.ID, server.ID)
Ash Wilson90f87ba2014-10-21 18:36:18 -0400133
134 err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
135 th.AssertNoErr(t, err)
136
137 t.Logf("Server successfully rebuilt.")
138 logServer(t, after, -1)
139}
Ash Wilson5b32ca72014-10-22 08:11:37 -0400140
141func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
142 t.Logf("> servers.Delete")
143
144 err := servers.Delete(client, server.ID)
145 th.AssertNoErr(t, err)
146
147 t.Logf("Server deleted successfully.")
148}
149
150func TestServerOperations(t *testing.T) {
151 client, err := newClient()
152 th.AssertNoErr(t, err)
153
154 server := createServer(t, client)
155 defer deleteServer(t, client, server)
156
157 getServer(t, client, server)
158 listServers(t, client)
159 changeAdminPassword(t, client, server)
160 rebootServer(t, client, server)
161 rebuildServer(t, client, server)
162}