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 | |
| 12 | func TestCreateSuccessful(t *testing.T) { |
| 13 | testhelper.SetupHTTP() |
| 14 | defer testhelper.TeardownHTTP() |
| 15 | |
| 16 | testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { |
| 17 | testhelper.TestMethod(t, r, "POST") |
| 18 | testhelper.TestHeader(t, r, "X-Auth-Token", "1111") |
| 19 | testhelper.TestJSONRequest(t, r, `{ "type": "compute" }`) |
| 20 | |
| 21 | w.Header().Add("Content-Type", "application/json") |
| 22 | w.WriteHeader(http.StatusCreated) |
| 23 | fmt.Fprintf(w, `{ |
| 24 | "service": { |
| 25 | "description": "Here's your service", |
| 26 | "id": "1234", |
| 27 | "name": "InscrutableOpenStackProjectName", |
| 28 | "type": "compute" |
| 29 | } |
| 30 | }`) |
| 31 | }) |
| 32 | |
| 33 | client := gophercloud.ServiceClient{ |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 34 | Provider: &gophercloud.ProviderClient{ |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 35 | TokenID: "1111", |
| 36 | }, |
| 37 | Endpoint: testhelper.Endpoint(), |
| 38 | } |
| 39 | |
| 40 | result, err := Create(&client, "compute") |
| 41 | if err != nil { |
| 42 | t.Fatalf("Unexpected error from Create: %v", err) |
| 43 | } |
| 44 | |
| 45 | if result.Description == nil || *result.Description != "Here's your service" { |
| 46 | t.Errorf("Service description was unexpected [%s]", result.Description) |
| 47 | } |
| 48 | if result.ID != "1234" { |
| 49 | t.Errorf("Service ID was unexpected [%s]", result.ID) |
| 50 | } |
| 51 | if result.Name != "InscrutableOpenStackProjectName" { |
| 52 | t.Errorf("Service name was unexpected [%s]", result.Name) |
| 53 | } |
| 54 | if result.Type != "compute" { |
| 55 | t.Errorf("Service type was unexpected [%s]", result.Type) |
| 56 | } |
| 57 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 58 | |
| 59 | func TestListSinglePage(t *testing.T) { |
| 60 | testhelper.SetupHTTP() |
| 61 | defer testhelper.TeardownHTTP() |
| 62 | |
| 63 | testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) { |
| 64 | testhelper.TestMethod(t, r, "GET") |
| 65 | testhelper.TestHeader(t, r, "X-Auth-Token", "1111") |
| 66 | |
| 67 | w.Header().Add("Content-Type", "application/json") |
| 68 | fmt.Fprintf(w, ` |
| 69 | { |
| 70 | "links": { |
| 71 | "next": null, |
| 72 | "previous": null |
| 73 | }, |
| 74 | "services": [ |
| 75 | { |
| 76 | "description": "Service One", |
| 77 | "id": "1234", |
| 78 | "name": "service-one", |
| 79 | "type": "identity" |
| 80 | }, |
| 81 | { |
| 82 | "description": "Service Two", |
| 83 | "id": "9876", |
| 84 | "name": "service-two", |
| 85 | "type": "compute" |
| 86 | } |
| 87 | ] |
| 88 | } |
| 89 | `) |
| 90 | }) |
| 91 | |
| 92 | client := gophercloud.ServiceClient{ |
| 93 | Provider: &gophercloud.ProviderClient{ |
| 94 | TokenID: "1111", |
| 95 | }, |
| 96 | Endpoint: testhelper.Endpoint(), |
| 97 | } |
| 98 | |
| 99 | result, err := List(&client, ListOpts{}) |
| 100 | if err != nil { |
| 101 | t.Fatalf("Error listing services: %v", err) |
| 102 | } |
| 103 | |
| 104 | if result.Pagination.Next != nil { |
| 105 | t.Errorf("Unexpected next link: %s", result.Pagination.Next) |
| 106 | } |
| 107 | if result.Pagination.Previous != nil { |
| 108 | t.Errorf("Unexpected previous link: %s", result.Pagination.Previous) |
| 109 | } |
| 110 | if len(result.Services) != 2 { |
| 111 | t.Errorf("Unexpected number of services: %s", len(result.Services)) |
| 112 | } |
| 113 | if result.Services[0].ID != "1234" { |
| 114 | t.Errorf("Unexpected service: %#v", result.Services[0]) |
| 115 | } |
| 116 | if result.Services[0].ID == "9876" { |
| 117 | t.Errorf("Unexpected service: %#v", result.Services[1]) |
| 118 | } |
| 119 | } |