Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame] | 1 | package stacktemplates |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
| 12 | // GetExpected represents the expected object from a Get request. |
| 13 | var GetExpected = &Template{ |
| 14 | Description: "Simple template to test heat commands", |
| 15 | HeatTemplateVersion: "2013-05-23", |
| 16 | Parameters: map[string]interface{}{ |
| 17 | "flavor": map[string]interface{}{ |
| 18 | "default": "m1.tiny", |
| 19 | "type": "string", |
| 20 | }, |
| 21 | }, |
| 22 | Resources: map[string]interface{}{ |
| 23 | "hello_world": map[string]interface{}{ |
| 24 | "type": "OS::Nova::Server", |
| 25 | "properties": map[string]interface{}{ |
| 26 | "key_name": "heat_key", |
| 27 | "flavor": map[string]interface{}{ |
| 28 | "get_param": "flavor", |
| 29 | }, |
| 30 | "image": "ad091b52-742f-469e-8f3c-fd81cadf0743", |
| 31 | "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n", |
| 32 | }, |
| 33 | }, |
| 34 | }, |
| 35 | } |
| 36 | |
| 37 | // GetOutput represents the response body from a Get request. |
| 38 | const GetOutput = ` |
| 39 | { |
| 40 | "heat_template_version": "2013-05-23", |
| 41 | "description": "Simple template to test heat commands", |
| 42 | "parameters": { |
| 43 | "flavor": { |
| 44 | "default": "m1.tiny", |
| 45 | "type": "string" |
| 46 | } |
| 47 | }, |
| 48 | "resources": { |
| 49 | "hello_world": { |
| 50 | "type": "OS::Nova::Server", |
| 51 | "properties": { |
| 52 | "key_name": "heat_key", |
| 53 | "flavor": { |
| 54 | "get_param": "flavor" |
| 55 | }, |
| 56 | "image": "ad091b52-742f-469e-8f3c-fd81cadf0743", |
| 57 | "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n" |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | }` |
| 62 | |
| 63 | // HandleGetSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87/template` |
| 64 | // on the test handler mux that responds with a `Get` response. |
| 65 | func HandleGetSuccessfully(t *testing.T, output string) { |
| 66 | th.Mux.HandleFunc("/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87/template", 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 | |
Jon Perritt | 8ade432 | 2015-02-09 11:34:59 -0700 | [diff] [blame] | 77 | // ValidateExpected represents the expected object from a Validate request. |
Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame] | 78 | var ValidateExpected = &ValidatedTemplate{ |
| 79 | Description: "Simple template to test heat commands", |
| 80 | Parameters: map[string]interface{}{ |
| 81 | "flavor": map[string]interface{}{ |
| 82 | "Default": "m1.tiny", |
| 83 | "Type": "String", |
| 84 | "NoEcho": "false", |
| 85 | "Description": "", |
| 86 | "Label": "flavor", |
| 87 | }, |
| 88 | }, |
| 89 | } |
| 90 | |
Jon Perritt | 8ade432 | 2015-02-09 11:34:59 -0700 | [diff] [blame] | 91 | // ValidateOutput represents the response body from a Validate request. |
Jon Perritt | f799b94 | 2015-02-09 11:23:28 -0700 | [diff] [blame] | 92 | const ValidateOutput = ` |
| 93 | { |
| 94 | "Description": "Simple template to test heat commands", |
| 95 | "Parameters": { |
| 96 | "flavor": { |
| 97 | "Default": "m1.tiny", |
| 98 | "Type": "String", |
| 99 | "NoEcho": "false", |
| 100 | "Description": "", |
| 101 | "Label": "flavor" |
| 102 | } |
| 103 | } |
| 104 | }` |
| 105 | |
| 106 | // HandleValidateSuccessfully creates an HTTP handler at `/validate` |
| 107 | // on the test handler mux that responds with a `Validate` response. |
| 108 | func HandleValidateSuccessfully(t *testing.T, output string) { |
| 109 | th.Mux.HandleFunc("/validate", func(w http.ResponseWriter, r *http.Request) { |
| 110 | th.TestMethod(t, r, "POST") |
| 111 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 112 | th.TestHeader(t, r, "Accept", "application/json") |
| 113 | |
| 114 | w.Header().Set("Content-Type", "application/json") |
| 115 | w.WriteHeader(http.StatusOK) |
| 116 | fmt.Fprintf(w, output) |
| 117 | }) |
| 118 | } |