blob: b2249e64e3ddf1d49069eb7d33ed978f70ad9f0d [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/testhelper"
10)
11
Ash Wilsond1b72132014-09-03 15:26:26 -040012const tokenID = "111111"
13
14func serviceClient() *gophercloud.ServiceClient {
15 return &gophercloud.ServiceClient{
16 Provider: &gophercloud.ProviderClient{
17 TokenID: tokenID,
18 },
19 Endpoint: testhelper.Endpoint(),
20 }
21}
22
Ash Wilsonb73b7f82014-08-29 15:38:06 -040023func TestCreateSuccessful(t *testing.T) {
24 testhelper.SetupHTTP()
25 defer testhelper.TeardownHTTP()
26
27 testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
28 testhelper.TestMethod(t, r, "POST")
Ash Wilsond1b72132014-09-03 15:26:26 -040029 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040030 testhelper.TestJSONRequest(t, r, `{ "type": "compute" }`)
31
32 w.Header().Add("Content-Type", "application/json")
33 w.WriteHeader(http.StatusCreated)
34 fmt.Fprintf(w, `{
35 "service": {
36 "description": "Here's your service",
37 "id": "1234",
38 "name": "InscrutableOpenStackProjectName",
39 "type": "compute"
40 }
41 }`)
42 })
43
Ash Wilsond1b72132014-09-03 15:26:26 -040044 client := serviceClient()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040045
Ash Wilsond1b72132014-09-03 15:26:26 -040046 result, err := Create(client, "compute")
Ash Wilsonb73b7f82014-08-29 15:38:06 -040047 if err != nil {
48 t.Fatalf("Unexpected error from Create: %v", err)
49 }
50
51 if result.Description == nil || *result.Description != "Here's your service" {
52 t.Errorf("Service description was unexpected [%s]", result.Description)
53 }
54 if result.ID != "1234" {
55 t.Errorf("Service ID was unexpected [%s]", result.ID)
56 }
57 if result.Name != "InscrutableOpenStackProjectName" {
58 t.Errorf("Service name was unexpected [%s]", result.Name)
59 }
60 if result.Type != "compute" {
61 t.Errorf("Service type was unexpected [%s]", result.Type)
62 }
63}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040064
65func TestListSinglePage(t *testing.T) {
66 testhelper.SetupHTTP()
67 defer testhelper.TeardownHTTP()
68
69 testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
70 testhelper.TestMethod(t, r, "GET")
Ash Wilsond1b72132014-09-03 15:26:26 -040071 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040072
73 w.Header().Add("Content-Type", "application/json")
74 fmt.Fprintf(w, `
75 {
76 "links": {
77 "next": null,
78 "previous": null
79 },
80 "services": [
81 {
82 "description": "Service One",
83 "id": "1234",
84 "name": "service-one",
85 "type": "identity"
86 },
87 {
88 "description": "Service Two",
89 "id": "9876",
90 "name": "service-two",
91 "type": "compute"
92 }
93 ]
94 }
95 `)
96 })
97
Ash Wilsond1b72132014-09-03 15:26:26 -040098 client := serviceClient()
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040099
Ash Wilsond1b72132014-09-03 15:26:26 -0400100 result, err := List(client, ListOpts{})
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400101 if err != nil {
102 t.Fatalf("Error listing services: %v", err)
103 }
104
105 if result.Pagination.Next != nil {
106 t.Errorf("Unexpected next link: %s", result.Pagination.Next)
107 }
108 if result.Pagination.Previous != nil {
109 t.Errorf("Unexpected previous link: %s", result.Pagination.Previous)
110 }
111 if len(result.Services) != 2 {
112 t.Errorf("Unexpected number of services: %s", len(result.Services))
113 }
114 if result.Services[0].ID != "1234" {
115 t.Errorf("Unexpected service: %#v", result.Services[0])
116 }
Ash Wilsonb1129972014-09-03 14:45:21 -0400117 if result.Services[1].ID != "9876" {
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400118 t.Errorf("Unexpected service: %#v", result.Services[1])
119 }
120}
Ash Wilsonb1129972014-09-03 14:45:21 -0400121
122func TestInfoSuccessful(t *testing.T) {
123 testhelper.SetupHTTP()
124 defer testhelper.TeardownHTTP()
125
126 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
127 testhelper.TestMethod(t, r, "GET")
Ash Wilsond1b72132014-09-03 15:26:26 -0400128 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
Ash Wilsonb1129972014-09-03 14:45:21 -0400129
130 w.Header().Add("Content-Type", "application/json")
131 fmt.Fprintf(w, `
132 {
133 "service": {
134 "description": "Service One",
Ash Wilsond1b72132014-09-03 15:26:26 -0400135 "id": "12345",
Ash Wilsonb1129972014-09-03 14:45:21 -0400136 "name": "service-one",
137 "type": "identity"
138 }
139 }
140 `)
141 })
142
Ash Wilsond1b72132014-09-03 15:26:26 -0400143 client := serviceClient()
Ash Wilsonb1129972014-09-03 14:45:21 -0400144
Ash Wilsond1b72132014-09-03 15:26:26 -0400145 result, err := Info(client, "12345")
Ash Wilsonb1129972014-09-03 14:45:21 -0400146 if err != nil {
147 t.Fatalf("Error fetching service information: %v", err)
148 }
149
Ash Wilsond1b72132014-09-03 15:26:26 -0400150 if result.ID != "12345" {
Ash Wilsonb1129972014-09-03 14:45:21 -0400151 t.Errorf("Unexpected service ID: %s", result.ID)
152 }
153 if *result.Description != "Service One" {
154 t.Errorf("Unexpected service description: [%s]", *result.Description)
155 }
156 if result.Name != "service-one" {
157 t.Errorf("Unexpected service name: [%s]", result.Name)
158 }
159 if result.Type != "identity" {
160 t.Errorf("Unexpected service type: [%s]", result.Type)
161 }
162}
Ash Wilsond1b72132014-09-03 15:26:26 -0400163
164func TestUpdateSuccessful(t *testing.T) {
165 testhelper.SetupHTTP()
166 defer testhelper.TeardownHTTP()
167
168 testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
169 testhelper.TestMethod(t, r, "PATCH")
170 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
171 testhelper.TestJSONRequest(t, r, `{ "type": "lasermagic" }`)
172
173 w.Header().Add("Content-Type", "application/json")
174 fmt.Fprintf(w, `
175 {
176 "service": {
177 "id": "12345",
178 "type": "lasermagic"
179 }
180 }
181 `)
182 })
183
184 client := serviceClient()
185
186 result, err := Update(client, "12345", "lasermagic")
187 if err != nil {
188 t.Fatalf("Unable to update service")
189 }
190
191 if result.ID != "12345" {
192
193 }
194}