Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 1 | package stackresources |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
| 14 | var FindExpected = []Resource{ |
| 15 | Resource{ |
| 16 | Name: "hello_world", |
| 17 | Links: []gophercloud.Link{ |
| 18 | gophercloud.Link{ |
| 19 | Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world", |
| 20 | Rel: "self", |
| 21 | }, |
| 22 | gophercloud.Link{ |
| 23 | Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b", |
| 24 | Rel: "stack", |
| 25 | }, |
| 26 | }, |
| 27 | LogicalID: "hello_world", |
| 28 | StatusReason: "state changed", |
| 29 | UpdatedTime: time.Date(2015, 2, 5, 21, 33, 11, 0, time.UTC), |
| 30 | RequiredBy: []interface{}{}, |
| 31 | Status: "CREATE_IN_PROGRESS", |
| 32 | PhysicalID: "49181cd6-169a-4130-9455-31185bbfc5bf", |
| 33 | Type: "OS::Nova::Server", |
| 34 | }, |
| 35 | } |
| 36 | |
| 37 | const FindOutput = ` |
| 38 | { |
| 39 | "resources": [ |
| 40 | { |
| 41 | "resource_name": "hello_world", |
| 42 | "links": [ |
| 43 | { |
| 44 | "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world", |
| 45 | "rel": "self" |
| 46 | }, |
| 47 | { |
| 48 | "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b", |
| 49 | "rel": "stack" |
| 50 | } |
| 51 | ], |
| 52 | "logical_resource_id": "hello_world", |
| 53 | "resource_status_reason": "state changed", |
| 54 | "updated_time": "2015-02-05T21:33:11Z", |
| 55 | "required_by": [], |
| 56 | "resource_status": "CREATE_IN_PROGRESS", |
| 57 | "physical_resource_id": "49181cd6-169a-4130-9455-31185bbfc5bf", |
| 58 | "resource_type": "OS::Nova::Server" |
| 59 | } |
| 60 | ] |
| 61 | }` |
| 62 | |
| 63 | // HandleFindSuccessfully creates an HTTP handler at `/stacks/hello_world/resources` |
| 64 | // on the test handler mux that responds with a `Find` response. |
| 65 | func HandleFindSuccessfully(t *testing.T, output string) { |
| 66 | th.Mux.HandleFunc("/stacks/hello_world/resources", func(w http.ResponseWriter, r *http.Request) { |
| 67 | th.TestMethod(t, r, "GET") |
| 68 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 69 | th.TestHeader(t, r, "Accept", "application/json") |
| 70 | |
| 71 | w.Header().Set("Content-Type", "application/json") |
| 72 | w.WriteHeader(http.StatusOK) |
| 73 | fmt.Fprintf(w, output) |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | var ListExpected = []Resource{ |
| 78 | Resource{ |
| 79 | Name: "hello_world", |
| 80 | Links: []gophercloud.Link{ |
| 81 | gophercloud.Link{ |
| 82 | Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world", |
| 83 | Rel: "self", |
| 84 | }, |
| 85 | gophercloud.Link{ |
| 86 | Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b", |
| 87 | Rel: "stack", |
| 88 | }, |
| 89 | }, |
| 90 | LogicalID: "hello_world", |
| 91 | StatusReason: "state changed", |
| 92 | UpdatedTime: time.Date(2015, 2, 5, 21, 33, 11, 0, time.UTC), |
| 93 | RequiredBy: []interface{}{}, |
| 94 | Status: "CREATE_IN_PROGRESS", |
| 95 | PhysicalID: "49181cd6-169a-4130-9455-31185bbfc5bf", |
| 96 | Type: "OS::Nova::Server", |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | const ListOutput = `{ |
| 101 | "resources": [ |
| 102 | { |
| 103 | "resource_name": "hello_world", |
| 104 | "links": [ |
| 105 | { |
| 106 | "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world", |
| 107 | "rel": "self" |
| 108 | }, |
| 109 | { |
| 110 | "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b", |
| 111 | "rel": "stack" |
| 112 | } |
| 113 | ], |
| 114 | "logical_resource_id": "hello_world", |
| 115 | "resource_status_reason": "state changed", |
| 116 | "updated_time": "2015-02-05T21:33:11Z", |
| 117 | "required_by": [], |
| 118 | "resource_status": "CREATE_IN_PROGRESS", |
| 119 | "physical_resource_id": "49181cd6-169a-4130-9455-31185bbfc5bf", |
| 120 | "resource_type": "OS::Nova::Server" |
| 121 | } |
| 122 | ] |
| 123 | }` |
| 124 | |
| 125 | // HandleListSuccessfully creates an HTTP handler at `/stacks/hello_world/49181cd6-169a-4130-9455-31185bbfc5bf/resources` |
| 126 | // on the test handler mux that responds with a `List` response. |
| 127 | func HandleListSuccessfully(t *testing.T, output string) { |
| 128 | th.Mux.HandleFunc("/stacks/hello_world/49181cd6-169a-4130-9455-31185bbfc5bf/resources", func(w http.ResponseWriter, r *http.Request) { |
| 129 | th.TestMethod(t, r, "GET") |
| 130 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 131 | th.TestHeader(t, r, "Accept", "application/json") |
| 132 | |
| 133 | w.Header().Set("Content-Type", "application/json") |
| 134 | r.ParseForm() |
| 135 | marker := r.Form.Get("marker") |
| 136 | switch marker { |
| 137 | case "": |
| 138 | fmt.Fprintf(w, output) |
| 139 | default: |
| 140 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 141 | } |
| 142 | }) |
| 143 | } |
| 144 | |
| 145 | var GetExpected = &Resource{ |
| 146 | Name: "wordpress_instance", |
| 147 | Links: []gophercloud.Link{ |
| 148 | gophercloud.Link{ |
| 149 | Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance", |
| 150 | Rel: "self", |
| 151 | }, |
| 152 | gophercloud.Link{ |
| 153 | Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e", |
| 154 | Rel: "stack", |
| 155 | }, |
| 156 | }, |
| 157 | LogicalID: "wordpress_instance", |
| 158 | StatusReason: "state changed", |
| 159 | UpdatedTime: time.Date(2014, 12, 10, 18, 34, 35, 0, time.UTC), |
| 160 | RequiredBy: []interface{}{}, |
| 161 | Status: "CREATE_COMPLETE", |
| 162 | PhysicalID: "00e3a2fe-c65d-403c-9483-4db9930dd194", |
| 163 | Type: "OS::Nova::Server", |
| 164 | } |
| 165 | |
| 166 | const GetOutput = ` |
| 167 | { |
| 168 | "resource": { |
| 169 | "resource_name": "wordpress_instance", |
| 170 | "description": "", |
| 171 | "links": [ |
| 172 | { |
| 173 | "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance", |
| 174 | "rel": "self" |
| 175 | }, |
| 176 | { |
| 177 | "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e", |
| 178 | "rel": "stack" |
| 179 | } |
| 180 | ], |
| 181 | "logical_resource_id": "wordpress_instance", |
| 182 | "resource_status": "CREATE_COMPLETE", |
| 183 | "updated_time": "2014-12-10T18:34:35Z", |
| 184 | "required_by": [], |
| 185 | "resource_status_reason": "state changed", |
| 186 | "physical_resource_id": "00e3a2fe-c65d-403c-9483-4db9930dd194", |
| 187 | "resource_type": "OS::Nova::Server" |
| 188 | } |
| 189 | }` |
| 190 | |
| 191 | // HandleGetSuccessfully creates an HTTP handler at `/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance` |
| 192 | // on the test handler mux that responds with a `Get` response. |
| 193 | func HandleGetSuccessfully(t *testing.T, output string) { |
| 194 | th.Mux.HandleFunc("/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance", func(w http.ResponseWriter, r *http.Request) { |
| 195 | th.TestMethod(t, r, "GET") |
| 196 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 197 | th.TestHeader(t, r, "Accept", "application/json") |
| 198 | |
| 199 | w.Header().Set("Content-Type", "application/json") |
| 200 | w.WriteHeader(http.StatusOK) |
| 201 | fmt.Fprintf(w, output) |
| 202 | }) |
| 203 | } |
| 204 | |
| 205 | var MetadataExpected = map[string]string{ |
| 206 | "number": "7", |
| 207 | "animal": "auk", |
| 208 | } |
| 209 | |
| 210 | const MetadataOutput = ` |
| 211 | { |
| 212 | "metadata": { |
| 213 | "number": "7", |
| 214 | "animal": "auk" |
| 215 | } |
| 216 | }` |
| 217 | |
| 218 | // HandleMetadataSuccessfully creates an HTTP handler at `/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance/metadata` |
| 219 | // on the test handler mux that responds with a `Metadata` response. |
| 220 | func HandleMetadataSuccessfully(t *testing.T, output string) { |
| 221 | th.Mux.HandleFunc("/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance/metadata", func(w http.ResponseWriter, r *http.Request) { |
| 222 | th.TestMethod(t, r, "GET") |
| 223 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 224 | th.TestHeader(t, r, "Accept", "application/json") |
| 225 | |
| 226 | w.Header().Set("Content-Type", "application/json") |
| 227 | w.WriteHeader(http.StatusOK) |
| 228 | fmt.Fprintf(w, output) |
| 229 | }) |
| 230 | } |
| 231 | |
| 232 | var ListTypesExpected = []string{ |
| 233 | "OS::Nova::Server", |
| 234 | "OS::Heat::RandomString", |
| 235 | "OS::Swift::Container", |
| 236 | "OS::Trove::Instance", |
| 237 | "OS::Nova::FloatingIPAssociation", |
| 238 | "OS::Cinder::VolumeAttachment", |
| 239 | "OS::Nova::FloatingIP", |
| 240 | "OS::Nova::KeyPair", |
| 241 | } |
| 242 | |
| 243 | const ListTypesOutput = ` |
| 244 | { |
| 245 | "resource_types": [ |
| 246 | "OS::Nova::Server", |
| 247 | "OS::Heat::RandomString", |
| 248 | "OS::Swift::Container", |
| 249 | "OS::Trove::Instance", |
| 250 | "OS::Nova::FloatingIPAssociation", |
| 251 | "OS::Cinder::VolumeAttachment", |
| 252 | "OS::Nova::FloatingIP", |
| 253 | "OS::Nova::KeyPair" |
| 254 | ] |
| 255 | }` |
| 256 | |
| 257 | // HandleListTypesSuccessfully creates an HTTP handler at `/resource_types` |
| 258 | // on the test handler mux that responds with a `ListTypes` response. |
| 259 | func HandleListTypesSuccessfully(t *testing.T, output string) { |
| 260 | th.Mux.HandleFunc("/resource_types", func(w http.ResponseWriter, r *http.Request) { |
| 261 | th.TestMethod(t, r, "GET") |
| 262 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 263 | th.TestHeader(t, r, "Accept", "application/json") |
| 264 | |
| 265 | w.Header().Set("Content-Type", "application/json") |
| 266 | w.WriteHeader(http.StatusOK) |
| 267 | fmt.Fprintf(w, output) |
| 268 | }) |
| 269 | } |