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 | |
| 42 | opts := &servers.CreateOpts{ |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 43 | Name: name, |
| 44 | ImageRef: options.imageID, |
| 45 | FlavorRef: options.flavorID, |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 46 | DiskConfig: diskconfig.Manual, |
Ash Wilson | efbae20 | 2014-10-24 14:23:45 -0400 | [diff] [blame] | 47 | } |
| 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 Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 55 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 56 | t.Logf("Creating server.") |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 57 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 58 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 59 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 60 | t.Logf("Server created successfully.") |
| 61 | |
| 62 | return s |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func 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 Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 90 | func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 91 | t.Logf("> servers.Get") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 92 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 93 | details, err := servers.Get(client, server.ID).Extract() |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 94 | th.AssertNoErr(t, err) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 95 | logServer(t, details, -1) |
| 96 | } |
| 97 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 98 | func listServers(t *testing.T, client *gophercloud.ServiceClient) { |
| 99 | t.Logf("> servers.List") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 100 | |
| 101 | count := 0 |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 102 | err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 103 | count++ |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 104 | t.Logf("--- Page %02d ---", count) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 105 | |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 106 | 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 Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 115 | } |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 116 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 117 | func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 118 | t.Logf("> servers.ChangeAdminPassword") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 119 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 120 | original := server.AdminPass |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 121 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 122 | t.Logf("Changing server password.") |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 123 | err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).Extract() |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 124 | th.AssertNoErr(t, err) |
| 125 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 126 | err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300) |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 127 | th.AssertNoErr(t, err) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 128 | t.Logf("Password changed successfully.") |
| 129 | } |
| 130 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 131 | func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 132 | t.Logf("> servers.Reboot") |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 133 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 134 | err := servers.Reboot(client, server.ID, os.HardReboot).Extract() |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 135 | th.AssertNoErr(t, err) |
| 136 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 137 | err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 138 | th.AssertNoErr(t, err) |
| 139 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 140 | t.Logf("Server successfully rebooted.") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 141 | } |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 142 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 143 | func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 144 | t.Logf("> servers.Rebuild") |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 145 | |
| 146 | options, err := optionsFromEnv() |
| 147 | th.AssertNoErr(t, err) |
| 148 | |
Ash Wilson | 5d68672 | 2014-10-23 12:51:30 -0400 | [diff] [blame] | 149 | opts := servers.RebuildOpts{ |
| 150 | Name: tools.RandomString("RenamedGopher", 16), |
| 151 | AdminPass: tools.MakeNewPassword(server.AdminPass), |
| 152 | ImageID: options.imageID, |
| 153 | DiskConfig: diskconfig.Manual, |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 154 | } |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 155 | after, err := servers.Rebuild(client, server.ID, opts).Extract() |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 156 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 157 | th.CheckEquals(t, after.ID, server.ID) |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 158 | |
| 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 Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 165 | |
| 166 | func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 167 | t.Logf("> servers.Delete") |
| 168 | |
Jamie Hannaford | e4e6dfe | 2014-10-27 11:50:29 +0100 | [diff] [blame] | 169 | res := servers.Delete(client, server.ID) |
| 170 | th.AssertNoErr(t, res.Err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 171 | |
| 172 | t.Logf("Server deleted successfully.") |
| 173 | } |
| 174 | |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 175 | func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) { |
| 176 | t.Logf("> keypairs.Delete") |
| 177 | |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 178 | err := keypairs.Delete(client, k.Name).ExtractErr() |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 179 | th.AssertNoErr(t, err) |
| 180 | |
| 181 | t.Logf("Keypair deleted successfully.") |
| 182 | } |
| 183 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 184 | func TestServerOperations(t *testing.T) { |
| 185 | client, err := newClient() |
| 186 | th.AssertNoErr(t, err) |
| 187 | |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 188 | kp := createServerKeyPair(t, client) |
| 189 | defer deleteServerKeyPair(t, client, kp) |
| 190 | |
| 191 | server := createServer(t, client, kp.Name) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 192 | 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 | } |