Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 9 | th "github.com/gophercloud/gophercloud/testhelper" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/testhelper/client" |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | func TestCreateSuccessful(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 16 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 17 | th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { |
| 18 | th.TestMethod(t, r, "POST") |
| 19 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 20 | th.TestJSONRequest(t, r, `{ "type": "compute" }`) |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 21 | |
| 22 | w.Header().Add("Content-Type", "application/json") |
| 23 | w.WriteHeader(http.StatusCreated) |
| 24 | fmt.Fprintf(w, `{ |
| 25 | "service": { |
| 26 | "description": "Here's your service", |
| 27 | "id": "1234", |
| 28 | "name": "InscrutableOpenStackProjectName", |
| 29 | "type": "compute" |
| 30 | } |
| 31 | }`) |
| 32 | }) |
| 33 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 34 | expected := &Service{ |
| 35 | Description: "Here's your service", |
| 36 | ID: "1234", |
| 37 | Name: "InscrutableOpenStackProjectName", |
| 38 | Type: "compute", |
| 39 | } |
| 40 | |
| 41 | actual, err := Create(client.ServiceClient(), "compute").Extract() |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 42 | if err != nil { |
| 43 | t.Fatalf("Unexpected error from Create: %v", err) |
| 44 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 45 | th.AssertDeepEquals(t, expected, actual) |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 46 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 47 | |
| 48 | func TestListSinglePage(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 49 | th.SetupHTTP() |
| 50 | defer th.TeardownHTTP() |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 51 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 52 | th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { |
| 53 | th.TestMethod(t, r, "GET") |
| 54 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 55 | |
| 56 | w.Header().Add("Content-Type", "application/json") |
| 57 | fmt.Fprintf(w, ` |
| 58 | { |
| 59 | "links": { |
| 60 | "next": null, |
| 61 | "previous": null |
| 62 | }, |
| 63 | "services": [ |
| 64 | { |
| 65 | "description": "Service One", |
| 66 | "id": "1234", |
| 67 | "name": "service-one", |
| 68 | "type": "identity" |
| 69 | }, |
| 70 | { |
| 71 | "description": "Service Two", |
| 72 | "id": "9876", |
| 73 | "name": "service-two", |
| 74 | "type": "compute" |
| 75 | } |
| 76 | ] |
| 77 | } |
| 78 | `) |
| 79 | }) |
| 80 | |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 81 | count := 0 |
Ash Wilson | ea8c7f3 | 2014-10-22 09:23:04 -0400 | [diff] [blame] | 82 | err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 83 | count++ |
| 84 | actual, err := ExtractServices(page) |
| 85 | if err != nil { |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 86 | return false, err |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 87 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 88 | |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 89 | expected := []Service{ |
| 90 | Service{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 91 | Description: "Service One", |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 92 | ID: "1234", |
| 93 | Name: "service-one", |
| 94 | Type: "identity", |
| 95 | }, |
| 96 | Service{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 97 | Description: "Service Two", |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 98 | ID: "9876", |
| 99 | Name: "service-two", |
| 100 | Type: "compute", |
| 101 | }, |
| 102 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 103 | th.AssertDeepEquals(t, expected, actual) |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 104 | return true, nil |
Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 105 | }) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 106 | th.AssertNoErr(t, err) |
| 107 | th.AssertEquals(t, 1, count) |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 108 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 109 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 110 | func TestGetSuccessful(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 111 | th.SetupHTTP() |
| 112 | defer th.TeardownHTTP() |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 113 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 114 | th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 115 | th.TestMethod(t, r, "GET") |
| 116 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 117 | |
| 118 | w.Header().Add("Content-Type", "application/json") |
| 119 | fmt.Fprintf(w, ` |
| 120 | { |
| 121 | "service": { |
| 122 | "description": "Service One", |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 123 | "id": "12345", |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 124 | "name": "service-one", |
| 125 | "type": "identity" |
| 126 | } |
| 127 | } |
| 128 | `) |
| 129 | }) |
| 130 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 131 | actual, err := Get(client.ServiceClient(), "12345").Extract() |
| 132 | th.AssertNoErr(t, err) |
| 133 | |
| 134 | expected := &Service{ |
| 135 | ID: "12345", |
| 136 | Description: "Service One", |
| 137 | Name: "service-one", |
| 138 | Type: "identity", |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 141 | th.AssertDeepEquals(t, expected, actual) |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 142 | } |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 143 | |
| 144 | func TestUpdateSuccessful(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 145 | th.SetupHTTP() |
| 146 | defer th.TeardownHTTP() |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 147 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 148 | th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 149 | th.TestMethod(t, r, "PATCH") |
| 150 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 151 | th.TestJSONRequest(t, r, `{ "type": "lasermagic" }`) |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 152 | |
| 153 | w.Header().Add("Content-Type", "application/json") |
| 154 | fmt.Fprintf(w, ` |
| 155 | { |
| 156 | "service": { |
| 157 | "id": "12345", |
| 158 | "type": "lasermagic" |
| 159 | } |
| 160 | } |
| 161 | `) |
| 162 | }) |
| 163 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 164 | expected := &Service{ |
| 165 | ID: "12345", |
| 166 | Type: "lasermagic", |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 169 | actual, err := Update(client.ServiceClient(), "12345", "lasermagic").Extract() |
| 170 | th.AssertNoErr(t, err) |
| 171 | th.AssertDeepEquals(t, expected, actual) |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 172 | } |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 173 | |
| 174 | func TestDeleteSuccessful(t *testing.T) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 175 | th.SetupHTTP() |
| 176 | defer th.TeardownHTTP() |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 177 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 178 | th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 179 | th.TestMethod(t, r, "DELETE") |
| 180 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 181 | w.WriteHeader(http.StatusNoContent) |
| 182 | }) |
| 183 | |
Jamie Hannaford | 8ab3c14 | 2014-10-27 11:33:39 +0100 | [diff] [blame] | 184 | res := Delete(client.ServiceClient(), "12345") |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 185 | th.AssertNoErr(t, res.Err) |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 186 | } |