blob: 59cefe3a105f0eb897a689ff499b71543e10428e [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"
10 "github.com/rackspace/gophercloud/testhelper"
11)
12
Ash Wilsond1b72132014-09-03 15:26:26 -040013const tokenID = "111111"
14
15func serviceClient() *gophercloud.ServiceClient {
16 return &gophercloud.ServiceClient{
17 Provider: &gophercloud.ProviderClient{
18 TokenID: tokenID,
19 },
20 Endpoint: testhelper.Endpoint(),
21 }
22}
23
Ash Wilsonb73b7f82014-08-29 15:38:06 -040024func 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 Wilsond1b72132014-09-03 15:26:26 -040030 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040031 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 Wilsond1b72132014-09-03 15:26:26 -040045 client := serviceClient()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040046
Ash Wilsond1b72132014-09-03 15:26:26 -040047 result, err := Create(client, "compute")
Ash Wilsonb73b7f82014-08-29 15:38:06 -040048 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 Wilson2f5dd1f2014-09-03 14:01:37 -040065
66func 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 Wilsond1b72132014-09-03 15:26:26 -040072 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040073
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 Wilsond1b72132014-09-03 15:26:26 -040099 client := serviceClient()
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400100
Ash Wilsonbddac132014-09-12 14:20:16 -0400101 count := 0
Ash Wilson6b35e502014-09-12 15:15:23 -0400102 err := List(client, ListOpts{}).EachPage(func(page gophercloud.Page) (bool, error) {
Ash Wilsonbddac132014-09-12 14:20:16 -0400103 count++
104 actual, err := ExtractServices(page)
105 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -0400106 return false, err
Ash Wilsonbddac132014-09-12 14:20:16 -0400107 }
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400108
Ash Wilsonbddac132014-09-12 14:20:16 -0400109 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 },
124 }
Ash Wilson64f44152014-09-05 13:45:03 -0400125
Ash Wilsonbddac132014-09-12 14:20:16 -0400126 if !reflect.DeepEqual(expected, actual) {
127 t.Errorf("Expected %#v, got %#v", expected, actual)
128 }
Ash Wilson64f44152014-09-05 13:45:03 -0400129
Ash Wilson6b35e502014-09-12 15:15:23 -0400130 return true, nil
Ash Wilsonbddac132014-09-12 14:20:16 -0400131 })
Ash Wilson6b35e502014-09-12 15:15:23 -0400132 if err != nil {
133 t.Errorf("Unexpected error while paging: %v", err)
134 }
Ash Wilsonbddac132014-09-12 14:20:16 -0400135 if count != 1 {
136 t.Errorf("Expected 1 page, got %d", count)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400137 }
138}
Ash Wilsonb1129972014-09-03 14:45:21 -0400139
Ash Wilson5266e492014-09-09 15:44:30 -0400140func TestGetSuccessful(t *testing.T) {
Ash Wilsonb1129972014-09-03 14:45:21 -0400141 testhelper.SetupHTTP()
142 defer testhelper.TeardownHTTP()
143
144 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
145 testhelper.TestMethod(t, r, "GET")
Ash Wilsond1b72132014-09-03 15:26:26 -0400146 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilsonb1129972014-09-03 14:45:21 -0400147
148 w.Header().Add("Content-Type", "application/json")
149 fmt.Fprintf(w, `
150 {
151 "service": {
152 "description": "Service One",
Ash Wilsond1b72132014-09-03 15:26:26 -0400153 "id": "12345",
Ash Wilsonb1129972014-09-03 14:45:21 -0400154 "name": "service-one",
155 "type": "identity"
156 }
157 }
158 `)
159 })
160
Ash Wilsond1b72132014-09-03 15:26:26 -0400161 client := serviceClient()
Ash Wilsonb1129972014-09-03 14:45:21 -0400162
Ash Wilson5266e492014-09-09 15:44:30 -0400163 result, err := Get(client, "12345")
Ash Wilsonb1129972014-09-03 14:45:21 -0400164 if err != nil {
165 t.Fatalf("Error fetching service information: %v", err)
166 }
167
Ash Wilsond1b72132014-09-03 15:26:26 -0400168 if result.ID != "12345" {
Ash Wilsonb1129972014-09-03 14:45:21 -0400169 t.Errorf("Unexpected service ID: %s", result.ID)
170 }
171 if *result.Description != "Service One" {
172 t.Errorf("Unexpected service description: [%s]", *result.Description)
173 }
174 if result.Name != "service-one" {
175 t.Errorf("Unexpected service name: [%s]", result.Name)
176 }
177 if result.Type != "identity" {
178 t.Errorf("Unexpected service type: [%s]", result.Type)
179 }
180}
Ash Wilsond1b72132014-09-03 15:26:26 -0400181
182func TestUpdateSuccessful(t *testing.T) {
183 testhelper.SetupHTTP()
184 defer testhelper.TeardownHTTP()
185
186 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
187 testhelper.TestMethod(t, r, "PATCH")
188 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
189 testhelper.TestJSONRequest(t, r, `{ "type": "lasermagic" }`)
190
191 w.Header().Add("Content-Type", "application/json")
192 fmt.Fprintf(w, `
193 {
194 "service": {
195 "id": "12345",
196 "type": "lasermagic"
197 }
198 }
199 `)
200 })
201
202 client := serviceClient()
203
204 result, err := Update(client, "12345", "lasermagic")
205 if err != nil {
Ash Wilsond24786d2014-09-03 15:38:00 -0400206 t.Fatalf("Unable to update service: %v", err)
Ash Wilsond1b72132014-09-03 15:26:26 -0400207 }
208
209 if result.ID != "12345" {
210
211 }
212}
Ash Wilsond24786d2014-09-03 15:38:00 -0400213
214func TestDeleteSuccessful(t *testing.T) {
215 testhelper.SetupHTTP()
216 defer testhelper.TeardownHTTP()
217
218 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
219 testhelper.TestMethod(t, r, "DELETE")
220 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
221
222 w.WriteHeader(http.StatusNoContent)
223 })
224
225 client := serviceClient()
226
227 err := Delete(client, "12345")
228 if err != nil {
229 t.Fatalf("Unable to delete service: %v", err)
230 }
231}