blob: 969ea80cf9aa3996668f17881472d56445db4822 [file] [log] [blame]
Jamie Hannaford4721abc2014-09-16 16:29:04 +02001package extensions
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
Ash Wilson318666f2014-10-03 08:38:39 -04009 common "github.com/rackspace/gophercloud/openstack/common/extensions"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020010 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020011 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) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020026 th.SetupHTTP()
27 defer th.TeardownHTTP()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020028
Ash Wilsonf0af2262014-10-03 16:36:39 -040029 th.Mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020030 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{
37 "extensions": [
38 {
39 "updated": "2013-01-20T00:00:00-00:00",
40 "name": "Neutron Service Type Management",
41 "links": [],
42 "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
43 "alias": "service-type",
44 "description": "API for retrieving service providers for Neutron advanced services"
45 }
46 ]
47}
Ash Wilson318666f2014-10-03 08:38:39 -040048 `)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020049 })
50
51 count := 0
52
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020053 List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020054 count++
55 actual, err := ExtractExtensions(page)
56 if err != nil {
57 t.Errorf("Failed to extract extensions: %v", err)
58 }
59
60 expected := []Extension{
61 Extension{
Ash Wilson318666f2014-10-03 08:38:39 -040062 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 },
Jamie Hannaford4721abc2014-09-16 16:29:04 +020070 },
71 }
72
73 th.AssertDeepEquals(t, expected, actual)
74
75 return true, nil
76 })
77
78 if count != 1 {
79 t.Errorf("Expected 1 page, got %d", count)
80 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020081}
82
83func TestGet(t *testing.T) {
84 th.SetupHTTP()
85 defer th.TeardownHTTP()
86
Ash Wilsonf0af2262014-10-03 16:36:39 -040087 th.Mux.HandleFunc("/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020088 th.TestMethod(t, r, "GET")
89 th.TestHeader(t, r, "X-Auth-Token", TokenID)
90
91 w.Header().Add("Content-Type", "application/json")
92 w.WriteHeader(http.StatusOK)
93
94 fmt.Fprintf(w, `
95{
96 "extension": {
97 "updated": "2013-02-03T10:00:00-00:00",
98 "name": "agent",
99 "links": [],
100 "namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
101 "alias": "agent",
102 "description": "The agent management extension."
103 }
104}
Ash Wilson318666f2014-10-03 08:38:39 -0400105 `)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200106
Jamie Hannafordd9036422014-09-23 17:50:24 +0200107 ext, err := Get(ServiceClient(), "agent").Extract()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200108 th.AssertNoErr(t, err)
109
110 th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
111 th.AssertEquals(t, ext.Name, "agent")
112 th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
113 th.AssertEquals(t, ext.Alias, "agent")
114 th.AssertEquals(t, ext.Description, "The agent management extension.")
115 })
116}