blob: 81c8599f3d7f37c1aeef42cdba20fa655eaf6328 [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
Ash Wilson5b32ca72014-10-22 08:11:37 -0400103func listServers(t *testing.T, client *gophercloud.ServiceClient) {
104 t.Logf("> servers.List")
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400105
106 count := 0
Ash Wilson5b32ca72014-10-22 08:11:37 -0400107 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400108 count++
Ash Wilson5b32ca72014-10-22 08:11:37 -0400109 t.Logf("--- Page %02d ---", count)
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400110
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400111 s, err := servers.ExtractServers(page)
112 th.AssertNoErr(t, err)
113 for index, server := range s {
114 logServer(t, &server, index)
115 }
116
117 return true, nil
118 })
119 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -0400120}
Ash Wilson86793322014-10-21 14:30:09 -0400121
Ash Wilson5b32ca72014-10-22 08:11:37 -0400122func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
123 t.Logf("> servers.ChangeAdminPassword")
Ash Wilson86793322014-10-21 14:30:09 -0400124
Ash Wilson5b32ca72014-10-22 08:11:37 -0400125 original := server.AdminPass
Ash Wilson86793322014-10-21 14:30:09 -0400126
Ash Wilson88bbde32014-10-21 15:13:58 -0400127 t.Logf("Changing server password.")
Jon Perritte7565122014-11-03 09:21:31 -0600128 err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).ExtractErr()
Ash Wilson86793322014-10-21 14:30:09 -0400129 th.AssertNoErr(t, err)
130
Ash Wilson5b32ca72014-10-22 08:11:37 -0400131 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson86793322014-10-21 14:30:09 -0400132 th.AssertNoErr(t, err)
Ash Wilson88bbde32014-10-21 15:13:58 -0400133 t.Logf("Password changed successfully.")
134}
135
Ash Wilson5b32ca72014-10-22 08:11:37 -0400136func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
137 t.Logf("> servers.Reboot")
Ash Wilson88bbde32014-10-21 15:13:58 -0400138
Jon Perritte7565122014-11-03 09:21:31 -0600139 err := servers.Reboot(client, server.ID, os.HardReboot).ExtractErr()
Ash Wilson88bbde32014-10-21 15:13:58 -0400140 th.AssertNoErr(t, err)
141
Ash Wilson5b32ca72014-10-22 08:11:37 -0400142 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson88bbde32014-10-21 15:13:58 -0400143 th.AssertNoErr(t, err)
144
Ash Wilson88bbde32014-10-21 15:13:58 -0400145 t.Logf("Server successfully rebooted.")
Ash Wilson86793322014-10-21 14:30:09 -0400146}
Ash Wilson90f87ba2014-10-21 18:36:18 -0400147
Ash Wilson5b32ca72014-10-22 08:11:37 -0400148func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
149 t.Logf("> servers.Rebuild")
Ash Wilson90f87ba2014-10-21 18:36:18 -0400150
151 options, err := optionsFromEnv()
152 th.AssertNoErr(t, err)
153
Ash Wilson5d686722014-10-23 12:51:30 -0400154 opts := servers.RebuildOpts{
155 Name: tools.RandomString("RenamedGopher", 16),
156 AdminPass: tools.MakeNewPassword(server.AdminPass),
157 ImageID: options.imageID,
158 DiskConfig: diskconfig.Manual,
Ash Wilson90f87ba2014-10-21 18:36:18 -0400159 }
Ash Wilson5b32ca72014-10-22 08:11:37 -0400160 after, err := servers.Rebuild(client, server.ID, opts).Extract()
Ash Wilson90f87ba2014-10-21 18:36:18 -0400161 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400162 th.CheckEquals(t, after.ID, server.ID)
Ash Wilson90f87ba2014-10-21 18:36:18 -0400163
164 err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
165 th.AssertNoErr(t, err)
166
167 t.Logf("Server successfully rebuilt.")
168 logServer(t, after, -1)
169}
Ash Wilson5b32ca72014-10-22 08:11:37 -0400170
171func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
172 t.Logf("> servers.Delete")
173
Jamie Hannaforde4e6dfe2014-10-27 11:50:29 +0100174 res := servers.Delete(client, server.ID)
175 th.AssertNoErr(t, res.Err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400176
177 t.Logf("Server deleted successfully.")
178}
179
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400180func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) {
181 t.Logf("> keypairs.Delete")
182
Jon Perrittba2395e2014-10-27 15:23:21 -0500183 err := keypairs.Delete(client, k.Name).ExtractErr()
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400184 th.AssertNoErr(t, err)
185
186 t.Logf("Keypair deleted successfully.")
187}
188
Ash Wilson5b32ca72014-10-22 08:11:37 -0400189func TestServerOperations(t *testing.T) {
190 client, err := newClient()
191 th.AssertNoErr(t, err)
192
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400193 kp := createServerKeyPair(t, client)
194 defer deleteServerKeyPair(t, client, kp)
195
196 server := createServer(t, client, kp.Name)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400197 defer deleteServer(t, client, server)
198
199 getServer(t, client, server)
200 listServers(t, client)
201 changeAdminPassword(t, client, server)
202 rebootServer(t, client, server)
203 rebuildServer(t, client, server)
204}