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