blob: af4bbe07fb37558ba7bbc39818370ed0fd071908 [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)
41 t.Logf("Creating server [%s].", name)
Ash Wilson9c24f6b2014-10-23 12:41:19 -040042 s, err := servers.Create(client, &servers.CreateOpts{
43 Name: name,
44 ImageRef: options.imageID,
45 FlavorRef: options.flavorID,
46 KeyPair: keyName,
47 DiskConfig: diskconfig.Manual,
Ash Wilson7e571412014-10-21 14:29:55 -040048 }).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040049 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040050 t.Logf("Creating server.")
Ash Wilson88074d52014-10-21 10:04:17 -040051
Ash Wilson5b32ca72014-10-22 08:11:37 -040052 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
Ash Wilson88074d52014-10-21 10:04:17 -040053 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040054 t.Logf("Server created successfully.")
55
56 return s
Ash Wilson88074d52014-10-21 10:04:17 -040057}
58
59func logServer(t *testing.T, server *os.Server, index int) {
60 if index == -1 {
61 t.Logf(" id=[%s]", server.ID)
62 } else {
63 t.Logf("[%02d] id=[%s]", index, server.ID)
64 }
65 t.Logf(" name=[%s]", server.Name)
66 t.Logf(" tenant ID=[%s]", server.TenantID)
67 t.Logf(" user ID=[%s]", server.UserID)
68 t.Logf(" updated=[%s]", server.Updated)
69 t.Logf(" created=[%s]", server.Created)
70 t.Logf(" host ID=[%s]", server.HostID)
71 t.Logf(" access IPv4=[%s]", server.AccessIPv4)
72 t.Logf(" access IPv6=[%s]", server.AccessIPv6)
73 t.Logf(" image=[%v]", server.Image)
74 t.Logf(" flavor=[%v]", server.Flavor)
75 t.Logf(" addresses=[%v]", server.Addresses)
76 t.Logf(" metadata=[%v]", server.Metadata)
77 t.Logf(" links=[%v]", server.Links)
78 t.Logf(" keyname=[%s]", server.KeyName)
79 t.Logf(" admin password=[%s]", server.AdminPass)
80 t.Logf(" status=[%s]", server.Status)
81 t.Logf(" progress=[%d]", server.Progress)
82}
83
Ash Wilson5b32ca72014-10-22 08:11:37 -040084func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
85 t.Logf("> servers.Get")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040086
Ash Wilson5b32ca72014-10-22 08:11:37 -040087 details, err := servers.Get(client, server.ID).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040088 th.AssertNoErr(t, err)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040089 logServer(t, details, -1)
90}
91
Ash Wilson5b32ca72014-10-22 08:11:37 -040092func listServers(t *testing.T, client *gophercloud.ServiceClient) {
93 t.Logf("> servers.List")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040094
95 count := 0
Ash Wilson5b32ca72014-10-22 08:11:37 -040096 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -040097 count++
Ash Wilson5b32ca72014-10-22 08:11:37 -040098 t.Logf("--- Page %02d ---", count)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040099
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400100 s, err := servers.ExtractServers(page)
101 th.AssertNoErr(t, err)
102 for index, server := range s {
103 logServer(t, &server, index)
104 }
105
106 return true, nil
107 })
108 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -0400109}
Ash Wilson86793322014-10-21 14:30:09 -0400110
Ash Wilson5b32ca72014-10-22 08:11:37 -0400111func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
112 t.Logf("> servers.ChangeAdminPassword")
Ash Wilson86793322014-10-21 14:30:09 -0400113
Ash Wilson5b32ca72014-10-22 08:11:37 -0400114 original := server.AdminPass
Ash Wilson86793322014-10-21 14:30:09 -0400115
Ash Wilson88bbde32014-10-21 15:13:58 -0400116 t.Logf("Changing server password.")
Ash Wilson5b32ca72014-10-22 08:11:37 -0400117 err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).Extract()
Ash Wilson86793322014-10-21 14:30:09 -0400118 th.AssertNoErr(t, err)
119
Ash Wilson5b32ca72014-10-22 08:11:37 -0400120 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson86793322014-10-21 14:30:09 -0400121 th.AssertNoErr(t, err)
Ash Wilson88bbde32014-10-21 15:13:58 -0400122 t.Logf("Password changed successfully.")
123}
124
Ash Wilson5b32ca72014-10-22 08:11:37 -0400125func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
126 t.Logf("> servers.Reboot")
Ash Wilson88bbde32014-10-21 15:13:58 -0400127
Ash Wilson5b32ca72014-10-22 08:11:37 -0400128 err := servers.Reboot(client, server.ID, os.HardReboot).Extract()
Ash Wilson88bbde32014-10-21 15:13:58 -0400129 th.AssertNoErr(t, err)
130
Ash Wilson5b32ca72014-10-22 08:11:37 -0400131 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson88bbde32014-10-21 15:13:58 -0400132 th.AssertNoErr(t, err)
133
Ash Wilson88bbde32014-10-21 15:13:58 -0400134 t.Logf("Server successfully rebooted.")
Ash Wilson86793322014-10-21 14:30:09 -0400135}
Ash Wilson90f87ba2014-10-21 18:36:18 -0400136
Ash Wilson5b32ca72014-10-22 08:11:37 -0400137func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
138 t.Logf("> servers.Rebuild")
Ash Wilson90f87ba2014-10-21 18:36:18 -0400139
140 options, err := optionsFromEnv()
141 th.AssertNoErr(t, err)
142
Ash Wilson5d686722014-10-23 12:51:30 -0400143 opts := servers.RebuildOpts{
144 Name: tools.RandomString("RenamedGopher", 16),
145 AdminPass: tools.MakeNewPassword(server.AdminPass),
146 ImageID: options.imageID,
147 DiskConfig: diskconfig.Manual,
Ash Wilson90f87ba2014-10-21 18:36:18 -0400148 }
Ash Wilson5b32ca72014-10-22 08:11:37 -0400149 after, err := servers.Rebuild(client, server.ID, opts).Extract()
Ash Wilson90f87ba2014-10-21 18:36:18 -0400150 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400151 th.CheckEquals(t, after.ID, server.ID)
Ash Wilson90f87ba2014-10-21 18:36:18 -0400152
153 err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
154 th.AssertNoErr(t, err)
155
156 t.Logf("Server successfully rebuilt.")
157 logServer(t, after, -1)
158}
Ash Wilson5b32ca72014-10-22 08:11:37 -0400159
160func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
161 t.Logf("> servers.Delete")
162
163 err := servers.Delete(client, server.ID)
164 th.AssertNoErr(t, err)
165
166 t.Logf("Server deleted successfully.")
167}
168
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400169func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) {
170 t.Logf("> keypairs.Delete")
171
172 err := keypairs.Delete(client, k.Name).Extract()
173 th.AssertNoErr(t, err)
174
175 t.Logf("Keypair deleted successfully.")
176}
177
Ash Wilson5b32ca72014-10-22 08:11:37 -0400178func TestServerOperations(t *testing.T) {
179 client, err := newClient()
180 th.AssertNoErr(t, err)
181
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400182 kp := createServerKeyPair(t, client)
183 defer deleteServerKeyPair(t, client, kp)
184
185 server := createServer(t, client, kp.Name)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400186 defer deleteServer(t, client, server)
187
188 getServer(t, client, server)
189 listServers(t, client)
190 changeAdminPassword(t, client, server)
191 rebootServer(t, client, server)
192 rebuildServer(t, client, server)
193}