blob: 4f996106e43a4d13eb0677ab9193f611be418ee9 [file] [log] [blame]
Sreekanth Pothanis07400f32015-09-08 00:26:14 -07001package testing
Joe Topjian520307e2015-02-07 05:22:12 +00002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 th "github.com/gophercloud/gophercloud/testhelper"
9 "github.com/gophercloud/gophercloud/testhelper/client"
Joe Topjian520307e2015-02-07 05:22:12 +000010)
11
12// ListOutput is a sample response to a List call.
13const ListOutput = `
14{
15 "volumeAttachments": [
16 {
17 "device": "/dev/vdd",
18 "id": "a26887c6-c47b-4654-abb5-dfadf7d3f803",
19 "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
20 "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f803"
21 },
22 {
23 "device": "/dev/vdc",
24 "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
25 "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
26 "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
27 }
28 ]
29}
30`
31
32// GetOutput is a sample response to a Get call.
33const GetOutput = `
34{
35 "volumeAttachment": {
36 "device": "/dev/vdc",
37 "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
38 "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
39 "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
40 }
41}
42`
43
44// CreateOutput is a sample response to a Create call.
45const CreateOutput = `
46{
47 "volumeAttachment": {
48 "device": "/dev/vdc",
49 "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
50 "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
51 "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
52 }
53}
54`
55
Joe Topjian520307e2015-02-07 05:22:12 +000056// HandleListSuccessfully configures the test server to respond to a List request.
57func HandleListSuccessfully(t *testing.T) {
58 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) {
59 th.TestMethod(t, r, "GET")
60 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
61
62 w.Header().Add("Content-Type", "application/json")
63 fmt.Fprintf(w, ListOutput)
64 })
65}
66
67// HandleGetSuccessfully configures the test server to respond to a Get request
68// for an existing attachment
69func HandleGetSuccessfully(t *testing.T) {
70 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) {
71 th.TestMethod(t, r, "GET")
72 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
73
74 w.Header().Add("Content-Type", "application/json")
75 fmt.Fprintf(w, GetOutput)
76 })
77}
78
79// HandleCreateSuccessfully configures the test server to respond to a Create request
80// for a new attachment
81func HandleCreateSuccessfully(t *testing.T) {
82 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) {
83 th.TestMethod(t, r, "POST")
84 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
85 th.TestJSONRequest(t, r, `
86{
87 "volumeAttachment": {
88 "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
Joe Topjian520307e2015-02-07 05:22:12 +000089 "device": "/dev/vdc"
90 }
91}
92`)
93
94 w.Header().Add("Content-Type", "application/json")
95 fmt.Fprintf(w, CreateOutput)
96 })
97}
98
99// HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
100// an existing attachment
101func HandleDeleteSuccessfully(t *testing.T) {
102 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) {
103 th.TestMethod(t, r, "DELETE")
104 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
105
106 w.WriteHeader(http.StatusAccepted)
107 })
108}