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