blob: e9649fe4561e37e0dd0f9b0e3ac33ca9e32bb9c9 [file] [log] [blame]
Ash Wilson88074d52014-10-21 10:04:17 -04001// +build acceptance
2
3package v2
4
5import (
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 Wilson74b9bcf2014-10-21 14:03:40 -040011 "github.com/rackspace/gophercloud/pagination"
Ash Wilson88074d52014-10-21 10:04:17 -040012 "github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
13 th "github.com/rackspace/gophercloud/testhelper"
14)
15
16func createServer(t *testing.T, client *gophercloud.ServiceClient) *os.Server {
17 options, err := optionsFromEnv()
18 th.AssertNoErr(t, err)
19
Ash Wilson7e571412014-10-21 14:29:55 -040020 name := tools.RandomString("Gophercloud-", 8)
21 t.Logf("Creating server [%s].", name)
22 s, err := servers.Create(client, &os.CreateOpts{
23 Name: name,
Ash Wilson88074d52014-10-21 10:04:17 -040024 ImageRef: options.imageID,
25 FlavorRef: options.flavorID,
Ash Wilson7e571412014-10-21 14:29:55 -040026 }).Extract()
Ash Wilson88074d52014-10-21 10:04:17 -040027 th.AssertNoErr(t, err)
Ash Wilson7e571412014-10-21 14:29:55 -040028 t.Logf("Server created successfully.")
Ash Wilson88074d52014-10-21 10:04:17 -040029 return s
30}
31
32func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
Ash Wilson7e571412014-10-21 14:29:55 -040033 t.Logf("Deleting server [%s].", server.ID)
Ash Wilson88074d52014-10-21 10:04:17 -040034 err := servers.Delete(client, server.ID)
35 th.AssertNoErr(t, err)
Ash Wilson7e571412014-10-21 14:29:55 -040036 t.Logf("Server deleted successfully.")
Ash Wilson88074d52014-10-21 10:04:17 -040037}
38
39func 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
64func TestCreateServer(t *testing.T) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -040065 t.Parallel()
66
Ash Wilson88074d52014-10-21 10:04:17 -040067 client, err := newClient()
68 th.AssertNoErr(t, err)
69
Ash Wilson88074d52014-10-21 10:04:17 -040070 s := createServer(t, client)
Ash Wilson7e571412014-10-21 14:29:55 -040071 defer deleteServer(t, client, s)
Ash Wilson88074d52014-10-21 10:04:17 -040072
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 Wilson7e571412014-10-21 14:29:55 -040077 t.Logf("Server launched.")
Ash Wilson88074d52014-10-21 10:04:17 -040078 logServer(t, s, -1)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040079
Ash Wilson7e571412014-10-21 14:29:55 -040080 t.Logf("Getting additional server details.")
81 details, err := servers.Get(client, s.ID).Extract()
Ash Wilson74b9bcf2014-10-21 14:03:40 -040082 logServer(t, details, -1)
83}
84
85func 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 Wilson88074d52014-10-21 10:04:17 -0400107}