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