Jon Perritt | e747a0f | 2014-09-29 19:54:55 -0500 | [diff] [blame] | 1 | package volumes |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 4 | "testing" |
| 5 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | th "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 11 | func TestList(t *testing.T) { |
| 12 | th.SetupHTTP() |
| 13 | defer th.TeardownHTTP() |
| 14 | |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 15 | MockListResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 16 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 17 | count := 0 |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 18 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 19 | List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 20 | count++ |
| 21 | actual, err := ExtractVolumes(page) |
| 22 | if err != nil { |
| 23 | t.Errorf("Failed to extract volumes: %v", err) |
| 24 | return false, err |
| 25 | } |
| 26 | |
| 27 | expected := []Volume{ |
| 28 | Volume{ |
| 29 | ID: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 30 | Name: "vol-001", |
| 31 | }, |
| 32 | Volume{ |
| 33 | ID: "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 34 | Name: "vol-002", |
| 35 | }, |
| 36 | } |
| 37 | |
| 38 | th.CheckDeepEquals(t, expected, actual) |
| 39 | |
| 40 | return true, nil |
| 41 | }) |
| 42 | |
| 43 | if count != 1 { |
| 44 | t.Errorf("Expected 1 page, got %d", count) |
| 45 | } |
| 46 | } |
| 47 | |
Jon Perritt | e7017d6 | 2015-02-18 10:53:53 -0700 | [diff] [blame] | 48 | func TestListAll(t *testing.T) { |
| 49 | th.SetupHTTP() |
| 50 | defer th.TeardownHTTP() |
| 51 | |
| 52 | MockListResponse(t) |
| 53 | |
| 54 | allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages() |
| 55 | th.AssertNoErr(t, err) |
| 56 | actual, err := ExtractVolumes(allPages) |
| 57 | th.AssertNoErr(t, err) |
| 58 | |
| 59 | expected := []Volume{ |
| 60 | Volume{ |
| 61 | ID: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 62 | Name: "vol-001", |
| 63 | }, |
| 64 | Volume{ |
| 65 | ID: "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 66 | Name: "vol-002", |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | th.CheckDeepEquals(t, expected, actual) |
| 71 | |
| 72 | } |
| 73 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 74 | func TestGet(t *testing.T) { |
| 75 | th.SetupHTTP() |
| 76 | defer th.TeardownHTTP() |
| 77 | |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 78 | MockGetResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 79 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 80 | v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 81 | th.AssertNoErr(t, err) |
| 82 | |
| 83 | th.AssertEquals(t, v.Name, "vol-001") |
| 84 | th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
Joe Topjian | b75567d | 2015-02-06 04:23:16 +0000 | [diff] [blame] | 85 | th.AssertEquals(t, v.Attachments[0]["device"], "/dev/vde") |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func TestCreate(t *testing.T) { |
| 89 | th.SetupHTTP() |
| 90 | defer th.TeardownHTTP() |
| 91 | |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 92 | MockCreateResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 93 | |
Jamie Hannaford | 59f2207 | 2014-10-23 17:00:59 +0200 | [diff] [blame] | 94 | options := &CreateOpts{Size: 75} |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 95 | n, err := Create(client.ServiceClient(), options).Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 96 | th.AssertNoErr(t, err) |
| 97 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 98 | th.AssertEquals(t, n.Size, 4) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 99 | th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 100 | } |
| 101 | |
| 102 | func TestDelete(t *testing.T) { |
| 103 | th.SetupHTTP() |
| 104 | defer th.TeardownHTTP() |
| 105 | |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 106 | MockDeleteResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 107 | |
Jamie Hannaford | ce9f908 | 2014-10-27 11:27:12 +0100 | [diff] [blame] | 108 | res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 109 | th.AssertNoErr(t, res.Err) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 110 | } |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 111 | |
| 112 | func TestUpdate(t *testing.T) { |
| 113 | th.SetupHTTP() |
| 114 | defer th.TeardownHTTP() |
| 115 | |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 116 | MockUpdateResponse(t) |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 117 | |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 118 | options := UpdateOpts{Name: "vol-002"} |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 119 | v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 120 | th.AssertNoErr(t, err) |
| 121 | th.CheckEquals(t, "vol-002", v.Name) |
| 122 | } |