Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 1 | package extensions |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | common "github.com/rackspace/gophercloud/openstack/common/extensions" |
| 10 | "github.com/rackspace/gophercloud/pagination" |
| 11 | th "github.com/rackspace/gophercloud/testhelper" |
| 12 | ) |
| 13 | |
| 14 | const TokenID = "123" |
| 15 | |
| 16 | func ServiceClient() *gophercloud.ServiceClient { |
| 17 | return &gophercloud.ServiceClient{ |
| 18 | Provider: &gophercloud.ProviderClient{ |
| 19 | TokenID: TokenID, |
| 20 | }, |
| 21 | Endpoint: th.Endpoint(), |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestList(t *testing.T) { |
| 26 | th.SetupHTTP() |
| 27 | defer th.TeardownHTTP() |
| 28 | |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame^] | 29 | th.Mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 30 | 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 | { |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame^] | 37 | "extensions": { |
| 38 | "values": [ |
| 39 | { |
| 40 | "updated": "2013-01-20T00:00:00-00:00", |
| 41 | "name": "Neutron Service Type Management", |
| 42 | "links": [], |
| 43 | "namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0", |
| 44 | "alias": "service-type", |
| 45 | "description": "API for retrieving service providers for Neutron advanced services" |
| 46 | } |
| 47 | ] |
| 48 | } |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 49 | } |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame^] | 50 | `) |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 51 | }) |
| 52 | |
| 53 | count := 0 |
| 54 | |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame^] | 55 | err := List(ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 56 | count++ |
| 57 | actual, err := ExtractExtensions(page) |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame^] | 58 | th.AssertNoErr(t, err) |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 59 | |
| 60 | expected := []Extension{ |
| 61 | Extension{ |
| 62 | 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 | }, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | th.AssertDeepEquals(t, expected, actual) |
| 74 | |
| 75 | return true, nil |
| 76 | }) |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame^] | 77 | th.AssertNoErr(t, err) |
| 78 | th.CheckEquals(t, 1, count) |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | func TestGet(t *testing.T) { |
| 82 | th.SetupHTTP() |
| 83 | defer th.TeardownHTTP() |
| 84 | |
Ash Wilson | fbedc67 | 2014-10-03 16:34:22 -0400 | [diff] [blame^] | 85 | th.Mux.HandleFunc("/extensions/agent", func(w http.ResponseWriter, r *http.Request) { |
Ash Wilson | ad13c42 | 2014-10-03 08:35:46 -0400 | [diff] [blame] | 86 | th.TestMethod(t, r, "GET") |
| 87 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 88 | |
| 89 | w.Header().Add("Content-Type", "application/json") |
| 90 | w.WriteHeader(http.StatusOK) |
| 91 | |
| 92 | fmt.Fprintf(w, ` |
| 93 | { |
| 94 | "extension": { |
| 95 | "updated": "2013-02-03T10:00:00-00:00", |
| 96 | "name": "agent", |
| 97 | "links": [], |
| 98 | "namespace": "http://docs.openstack.org/ext/agent/api/v2.0", |
| 99 | "alias": "agent", |
| 100 | "description": "The agent management extension." |
| 101 | } |
| 102 | } |
| 103 | `) |
| 104 | |
| 105 | ext, err := Get(ServiceClient(), "agent").Extract() |
| 106 | th.AssertNoErr(t, err) |
| 107 | |
| 108 | th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00") |
| 109 | th.AssertEquals(t, ext.Name, "agent") |
| 110 | th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0") |
| 111 | th.AssertEquals(t, ext.Alias, "agent") |
| 112 | th.AssertEquals(t, ext.Description, "The agent management extension.") |
| 113 | }) |
| 114 | } |