github.com/gophercloud/gophercloud@v1.11.0/openstack/keymanager/v1/containers/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/gophercloud/gophercloud/openstack/keymanager/v1/containers" 10 th "github.com/gophercloud/gophercloud/testhelper" 11 "github.com/gophercloud/gophercloud/testhelper/client" 12 ) 13 14 // ListResponse provides a single page of container results. 15 const ListResponse = ` 16 { 17 "containers": [ 18 { 19 "consumers": [], 20 "container_ref": "http://barbican:9311/v1/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", 21 "created": "2018-06-21T21:28:37", 22 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 23 "name": "mycontainer", 24 "secret_refs": [ 25 { 26 "name": "mysecret", 27 "secret_ref": "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c" 28 } 29 ], 30 "status": "ACTIVE", 31 "type": "generic", 32 "updated": "2018-06-21T21:28:37" 33 }, 34 { 35 "consumers": [], 36 "container_ref": "http://barbican:9311/v1/containers/47b20e73-335b-4867-82dc-3796524d5e20", 37 "created": "2018-06-21T21:30:09", 38 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 39 "name": "anothercontainer", 40 "secret_refs": [ 41 { 42 "name": "another", 43 "secret_ref": "http://barbican:9311/v1/secrets/1b12b69a-8822-442e-a303-da24ade648ac" 44 } 45 ], 46 "status": "ACTIVE", 47 "type": "generic", 48 "updated": "2018-06-21T21:30:09" 49 } 50 ], 51 "total": 2 52 }` 53 54 // GetResponse provides a Get result. 55 const GetResponse = ` 56 { 57 "consumers": [], 58 "container_ref": "http://barbican:9311/v1/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", 59 "created": "2018-06-21T21:28:37", 60 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 61 "name": "mycontainer", 62 "secret_refs": [ 63 { 64 "name": "mysecret", 65 "secret_ref": "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c" 66 } 67 ], 68 "status": "ACTIVE", 69 "type": "generic", 70 "updated": "2018-06-21T21:28:37" 71 }` 72 73 // CreateRequest provides the input to a Create request. 74 const CreateRequest = ` 75 { 76 "name": "mycontainer", 77 "secret_refs": [ 78 { 79 "name": "mysecret", 80 "secret_ref": "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c" 81 } 82 ], 83 "type": "generic" 84 }` 85 86 // CreateResponse is the response of a Create request. 87 const CreateResponse = ` 88 { 89 "consumers": [], 90 "container_ref": "http://barbican:9311/v1/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", 91 "created": "2018-06-21T21:28:37", 92 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 93 "name": "mycontainer", 94 "secret_refs": [ 95 { 96 "name": "mysecret", 97 "secret_ref": "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c" 98 } 99 ], 100 "status": "ACTIVE", 101 "type": "generic", 102 "updated": "2018-06-21T21:28:37" 103 }` 104 105 // FirstContainer is the first resource in the List request. 106 var FirstContainer = containers.Container{ 107 Consumers: []containers.ConsumerRef{}, 108 ContainerRef: "http://barbican:9311/v1/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", 109 Created: time.Date(2018, 6, 21, 21, 28, 37, 0, time.UTC), 110 CreatorID: "5c70d99f4a8641c38f8084b32b5e5c0e", 111 Name: "mycontainer", 112 SecretRefs: []containers.SecretRef{ 113 { 114 Name: "mysecret", 115 SecretRef: "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c", 116 }, 117 }, 118 Status: "ACTIVE", 119 Type: "generic", 120 Updated: time.Date(2018, 6, 21, 21, 28, 37, 0, time.UTC), 121 } 122 123 // SecondContainer is the second resource in the List request. 124 var SecondContainer = containers.Container{ 125 Consumers: []containers.ConsumerRef{}, 126 ContainerRef: "http://barbican:9311/v1/containers/47b20e73-335b-4867-82dc-3796524d5e20", 127 Created: time.Date(2018, 6, 21, 21, 30, 9, 0, time.UTC), 128 CreatorID: "5c70d99f4a8641c38f8084b32b5e5c0e", 129 Name: "anothercontainer", 130 SecretRefs: []containers.SecretRef{ 131 { 132 Name: "another", 133 SecretRef: "http://barbican:9311/v1/secrets/1b12b69a-8822-442e-a303-da24ade648ac", 134 }, 135 }, 136 Status: "ACTIVE", 137 Type: "generic", 138 Updated: time.Date(2018, 6, 21, 21, 30, 9, 0, time.UTC), 139 } 140 141 // ExpectedContainersSlice is the slice of containers expected to be returned from ListResponse. 142 var ExpectedContainersSlice = []containers.Container{FirstContainer, SecondContainer} 143 144 const ListConsumersResponse = ` 145 { 146 "consumers": [ 147 { 148 "URL": "http://example.com", 149 "created": "2018-06-22T16:26:25", 150 "name": "CONSUMER-LZILN1zq", 151 "status": "ACTIVE", 152 "updated": "2018-06-22T16:26:25" 153 } 154 ], 155 "total": 1 156 }` 157 158 // ExpectedConsumer is the expected result of a consumer retrieval. 159 var ExpectedConsumer = containers.Consumer{ 160 URL: "http://example.com", 161 Created: time.Date(2018, 6, 22, 16, 26, 25, 0, time.UTC), 162 Name: "CONSUMER-LZILN1zq", 163 Status: "ACTIVE", 164 Updated: time.Date(2018, 6, 22, 16, 26, 25, 0, time.UTC), 165 } 166 167 // ExpectedConsumersSlice is an expected slice of consumers. 168 var ExpectedConsumersSlice = []containers.Consumer{ExpectedConsumer} 169 170 const CreateConsumerRequest = ` 171 { 172 "URL": "http://example.com", 173 "name": "CONSUMER-LZILN1zq" 174 }` 175 176 const CreateConsumerResponse = ` 177 { 178 "consumers": [ 179 { 180 "URL": "http://example.com", 181 "name": "CONSUMER-LZILN1zq" 182 } 183 ], 184 "container_ref": "http://barbican:9311/v1/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", 185 "created": "2018-06-21T21:28:37", 186 "creator_id": "5c70d99f4a8641c38f8084b32b5e5c0e", 187 "name": "mycontainer", 188 "secret_refs": [ 189 { 190 "name": "mysecret", 191 "secret_ref": "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c" 192 } 193 ], 194 "status": "ACTIVE", 195 "type": "generic", 196 "updated": "2018-06-21T21:28:37" 197 }` 198 199 // ExpectedCreatedConsumer is the expected result of adding a consumer. 200 var ExpectedCreatedConsumer = containers.Container{ 201 Consumers: []containers.ConsumerRef{ 202 { 203 Name: "CONSUMER-LZILN1zq", 204 URL: "http://example.com", 205 }, 206 }, 207 ContainerRef: "http://barbican:9311/v1/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", 208 Created: time.Date(2018, 6, 21, 21, 28, 37, 0, time.UTC), 209 CreatorID: "5c70d99f4a8641c38f8084b32b5e5c0e", 210 Name: "mycontainer", 211 SecretRefs: []containers.SecretRef{ 212 { 213 Name: "mysecret", 214 SecretRef: "http://barbican:9311/v1/secrets/1b8068c4-3bb6-4be6-8f1e-da0d1ea0b67c", 215 }, 216 }, 217 Status: "ACTIVE", 218 Type: "generic", 219 Updated: time.Date(2018, 6, 21, 21, 28, 37, 0, time.UTC), 220 } 221 222 const DeleteConsumerRequest = ` 223 { 224 "URL": "http://example.com", 225 "name": "CONSUMER-LZILN1zq" 226 }` 227 228 // HandleListContainersSuccessfully creates an HTTP handler at `/containers` on the 229 // test handler mux that responds with a list of two containers. 230 func HandleListContainersSuccessfully(t *testing.T) { 231 th.Mux.HandleFunc("/containers", func(w http.ResponseWriter, r *http.Request) { 232 th.TestMethod(t, r, "GET") 233 th.TestHeader(t, r, "Accept", "application/json") 234 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 235 236 w.Header().Set("Content-Type", "application/json") 237 w.WriteHeader(http.StatusOK) 238 fmt.Fprintf(w, ListResponse) 239 }) 240 } 241 242 // HandleGetContainerSuccessfully creates an HTTP handler at `/containers` on the 243 // test handler mux that responds with a single resource. 244 func HandleGetContainerSuccessfully(t *testing.T) { 245 th.Mux.HandleFunc("/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", func(w http.ResponseWriter, r *http.Request) { 246 th.TestMethod(t, r, "GET") 247 th.TestHeader(t, r, "Accept", "application/json") 248 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 249 250 w.Header().Set("Content-Type", "application/json") 251 w.WriteHeader(http.StatusOK) 252 fmt.Fprintf(w, GetResponse) 253 }) 254 } 255 256 // HandleCreateContainerSuccessfully creates an HTTP handler at `/containers` on the 257 // test handler mux that tests resource creation. 258 func HandleCreateContainerSuccessfully(t *testing.T) { 259 th.Mux.HandleFunc("/containers", func(w http.ResponseWriter, r *http.Request) { 260 th.TestMethod(t, r, "POST") 261 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 262 th.TestJSONRequest(t, r, CreateRequest) 263 264 w.WriteHeader(http.StatusCreated) 265 fmt.Fprintf(w, GetResponse) 266 }) 267 } 268 269 // HandleDeleteContainerSuccessfully creates an HTTP handler at `/containers` on the 270 // test handler mux that tests resource deletion. 271 func HandleDeleteContainerSuccessfully(t *testing.T) { 272 th.Mux.HandleFunc("/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0", func(w http.ResponseWriter, r *http.Request) { 273 th.TestMethod(t, r, "DELETE") 274 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 275 276 w.WriteHeader(http.StatusNoContent) 277 }) 278 } 279 280 // HandleListConsumersSuccessfully creates an HTTP handler at 281 // `/containers/uuid/consumers` on the test handler mux that responds with 282 // a list of consumers. 283 func HandleListConsumersSuccessfully(t *testing.T) { 284 th.Mux.HandleFunc("/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0/consumers", func(w http.ResponseWriter, r *http.Request) { 285 th.TestMethod(t, r, "GET") 286 th.TestHeader(t, r, "Accept", "application/json") 287 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 288 289 w.Header().Set("Content-Type", "application/json") 290 w.WriteHeader(http.StatusOK) 291 fmt.Fprintf(w, ListConsumersResponse) 292 }) 293 } 294 295 // HandleCreateConsumerSuccessfully creates an HTTP handler at 296 // `/containers/uuid/consumers` on the test handler mux that tests resource 297 // creation. 298 func HandleCreateConsumerSuccessfully(t *testing.T) { 299 th.Mux.HandleFunc("/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0/consumers", func(w http.ResponseWriter, r *http.Request) { 300 th.TestMethod(t, r, "POST") 301 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 302 th.TestJSONRequest(t, r, CreateConsumerRequest) 303 304 w.WriteHeader(http.StatusOK) 305 fmt.Fprintf(w, CreateConsumerResponse) 306 }) 307 } 308 309 // HandleDeleteConsumerSuccessfully creates an HTTP handler at 310 // `/containers/uuid/consumers` on the test handler mux that tests resource 311 // deletion. 312 func HandleDeleteConsumerSuccessfully(t *testing.T) { 313 th.Mux.HandleFunc("/containers/dfdb88f3-4ddb-4525-9da6-066453caa9b0/consumers", func(w http.ResponseWriter, r *http.Request) { 314 th.TestMethod(t, r, "DELETE") 315 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 316 th.TestJSONRequest(t, r, CreateConsumerRequest) 317 318 w.WriteHeader(http.StatusOK) 319 fmt.Fprintf(w, GetResponse) 320 }) 321 }