blob: 8c034cf3339310e89821ea71e6f1c084c11a607a [file] [log] [blame]
Ash Wilson69c08742014-10-21 14:04:40 -04001package servers
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Ash Wilson7a20cc92014-10-21 15:53:03 -04008 os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Ash Wilson69c08742014-10-21 14:04:40 -04009 "github.com/rackspace/gophercloud/pagination"
10 th "github.com/rackspace/gophercloud/testhelper"
11 "github.com/rackspace/gophercloud/testhelper/client"
12)
13
14func TestListServers(t *testing.T) {
15 th.SetupHTTP()
16 defer th.TeardownHTTP()
17
18 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "GET")
20 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
21
22 w.Header().Add("Content-Type", "application/json")
23 fmt.Fprintf(w, ListOutput)
24 })
25
26 count := 0
27 err := List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
28 count++
29 actual, err := ExtractServers(page)
30 th.AssertNoErr(t, err)
31 th.CheckDeepEquals(t, ExpectedServerSlice, actual)
32
33 return true, nil
34 })
35 th.AssertNoErr(t, err)
36 th.CheckEquals(t, 1, count)
37}
38
Ash Wilson7a20cc92014-10-21 15:53:03 -040039func TestCreateServer(t *testing.T) {
40 th.SetupHTTP()
41 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040042 os.HandleServerCreationSuccessfully(t, CreateOutput)
Ash Wilson7a20cc92014-10-21 15:53:03 -040043
Ash Wilson664fe332014-10-21 17:47:49 -040044 actual, err := Create(client.ServiceClient(), os.CreateOpts{
Ash Wilson7a20cc92014-10-21 15:53:03 -040045 Name: "derp",
46 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
47 FlavorRef: "1",
48 }).Extract()
49 th.AssertNoErr(t, err)
50
51 th.CheckDeepEquals(t, &CreatedServer, actual)
52}
53
Ash Wilson664fe332014-10-21 17:47:49 -040054func TestDeleteServer(t *testing.T) {
55 th.SetupHTTP()
56 defer th.TeardownHTTP()
57 os.HandleServerDeletionSuccessfully(t)
58
59 err := Delete(client.ServiceClient(), "asdfasdfasdf")
60 th.AssertNoErr(t, err)
61}
62
Ash Wilson69c08742014-10-21 14:04:40 -040063func TestGetServer(t *testing.T) {
64 th.SetupHTTP()
65 defer th.TeardownHTTP()
66
67 th.Mux.HandleFunc("/servers/8c65cb68-0681-4c30-bc88-6b83a8a26aee", func(w http.ResponseWriter, r *http.Request) {
68 th.TestMethod(t, r, "GET")
69 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
70
71 w.Header().Add("Content-Type", "application/json")
72 fmt.Fprintf(w, GetOutput)
73 })
74
75 actual, err := Get(client.ServiceClient(), "8c65cb68-0681-4c30-bc88-6b83a8a26aee").Extract()
76 th.AssertNoErr(t, err)
77 th.CheckDeepEquals(t, &GophercloudServer, actual)
78}
Ash Wilsone846e172014-10-21 14:37:24 -040079
80func TestChangeAdminPassword(t *testing.T) {
81 th.SetupHTTP()
82 defer th.TeardownHTTP()
83
84 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
85 th.TestMethod(t, r, "POST")
86 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
87 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
88
89 w.WriteHeader(http.StatusAccepted)
90 })
91
92 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
93 th.AssertNoErr(t, res.Err)
94}