blob: 0a065a2afc3d57566ce12b6820c46794d0bd3fd3 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonb73b7f82014-08-29 15:38:06 -04002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
jrperritt3d966162016-06-06 14:08:54 -05008 "github.com/gophercloud/gophercloud/openstack/identity/v3/services"
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud/pagination"
Jon Perrittdb0ae142016-03-13 00:33:41 -060010 th "github.com/gophercloud/gophercloud/testhelper"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonb73b7f82014-08-29 15:38:06 -040012)
13
14func TestCreateSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060015 th.SetupHTTP()
16 defer th.TeardownHTTP()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040017
Jon Perrittdb0ae142016-03-13 00:33:41 -060018 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
19 th.TestMethod(t, r, "POST")
20 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
21 th.TestJSONRequest(t, r, `{ "type": "compute" }`)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040022
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
jrperritt3d966162016-06-06 14:08:54 -050035 expected := &services.Service{
Jon Perrittdb0ae142016-03-13 00:33:41 -060036 Description: "Here's your service",
37 ID: "1234",
38 Name: "InscrutableOpenStackProjectName",
39 Type: "compute",
40 }
41
jrperritt3d966162016-06-06 14:08:54 -050042 actual, err := services.Create(client.ServiceClient(), "compute").Extract()
Ash Wilsonb73b7f82014-08-29 15:38:06 -040043 if err != nil {
44 t.Fatalf("Unexpected error from Create: %v", err)
45 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060046 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040047}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040048
49func TestListSinglePage(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060050 th.SetupHTTP()
51 defer th.TeardownHTTP()
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040052
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 th.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
54 th.TestMethod(t, r, "GET")
55 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040056
57 w.Header().Add("Content-Type", "application/json")
58 fmt.Fprintf(w, `
59 {
60 "links": {
61 "next": null,
62 "previous": null
63 },
64 "services": [
65 {
66 "description": "Service One",
67 "id": "1234",
68 "name": "service-one",
69 "type": "identity"
70 },
71 {
72 "description": "Service Two",
73 "id": "9876",
74 "name": "service-two",
75 "type": "compute"
76 }
77 ]
78 }
79 `)
80 })
81
Ash Wilsonbddac132014-09-12 14:20:16 -040082 count := 0
jrperritt3d966162016-06-06 14:08:54 -050083 err := services.List(client.ServiceClient(), services.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonbddac132014-09-12 14:20:16 -040084 count++
jrperritt3d966162016-06-06 14:08:54 -050085 actual, err := services.ExtractServices(page)
Ash Wilsonbddac132014-09-12 14:20:16 -040086 if err != nil {
Ash Wilson6b35e502014-09-12 15:15:23 -040087 return false, err
Ash Wilsonbddac132014-09-12 14:20:16 -040088 }
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040089
jrperritt3d966162016-06-06 14:08:54 -050090 expected := []services.Service{
91 {
Jon Perrittdb0ae142016-03-13 00:33:41 -060092 Description: "Service One",
Ash Wilsonbddac132014-09-12 14:20:16 -040093 ID: "1234",
94 Name: "service-one",
95 Type: "identity",
96 },
jrperritt3d966162016-06-06 14:08:54 -050097 {
Jon Perrittdb0ae142016-03-13 00:33:41 -060098 Description: "Service Two",
Ash Wilsonbddac132014-09-12 14:20:16 -040099 ID: "9876",
100 Name: "service-two",
101 Type: "compute",
102 },
103 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600104 th.AssertDeepEquals(t, expected, actual)
Ash Wilson6b35e502014-09-12 15:15:23 -0400105 return true, nil
Ash Wilsonbddac132014-09-12 14:20:16 -0400106 })
Jon Perrittdb0ae142016-03-13 00:33:41 -0600107 th.AssertNoErr(t, err)
108 th.AssertEquals(t, 1, count)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -0400109}
Ash Wilsonb1129972014-09-03 14:45:21 -0400110
Ash Wilson5266e492014-09-09 15:44:30 -0400111func TestGetSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600112 th.SetupHTTP()
113 defer th.TeardownHTTP()
Ash Wilsonb1129972014-09-03 14:45:21 -0400114
Jon Perrittdb0ae142016-03-13 00:33:41 -0600115 th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
116 th.TestMethod(t, r, "GET")
117 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonb1129972014-09-03 14:45:21 -0400118
119 w.Header().Add("Content-Type", "application/json")
120 fmt.Fprintf(w, `
121 {
122 "service": {
123 "description": "Service One",
Ash Wilsond1b72132014-09-03 15:26:26 -0400124 "id": "12345",
Ash Wilsonb1129972014-09-03 14:45:21 -0400125 "name": "service-one",
126 "type": "identity"
127 }
128 }
129 `)
130 })
131
jrperritt3d966162016-06-06 14:08:54 -0500132 actual, err := services.Get(client.ServiceClient(), "12345").Extract()
Jon Perrittdb0ae142016-03-13 00:33:41 -0600133 th.AssertNoErr(t, err)
134
jrperritt3d966162016-06-06 14:08:54 -0500135 expected := &services.Service{
Jon Perrittdb0ae142016-03-13 00:33:41 -0600136 ID: "12345",
137 Description: "Service One",
138 Name: "service-one",
139 Type: "identity",
Ash Wilsonb1129972014-09-03 14:45:21 -0400140 }
141
Jon Perrittdb0ae142016-03-13 00:33:41 -0600142 th.AssertDeepEquals(t, expected, actual)
Ash Wilsonb1129972014-09-03 14:45:21 -0400143}
Ash Wilsond1b72132014-09-03 15:26:26 -0400144
145func TestUpdateSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600146 th.SetupHTTP()
147 defer th.TeardownHTTP()
Ash Wilsond1b72132014-09-03 15:26:26 -0400148
Jon Perrittdb0ae142016-03-13 00:33:41 -0600149 th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
150 th.TestMethod(t, r, "PATCH")
151 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
152 th.TestJSONRequest(t, r, `{ "type": "lasermagic" }`)
Ash Wilsond1b72132014-09-03 15:26:26 -0400153
154 w.Header().Add("Content-Type", "application/json")
155 fmt.Fprintf(w, `
156 {
157 "service": {
158 "id": "12345",
159 "type": "lasermagic"
160 }
161 }
162 `)
163 })
164
jrperritt3d966162016-06-06 14:08:54 -0500165 expected := &services.Service{
Jon Perrittdb0ae142016-03-13 00:33:41 -0600166 ID: "12345",
167 Type: "lasermagic",
Ash Wilsond1b72132014-09-03 15:26:26 -0400168 }
169
jrperritt3d966162016-06-06 14:08:54 -0500170 actual, err := services.Update(client.ServiceClient(), "12345", "lasermagic").Extract()
Jon Perrittdb0ae142016-03-13 00:33:41 -0600171 th.AssertNoErr(t, err)
172 th.AssertDeepEquals(t, expected, actual)
Ash Wilsond1b72132014-09-03 15:26:26 -0400173}
Ash Wilsond24786d2014-09-03 15:38:00 -0400174
175func TestDeleteSuccessful(t *testing.T) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600176 th.SetupHTTP()
177 defer th.TeardownHTTP()
Ash Wilsond24786d2014-09-03 15:38:00 -0400178
Jon Perrittdb0ae142016-03-13 00:33:41 -0600179 th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
180 th.TestMethod(t, r, "DELETE")
181 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsond24786d2014-09-03 15:38:00 -0400182 w.WriteHeader(http.StatusNoContent)
183 })
184
jrperritt3d966162016-06-06 14:08:54 -0500185 res := services.Delete(client.ServiceClient(), "12345")
Jon Perrittdb0ae142016-03-13 00:33:41 -0600186 th.AssertNoErr(t, res.Err)
Ash Wilsond24786d2014-09-03 15:38:00 -0400187}