blob: 42f05d365a5b926ef94f5dec42ef1289ca54b625 [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
Ash Wilson3c8cc772014-09-16 11:40:49 -04009 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonb73b7f82014-08-29 15:38:06 -040010 "github.com/rackspace/gophercloud/testhelper"
Ash Wilsonea8c7f32014-10-22 09:23:04 -040011 "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilsonb73b7f82014-08-29 15:38:06 -040012)
13
14func TestCreateSuccessful(t *testing.T) {
15 testhelper.SetupHTTP()
16 defer testhelper.TeardownHTTP()
17
18 testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
19 testhelper.TestMethod(t, r, "POST")
Ash Wilsonea8c7f32014-10-22 09:23:04 -040020 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040021 testhelper.TestJSONRequest(t, r, `{ "type": "compute" }`)
22
23 w.Header().Add("Content-Type", "application/json")
24 w.WriteHeader(http.StatusCreated)
25 fmt.Fprintf(w, `{
26 "service": {
27 "description": "Here's your service",
28 "id": "1234",
29 "name": "InscrutableOpenStackProjectName",
30 "type": "compute"
31 }
32 }`)
33 })
34
Ash Wilsonea8c7f32014-10-22 09:23:04 -040035 result, err := Create(client.ServiceClient(), "compute").Extract()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040036 if err != nil {
37 t.Fatalf("Unexpected error from Create: %v", err)
38 }
39
40 if result.Description == nil || *result.Description != "Here's your service" {
Alex Gaynor2f071742014-11-13 12:03:29 -080041 t.Errorf("Service description was unexpected [%s]", *result.Description)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040042 }
43 if result.ID != "1234" {
44 t.Errorf("Service ID was unexpected [%s]", result.ID)
45 }
46 if result.Name != "InscrutableOpenStackProjectName" {
47 t.Errorf("Service name was unexpected [%s]", result.Name)
48 }
49 if result.Type != "compute" {
50 t.Errorf("Service type was unexpected [%s]", result.Type)
51 }
52}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040053
54func TestListSinglePage(t *testing.T) {
55 testhelper.SetupHTTP()
56 defer testhelper.TeardownHTTP()
57
58 testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
59 testhelper.TestMethod(t, r, "GET")
Ash Wilsonea8c7f32014-10-22 09:23:04 -040060 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040061
62 w.Header().Add("Content-Type", "application/json")
63 fmt.Fprintf(w, `
64 {
65 "links": {
66 "next": null,
67 "previous": null
68 },
69 "services": [
70 {
71 "description": "Service One",
72 "id": "1234",
73 "name": "service-one",
74 "type": "identity"
75 },
76 {
77 "description": "Service Two",
78 "id": "9876",
79 "name": "service-two",
80 "type": "compute"
81 }
82 ]
83 }
84 `)
85 })
86
Ash Wilsonbddac132014-09-12 14:20:16 -040087 count := 0
Ash Wilsonea8c7f32014-10-22 09:23:04 -040088 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonbddac132014-09-12 14:20:16 -040089 count++
90 actual, err := ExtractServices(page)
91 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -040092 return false, err
Ash Wilsonbddac132014-09-12 14:20:16 -040093 }
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040094
Ash Wilsonbddac132014-09-12 14:20:16 -040095 desc0 := "Service One"
96 desc1 := "Service Two"
97 expected := []Service{
98 Service{
99 Description: &desc0,
100 ID: "1234",
101 Name: "service-one",
102 Type: "identity",
103 },
104 Service{
105 Description: &desc1,
106 ID: "9876",
107 Name: "service-two",
108 Type: "compute",
109 },
110 }
Ash Wilson64f44152014-09-05 13:45:03 -0400111
Ash Wilsonbddac132014-09-12 14:20:16 -0400112 if !reflect.DeepEqual(expected, actual) {
113 t.Errorf("Expected %#v, got %#v", expected, actual)
114 }
Ash Wilson64f44152014-09-05 13:45:03 -0400115
Ash Wilson6b35e502014-09-12 15:15:23 -0400116 return true, nil
Ash Wilsonbddac132014-09-12 14:20:16 -0400117 })
Ash Wilson6b35e502014-09-12 15:15:23 -0400118 if err != nil {
119 t.Errorf("Unexpected error while paging: %v", err)
120 }
Ash Wilsonbddac132014-09-12 14:20:16 -0400121 if count != 1 {
122 t.Errorf("Expected 1 page, got %d", count)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400123 }
124}
Ash Wilsonb1129972014-09-03 14:45:21 -0400125
Ash Wilson5266e492014-09-09 15:44:30 -0400126func TestGetSuccessful(t *testing.T) {
Ash Wilsonb1129972014-09-03 14:45:21 -0400127 testhelper.SetupHTTP()
128 defer testhelper.TeardownHTTP()
129
130 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
131 testhelper.TestMethod(t, r, "GET")
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400132 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonb1129972014-09-03 14:45:21 -0400133
134 w.Header().Add("Content-Type", "application/json")
135 fmt.Fprintf(w, `
136 {
137 "service": {
138 "description": "Service One",
Ash Wilsond1b72132014-09-03 15:26:26 -0400139 "id": "12345",
Ash Wilsonb1129972014-09-03 14:45:21 -0400140 "name": "service-one",
141 "type": "identity"
142 }
143 }
144 `)
145 })
146
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400147 result, err := Get(client.ServiceClient(), "12345").Extract()
Ash Wilsonb1129972014-09-03 14:45:21 -0400148 if err != nil {
149 t.Fatalf("Error fetching service information: %v", err)
150 }
151
Ash Wilsond1b72132014-09-03 15:26:26 -0400152 if result.ID != "12345" {
Ash Wilsonb1129972014-09-03 14:45:21 -0400153 t.Errorf("Unexpected service ID: %s", result.ID)
154 }
155 if *result.Description != "Service One" {
156 t.Errorf("Unexpected service description: [%s]", *result.Description)
157 }
158 if result.Name != "service-one" {
159 t.Errorf("Unexpected service name: [%s]", result.Name)
160 }
161 if result.Type != "identity" {
162 t.Errorf("Unexpected service type: [%s]", result.Type)
163 }
164}
Ash Wilsond1b72132014-09-03 15:26:26 -0400165
166func TestUpdateSuccessful(t *testing.T) {
167 testhelper.SetupHTTP()
168 defer testhelper.TeardownHTTP()
169
170 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
171 testhelper.TestMethod(t, r, "PATCH")
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400172 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsond1b72132014-09-03 15:26:26 -0400173 testhelper.TestJSONRequest(t, r, `{ "type": "lasermagic" }`)
174
175 w.Header().Add("Content-Type", "application/json")
176 fmt.Fprintf(w, `
177 {
178 "service": {
179 "id": "12345",
180 "type": "lasermagic"
181 }
182 }
183 `)
184 })
185
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400186 result, err := Update(client.ServiceClient(), "12345", "lasermagic").Extract()
Ash Wilsond1b72132014-09-03 15:26:26 -0400187 if err != nil {
Ash Wilsond24786d2014-09-03 15:38:00 -0400188 t.Fatalf("Unable to update service: %v", err)
Ash Wilsond1b72132014-09-03 15:26:26 -0400189 }
190
191 if result.ID != "12345" {
Ash Wilson3f59ade2014-10-02 09:22:23 -0400192 t.Fatalf("Expected ID 12345, was %s", result.ID)
Ash Wilsond1b72132014-09-03 15:26:26 -0400193 }
194}
Ash Wilsond24786d2014-09-03 15:38:00 -0400195
196func TestDeleteSuccessful(t *testing.T) {
197 testhelper.SetupHTTP()
198 defer testhelper.TeardownHTTP()
199
200 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
201 testhelper.TestMethod(t, r, "DELETE")
Ash Wilsonea8c7f32014-10-22 09:23:04 -0400202 testhelper.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsond24786d2014-09-03 15:38:00 -0400203
204 w.WriteHeader(http.StatusNoContent)
205 })
206
Jamie Hannaford8ab3c142014-10-27 11:33:39 +0100207 res := Delete(client.ServiceClient(), "12345")
208 testhelper.AssertNoErr(t, res.Err)
Ash Wilsond24786d2014-09-03 15:38:00 -0400209}