blob: 3d2ac78d48259166e001f072881c7c56b2ca878c [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
Ash Wilson318666f2014-10-03 08:38:39 -04008 common "github.com/rackspace/gophercloud/openstack/common/extensions"
Jamie Hannaford9695d512014-10-09 10:33:37 +02009 fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
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
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020014func TestList(t *testing.T) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020015 th.SetupHTTP()
16 defer th.TeardownHTTP()
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020017
Jamie Hannaford9695d512014-10-09 10:33:37 +020018 th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020019 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +020020 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020021
22 w.Header().Add("Content-Type", "application/json")
23
24 fmt.Fprintf(w, `
25{
26 "extensions": [
27 {
28 "updated": "2013-01-20T00:00:00-00:00",
29 "name": "Neutron Service Type Management",
30 "links": [],
31 "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
32 "alias": "service-type",
33 "description": "API for retrieving service providers for Neutron advanced services"
34 }
35 ]
36}
Ash Wilson318666f2014-10-03 08:38:39 -040037 `)
Jamie Hannaford4721abc2014-09-16 16:29:04 +020038 })
39
40 count := 0
41
Jamie Hannaford58b008f2014-10-06 10:07:47 +020042 List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020043 count++
44 actual, err := ExtractExtensions(page)
45 if err != nil {
46 t.Errorf("Failed to extract extensions: %v", err)
47 }
48
49 expected := []Extension{
50 Extension{
Ash Wilson318666f2014-10-03 08:38:39 -040051 common.Extension{
52 Updated: "2013-01-20T00:00:00-00:00",
53 Name: "Neutron Service Type Management",
54 Links: []interface{}{},
55 Namespace: "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
56 Alias: "service-type",
57 Description: "API for retrieving service providers for Neutron advanced services",
58 },
Jamie Hannaford4721abc2014-09-16 16:29:04 +020059 },
60 }
61
62 th.AssertDeepEquals(t, expected, actual)
63
64 return true, nil
65 })
66
67 if count != 1 {
68 t.Errorf("Expected 1 page, got %d", count)
69 }
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020070}
71
72func TestGet(t *testing.T) {
73 th.SetupHTTP()
74 defer th.TeardownHTTP()
75
Jamie Hannaford9695d512014-10-09 10:33:37 +020076 th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020077 th.TestMethod(t, r, "GET")
Jamie Hannaford58b008f2014-10-06 10:07:47 +020078 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020079
80 w.Header().Add("Content-Type", "application/json")
81 w.WriteHeader(http.StatusOK)
82
83 fmt.Fprintf(w, `
84{
85 "extension": {
86 "updated": "2013-02-03T10:00:00-00:00",
87 "name": "agent",
88 "links": [],
89 "namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
90 "alias": "agent",
91 "description": "The agent management extension."
92 }
93}
Ash Wilson318666f2014-10-03 08:38:39 -040094 `)
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020095 })
Jamie Hannafordca3821d2014-10-09 11:12:51 +020096
97 ext, err := Get(fake.ServiceClient(), "agent").Extract()
98 th.AssertNoErr(t, err)
99
100 th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
101 th.AssertEquals(t, ext.Name, "agent")
102 th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
103 th.AssertEquals(t, ext.Alias, "agent")
104 th.AssertEquals(t, ext.Description, "The agent management extension.")
Jamie Hannaford1ce30f22014-09-16 11:23:34 +0200105}