blob: 0db92f983426fbc637575904f0051e3a6f077f00 [file] [log] [blame]
package servers
import (
"fmt"
"net/http"
"testing"
"github.com/rackspace/gophercloud/pagination"
"github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
)
func TestListServers(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "GET")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
r.ParseForm()
marker := r.Form.Get("marker")
switch marker {
case "":
fmt.Fprintf(w, serverListBody)
case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
fmt.Fprintf(w, `{ "servers": [] }`)
default:
t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
}
})
pages := 0
err := List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
pages++
actual, err := ExtractServers(page)
if err != nil {
return false, err
}
if len(actual) != 2 {
t.Fatalf("Expected 2 servers, got %d", len(actual))
}
equalServers(t, serverHerp, actual[0])
equalServers(t, serverDerp, actual[1])
return true, nil
})
testhelper.AssertNoErr(t, err)
if pages != 1 {
t.Errorf("Expected 1 page, saw %d", pages)
}
}
func TestCreateServer(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestJSONRequest(t, r, `{
"server": {
"name": "derp",
"imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
"flavorRef": "1"
}
}`)
w.WriteHeader(http.StatusAccepted)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, singleServerBody)
})
client := fake.ServiceClient()
actual, err := Create(client, CreateOpts{
Name: "derp",
ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
FlavorRef: "1",
}).Extract()
if err != nil {
t.Fatalf("Unexpected Create error: %v", err)
}
equalServers(t, serverDerp, *actual)
}
func TestDeleteServer(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "DELETE")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
client := fake.ServiceClient()
err := Delete(client, "asdfasdfasdf")
if err != nil {
t.Fatalf("Unexpected Delete error: %v", err)
}
}
func TestGetServer(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "GET")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestHeader(t, r, "Accept", "application/json")
fmt.Fprintf(w, singleServerBody)
})
client := fake.ServiceClient()
actual, err := Get(client, "1234asdf").Extract()
if err != nil {
t.Fatalf("Unexpected Get error: %v", err)
}
equalServers(t, serverDerp, *actual)
}
func TestUpdateServer(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "PUT")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestHeader(t, r, "Accept", "application/json")
testhelper.TestHeader(t, r, "Content-Type", "application/json")
testhelper.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
fmt.Fprintf(w, singleServerBody)
})
client := fake.ServiceClient()
actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
if err != nil {
t.Fatalf("Unexpected Update error: %v", err)
}
equalServers(t, serverDerp, *actual)
}
func TestChangeServerAdminPassword(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
w.WriteHeader(http.StatusAccepted)
})
res := ChangeAdminPassword(fake.ServiceClient(), "1234asdf", "new-password")
testhelper.AssertNoErr(t, res.Err)
}
func TestRebootServer(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
w.WriteHeader(http.StatusAccepted)
})
res := Reboot(fake.ServiceClient(), "1234asdf", SoftReboot)
testhelper.AssertNoErr(t, res.Err)
}
func TestRebuildServer(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestJSONRequest(t, r, `
{
"rebuild": {
"name": "new-name",
"adminPass": "swordfish",
"imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
"accessIPv4": "1.2.3.4"
}
}
`)
w.WriteHeader(http.StatusAccepted)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, singleServerBody)
})
opts := RebuildOpts{
Name: "new-name",
AdminPass: "swordfish",
ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
AccessIPv4: "1.2.3.4",
}
actual, err := Rebuild(fake.ServiceClient(), "1234asdf", opts).Extract()
testhelper.AssertNoErr(t, err)
equalServers(t, serverDerp, *actual)
}
func TestResizeServer(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
w.WriteHeader(http.StatusAccepted)
})
res := Resize(fake.ServiceClient(), "1234asdf", "2")
testhelper.AssertNoErr(t, res.Err)
}
func TestConfirmResize(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestJSONRequest(t, r, `{ "confirmResize": null }`)
w.WriteHeader(http.StatusNoContent)
})
res := ConfirmResize(fake.ServiceClient(), "1234asdf")
testhelper.AssertNoErr(t, res.Err)
}
func TestRevertResize(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
testhelper.TestJSONRequest(t, r, `{ "revertResize": null }`)
w.WriteHeader(http.StatusAccepted)
})
res := RevertResize(fake.ServiceClient(), "1234asdf")
testhelper.AssertNoErr(t, res.Err)
}