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