Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/acceptance/tools" |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig" |
| 11 | oskey "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs" |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 12 | os "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/rackspace/compute/v2/keypairs" |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 15 | "github.com/rackspace/gophercloud/rackspace/compute/v2/servers" |
| 16 | th "github.com/rackspace/gophercloud/testhelper" |
| 17 | ) |
| 18 | |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 19 | func 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 | |
| 32 | func createServer(t *testing.T, client *gophercloud.ServiceClient, keyName string) *os.Server { |
| 33 | if testing.Short() { |
Jon Perritt | 8135c6f | 2014-10-23 12:29:14 -0500 | [diff] [blame] | 34 | t.Skip("Skipping test that requires server creation in short mode.") |
| 35 | } |
| 36 | |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 37 | options, err := optionsFromEnv() |
| 38 | th.AssertNoErr(t, err) |
| 39 | |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 40 | name := tools.RandomString("Gophercloud-", 8) |
Ash Wilson | efbae20 | 2014-10-24 14:23:45 -0400 | [diff] [blame] | 41 | |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame] | 42 | pwd := tools.MakeNewPassword("") |
| 43 | |
Ash Wilson | efbae20 | 2014-10-24 14:23:45 -0400 | [diff] [blame] | 44 | opts := &servers.CreateOpts{ |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 45 | Name: name, |
| 46 | ImageRef: options.imageID, |
| 47 | FlavorRef: options.flavorID, |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 48 | DiskConfig: diskconfig.Manual, |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame] | 49 | AdminPass: pwd, |
Ash Wilson | efbae20 | 2014-10-24 14:23:45 -0400 | [diff] [blame] | 50 | } |
| 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 Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 58 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 59 | t.Logf("Creating server.") |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 60 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 61 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 62 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 63 | t.Logf("Server created successfully.") |
| 64 | |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame] | 65 | th.CheckEquals(t, pwd, s.AdminPass) |
| 66 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 67 | return s |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | func 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 Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 95 | func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 96 | t.Logf("> servers.Get") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 97 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 98 | details, err := servers.Get(client, server.ID).Extract() |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 99 | th.AssertNoErr(t, err) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 100 | logServer(t, details, -1) |
| 101 | } |
| 102 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 103 | func listServers(t *testing.T, client *gophercloud.ServiceClient) { |
| 104 | t.Logf("> servers.List") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 105 | |
| 106 | count := 0 |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 107 | err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 108 | count++ |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 109 | t.Logf("--- Page %02d ---", count) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 110 | |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 111 | 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 Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 120 | } |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 121 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 122 | func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 123 | t.Logf("> servers.ChangeAdminPassword") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 124 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 125 | original := server.AdminPass |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 126 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 127 | t.Logf("Changing server password.") |
Jon Perritt | e756512 | 2014-11-03 09:21:31 -0600 | [diff] [blame] | 128 | err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).ExtractErr() |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 129 | th.AssertNoErr(t, err) |
| 130 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 131 | err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300) |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 132 | th.AssertNoErr(t, err) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 133 | t.Logf("Password changed successfully.") |
| 134 | } |
| 135 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 136 | func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 137 | t.Logf("> servers.Reboot") |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 138 | |
Jon Perritt | e756512 | 2014-11-03 09:21:31 -0600 | [diff] [blame] | 139 | err := servers.Reboot(client, server.ID, os.HardReboot).ExtractErr() |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 140 | th.AssertNoErr(t, err) |
| 141 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 142 | err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 143 | th.AssertNoErr(t, err) |
| 144 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 145 | t.Logf("Server successfully rebooted.") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 146 | } |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 147 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 148 | func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 149 | t.Logf("> servers.Rebuild") |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 150 | |
| 151 | options, err := optionsFromEnv() |
| 152 | th.AssertNoErr(t, err) |
| 153 | |
Ash Wilson | 5d68672 | 2014-10-23 12:51:30 -0400 | [diff] [blame] | 154 | opts := servers.RebuildOpts{ |
| 155 | Name: tools.RandomString("RenamedGopher", 16), |
| 156 | AdminPass: tools.MakeNewPassword(server.AdminPass), |
| 157 | ImageID: options.imageID, |
| 158 | DiskConfig: diskconfig.Manual, |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 159 | } |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 160 | after, err := servers.Rebuild(client, server.ID, opts).Extract() |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 161 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 162 | th.CheckEquals(t, after.ID, server.ID) |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 163 | |
| 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 Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 170 | |
| 171 | func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 172 | t.Logf("> servers.Delete") |
| 173 | |
Jamie Hannaford | e4e6dfe | 2014-10-27 11:50:29 +0100 | [diff] [blame] | 174 | res := servers.Delete(client, server.ID) |
| 175 | th.AssertNoErr(t, res.Err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 176 | |
| 177 | t.Logf("Server deleted successfully.") |
| 178 | } |
| 179 | |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 180 | func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) { |
| 181 | t.Logf("> keypairs.Delete") |
| 182 | |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 183 | err := keypairs.Delete(client, k.Name).ExtractErr() |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 184 | th.AssertNoErr(t, err) |
| 185 | |
| 186 | t.Logf("Keypair deleted successfully.") |
| 187 | } |
| 188 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 189 | func TestServerOperations(t *testing.T) { |
| 190 | client, err := newClient() |
| 191 | th.AssertNoErr(t, err) |
| 192 | |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 193 | kp := createServerKeyPair(t, client) |
| 194 | defer deleteServerKeyPair(t, client, kp) |
| 195 | |
| 196 | server := createServer(t, client, kp.Name) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 197 | 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 | } |