Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
| 3 | package volumeattach |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
| 14 | // ListOutput is a sample response to a List call. |
| 15 | const ListOutput = ` |
| 16 | { |
| 17 | "volumeAttachments": [ |
| 18 | { |
| 19 | "device": "/dev/vdd", |
| 20 | "id": "a26887c6-c47b-4654-abb5-dfadf7d3f803", |
| 21 | "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 22 | "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f803" |
| 23 | }, |
| 24 | { |
| 25 | "device": "/dev/vdc", |
| 26 | "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
| 27 | "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 28 | "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804" |
| 29 | } |
| 30 | ] |
| 31 | } |
| 32 | ` |
| 33 | |
| 34 | // GetOutput is a sample response to a Get call. |
| 35 | const GetOutput = ` |
| 36 | { |
| 37 | "volumeAttachment": { |
| 38 | "device": "/dev/vdc", |
| 39 | "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
| 40 | "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 41 | "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804" |
| 42 | } |
| 43 | } |
| 44 | ` |
| 45 | |
| 46 | // CreateOutput is a sample response to a Create call. |
| 47 | const CreateOutput = ` |
| 48 | { |
| 49 | "volumeAttachment": { |
| 50 | "device": "/dev/vdc", |
| 51 | "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
| 52 | "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 53 | "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804" |
| 54 | } |
| 55 | } |
| 56 | ` |
| 57 | |
| 58 | // FirstVolumeAttachment is the first result in ListOutput. |
| 59 | var FirstVolumeAttachment = VolumeAttachment{ |
| 60 | Device: "/dev/vdd", |
| 61 | ID: "a26887c6-c47b-4654-abb5-dfadf7d3f803", |
| 62 | ServerID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 63 | VolumeID: "a26887c6-c47b-4654-abb5-dfadf7d3f803", |
| 64 | } |
| 65 | |
| 66 | // SecondVolumeAttachment is the first result in ListOutput. |
| 67 | var SecondVolumeAttachment = VolumeAttachment{ |
| 68 | Device: "/dev/vdc", |
| 69 | ID: "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
| 70 | ServerID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 71 | VolumeID: "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
| 72 | } |
| 73 | |
| 74 | // ExpectedVolumeAttachmentSlide is the slice of results that should be parsed |
| 75 | // from ListOutput, in the expected order. |
| 76 | var ExpectedVolumeAttachmentSlice = []VolumeAttachment{FirstVolumeAttachment, SecondVolumeAttachment} |
| 77 | |
| 78 | // CreatedVolumeAttachment is the parsed result from CreatedOutput. |
| 79 | var CreatedVolumeAttachment = VolumeAttachment{ |
| 80 | Device: "/dev/vdc", |
| 81 | ID: "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
| 82 | ServerID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 83 | VolumeID: "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
| 84 | } |
| 85 | |
| 86 | // HandleListSuccessfully configures the test server to respond to a List request. |
| 87 | func HandleListSuccessfully(t *testing.T) { |
| 88 | th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) { |
| 89 | th.TestMethod(t, r, "GET") |
| 90 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 91 | |
| 92 | w.Header().Add("Content-Type", "application/json") |
| 93 | fmt.Fprintf(w, ListOutput) |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | // HandleGetSuccessfully configures the test server to respond to a Get request |
| 98 | // for an existing attachment |
| 99 | func HandleGetSuccessfully(t *testing.T) { |
| 100 | th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) { |
| 101 | th.TestMethod(t, r, "GET") |
| 102 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 103 | |
| 104 | w.Header().Add("Content-Type", "application/json") |
| 105 | fmt.Fprintf(w, GetOutput) |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | // HandleCreateSuccessfully configures the test server to respond to a Create request |
| 110 | // for a new attachment |
| 111 | func HandleCreateSuccessfully(t *testing.T) { |
| 112 | th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) { |
| 113 | th.TestMethod(t, r, "POST") |
| 114 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 115 | th.TestJSONRequest(t, r, ` |
| 116 | { |
| 117 | "volumeAttachment": { |
| 118 | "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804", |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 119 | "device": "/dev/vdc" |
| 120 | } |
| 121 | } |
| 122 | `) |
| 123 | |
| 124 | w.Header().Add("Content-Type", "application/json") |
| 125 | fmt.Fprintf(w, CreateOutput) |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | // HandleDeleteSuccessfully configures the test server to respond to a Delete request for a |
| 130 | // an existing attachment |
| 131 | func HandleDeleteSuccessfully(t *testing.T) { |
| 132 | th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) { |
| 133 | th.TestMethod(t, r, "DELETE") |
| 134 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 135 | |
| 136 | w.WriteHeader(http.StatusAccepted) |
| 137 | }) |
| 138 | } |