Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 1 | package extensions |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame^] | 8 | common "github.com/gophercloud/gophercloud/openstack/common/extensions" |
| 9 | fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" |
| 10 | "github.com/gophercloud/gophercloud/pagination" |
| 11 | th "github.com/gophercloud/gophercloud/testhelper" |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 14 | func TestList(t *testing.T) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 15 | th.SetupHTTP() |
| 16 | defer th.TeardownHTTP() |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 17 | |
Jamie Hannaford | 9695d51 | 2014-10-09 10:33:37 +0200 | [diff] [blame] | 18 | th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 19 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 20 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 21 | |
| 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 Wilson | 318666f | 2014-10-03 08:38:39 -0400 | [diff] [blame] | 37 | `) |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 38 | }) |
| 39 | |
| 40 | count := 0 |
| 41 | |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 42 | List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 43 | 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 Wilson | 318666f | 2014-10-03 08:38:39 -0400 | [diff] [blame] | 51 | 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 Hannaford | 4721abc | 2014-09-16 16:29:04 +0200 | [diff] [blame] | 59 | }, |
| 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 Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | func TestGet(t *testing.T) { |
| 73 | th.SetupHTTP() |
| 74 | defer th.TeardownHTTP() |
| 75 | |
Jamie Hannaford | 9695d51 | 2014-10-09 10:33:37 +0200 | [diff] [blame] | 76 | th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 77 | th.TestMethod(t, r, "GET") |
Jamie Hannaford | 58b008f | 2014-10-06 10:07:47 +0200 | [diff] [blame] | 78 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 79 | |
| 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 Wilson | 318666f | 2014-10-03 08:38:39 -0400 | [diff] [blame] | 94 | `) |
Jamie Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 95 | }) |
Jamie Hannaford | ca3821d | 2014-10-09 11:12:51 +0200 | [diff] [blame] | 96 | |
| 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 Hannaford | 1ce30f2 | 2014-09-16 11:23:34 +0200 | [diff] [blame] | 105 | } |