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" |
| 10 | os "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/rackspace/compute/v2/servers" |
| 13 | th "github.com/rackspace/gophercloud/testhelper" |
| 14 | ) |
| 15 | |
| 16 | func createServer(t *testing.T, client *gophercloud.ServiceClient) *os.Server { |
| 17 | options, err := optionsFromEnv() |
| 18 | th.AssertNoErr(t, err) |
| 19 | |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 20 | name := tools.RandomString("Gophercloud-", 8) |
| 21 | t.Logf("Creating server [%s].", name) |
| 22 | s, err := servers.Create(client, &os.CreateOpts{ |
| 23 | Name: name, |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 24 | ImageRef: options.imageID, |
| 25 | FlavorRef: options.flavorID, |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 26 | }).Extract() |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 27 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 28 | t.Logf("Creating server.") |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 29 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 30 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 31 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 32 | t.Logf("Server created successfully.") |
| 33 | |
| 34 | return s |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func logServer(t *testing.T, server *os.Server, index int) { |
| 38 | if index == -1 { |
| 39 | t.Logf(" id=[%s]", server.ID) |
| 40 | } else { |
| 41 | t.Logf("[%02d] id=[%s]", index, server.ID) |
| 42 | } |
| 43 | t.Logf(" name=[%s]", server.Name) |
| 44 | t.Logf(" tenant ID=[%s]", server.TenantID) |
| 45 | t.Logf(" user ID=[%s]", server.UserID) |
| 46 | t.Logf(" updated=[%s]", server.Updated) |
| 47 | t.Logf(" created=[%s]", server.Created) |
| 48 | t.Logf(" host ID=[%s]", server.HostID) |
| 49 | t.Logf(" access IPv4=[%s]", server.AccessIPv4) |
| 50 | t.Logf(" access IPv6=[%s]", server.AccessIPv6) |
| 51 | t.Logf(" image=[%v]", server.Image) |
| 52 | t.Logf(" flavor=[%v]", server.Flavor) |
| 53 | t.Logf(" addresses=[%v]", server.Addresses) |
| 54 | t.Logf(" metadata=[%v]", server.Metadata) |
| 55 | t.Logf(" links=[%v]", server.Links) |
| 56 | t.Logf(" keyname=[%s]", server.KeyName) |
| 57 | t.Logf(" admin password=[%s]", server.AdminPass) |
| 58 | t.Logf(" status=[%s]", server.Status) |
| 59 | t.Logf(" progress=[%d]", server.Progress) |
| 60 | } |
| 61 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 62 | func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 63 | t.Logf("> servers.Get") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 64 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 65 | details, err := servers.Get(client, server.ID).Extract() |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 66 | th.AssertNoErr(t, err) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 67 | logServer(t, details, -1) |
| 68 | } |
| 69 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 70 | func listServers(t *testing.T, client *gophercloud.ServiceClient) { |
| 71 | t.Logf("> servers.List") |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 72 | |
| 73 | count := 0 |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 74 | err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 75 | count++ |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 76 | t.Logf("--- Page %02d ---", count) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 77 | |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 78 | s, err := servers.ExtractServers(page) |
| 79 | th.AssertNoErr(t, err) |
| 80 | for index, server := range s { |
| 81 | logServer(t, &server, index) |
| 82 | } |
| 83 | |
| 84 | return true, nil |
| 85 | }) |
| 86 | th.AssertNoErr(t, err) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 87 | } |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 88 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 89 | func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 90 | t.Logf("> servers.ChangeAdminPassword") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 91 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 92 | original := server.AdminPass |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 93 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 94 | t.Logf("Changing server password.") |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 95 | err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).Extract() |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 96 | th.AssertNoErr(t, err) |
| 97 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 98 | err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300) |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 99 | th.AssertNoErr(t, err) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 100 | t.Logf("Password changed successfully.") |
| 101 | } |
| 102 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 103 | func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 104 | t.Logf("> servers.Reboot") |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 105 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 106 | err := servers.Reboot(client, server.ID, os.HardReboot).Extract() |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 107 | th.AssertNoErr(t, err) |
| 108 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 109 | err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 110 | th.AssertNoErr(t, err) |
| 111 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 112 | t.Logf("Server successfully rebooted.") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 113 | } |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 114 | |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 115 | func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 116 | t.Logf("> servers.Rebuild") |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 117 | |
| 118 | options, err := optionsFromEnv() |
| 119 | th.AssertNoErr(t, err) |
| 120 | |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 121 | opts := os.RebuildOpts{ |
| 122 | Name: tools.RandomString("RenamedGopher", 16), |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 123 | AdminPass: tools.MakeNewPassword(server.AdminPass), |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 124 | ImageID: options.imageID, |
| 125 | } |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 126 | after, err := servers.Rebuild(client, server.ID, opts).Extract() |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 127 | th.AssertNoErr(t, err) |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 128 | th.CheckEquals(t, after.ID, server.ID) |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame] | 129 | |
| 130 | err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300) |
| 131 | th.AssertNoErr(t, err) |
| 132 | |
| 133 | t.Logf("Server successfully rebuilt.") |
| 134 | logServer(t, after, -1) |
| 135 | } |
Ash Wilson | 5b32ca7 | 2014-10-22 08:11:37 -0400 | [diff] [blame^] | 136 | |
| 137 | func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
| 138 | t.Logf("> servers.Delete") |
| 139 | |
| 140 | err := servers.Delete(client, server.ID) |
| 141 | th.AssertNoErr(t, err) |
| 142 | |
| 143 | t.Logf("Server deleted successfully.") |
| 144 | } |
| 145 | |
| 146 | func TestServerOperations(t *testing.T) { |
| 147 | client, err := newClient() |
| 148 | th.AssertNoErr(t, err) |
| 149 | |
| 150 | server := createServer(t, client) |
| 151 | defer deleteServer(t, client, server) |
| 152 | |
| 153 | getServer(t, client, server) |
| 154 | listServers(t, client) |
| 155 | changeAdminPassword(t, client, server) |
| 156 | rebootServer(t, client, server) |
| 157 | rebuildServer(t, client, server) |
| 158 | } |