blob: d9224ec89d429d2756aeb9fdabce51f3a6de1e93 [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
20 r := servers.Create(client, &os.CreateOpts{
21 Name: tools.RandomString("Gophercloud-", 8),
22 ImageRef: options.imageID,
23 FlavorRef: options.flavorID,
24 })
25 t.Logf("\n%s", r.PrettyPrintJSON())
26 s, err := r.Extract()
27 th.AssertNoErr(t, err)
28 return s
29}
30
31func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
32 err := servers.Delete(client, server.ID)
33 th.AssertNoErr(t, err)
34}
35
36func logServer(t *testing.T, server *os.Server, index int) {
37 if index == -1 {
38 t.Logf(" id=[%s]", server.ID)
39 } else {
40 t.Logf("[%02d] id=[%s]", index, server.ID)
41 }
42 t.Logf(" name=[%s]", server.Name)
43 t.Logf(" tenant ID=[%s]", server.TenantID)
44 t.Logf(" user ID=[%s]", server.UserID)
45 t.Logf(" updated=[%s]", server.Updated)
46 t.Logf(" created=[%s]", server.Created)
47 t.Logf(" host ID=[%s]", server.HostID)
48 t.Logf(" access IPv4=[%s]", server.AccessIPv4)
49 t.Logf(" access IPv6=[%s]", server.AccessIPv6)
50 t.Logf(" image=[%v]", server.Image)
51 t.Logf(" flavor=[%v]", server.Flavor)
52 t.Logf(" addresses=[%v]", server.Addresses)
53 t.Logf(" metadata=[%v]", server.Metadata)
54 t.Logf(" links=[%v]", server.Links)
55 t.Logf(" keyname=[%s]", server.KeyName)
56 t.Logf(" admin password=[%s]", server.AdminPass)
57 t.Logf(" status=[%s]", server.Status)
58 t.Logf(" progress=[%d]", server.Progress)
59}
60
61func TestCreateServer(t *testing.T) {
Ash Wilson74b9bcf2014-10-21 14:03:40 -040062 t.Parallel()
63
Ash Wilson88074d52014-10-21 10:04:17 -040064 client, err := newClient()
65 th.AssertNoErr(t, err)
66
67 t.Logf("Creating a new server:")
68 s := createServer(t, client)
69 defer func() {
70 t.Logf("Deleting server")
71 deleteServer(t, client, s)
72 }()
73
74 t.Logf("Waiting for server to become active ...")
75 err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
76 th.AssertNoErr(t, err)
77
78 t.Logf("Server launched:")
79 logServer(t, s, -1)
Ash Wilson74b9bcf2014-10-21 14:03:40 -040080
81 t.Logf("Getting additional server details:")
82 r := servers.Get(client, s.ID)
83 t.Logf("\n%s", r.PrettyPrintJSON())
84 details, err := r.Extract()
85 logServer(t, details, -1)
86}
87
88func TestListServers(t *testing.T) {
89 t.Parallel()
90
91 client, err := newClient()
92 th.AssertNoErr(t, err)
93
94 count := 0
95 err = servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
96 count++
97 t.Logf("-- Page %02d --", count)
98
99 t.Logf("\n%s", page.(os.ServerPage).PrettyPrintJSON())
100
101 s, err := servers.ExtractServers(page)
102 th.AssertNoErr(t, err)
103 for index, server := range s {
104 logServer(t, &server, index)
105 }
106
107 return true, nil
108 })
109 th.AssertNoErr(t, err)
Ash Wilson88074d52014-10-21 10:04:17 -0400110}