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 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame^] | 116 | if result.Services[1].ID != "9876" { |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 117 | t.Errorf("Unexpected service: %#v", result.Services[1]) |
| 118 | } |
| 119 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame^] | 120 | |
| 121 | func TestInfoSuccessful(t *testing.T) { |
| 122 | testhelper.SetupHTTP() |
| 123 | defer testhelper.TeardownHTTP() |
| 124 | |
| 125 | testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { |
| 126 | testhelper.TestMethod(t, r, "GET") |
| 127 | testhelper.TestHeader(t, r, "X-Auth-Token", "1111") |
| 128 | |
| 129 | w.Header().Add("Content-Type", "application/json") |
| 130 | fmt.Fprintf(w, ` |
| 131 | { |
| 132 | "service": { |
| 133 | "description": "Service One", |
| 134 | "id": "1234", |
| 135 | "name": "service-one", |
| 136 | "type": "identity" |
| 137 | } |
| 138 | } |
| 139 | `) |
| 140 | }) |
| 141 | |
| 142 | client := gophercloud.ServiceClient{ |
| 143 | Provider: &gophercloud.ProviderClient{ |
| 144 | TokenID: "1111", |
| 145 | }, |
| 146 | Endpoint: testhelper.Endpoint(), |
| 147 | } |
| 148 | |
| 149 | result, err := Info(&client, "12345") |
| 150 | if err != nil { |
| 151 | t.Fatalf("Error fetching service information: %v", err) |
| 152 | } |
| 153 | |
| 154 | if result.ID != "1234" { |
| 155 | t.Errorf("Unexpected service ID: %s", result.ID) |
| 156 | } |
| 157 | if *result.Description != "Service One" { |
| 158 | t.Errorf("Unexpected service description: [%s]", *result.Description) |
| 159 | } |
| 160 | if result.Name != "service-one" { |
| 161 | t.Errorf("Unexpected service name: [%s]", result.Name) |
| 162 | } |
| 163 | if result.Type != "identity" { |
| 164 | t.Errorf("Unexpected service type: [%s]", result.Type) |
| 165 | } |
| 166 | } |