blob: ac787476388c667ab70556e6f1fc62d51dec6b53 [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
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford1ce30f22014-09-16 11:23:34 +02009 th "github.com/rackspace/gophercloud/testhelper"
10)
11
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020012func TestList(t *testing.T) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020013 th.SetupHTTP()
14 defer th.TeardownHTTP()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020015
Jamie Hannaford4721abc2014-09-16 16:29:04 +020016 th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) {
17 th.TestMethod(t, r, "GET")
Jamie Hannaford09cc0a72014-10-02 17:28:25 +020018 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020019
20 w.Header().Add("Content-Type", "application/json")
21
22 fmt.Fprintf(w, `
23{
24 "extensions": [
25 {
26 "updated": "2013-01-20T00:00:00-00:00",
27 "name": "Neutron Service Type Management",
28 "links": [],
29 "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
30 "alias": "service-type",
31 "description": "API for retrieving service providers for Neutron advanced services"
32 }
33 ]
34}
35 `)
36 })
37
38 count := 0
39
Jamie Hannaford09cc0a72014-10-02 17:28:25 +020040 List(th.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020041 count++
42 actual, err := ExtractExtensions(page)
43 if err != nil {
44 t.Errorf("Failed to extract extensions: %v", err)
45 }
46
47 expected := []Extension{
48 Extension{
49 Updated: "2013-01-20T00:00:00-00:00",
50 Name: "Neutron Service Type Management",
51 Links: []interface{}{},
52 Namespace: "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
53 Alias: "service-type",
54 Description: "API for retrieving service providers for Neutron advanced services",
55 },
56 }
57
58 th.AssertDeepEquals(t, expected, actual)
59
60 return true, nil
61 })
62
63 if count != 1 {
64 t.Errorf("Expected 1 page, got %d", count)
65 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020066}
67
68func TestGet(t *testing.T) {
69 th.SetupHTTP()
70 defer th.TeardownHTTP()
71
Jamie Hannaford4721abc2014-09-16 16:29:04 +020072 th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020073 th.TestMethod(t, r, "GET")
Jamie Hannaford09cc0a72014-10-02 17:28:25 +020074 th.TestHeader(t, r, "X-Auth-Token", th.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020075
76 w.Header().Add("Content-Type", "application/json")
77 w.WriteHeader(http.StatusOK)
78
79 fmt.Fprintf(w, `
80{
81 "extension": {
82 "updated": "2013-02-03T10:00:00-00:00",
83 "name": "agent",
84 "links": [],
85 "namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
86 "alias": "agent",
87 "description": "The agent management extension."
88 }
89}
90 `)
91
Jamie Hannaford09cc0a72014-10-02 17:28:25 +020092 ext, err := Get(th.ServiceClient(), "agent").Extract()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020093 th.AssertNoErr(t, err)
94
95 th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
96 th.AssertEquals(t, ext.Name, "agent")
97 th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
98 th.AssertEquals(t, ext.Alias, "agent")
99 th.AssertEquals(t, ext.Description, "The agent management extension.")
100 })
101}