Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame^] | 1 | // +build fixtures |
| 2 | |
| 3 | package servergroups |
| 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 | "server_groups": [ |
| 18 | { |
| 19 | "id": "616fb98f-46ca-475e-917e-2563e5a8cd19", |
| 20 | "name": "test", |
| 21 | "policies": [ |
| 22 | "anti-affinity" |
| 23 | ], |
| 24 | "members": [], |
| 25 | "metadata": {} |
| 26 | }, |
| 27 | { |
| 28 | "id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 29 | "name": "test2", |
| 30 | "policies": [ |
| 31 | "affinity" |
| 32 | ], |
| 33 | "members": [], |
| 34 | "metadata": {} |
| 35 | } |
| 36 | ] |
| 37 | } |
| 38 | ` |
| 39 | |
| 40 | // GetOutput is a sample response to a Get call. |
| 41 | const GetOutput = ` |
| 42 | { |
| 43 | "server_group": { |
| 44 | "id": "616fb98f-46ca-475e-917e-2563e5a8cd19", |
| 45 | "name": "test", |
| 46 | "policies": [ |
| 47 | "anti-affinity" |
| 48 | ], |
| 49 | "members": [], |
| 50 | "metadata": {} |
| 51 | } |
| 52 | } |
| 53 | ` |
| 54 | |
| 55 | // CreateOutput is a sample response to a Post call |
| 56 | const CreateOutput = ` |
| 57 | { |
| 58 | "server_group": { |
| 59 | "id": "616fb98f-46ca-475e-917e-2563e5a8cd19", |
| 60 | "name": "test", |
| 61 | "policies": [ |
| 62 | "anti-affinity" |
| 63 | ], |
| 64 | "members": [], |
| 65 | "metadata": {} |
| 66 | } |
| 67 | } |
| 68 | ` |
| 69 | |
| 70 | // FirstServerGroup is the first result in ListOutput. |
| 71 | var FirstServerGroup = ServerGroup{ |
| 72 | ID: "616fb98f-46ca-475e-917e-2563e5a8cd19", |
| 73 | Name: "test", |
| 74 | Policies: []string{ |
| 75 | "anti-affinity", |
| 76 | }, |
| 77 | Members: []string{}, |
| 78 | Metadata: map[string]interface{}{}, |
| 79 | } |
| 80 | |
| 81 | // SecondServerGroup is the second result in ListOutput. |
| 82 | var SecondServerGroup = ServerGroup{ |
| 83 | ID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 84 | Name: "test2", |
| 85 | Policies: []string{ |
| 86 | "affinity", |
| 87 | }, |
| 88 | Members: []string{}, |
| 89 | Metadata: map[string]interface{}{}, |
| 90 | } |
| 91 | |
| 92 | // ExpectedServerGroupSlice is the slice of results that should be parsed |
| 93 | // from ListOutput, in the expected order. |
| 94 | var ExpectedServerGroupSlice = []ServerGroup{FirstServerGroup, SecondServerGroup} |
| 95 | |
| 96 | // CreatedServerGroup is the parsed result from CreateOutput. |
| 97 | var CreatedServerGroup = ServerGroup{ |
| 98 | ID: "616fb98f-46ca-475e-917e-2563e5a8cd19", |
| 99 | Name: "test", |
| 100 | Policies: []string{ |
| 101 | "anti-affinity", |
| 102 | }, |
| 103 | Members: []string{}, |
| 104 | Metadata: map[string]interface{}{}, |
| 105 | } |
| 106 | |
| 107 | // HandleListSuccessfully configures the test server to respond to a List request. |
| 108 | func HandleListSuccessfully(t *testing.T) { |
| 109 | th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) { |
| 110 | th.TestMethod(t, r, "GET") |
| 111 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 112 | |
| 113 | w.Header().Add("Content-Type", "application/json") |
| 114 | fmt.Fprintf(w, ListOutput) |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | // HandleGetSuccessfully configures the test server to respond to a Get request |
| 119 | // for an existing server group |
| 120 | func HandleGetSuccessfully(t *testing.T) { |
| 121 | th.Mux.HandleFunc("/os-server-groups/4d8c3732-a248-40ed-bebc-539a6ffd25c0", func(w http.ResponseWriter, r *http.Request) { |
| 122 | th.TestMethod(t, r, "GET") |
| 123 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 124 | |
| 125 | w.Header().Add("Content-Type", "application/json") |
| 126 | fmt.Fprintf(w, GetOutput) |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | // HandleCreateSuccessfully configures the test server to respond to a Create request |
| 131 | // for a new server group |
| 132 | func HandleCreateSuccessfully(t *testing.T) { |
| 133 | th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) { |
| 134 | th.TestMethod(t, r, "POST") |
| 135 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 136 | th.TestJSONRequest(t, r, ` |
| 137 | { |
| 138 | "server_group": { |
| 139 | "name": "test", |
| 140 | "policies": [ |
| 141 | "anti-affinity" |
| 142 | ] |
| 143 | } |
| 144 | } |
| 145 | `) |
| 146 | |
| 147 | w.Header().Add("Content-Type", "application/json") |
| 148 | fmt.Fprintf(w, CreateOutput) |
| 149 | }) |
| 150 | } |
| 151 | |
| 152 | // HandleDeleteSuccessfully configures the test server to respond to a Delete request for a |
| 153 | // an existing server group |
| 154 | func HandleDeleteSuccessfully(t *testing.T) { |
| 155 | th.Mux.HandleFunc("/os-server-groups/616fb98f-46ca-475e-917e-2563e5a8cd19", func(w http.ResponseWriter, r *http.Request) { |
| 156 | th.TestMethod(t, r, "DELETE") |
| 157 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 158 | |
| 159 | w.WriteHeader(http.StatusAccepted) |
| 160 | }) |
| 161 | } |