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 | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 28 | t.Logf("Server created successfully.") |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 29 | return s |
| 30 | } |
| 31 | |
| 32 | func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) { |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 33 | t.Logf("Deleting server [%s].", server.ID) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 34 | err := servers.Delete(client, server.ID) |
| 35 | th.AssertNoErr(t, err) |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 36 | t.Logf("Server deleted successfully.") |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | func logServer(t *testing.T, server *os.Server, index int) { |
| 40 | if index == -1 { |
| 41 | t.Logf(" id=[%s]", server.ID) |
| 42 | } else { |
| 43 | t.Logf("[%02d] id=[%s]", index, server.ID) |
| 44 | } |
| 45 | t.Logf(" name=[%s]", server.Name) |
| 46 | t.Logf(" tenant ID=[%s]", server.TenantID) |
| 47 | t.Logf(" user ID=[%s]", server.UserID) |
| 48 | t.Logf(" updated=[%s]", server.Updated) |
| 49 | t.Logf(" created=[%s]", server.Created) |
| 50 | t.Logf(" host ID=[%s]", server.HostID) |
| 51 | t.Logf(" access IPv4=[%s]", server.AccessIPv4) |
| 52 | t.Logf(" access IPv6=[%s]", server.AccessIPv6) |
| 53 | t.Logf(" image=[%v]", server.Image) |
| 54 | t.Logf(" flavor=[%v]", server.Flavor) |
| 55 | t.Logf(" addresses=[%v]", server.Addresses) |
| 56 | t.Logf(" metadata=[%v]", server.Metadata) |
| 57 | t.Logf(" links=[%v]", server.Links) |
| 58 | t.Logf(" keyname=[%s]", server.KeyName) |
| 59 | t.Logf(" admin password=[%s]", server.AdminPass) |
| 60 | t.Logf(" status=[%s]", server.Status) |
| 61 | t.Logf(" progress=[%d]", server.Progress) |
| 62 | } |
| 63 | |
| 64 | func TestCreateServer(t *testing.T) { |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 65 | t.Parallel() |
| 66 | |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 67 | client, err := newClient() |
| 68 | th.AssertNoErr(t, err) |
| 69 | |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 70 | s := createServer(t, client) |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 71 | defer deleteServer(t, client, s) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 72 | |
| 73 | t.Logf("Waiting for server to become active ...") |
| 74 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
| 75 | th.AssertNoErr(t, err) |
| 76 | |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 77 | t.Logf("Server launched.") |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 78 | logServer(t, s, -1) |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 79 | |
Ash Wilson | 7e57141 | 2014-10-21 14:29:55 -0400 | [diff] [blame] | 80 | t.Logf("Getting additional server details.") |
| 81 | details, err := servers.Get(client, s.ID).Extract() |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 82 | logServer(t, details, -1) |
| 83 | } |
| 84 | |
| 85 | func TestListServers(t *testing.T) { |
| 86 | t.Parallel() |
| 87 | |
| 88 | client, err := newClient() |
| 89 | th.AssertNoErr(t, err) |
| 90 | |
| 91 | count := 0 |
| 92 | err = servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
| 93 | count++ |
| 94 | t.Logf("-- Page %02d --", count) |
| 95 | |
Ash Wilson | 74b9bcf | 2014-10-21 14:03:40 -0400 | [diff] [blame] | 96 | s, err := servers.ExtractServers(page) |
| 97 | th.AssertNoErr(t, err) |
| 98 | for index, server := range s { |
| 99 | logServer(t, &server, index) |
| 100 | } |
| 101 | |
| 102 | return true, nil |
| 103 | }) |
| 104 | th.AssertNoErr(t, err) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 105 | } |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 106 | |
| 107 | func TestChangeAdminPassword(t *testing.T) { |
| 108 | t.Parallel() |
| 109 | |
| 110 | client, err := newClient() |
| 111 | th.AssertNoErr(t, err) |
| 112 | |
| 113 | s := createServer(t, client) |
| 114 | defer deleteServer(t, client, s) |
| 115 | original := s.AdminPass |
| 116 | |
| 117 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
| 118 | th.AssertNoErr(t, err) |
| 119 | |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 120 | t.Logf("Changing server password.") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 121 | err = servers.ChangeAdminPassword(client, s.ID, tools.MakeNewPassword(original)).Extract() |
| 122 | th.AssertNoErr(t, err) |
| 123 | |
| 124 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
| 125 | th.AssertNoErr(t, err) |
Ash Wilson | 88bbde3 | 2014-10-21 15:13:58 -0400 | [diff] [blame] | 126 | t.Logf("Password changed successfully.") |
| 127 | } |
| 128 | |
| 129 | func TestReboot(t *testing.T) { |
| 130 | t.Parallel() |
| 131 | |
| 132 | client, err := newClient() |
| 133 | th.AssertNoErr(t, err) |
| 134 | |
| 135 | s := createServer(t, client) |
| 136 | defer deleteServer(t, client, s) |
| 137 | |
| 138 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
| 139 | th.AssertNoErr(t, err) |
| 140 | |
| 141 | t.Logf("Rebooting server.") |
| 142 | err = servers.Reboot(client, s.ID, os.HardReboot).Extract() |
| 143 | th.AssertNoErr(t, err) |
| 144 | |
| 145 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
| 146 | th.AssertNoErr(t, err) |
| 147 | t.Logf("Server successfully rebooted.") |
Ash Wilson | 8679332 | 2014-10-21 14:30:09 -0400 | [diff] [blame] | 148 | } |
Ash Wilson | 90f87ba | 2014-10-21 18:36:18 -0400 | [diff] [blame^] | 149 | |
| 150 | func TestRebuild(t *testing.T) { |
| 151 | t.Parallel() |
| 152 | |
| 153 | options, err := optionsFromEnv() |
| 154 | th.AssertNoErr(t, err) |
| 155 | |
| 156 | client, err := newClient() |
| 157 | th.AssertNoErr(t, err) |
| 158 | |
| 159 | s := createServer(t, client) |
| 160 | defer deleteServer(t, client, s) |
| 161 | |
| 162 | err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300) |
| 163 | th.AssertNoErr(t, err) |
| 164 | |
| 165 | t.Logf("Rebuilding server.") |
| 166 | opts := os.RebuildOpts{ |
| 167 | Name: tools.RandomString("RenamedGopher", 16), |
| 168 | AdminPass: tools.MakeNewPassword(s.AdminPass), |
| 169 | ImageID: options.imageID, |
| 170 | } |
| 171 | after, err := servers.Rebuild(client, s.ID, opts).Extract() |
| 172 | th.AssertNoErr(t, err) |
| 173 | |
| 174 | err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300) |
| 175 | th.AssertNoErr(t, err) |
| 176 | |
| 177 | t.Logf("Server successfully rebuilt.") |
| 178 | logServer(t, after, -1) |
| 179 | } |