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 | |
| 96 | t.Logf("\n%s", page.(os.ServerPage).PrettyPrintJSON()) |
| 97 | |
| 98 | s, err := servers.ExtractServers(page) |
| 99 | th.AssertNoErr(t, err) |
| 100 | for index, server := range s { |
| 101 | logServer(t, &server, index) |
| 102 | } |
| 103 | |
| 104 | return true, nil |
| 105 | }) |
| 106 | th.AssertNoErr(t, err) |
Ash Wilson | 88074d5 | 2014-10-21 10:04:17 -0400 | [diff] [blame] | 107 | } |