blob: d4bc34c257c51b4df6645be807ad02b1ad5c992a [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"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02009 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020010 th "github.com/rackspace/gophercloud/testhelper"
11)
12
13const TokenID = "123"
14
15func ServiceClient() *gophercloud.ServiceClient {
16 return &gophercloud.ServiceClient{
17 Provider: &gophercloud.ProviderClient{
18 TokenID: TokenID,
19 },
20 Endpoint: th.Endpoint(),
21 }
22}
23
24func TestList(t *testing.T) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020025 th.SetupHTTP()
26 defer th.TeardownHTTP()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020027
Jamie Hannaford4721abc2014-09-16 16:29:04 +020028 th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) {
29 th.TestMethod(t, r, "GET")
30 th.TestHeader(t, r, "X-Auth-Token", TokenID)
31
32 w.Header().Add("Content-Type", "application/json")
33
34 fmt.Fprintf(w, `
35{
36 "extensions": [
37 {
38 "updated": "2013-01-20T00:00:00-00:00",
39 "name": "Neutron Service Type Management",
40 "links": [],
41 "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
42 "alias": "service-type",
43 "description": "API for retrieving service providers for Neutron advanced services"
44 }
45 ]
46}
47 `)
48 })
49
50 count := 0
51
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020052 List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020053 count++
54 actual, err := ExtractExtensions(page)
55 if err != nil {
56 t.Errorf("Failed to extract extensions: %v", err)
57 }
58
59 expected := []Extension{
60 Extension{
61 Updated: "2013-01-20T00:00:00-00:00",
62 Name: "Neutron Service Type Management",
63 Links: []interface{}{},
64 Namespace: "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
65 Alias: "service-type",
66 Description: "API for retrieving service providers for Neutron advanced services",
67 },
68 }
69
70 th.AssertDeepEquals(t, expected, actual)
71
72 return true, nil
73 })
74
75 if count != 1 {
76 t.Errorf("Expected 1 page, got %d", count)
77 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020078}
79
80func TestGet(t *testing.T) {
81 th.SetupHTTP()
82 defer th.TeardownHTTP()
83
Jamie Hannaford4721abc2014-09-16 16:29:04 +020084 th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020085 th.TestMethod(t, r, "GET")
86 th.TestHeader(t, r, "X-Auth-Token", TokenID)
87
88 w.Header().Add("Content-Type", "application/json")
89 w.WriteHeader(http.StatusOK)
90
91 fmt.Fprintf(w, `
92{
93 "extension": {
94 "updated": "2013-02-03T10:00:00-00:00",
95 "name": "agent",
96 "links": [],
97 "namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
98 "alias": "agent",
99 "description": "The agent management extension."
100 }
101}
102 `)
103
104 ext, err := Get(ServiceClient(), "agent")
105 th.AssertNoErr(t, err)
106
107 th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
108 th.AssertEquals(t, ext.Name, "agent")
109 th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
110 th.AssertEquals(t, ext.Alias, "agent")
111 th.AssertEquals(t, ext.Description, "The agent management extension.")
112 })
113}