blob: 90eb21b7691e7a6672954349c6b51e03eef0cd8e [file] [log] [blame]
Ash Wilsonad13c422014-10-03 08:35:46 -04001package extensions
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 common "github.com/rackspace/gophercloud/openstack/common/extensions"
10 "github.com/rackspace/gophercloud/pagination"
11 th "github.com/rackspace/gophercloud/testhelper"
12)
13
14const TokenID = "123"
15
16func ServiceClient() *gophercloud.ServiceClient {
17 return &gophercloud.ServiceClient{
18 Provider: &gophercloud.ProviderClient{
19 TokenID: TokenID,
20 },
21 Endpoint: th.Endpoint(),
22 }
23}
24
25func TestList(t *testing.T) {
26 th.SetupHTTP()
27 defer th.TeardownHTTP()
28
Ash Wilsonfbedc672014-10-03 16:34:22 -040029 th.Mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsonad13c422014-10-03 08:35:46 -040030 th.TestMethod(t, r, "GET")
31 th.TestHeader(t, r, "X-Auth-Token", TokenID)
32
33 w.Header().Add("Content-Type", "application/json")
34
35 fmt.Fprintf(w, `
36{
Ash Wilsonfbedc672014-10-03 16:34:22 -040037 "extensions": {
38 "values": [
39 {
40 "updated": "2013-01-20T00:00:00-00:00",
41 "name": "Neutron Service Type Management",
42 "links": [],
43 "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
44 "alias": "service-type",
45 "description": "API for retrieving service providers for Neutron advanced services"
46 }
47 ]
48 }
Ash Wilsonad13c422014-10-03 08:35:46 -040049}
Ash Wilsonfbedc672014-10-03 16:34:22 -040050 `)
Ash Wilsonad13c422014-10-03 08:35:46 -040051 })
52
53 count := 0
54
Ash Wilsonfbedc672014-10-03 16:34:22 -040055 err := List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad13c422014-10-03 08:35:46 -040056 count++
57 actual, err := ExtractExtensions(page)
Ash Wilsonfbedc672014-10-03 16:34:22 -040058 th.AssertNoErr(t, err)
Ash Wilsonad13c422014-10-03 08:35:46 -040059
60 expected := []Extension{
61 Extension{
62 common.Extension{
63 Updated: "2013-01-20T00:00:00-00:00",
64 Name: "Neutron Service Type Management",
65 Links: []interface{}{},
66 Namespace: "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
67 Alias: "service-type",
68 Description: "API for retrieving service providers for Neutron advanced services",
69 },
70 },
71 }
72
73 th.AssertDeepEquals(t, expected, actual)
74
75 return true, nil
76 })
Ash Wilsonfbedc672014-10-03 16:34:22 -040077 th.AssertNoErr(t, err)
78 th.CheckEquals(t, 1, count)
Ash Wilsonad13c422014-10-03 08:35:46 -040079}
80
81func TestGet(t *testing.T) {
82 th.SetupHTTP()
83 defer th.TeardownHTTP()
84
Ash Wilsonfbedc672014-10-03 16:34:22 -040085 th.Mux.HandleFunc("/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
Ash Wilsonad13c422014-10-03 08:35:46 -040086 th.TestMethod(t, r, "GET")
87 th.TestHeader(t, r, "X-Auth-Token", TokenID)
88
89 w.Header().Add("Content-Type", "application/json")
90 w.WriteHeader(http.StatusOK)
91
92 fmt.Fprintf(w, `
93{
94 "extension": {
95 "updated": "2013-02-03T10:00:00-00:00",
96 "name": "agent",
97 "links": [],
98 "namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
99 "alias": "agent",
100 "description": "The agent management extension."
101 }
102}
103 `)
104
105 ext, err := Get(ServiceClient(), "agent").Extract()
106 th.AssertNoErr(t, err)
107
108 th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
109 th.AssertEquals(t, ext.Name, "agent")
110 th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
111 th.AssertEquals(t, ext.Alias, "agent")
112 th.AssertEquals(t, ext.Description, "The agent management extension.")
113 })
114}