Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/testhelper" |
| 10 | ) |
| 11 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 12 | const tokenID = "111111" |
| 13 | |
| 14 | func serviceClient() *gophercloud.ServiceClient { |
| 15 | return &gophercloud.ServiceClient{ |
| 16 | Provider: &gophercloud.ProviderClient{ |
| 17 | TokenID: tokenID, |
| 18 | }, |
| 19 | Endpoint: testhelper.Endpoint(), |
| 20 | } |
| 21 | } |
| 22 | |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 23 | func TestCreateSuccessful(t *testing.T) { |
| 24 | testhelper.SetupHTTP() |
| 25 | defer testhelper.TeardownHTTP() |
| 26 | |
| 27 | testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { |
| 28 | testhelper.TestMethod(t, r, "POST") |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 29 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 30 | testhelper.TestJSONRequest(t, r, `{ "type": "compute" }`) |
| 31 | |
| 32 | w.Header().Add("Content-Type", "application/json") |
| 33 | w.WriteHeader(http.StatusCreated) |
| 34 | fmt.Fprintf(w, `{ |
| 35 | "service": { |
| 36 | "description": "Here's your service", |
| 37 | "id": "1234", |
| 38 | "name": "InscrutableOpenStackProjectName", |
| 39 | "type": "compute" |
| 40 | } |
| 41 | }`) |
| 42 | }) |
| 43 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 44 | client := serviceClient() |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 45 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 46 | result, err := Create(client, "compute") |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 47 | if err != nil { |
| 48 | t.Fatalf("Unexpected error from Create: %v", err) |
| 49 | } |
| 50 | |
| 51 | if result.Description == nil || *result.Description != "Here's your service" { |
| 52 | t.Errorf("Service description was unexpected [%s]", result.Description) |
| 53 | } |
| 54 | if result.ID != "1234" { |
| 55 | t.Errorf("Service ID was unexpected [%s]", result.ID) |
| 56 | } |
| 57 | if result.Name != "InscrutableOpenStackProjectName" { |
| 58 | t.Errorf("Service name was unexpected [%s]", result.Name) |
| 59 | } |
| 60 | if result.Type != "compute" { |
| 61 | t.Errorf("Service type was unexpected [%s]", result.Type) |
| 62 | } |
| 63 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 64 | |
| 65 | func TestListSinglePage(t *testing.T) { |
| 66 | testhelper.SetupHTTP() |
| 67 | defer testhelper.TeardownHTTP() |
| 68 | |
| 69 | testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { |
| 70 | testhelper.TestMethod(t, r, "GET") |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 71 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 72 | |
| 73 | w.Header().Add("Content-Type", "application/json") |
| 74 | fmt.Fprintf(w, ` |
| 75 | { |
| 76 | "links": { |
| 77 | "next": null, |
| 78 | "previous": null |
| 79 | }, |
| 80 | "services": [ |
| 81 | { |
| 82 | "description": "Service One", |
| 83 | "id": "1234", |
| 84 | "name": "service-one", |
| 85 | "type": "identity" |
| 86 | }, |
| 87 | { |
| 88 | "description": "Service Two", |
| 89 | "id": "9876", |
| 90 | "name": "service-two", |
| 91 | "type": "compute" |
| 92 | } |
| 93 | ] |
| 94 | } |
| 95 | `) |
| 96 | }) |
| 97 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 98 | client := serviceClient() |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 99 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 100 | result, err := List(client, ListOpts{}) |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 101 | if err != nil { |
| 102 | t.Fatalf("Error listing services: %v", err) |
| 103 | } |
| 104 | |
| 105 | if result.Pagination.Next != nil { |
| 106 | t.Errorf("Unexpected next link: %s", result.Pagination.Next) |
| 107 | } |
| 108 | if result.Pagination.Previous != nil { |
| 109 | t.Errorf("Unexpected previous link: %s", result.Pagination.Previous) |
| 110 | } |
| 111 | if len(result.Services) != 2 { |
| 112 | t.Errorf("Unexpected number of services: %s", len(result.Services)) |
| 113 | } |
| 114 | if result.Services[0].ID != "1234" { |
| 115 | t.Errorf("Unexpected service: %#v", result.Services[0]) |
| 116 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 117 | if result.Services[1].ID != "9876" { |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 118 | t.Errorf("Unexpected service: %#v", result.Services[1]) |
| 119 | } |
| 120 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 121 | |
| 122 | func TestInfoSuccessful(t *testing.T) { |
| 123 | testhelper.SetupHTTP() |
| 124 | defer testhelper.TeardownHTTP() |
| 125 | |
| 126 | testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 127 | testhelper.TestMethod(t, r, "GET") |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 128 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 129 | |
| 130 | w.Header().Add("Content-Type", "application/json") |
| 131 | fmt.Fprintf(w, ` |
| 132 | { |
| 133 | "service": { |
| 134 | "description": "Service One", |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 135 | "id": "12345", |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 136 | "name": "service-one", |
| 137 | "type": "identity" |
| 138 | } |
| 139 | } |
| 140 | `) |
| 141 | }) |
| 142 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 143 | client := serviceClient() |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 144 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 145 | result, err := Info(client, "12345") |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 146 | if err != nil { |
| 147 | t.Fatalf("Error fetching service information: %v", err) |
| 148 | } |
| 149 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 150 | if result.ID != "12345" { |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 151 | t.Errorf("Unexpected service ID: %s", result.ID) |
| 152 | } |
| 153 | if *result.Description != "Service One" { |
| 154 | t.Errorf("Unexpected service description: [%s]", *result.Description) |
| 155 | } |
| 156 | if result.Name != "service-one" { |
| 157 | t.Errorf("Unexpected service name: [%s]", result.Name) |
| 158 | } |
| 159 | if result.Type != "identity" { |
| 160 | t.Errorf("Unexpected service type: [%s]", result.Type) |
| 161 | } |
| 162 | } |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 163 | |
| 164 | func TestUpdateSuccessful(t *testing.T) { |
| 165 | testhelper.SetupHTTP() |
| 166 | defer testhelper.TeardownHTTP() |
| 167 | |
| 168 | testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 169 | testhelper.TestMethod(t, r, "PATCH") |
| 170 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 171 | testhelper.TestJSONRequest(t, r, `{ "type": "lasermagic" }`) |
| 172 | |
| 173 | w.Header().Add("Content-Type", "application/json") |
| 174 | fmt.Fprintf(w, ` |
| 175 | { |
| 176 | "service": { |
| 177 | "id": "12345", |
| 178 | "type": "lasermagic" |
| 179 | } |
| 180 | } |
| 181 | `) |
| 182 | }) |
| 183 | |
| 184 | client := serviceClient() |
| 185 | |
| 186 | result, err := Update(client, "12345", "lasermagic") |
| 187 | if err != nil { |
| 188 | t.Fatalf("Unable to update service") |
| 189 | } |
| 190 | |
| 191 | if result.ID != "12345" { |
| 192 | |
| 193 | } |
| 194 | } |