blob: a3d345b08910827140e8403c724aa4cf578187fd [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
4 "fmt"
5 "net/http"
Ash Wilson64f44152014-09-05 13:45:03 -04006 "reflect"
Ash Wilsonb73b7f82014-08-29 15:38:06 -04007 "testing"
8
9 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -040010 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonb73b7f82014-08-29 15:38:06 -040011 "github.com/rackspace/gophercloud/testhelper"
12)
13
Ash Wilsond1b72132014-09-03 15:26:26 -040014const tokenID = "111111"
15
16func serviceClient() *gophercloud.ServiceClient {
17 return &gophercloud.ServiceClient{
18 Provider: &gophercloud.ProviderClient{
19 TokenID: tokenID,
20 },
21 Endpoint: testhelper.Endpoint(),
22 }
23}
24
Ash Wilsonb73b7f82014-08-29 15:38:06 -040025func 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 Wilsond1b72132014-09-03 15:26:26 -040031 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040032 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 Wilsond1b72132014-09-03 15:26:26 -040046 client := serviceClient()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040047
Ash Wilson3f59ade2014-10-02 09:22:23 -040048 result, err := Create(client, "compute").Extract()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040049 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 Wilson2f5dd1f2014-09-03 14:01:37 -040066
67func 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 Wilsond1b72132014-09-03 15:26:26 -040073 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040074
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 Wilsond1b72132014-09-03 15:26:26 -0400100 client := serviceClient()
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400101
Ash Wilsonbddac132014-09-12 14:20:16 -0400102 count := 0
Ash Wilson3c8cc772014-09-16 11:40:49 -0400103 err := List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonbddac132014-09-12 14:20:16 -0400104 count++
105 actual, err := ExtractServices(page)
106 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400107 return false, err
Ash Wilsonbddac132014-09-12 14:20:16 -0400108 }
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400109
Ash Wilsonbddac132014-09-12 14:20:16 -0400110 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 Wilson64f44152014-09-05 13:45:03 -0400126
Ash Wilsonbddac132014-09-12 14:20:16 -0400127 if !reflect.DeepEqual(expected, actual) {
128 t.Errorf("Expected %#v, got %#v", expected, actual)
129 }
Ash Wilson64f44152014-09-05 13:45:03 -0400130
Ash Wilson6b35e502014-09-12 15:15:23 -0400131 return true, nil
Ash Wilsonbddac132014-09-12 14:20:16 -0400132 })
Ash Wilson6b35e502014-09-12 15:15:23 -0400133 if err != nil {
134 t.Errorf("Unexpected error while paging: %v", err)
135 }
Ash Wilsonbddac132014-09-12 14:20:16 -0400136 if count != 1 {
137 t.Errorf("Expected 1 page, got %d", count)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400138 }
139}
Ash Wilsonb1129972014-09-03 14:45:21 -0400140
Ash Wilson5266e492014-09-09 15:44:30 -0400141func TestGetSuccessful(t *testing.T) {
Ash Wilsonb1129972014-09-03 14:45:21 -0400142 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 Wilsond1b72132014-09-03 15:26:26 -0400147 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilsonb1129972014-09-03 14:45:21 -0400148
149 w.Header().Add("Content-Type", "application/json")
150 fmt.Fprintf(w, `
151 {
152 "service": {
153 "description": "Service One",
Ash Wilsond1b72132014-09-03 15:26:26 -0400154 "id": "12345",
Ash Wilsonb1129972014-09-03 14:45:21 -0400155 "name": "service-one",
156 "type": "identity"
157 }
158 }
159 `)
160 })
161
Ash Wilsond1b72132014-09-03 15:26:26 -0400162 client := serviceClient()
Ash Wilsonb1129972014-09-03 14:45:21 -0400163
Ash Wilson3f59ade2014-10-02 09:22:23 -0400164 result, err := Get(client, "12345").Extract()
Ash Wilsonb1129972014-09-03 14:45:21 -0400165 if err != nil {
166 t.Fatalf("Error fetching service information: %v", err)
167 }
168
Ash Wilsond1b72132014-09-03 15:26:26 -0400169 if result.ID != "12345" {
Ash Wilsonb1129972014-09-03 14:45:21 -0400170 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 Wilsond1b72132014-09-03 15:26:26 -0400182
183func 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 Wilson3f59ade2014-10-02 09:22:23 -0400205 result, err := Update(client, "12345", "lasermagic").Extract()
Ash Wilsond1b72132014-09-03 15:26:26 -0400206 if err != nil {
Ash Wilsond24786d2014-09-03 15:38:00 -0400207 t.Fatalf("Unable to update service: %v", err)
Ash Wilsond1b72132014-09-03 15:26:26 -0400208 }
209
210 if result.ID != "12345" {
Ash Wilson3f59ade2014-10-02 09:22:23 -0400211 t.Fatalf("Expected ID 12345, was %s", result.ID)
Ash Wilsond1b72132014-09-03 15:26:26 -0400212 }
213}
Ash Wilsond24786d2014-09-03 15:38:00 -0400214
215func 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}