blob: 75c2bbc59657a789f8fb06d680a5ee0b13366453 [file] [log] [blame]
feiskyda546142015-09-17 12:28:23 +08001package volumes
2
3import (
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
12func 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
49func 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
75func 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
89func 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
103func 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
113func 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}