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 | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 101 | result, err := List(client, ListOpts{}) |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 102 | if err != nil { |
| 103 | t.Fatalf("Error listing services: %v", err) |
| 104 | } |
| 105 | |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 106 | collection, err := gophercloud.AllPages(result) |
| 107 | actual := AsServices(collection) |
| 108 | |
| 109 | desc0 := "Service One" |
| 110 | desc1 := "Service Two" |
| 111 | expected := []Service{ |
| 112 | Service{ |
| 113 | Description: &desc0, |
| 114 | ID: "1234", |
| 115 | Name: "service-one", |
| 116 | Type: "identity", |
| 117 | }, |
| 118 | Service{ |
| 119 | Description: &desc1, |
| 120 | ID: "9876", |
| 121 | Name: "service-two", |
| 122 | Type: "compute", |
| 123 | }, |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 124 | } |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 125 | |
| 126 | if !reflect.DeepEqual(expected, actual) { |
| 127 | t.Errorf("Expected %#v, got %#v", expected, actual) |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 130 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 131 | func TestGetSuccessful(t *testing.T) { |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 132 | testhelper.SetupHTTP() |
| 133 | defer testhelper.TeardownHTTP() |
| 134 | |
| 135 | testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 136 | testhelper.TestMethod(t, r, "GET") |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 137 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 138 | |
| 139 | w.Header().Add("Content-Type", "application/json") |
| 140 | fmt.Fprintf(w, ` |
| 141 | { |
| 142 | "service": { |
| 143 | "description": "Service One", |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 144 | "id": "12345", |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 145 | "name": "service-one", |
| 146 | "type": "identity" |
| 147 | } |
| 148 | } |
| 149 | `) |
| 150 | }) |
| 151 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 152 | client := serviceClient() |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 153 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 154 | result, err := Get(client, "12345") |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 155 | if err != nil { |
| 156 | t.Fatalf("Error fetching service information: %v", err) |
| 157 | } |
| 158 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 159 | if result.ID != "12345" { |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 160 | t.Errorf("Unexpected service ID: %s", result.ID) |
| 161 | } |
| 162 | if *result.Description != "Service One" { |
| 163 | t.Errorf("Unexpected service description: [%s]", *result.Description) |
| 164 | } |
| 165 | if result.Name != "service-one" { |
| 166 | t.Errorf("Unexpected service name: [%s]", result.Name) |
| 167 | } |
| 168 | if result.Type != "identity" { |
| 169 | t.Errorf("Unexpected service type: [%s]", result.Type) |
| 170 | } |
| 171 | } |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 172 | |
| 173 | func TestUpdateSuccessful(t *testing.T) { |
| 174 | testhelper.SetupHTTP() |
| 175 | defer testhelper.TeardownHTTP() |
| 176 | |
| 177 | testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 178 | testhelper.TestMethod(t, r, "PATCH") |
| 179 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 180 | testhelper.TestJSONRequest(t, r, `{ "type": "lasermagic" }`) |
| 181 | |
| 182 | w.Header().Add("Content-Type", "application/json") |
| 183 | fmt.Fprintf(w, ` |
| 184 | { |
| 185 | "service": { |
| 186 | "id": "12345", |
| 187 | "type": "lasermagic" |
| 188 | } |
| 189 | } |
| 190 | `) |
| 191 | }) |
| 192 | |
| 193 | client := serviceClient() |
| 194 | |
| 195 | result, err := Update(client, "12345", "lasermagic") |
| 196 | if err != nil { |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 197 | t.Fatalf("Unable to update service: %v", err) |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | if result.ID != "12345" { |
| 201 | |
| 202 | } |
| 203 | } |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 204 | |
| 205 | func TestDeleteSuccessful(t *testing.T) { |
| 206 | testhelper.SetupHTTP() |
| 207 | defer testhelper.TeardownHTTP() |
| 208 | |
| 209 | testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 210 | testhelper.TestMethod(t, r, "DELETE") |
| 211 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 212 | |
| 213 | w.WriteHeader(http.StatusNoContent) |
| 214 | }) |
| 215 | |
| 216 | client := serviceClient() |
| 217 | |
| 218 | err := Delete(client, "12345") |
| 219 | if err != nil { |
| 220 | t.Fatalf("Unable to delete service: %v", err) |
| 221 | } |
| 222 | } |