Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/testhelper/fixture" |
| 11 | ) |
| 12 | |
| 13 | var ( |
| 14 | flavorID = "{flavorID}" |
| 15 | _baseURL = "/flavors" |
| 16 | resURL = "/flavors/" + flavorID |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 17 | ) |
| 18 | |
| 19 | func TestListFlavors(t *testing.T) { |
| 20 | th.SetupHTTP() |
| 21 | defer th.TeardownHTTP() |
| 22 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 23 | fixture.SetupHandler(t, _baseURL, "GET", "", listFlavorsResp, 200) |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 24 | |
| 25 | pages := 0 |
| 26 | err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 27 | pages++ |
| 28 | |
| 29 | actual, err := ExtractFlavors(page) |
| 30 | if err != nil { |
| 31 | return false, err |
| 32 | } |
| 33 | |
| 34 | expected := []Flavor{ |
| 35 | Flavor{ |
| 36 | ID: 1, |
| 37 | Name: "m1.tiny", |
| 38 | RAM: 512, |
| 39 | Links: []gophercloud.Link{ |
| 40 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, |
| 41 | gophercloud.Link{Href: "https://openstack.example.com/flavors/1", Rel: "bookmark"}, |
| 42 | }, |
| 43 | }, |
| 44 | Flavor{ |
| 45 | ID: 2, |
| 46 | Name: "m1.small", |
| 47 | RAM: 1024, |
| 48 | Links: []gophercloud.Link{ |
| 49 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/2", Rel: "self"}, |
| 50 | gophercloud.Link{Href: "https://openstack.example.com/flavors/2", Rel: "bookmark"}, |
| 51 | }, |
| 52 | }, |
| 53 | Flavor{ |
| 54 | ID: 3, |
| 55 | Name: "m1.medium", |
| 56 | RAM: 2048, |
| 57 | Links: []gophercloud.Link{ |
| 58 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/3", Rel: "self"}, |
| 59 | gophercloud.Link{Href: "https://openstack.example.com/flavors/3", Rel: "bookmark"}, |
| 60 | }, |
| 61 | }, |
| 62 | Flavor{ |
| 63 | ID: 4, |
| 64 | Name: "m1.large", |
| 65 | RAM: 4096, |
| 66 | Links: []gophercloud.Link{ |
| 67 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/4", Rel: "self"}, |
| 68 | gophercloud.Link{Href: "https://openstack.example.com/flavors/4", Rel: "bookmark"}, |
| 69 | }, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | th.AssertDeepEquals(t, expected, actual) |
| 74 | |
| 75 | return true, nil |
| 76 | }) |
| 77 | |
| 78 | th.AssertNoErr(t, err) |
| 79 | if pages != 1 { |
| 80 | t.Errorf("Expected one page, got %d", pages) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestGetFlavor(t *testing.T) { |
| 85 | th.SetupHTTP() |
| 86 | defer th.TeardownHTTP() |
| 87 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 88 | fixture.SetupHandler(t, resURL, "GET", "", getFlavorResp, 200) |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 89 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 90 | actual, err := Get(fake.ServiceClient(), flavorID).Extract() |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 91 | th.AssertNoErr(t, err) |
| 92 | |
| 93 | expected := &Flavor{ |
| 94 | ID: 1, |
| 95 | Name: "m1.tiny", |
| 96 | RAM: 512, |
| 97 | Links: []gophercloud.Link{ |
| 98 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, |
| 99 | }, |
| 100 | } |
| 101 | |
| 102 | th.AssertDeepEquals(t, expected, actual) |
| 103 | } |