blob: 0c331eb52c3f61d972d3674e503eec8a0549608e [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()
Ash Wilson2295ba52014-10-21 18:19:28 -040083 os.HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsone846e172014-10-21 14:37:24 -040084
85 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
86 th.AssertNoErr(t, res.Err)
87}
Ash Wilson2295ba52014-10-21 18:19:28 -040088
89func TestReboot(t *testing.T) {
90 th.SetupHTTP()
91 defer th.TeardownHTTP()
92 os.HandleRebootSuccessfully(t)
93
94 res := Reboot(client.ServiceClient(), "1234asdf", os.SoftReboot)
95 th.AssertNoErr(t, res.Err)
96}
Ash Wilsonacf49c62014-10-21 18:25:11 -040097
98func TestRebuildServer(t *testing.T) {
99 th.SetupHTTP()
100 defer th.TeardownHTTP()
101 os.HandleRebuildSuccessfully(t, GetOutput)
102
103 opts := os.RebuildOpts{
104 Name: "new-name",
105 AdminPass: "swordfish",
106 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
107 AccessIPv4: "1.2.3.4",
108 }
109 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
110 th.AssertNoErr(t, err)
111 th.CheckDeepEquals(t, &GophercloudServer, actual)
112}