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