blob: a8b5937b6ee8ea0ef89c7930016c8eaeabb6fb58 [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"
Ash Wilson9c24f6b2014-10-23 12:41:19 -040010 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig"
11 oskey "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
Ash Wilson88074d52014-10-21 10:04:17 -040012 os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Ash Wilson74b9bcf2014-10-21 14:03:40 -040013 "github.com/rackspace/gophercloud/pagination"
Ash Wilson9c24f6b2014-10-23 12:41:19 -040014 "github.com/rackspace/gophercloud/rackspace/compute/v2/keypairs"
Ash Wilson88074d52014-10-21 10:04:17 -040015 "github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
16 th "github.com/rackspace/gophercloud/testhelper"
17)
18
Ash Wilson9c24f6b2014-10-23 12:41:19 -040019func createServerKeyPair(t *testing.T, client *gophercloud.ServiceClient) *oskey.KeyPair {
20 name := tools.RandomString("importedkey-", 8)
21 pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDlIQ3r+zd97kb9Hzmujd3V6pbO53eb3Go4q2E8iqVGWQfZTrFdL9KACJnqJIm9HmncfRkUTxE37hqeGCCv8uD+ZPmPiZG2E60OX1mGDjbbzAyReRwYWXgXHopggZTLak5k4mwZYaxwaufbVBDRn847e01lZnaXaszEToLM37NLw+uz29sl3TwYy2R0RGHPwPc160aWmdLjSyd1Nd4c9pvvOP/EoEuBjIC6NJJwg2Rvg9sjjx9jYj0QUgc8CqKLN25oMZ69kNJzlFylKRUoeeVr89txlR59yehJWk6Uw6lYFTdJmcmQOFVAJ12RMmS1hLWCM8UzAgtw+EDa0eqBxBDl smash@winter"
22
23 k, err := keypairs.Create(client, oskey.CreateOpts{
24 Name: name,
25 PublicKey: pubkey,
26 }).Extract()
27 th.AssertNoErr(t, err)
28
29 return k
30}
31
32func createServer(t *testing.T, client *gophercloud.ServiceClient, keyName string) *os.Server {
33 if testing.Short() {
Jon Perritt8135c6f2014-10-23 12:29:14 -050034 t.Skip("Skipping test that requires server creation in short mode.")
35 }
36
Ash Wilson88074d52014-10-21 10:04:17 -040037 options, err := optionsFromEnv()
38 th.AssertNoErr(t, err)
39
Ash Wilson7e571412014-10-21 14:29:55 -040040 name := tools.RandomString("Gophercloud-", 8)
Ash Wilsonefbae202014-10-24 14:23:45 -040041
Jon Perrittf3b2e142014-11-04 16:00:19 -060042 pwd := tools.MakeNewPassword("")
43
Ash Wilsonefbae202014-10-24 14:23:45 -040044 opts := &servers.CreateOpts{
Ash Wilson9c24f6b2014-10-23 12:41:19 -040045 Name: name,
46 ImageRef: options.imageID,
47 FlavorRef: options.flavorID,
Ash Wilson9c24f6b2014-10-23 12:41:19 -040048 DiskConfig: diskconfig.Manual,
Jon Perrittf3b2e142014-11-04 16:00:19 -060049 AdminPass: pwd,
Ash Wilsonefbae202014-10-24 14:23:45 -040050 }
51
52 if keyName != "" {
53 opts.KeyPair = keyName
54 }
55
56 t.Logf("Creating server [%s].", name)
57 s, err := servers.Create(client, opts).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040058 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040059 t.Logf("Creating server.")
Ash Wilson88074d52014-10-21 10:04:17 -040060
Ash Wilson5b32ca72014-10-22 08:11:37 -040061 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
Ash Wilson88074d52014-10-21 10:04:17 -040062 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040063 t.Logf("Server created successfully.")
64
Jon Perrittf3b2e142014-11-04 16:00:19 -060065 th.CheckEquals(t, pwd, s.AdminPass)
66
Ash Wilson5b32ca72014-10-22 08:11:37 -040067 return s
Ash Wilson88074d52014-10-21 10:04:17 -040068}
69
70func logServer(t *testing.T, server *os.Server, index int) {
71 if index == -1 {
72 t.Logf(" id=[%s]", server.ID)
73 } else {
74 t.Logf("[%02d] id=[%s]", index, server.ID)
75 }
76 t.Logf(" name=[%s]", server.Name)
77 t.Logf(" tenant ID=[%s]", server.TenantID)
78 t.Logf(" user ID=[%s]", server.UserID)
79 t.Logf(" updated=[%s]", server.Updated)
80 t.Logf(" created=[%s]", server.Created)
81 t.Logf(" host ID=[%s]", server.HostID)
82 t.Logf(" access IPv4=[%s]", server.AccessIPv4)
83 t.Logf(" access IPv6=[%s]", server.AccessIPv6)
84 t.Logf(" image=[%v]", server.Image)
85 t.Logf(" flavor=[%v]", server.Flavor)
86 t.Logf(" addresses=[%v]", server.Addresses)
87 t.Logf(" metadata=[%v]", server.Metadata)
88 t.Logf(" links=[%v]", server.Links)
89 t.Logf(" keyname=[%s]", server.KeyName)
90 t.Logf(" admin password=[%s]", server.AdminPass)
91 t.Logf(" status=[%s]", server.Status)
92 t.Logf(" progress=[%d]", server.Progress)
93}
94
Ash Wilson5b32ca72014-10-22 08:11:37 -040095func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
96 t.Logf("> servers.Get")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040097
Ash Wilson5b32ca72014-10-22 08:11:37 -040098 details, err := servers.Get(client, server.ID).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040099 th.AssertNoErr(t, err)
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400100 logServer(t, details, -1)
101}
102
Jon Perrittc09ae842015-02-02 10:15:03 -0700103func updateServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
104 t.Logf("> servers.Get")
105
106 opts := os.UpdateOpts{
107 Name: "updated-server",
108 }
109 updatedServer, err := servers.Update(client, server.ID, opts).Extract()
110 th.AssertNoErr(t, err)
111 th.AssertEquals(t, "updated-server", updatedServer.Name)
112 logServer(t, updatedServer, -1)
113}
114
Ash Wilson5b32ca72014-10-22 08:11:37 -0400115func listServers(t *testing.T, client *gophercloud.ServiceClient) {
116 t.Logf("> servers.List")
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400117
118 count := 0
Ash Wilson5b32ca72014-10-22 08:11:37 -0400119 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400120 count++
Ash Wilson5b32ca72014-10-22 08:11:37 -0400121 t.Logf("--- Page %02d ---", count)
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400122
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400123 s, err := servers.ExtractServers(page)
124 th.AssertNoErr(t, err)
125 for index, server := range s {
126 logServer(t, &server, index)
127 }
128
129 return true, nil
130 })
131 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -0400132}
Ash Wilson86793322014-10-21 14:30:09 -0400133
Ash Wilson5b32ca72014-10-22 08:11:37 -0400134func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
135 t.Logf("> servers.ChangeAdminPassword")
Ash Wilson86793322014-10-21 14:30:09 -0400136
Ash Wilson5b32ca72014-10-22 08:11:37 -0400137 original := server.AdminPass
Ash Wilson86793322014-10-21 14:30:09 -0400138
Ash Wilson88bbde32014-10-21 15:13:58 -0400139 t.Logf("Changing server password.")
Jon Perritte7565122014-11-03 09:21:31 -0600140 err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).ExtractErr()
Ash Wilson86793322014-10-21 14:30:09 -0400141 th.AssertNoErr(t, err)
142
Ash Wilson5b32ca72014-10-22 08:11:37 -0400143 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson86793322014-10-21 14:30:09 -0400144 th.AssertNoErr(t, err)
Ash Wilson88bbde32014-10-21 15:13:58 -0400145 t.Logf("Password changed successfully.")
146}
147
Ash Wilson5b32ca72014-10-22 08:11:37 -0400148func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
149 t.Logf("> servers.Reboot")
Ash Wilson88bbde32014-10-21 15:13:58 -0400150
Jon Perritte7565122014-11-03 09:21:31 -0600151 err := servers.Reboot(client, server.ID, os.HardReboot).ExtractErr()
Ash Wilson88bbde32014-10-21 15:13:58 -0400152 th.AssertNoErr(t, err)
153
Ash Wilson5b32ca72014-10-22 08:11:37 -0400154 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson88bbde32014-10-21 15:13:58 -0400155 th.AssertNoErr(t, err)
156
Ash Wilson88bbde32014-10-21 15:13:58 -0400157 t.Logf("Server successfully rebooted.")
Ash Wilson86793322014-10-21 14:30:09 -0400158}
Ash Wilson90f87ba2014-10-21 18:36:18 -0400159
Ash Wilson5b32ca72014-10-22 08:11:37 -0400160func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
161 t.Logf("> servers.Rebuild")
Ash Wilson90f87ba2014-10-21 18:36:18 -0400162
163 options, err := optionsFromEnv()
164 th.AssertNoErr(t, err)
165
Ash Wilson5d686722014-10-23 12:51:30 -0400166 opts := servers.RebuildOpts{
167 Name: tools.RandomString("RenamedGopher", 16),
168 AdminPass: tools.MakeNewPassword(server.AdminPass),
169 ImageID: options.imageID,
170 DiskConfig: diskconfig.Manual,
Ash Wilson90f87ba2014-10-21 18:36:18 -0400171 }
Ash Wilson5b32ca72014-10-22 08:11:37 -0400172 after, err := servers.Rebuild(client, server.ID, opts).Extract()
Ash Wilson90f87ba2014-10-21 18:36:18 -0400173 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400174 th.CheckEquals(t, after.ID, server.ID)
Ash Wilson90f87ba2014-10-21 18:36:18 -0400175
176 err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
177 th.AssertNoErr(t, err)
178
179 t.Logf("Server successfully rebuilt.")
180 logServer(t, after, -1)
181}
Ash Wilson5b32ca72014-10-22 08:11:37 -0400182
183func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
184 t.Logf("> servers.Delete")
185
Jamie Hannaforde4e6dfe2014-10-27 11:50:29 +0100186 res := servers.Delete(client, server.ID)
187 th.AssertNoErr(t, res.Err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400188
189 t.Logf("Server deleted successfully.")
190}
191
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400192func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) {
193 t.Logf("> keypairs.Delete")
194
Jon Perrittba2395e2014-10-27 15:23:21 -0500195 err := keypairs.Delete(client, k.Name).ExtractErr()
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400196 th.AssertNoErr(t, err)
197
198 t.Logf("Keypair deleted successfully.")
199}
200
Ash Wilson5b32ca72014-10-22 08:11:37 -0400201func TestServerOperations(t *testing.T) {
202 client, err := newClient()
203 th.AssertNoErr(t, err)
204
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400205 kp := createServerKeyPair(t, client)
206 defer deleteServerKeyPair(t, client, kp)
207
208 server := createServer(t, client, kp.Name)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400209 defer deleteServer(t, client, server)
210
211 getServer(t, client, server)
Jon Perrittc09ae842015-02-02 10:15:03 -0700212 updateServer(t, client, server)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400213 listServers(t, client)
214 changeAdminPassword(t, client, server)
215 rebootServer(t, client, server)
216 rebuildServer(t, client, server)
217}