blob: 511f0a96acd39df3ef766ac516b50455808e772e [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
42 opts := &servers.CreateOpts{
Ash Wilson9c24f6b2014-10-23 12:41:19 -040043 Name: name,
44 ImageRef: options.imageID,
45 FlavorRef: options.flavorID,
Ash Wilson9c24f6b2014-10-23 12:41:19 -040046 DiskConfig: diskconfig.Manual,
Ash Wilsonefbae202014-10-24 14:23:45 -040047 }
48
49 if keyName != "" {
50 opts.KeyPair = keyName
51 }
52
53 t.Logf("Creating server [%s].", name)
54 s, err := servers.Create(client, opts).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040055 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040056 t.Logf("Creating server.")
Ash Wilson88074d52014-10-21 10:04:17 -040057
Ash Wilson5b32ca72014-10-22 08:11:37 -040058 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
Ash Wilson88074d52014-10-21 10:04:17 -040059 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -040060 t.Logf("Server created successfully.")
61
62 return s
Ash Wilson88074d52014-10-21 10:04:17 -040063}
64
65func logServer(t *testing.T, server *os.Server, index int) {
66 if index == -1 {
67 t.Logf(" id=[%s]", server.ID)
68 } else {
69 t.Logf("[%02d] id=[%s]", index, server.ID)
70 }
71 t.Logf(" name=[%s]", server.Name)
72 t.Logf(" tenant ID=[%s]", server.TenantID)
73 t.Logf(" user ID=[%s]", server.UserID)
74 t.Logf(" updated=[%s]", server.Updated)
75 t.Logf(" created=[%s]", server.Created)
76 t.Logf(" host ID=[%s]", server.HostID)
77 t.Logf(" access IPv4=[%s]", server.AccessIPv4)
78 t.Logf(" access IPv6=[%s]", server.AccessIPv6)
79 t.Logf(" image=[%v]", server.Image)
80 t.Logf(" flavor=[%v]", server.Flavor)
81 t.Logf(" addresses=[%v]", server.Addresses)
82 t.Logf(" metadata=[%v]", server.Metadata)
83 t.Logf(" links=[%v]", server.Links)
84 t.Logf(" keyname=[%s]", server.KeyName)
85 t.Logf(" admin password=[%s]", server.AdminPass)
86 t.Logf(" status=[%s]", server.Status)
87 t.Logf(" progress=[%d]", server.Progress)
88}
89
Ash Wilson5b32ca72014-10-22 08:11:37 -040090func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
91 t.Logf("> servers.Get")
Ash Wilson74b9bcf2014-10-21 14:03:40 -040092
Ash Wilson5b32ca72014-10-22 08:11:37 -040093 details, err := servers.Get(client, server.ID).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040094 th.AssertNoErr(t, err)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040095 logServer(t, details, -1)
96}
97
Ash Wilson5b32ca72014-10-22 08:11:37 -040098func listServers(t *testing.T, client *gophercloud.ServiceClient) {
99 t.Logf("> servers.List")
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400100
101 count := 0
Ash Wilson5b32ca72014-10-22 08:11:37 -0400102 err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400103 count++
Ash Wilson5b32ca72014-10-22 08:11:37 -0400104 t.Logf("--- Page %02d ---", count)
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400105
Ash Wilson74b9bcf2014-10-21 14:03:40 -0400106 s, err := servers.ExtractServers(page)
107 th.AssertNoErr(t, err)
108 for index, server := range s {
109 logServer(t, &server, index)
110 }
111
112 return true, nil
113 })
114 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -0400115}
Ash Wilson86793322014-10-21 14:30:09 -0400116
Ash Wilson5b32ca72014-10-22 08:11:37 -0400117func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
118 t.Logf("> servers.ChangeAdminPassword")
Ash Wilson86793322014-10-21 14:30:09 -0400119
Ash Wilson5b32ca72014-10-22 08:11:37 -0400120 original := server.AdminPass
Ash Wilson86793322014-10-21 14:30:09 -0400121
Ash Wilson88bbde32014-10-21 15:13:58 -0400122 t.Logf("Changing server password.")
Ash Wilson5b32ca72014-10-22 08:11:37 -0400123 err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).Extract()
Ash Wilson86793322014-10-21 14:30:09 -0400124 th.AssertNoErr(t, err)
125
Ash Wilson5b32ca72014-10-22 08:11:37 -0400126 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson86793322014-10-21 14:30:09 -0400127 th.AssertNoErr(t, err)
Ash Wilson88bbde32014-10-21 15:13:58 -0400128 t.Logf("Password changed successfully.")
129}
130
Ash Wilson5b32ca72014-10-22 08:11:37 -0400131func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
132 t.Logf("> servers.Reboot")
Ash Wilson88bbde32014-10-21 15:13:58 -0400133
Ash Wilson5b32ca72014-10-22 08:11:37 -0400134 err := servers.Reboot(client, server.ID, os.HardReboot).Extract()
Ash Wilson88bbde32014-10-21 15:13:58 -0400135 th.AssertNoErr(t, err)
136
Ash Wilson5b32ca72014-10-22 08:11:37 -0400137 err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
Ash Wilson88bbde32014-10-21 15:13:58 -0400138 th.AssertNoErr(t, err)
139
Ash Wilson88bbde32014-10-21 15:13:58 -0400140 t.Logf("Server successfully rebooted.")
Ash Wilson86793322014-10-21 14:30:09 -0400141}
Ash Wilson90f87ba2014-10-21 18:36:18 -0400142
Ash Wilson5b32ca72014-10-22 08:11:37 -0400143func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
144 t.Logf("> servers.Rebuild")
Ash Wilson90f87ba2014-10-21 18:36:18 -0400145
146 options, err := optionsFromEnv()
147 th.AssertNoErr(t, err)
148
Ash Wilson5d686722014-10-23 12:51:30 -0400149 opts := servers.RebuildOpts{
150 Name: tools.RandomString("RenamedGopher", 16),
151 AdminPass: tools.MakeNewPassword(server.AdminPass),
152 ImageID: options.imageID,
153 DiskConfig: diskconfig.Manual,
Ash Wilson90f87ba2014-10-21 18:36:18 -0400154 }
Ash Wilson5b32ca72014-10-22 08:11:37 -0400155 after, err := servers.Rebuild(client, server.ID, opts).Extract()
Ash Wilson90f87ba2014-10-21 18:36:18 -0400156 th.AssertNoErr(t, err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400157 th.CheckEquals(t, after.ID, server.ID)
Ash Wilson90f87ba2014-10-21 18:36:18 -0400158
159 err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
160 th.AssertNoErr(t, err)
161
162 t.Logf("Server successfully rebuilt.")
163 logServer(t, after, -1)
164}
Ash Wilson5b32ca72014-10-22 08:11:37 -0400165
166func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
167 t.Logf("> servers.Delete")
168
Jamie Hannaforde4e6dfe2014-10-27 11:50:29 +0100169 res := servers.Delete(client, server.ID)
170 th.AssertNoErr(t, res.Err)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400171
172 t.Logf("Server deleted successfully.")
173}
174
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400175func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) {
176 t.Logf("> keypairs.Delete")
177
Jon Perrittba2395e2014-10-27 15:23:21 -0500178 err := keypairs.Delete(client, k.Name).ExtractErr()
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400179 th.AssertNoErr(t, err)
180
181 t.Logf("Keypair deleted successfully.")
182}
183
Ash Wilson5b32ca72014-10-22 08:11:37 -0400184func TestServerOperations(t *testing.T) {
185 client, err := newClient()
186 th.AssertNoErr(t, err)
187
Ash Wilson9c24f6b2014-10-23 12:41:19 -0400188 kp := createServerKeyPair(t, client)
189 defer deleteServerKeyPair(t, client, kp)
190
191 server := createServer(t, client, kp.Name)
Ash Wilson5b32ca72014-10-22 08:11:37 -0400192 defer deleteServer(t, client, server)
193
194 getServer(t, client, server)
195 listServers(t, client)
196 changeAdminPassword(t, client, server)
197 rebootServer(t, client, server)
198 rebuildServer(t, client, server)
199}