feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame^] | 1 | package volumes |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | fixtures "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes/testing" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
| 12 | func TestList(t *testing.T) { |
| 13 | th.SetupHTTP() |
| 14 | defer th.TeardownHTTP() |
| 15 | |
| 16 | fixtures.MockListResponse(t) |
| 17 | |
| 18 | count := 0 |
| 19 | |
| 20 | List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 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 | |
| 49 | func TestListAll(t *testing.T) { |
| 50 | th.SetupHTTP() |
| 51 | defer th.TeardownHTTP() |
| 52 | |
| 53 | fixtures.MockListResponse(t) |
| 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 | |
| 75 | func TestGet(t *testing.T) { |
| 76 | th.SetupHTTP() |
| 77 | defer th.TeardownHTTP() |
| 78 | |
| 79 | fixtures.MockGetResponse(t) |
| 80 | |
| 81 | v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
| 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") |
| 86 | th.AssertEquals(t, v.Attachments[0]["device"], "/dev/vde") |
| 87 | } |
| 88 | |
| 89 | func TestCreate(t *testing.T) { |
| 90 | th.SetupHTTP() |
| 91 | defer th.TeardownHTTP() |
| 92 | |
| 93 | fixtures.MockCreateResponse(t) |
| 94 | |
| 95 | options := &CreateOpts{Size: 75} |
| 96 | n, err := Create(client.ServiceClient(), options).Extract() |
| 97 | th.AssertNoErr(t, err) |
| 98 | |
| 99 | th.AssertEquals(t, n.Size, 4) |
| 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 | |
| 107 | fixtures.MockDeleteResponse(t) |
| 108 | |
| 109 | res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 110 | th.AssertNoErr(t, res.Err) |
| 111 | } |
| 112 | |
| 113 | func TestUpdate(t *testing.T) { |
| 114 | th.SetupHTTP() |
| 115 | defer th.TeardownHTTP() |
| 116 | |
| 117 | fixtures.MockUpdateResponse(t) |
| 118 | |
| 119 | options := UpdateOpts{Name: "vol-002"} |
| 120 | v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() |
| 121 | th.AssertNoErr(t, err) |
| 122 | th.CheckEquals(t, "vol-002", v.Name) |
| 123 | } |