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) |
| 41 | t.Logf("Creating server [%s].", name) |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 42 | 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 Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 48 | }).Extract() |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 49 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 50 | t.Logf("Creating server.") |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 51 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 52 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 53 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 54 | t.Logf("Server created successfully.") |
| 55 | |
| 56 | return s |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func 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 Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 84 | func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 85 | t.Logf("> servers.Get") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 86 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 87 | details, err := servers.Get(client, server.ID).Extract() |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 88 | th.AssertNoErr(t, err) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 89 | logServer(t, details, -1) |
| 90 | } |
| 91 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 92 | func listServers(t *testing.T, client *gophercloud.ServiceClient) { |
| 93 | t.Logf("> servers.List") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 94 | |
| 95 | count := 0 |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 96 | err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 97 | count++ |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 98 | t.Logf("--- Page %02d ---", count) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 99 | |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 100 | 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 Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 109 | } |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 110 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 111 | func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 112 | t.Logf("> servers.ChangeAdminPassword") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 113 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 114 | original := server.AdminPass |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 115 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 116 | t.Logf("Changing server password.") |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 117 | err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).Extract() |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 118 | th.AssertNoErr(t, err) |
| 119 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 120 | err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300) |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 121 | th.AssertNoErr(t, err) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 122 | t.Logf("Password changed successfully.") |
| 123 | } |
| 124 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 125 | func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 126 | t.Logf("> servers.Reboot") |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 127 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 128 | err := servers.Reboot(client, server.ID, os.HardReboot).Extract() |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -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 | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 132 | th.AssertNoErr(t, err) |
| 133 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 134 | t.Logf("Server successfully rebooted.") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 135 | } |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 136 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 137 | func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 138 | t.Logf("> servers.Rebuild") |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 139 | |
| 140 | options, err := optionsFromEnv() |
| 141 | th.AssertNoErr(t, err) |
| 142 | |
Ash Wilson | 5d68672 | 2014-10-23 12:51:30 -0400 | [diff] [blame^] | 143 | opts := servers.RebuildOpts{ |
| 144 | Name: tools.RandomString("RenamedGopher", 16), |
| 145 | AdminPass: tools.MakeNewPassword(server.AdminPass), |
| 146 | ImageID: options.imageID, |
| 147 | DiskConfig: diskconfig.Manual, |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 148 | } |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 149 | after, err := servers.Rebuild(client, server.ID, opts).Extract() |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 150 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 151 | th.CheckEquals(t, after.ID, server.ID) |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 152 | |
| 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 Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 159 | |
| 160 | func 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 Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 169 | func 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 Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 178 | func TestServerOperations(t *testing.T) { |
| 179 | client, err := newClient() |
| 180 | th.AssertNoErr(t, err) |
| 181 | |
Ash Wilson | 9c24f6b | 2014-10-23 12:41:19 -0400 | [diff] [blame] | 182 | kp := createServerKeyPair(t, client) |
| 183 | defer deleteServerKeyPair(t, client, kp) |
| 184 | |
| 185 | server := createServer(t, client, kp.Name) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame] | 186 | 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 | } |