blob: aa19bcc3a141fc52a234ac92cac7f00ba32fe0d3 [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud/pagination"
Jon Perrittdb0ae142016-03-13 00:33:41 -06009 th "github.com/gophercloud/gophercloud/testhelper"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonb73b7f82014-08-29 15:38:06 -040011)
12
13func TestCreateSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040016
Jon Perrittdb0ae142016-03-13 00:33:41 -060017 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "POST")
19 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
20 th.TestJSONRequest(t, r, `{ "type": "compute" }`)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040021
22 w.Header().Add("Content-Type", "application/json")
23 w.WriteHeader(http.StatusCreated)
24 fmt.Fprintf(w, `{
25 "service": {
26 "description": "Here's your service",
27 "id": "1234",
28 "name": "InscrutableOpenStackProjectName",
29 "type": "compute"
30 }
31 }`)
32 })
33
Jon Perrittdb0ae142016-03-13 00:33:41 -060034 expected := &Service{
35 Description: "Here's your service",
36 ID: "1234",
37 Name: "InscrutableOpenStackProjectName",
38 Type: "compute",
39 }
40
41 actual, err := Create(client.ServiceClient(), "compute").Extract()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040042 if err != nil {
43 t.Fatalf("Unexpected error from Create: %v", err)
44 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060045 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040046}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040047
48func TestListSinglePage(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060049 th.SetupHTTP()
50 defer th.TeardownHTTP()
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040051
Jon Perrittdb0ae142016-03-13 00:33:41 -060052 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
53 th.TestMethod(t, r, "GET")
54 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040055
56 w.Header().Add("Content-Type", "application/json")
57 fmt.Fprintf(w, `
58 {
59 "links": {
60 "next": null,
61 "previous": null
62 },
63 "services": [
64 {
65 "description": "Service One",
66 "id": "1234",
67 "name": "service-one",
68 "type": "identity"
69 },
70 {
71 "description": "Service Two",
72 "id": "9876",
73 "name": "service-two",
74 "type": "compute"
75 }
76 ]
77 }
78 `)
79 })
80
Ash Wilsonbddac132014-09-12 14:20:16 -040081 count := 0
Ash Wilsonea8c7f32014-10-22 09:23:04 -040082 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonbddac132014-09-12 14:20:16 -040083 count++
84 actual, err := ExtractServices(page)
85 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -040086 return false, err
Ash Wilsonbddac132014-09-12 14:20:16 -040087 }
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040088
Ash Wilsonbddac132014-09-12 14:20:16 -040089 expected := []Service{
90 Service{
Jon Perrittdb0ae142016-03-13 00:33:41 -060091 Description: "Service One",
Ash Wilsonbddac132014-09-12 14:20:16 -040092 ID: "1234",
93 Name: "service-one",
94 Type: "identity",
95 },
96 Service{
Jon Perrittdb0ae142016-03-13 00:33:41 -060097 Description: "Service Two",
Ash Wilsonbddac132014-09-12 14:20:16 -040098 ID: "9876",
99 Name: "service-two",
100 Type: "compute",
101 },
102 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600103 th.AssertDeepEquals(t, expected, actual)
Ash Wilson6b35e502014-09-12 15:15:23 -0400104 return true, nil
Ash Wilsonbddac132014-09-12 14:20:16 -0400105 })
Jon Perrittdb0ae142016-03-13 00:33:41 -0600106 th.AssertNoErr(t, err)
107 th.AssertEquals(t, 1, count)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400108}
Ash Wilsonb1129972014-09-03 14:45:21 -0400109
Ash Wilson5266e492014-09-09 15:44:30 -0400110func TestGetSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600111 th.SetupHTTP()
112 defer th.TeardownHTTP()
Ash Wilsonb1129972014-09-03 14:45:21 -0400113
Jon Perrittdb0ae142016-03-13 00:33:41 -0600114 th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
115 th.TestMethod(t, r, "GET")
116 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonb1129972014-09-03 14:45:21 -0400117
118 w.Header().Add("Content-Type", "application/json")
119 fmt.Fprintf(w, `
120 {
121 "service": {
122 "description": "Service One",
Ash Wilsond1b72132014-09-03 15:26:26 -0400123 "id": "12345",
Ash Wilsonb1129972014-09-03 14:45:21 -0400124 "name": "service-one",
125 "type": "identity"
126 }
127 }
128 `)
129 })
130
Jon Perrittdb0ae142016-03-13 00:33:41 -0600131 actual, err := Get(client.ServiceClient(), "12345").Extract()
132 th.AssertNoErr(t, err)
133
134 expected := &Service{
135 ID: "12345",
136 Description: "Service One",
137 Name: "service-one",
138 Type: "identity",
Ash Wilsonb1129972014-09-03 14:45:21 -0400139 }
140
Jon Perrittdb0ae142016-03-13 00:33:41 -0600141 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonb1129972014-09-03 14:45:21 -0400142}
Ash Wilsond1b72132014-09-03 15:26:26 -0400143
144func TestUpdateSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600145 th.SetupHTTP()
146 defer th.TeardownHTTP()
Ash Wilsond1b72132014-09-03 15:26:26 -0400147
Jon Perrittdb0ae142016-03-13 00:33:41 -0600148 th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
149 th.TestMethod(t, r, "PATCH")
150 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
151 th.TestJSONRequest(t, r, `{ "type": "lasermagic" }`)
Ash Wilsond1b72132014-09-03 15:26:26 -0400152
153 w.Header().Add("Content-Type", "application/json")
154 fmt.Fprintf(w, `
155 {
156 "service": {
157 "id": "12345",
158 "type": "lasermagic"
159 }
160 }
161 `)
162 })
163
Jon Perrittdb0ae142016-03-13 00:33:41 -0600164 expected := &Service{
165 ID: "12345",
166 Type: "lasermagic",
Ash Wilsond1b72132014-09-03 15:26:26 -0400167 }
168
Jon Perrittdb0ae142016-03-13 00:33:41 -0600169 actual, err := Update(client.ServiceClient(), "12345", "lasermagic").Extract()
170 th.AssertNoErr(t, err)
171 th.AssertDeepEquals(t, expected, actual)
Ash Wilsond1b72132014-09-03 15:26:26 -0400172}
Ash Wilsond24786d2014-09-03 15:38:00 -0400173
174func TestDeleteSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600175 th.SetupHTTP()
176 defer th.TeardownHTTP()
Ash Wilsond24786d2014-09-03 15:38:00 -0400177
Jon Perrittdb0ae142016-03-13 00:33:41 -0600178 th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
179 th.TestMethod(t, r, "DELETE")
180 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsond24786d2014-09-03 15:38:00 -0400181 w.WriteHeader(http.StatusNoContent)
182 })
183
Jamie Hannaford8ab3c142014-10-27 11:33:39 +0100184 res := Delete(client.ServiceClient(), "12345")
Jon Perrittdb0ae142016-03-13 00:33:41 -0600185 th.AssertNoErr(t, res.Err)
Ash Wilsond24786d2014-09-03 15:38:00 -0400186}