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