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