blob: 962d94bb1f1726fdc2b297f5776b55d964587c07 [file] [log] [blame]
feiskyda546142015-09-17 12:28:23 +08001package volumes
2
3import (
4 "testing"
5
feiskyda546142015-09-17 12:28:23 +08006 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
8 "github.com/rackspace/gophercloud/testhelper/client"
9)
10
11func TestList(t *testing.T) {
12 th.SetupHTTP()
13 defer th.TeardownHTTP()
14
feiskycf0c7fe2015-11-05 22:06:17 +080015 MockListResponse(t)
feiskyda546142015-09-17 12:28:23 +080016
17 count := 0
18
19 List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
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{
feiskycf0c7fe2015-11-05 22:06:17 +080028 {
feiskyda546142015-09-17 12:28:23 +080029 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
30 Name: "vol-001",
feiskycf0c7fe2015-11-05 22:06:17 +080031 Attachments: []map[string]interface{}{{
32 "AttachmentID": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
33 "ID": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
34 "VolumeID": "289da7f8-6440-407c-9fb4-7db01ec49164",
35 "ServerID": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
36 "HostName": "stack",
37 "Device": "/dev/vdc",
38 }},
39 AvailabilityZone: "nova",
40 Bootable: "false",
41 ConsistencyGroupID: "",
42 CreatedAt: "2015-09-17T03:35:03.000000",
43 Description: "",
44 Encrypted: false,
45 Metadata: map[string]string{"foo": "bar"},
46 Multiattach: false,
47 TenantID: "304dc00909ac4d0da6c62d816bcb3459",
48 ReplicationDriverData: "",
49 ReplicationExtendedStatus: "",
50 ReplicationStatus: "disabled",
51 Size: 75,
52 SnapshotID: "",
53 SourceVolID: "",
54 Status: "available",
55 UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
56 VolumeType: "lvmdriver-1",
feiskyda546142015-09-17 12:28:23 +080057 },
feiskycf0c7fe2015-11-05 22:06:17 +080058 {
59 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
60 Name: "vol-002",
61 Attachments: []map[string]interface{}{},
62 AvailabilityZone: "nova",
63 Bootable: "false",
64 ConsistencyGroupID: "",
65 CreatedAt: "2015-09-17T03:32:29.000000",
66 Description: "",
67 Encrypted: false,
68 Metadata: map[string]string{},
69 Multiattach: false,
70 TenantID: "304dc00909ac4d0da6c62d816bcb3459",
71 ReplicationDriverData: "",
72 ReplicationExtendedStatus: "",
73 ReplicationStatus: "disabled",
74 Size: 75,
75 SnapshotID: "",
76 SourceVolID: "",
77 Status: "available",
78 UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
79 VolumeType: "lvmdriver-1",
feiskyda546142015-09-17 12:28:23 +080080 },
81 }
82
83 th.CheckDeepEquals(t, expected, actual)
84
85 return true, nil
86 })
87
88 if count != 1 {
89 t.Errorf("Expected 1 page, got %d", count)
90 }
91}
92
93func TestListAll(t *testing.T) {
94 th.SetupHTTP()
95 defer th.TeardownHTTP()
96
feiskycf0c7fe2015-11-05 22:06:17 +080097 MockListResponse(t)
feiskyda546142015-09-17 12:28:23 +080098
99 allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages()
100 th.AssertNoErr(t, err)
101 actual, err := ExtractVolumes(allPages)
102 th.AssertNoErr(t, err)
103
104 expected := []Volume{
feiskycf0c7fe2015-11-05 22:06:17 +0800105 {
feiskyda546142015-09-17 12:28:23 +0800106 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
107 Name: "vol-001",
feiskycf0c7fe2015-11-05 22:06:17 +0800108 Attachments: []map[string]interface{}{{
109 "AttachmentID": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
110 "ID": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
111 "VolumeID": "289da7f8-6440-407c-9fb4-7db01ec49164",
112 "ServerID": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
113 "HostName": "stack",
114 "Device": "/dev/vdc",
115 }},
116 AvailabilityZone: "nova",
117 Bootable: "false",
118 ConsistencyGroupID: "",
119 CreatedAt: "2015-09-17T03:35:03.000000",
120 Description: "",
121 Encrypted: false,
122 Metadata: map[string]string{"foo": "bar"},
123 Multiattach: false,
124 TenantID: "304dc00909ac4d0da6c62d816bcb3459",
125 ReplicationDriverData: "",
126 ReplicationExtendedStatus: "",
127 ReplicationStatus: "disabled",
128 Size: 75,
129 SnapshotID: "",
130 SourceVolID: "",
131 Status: "available",
132 UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
133 VolumeType: "lvmdriver-1",
feiskyda546142015-09-17 12:28:23 +0800134 },
feiskycf0c7fe2015-11-05 22:06:17 +0800135 {
136 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
137 Name: "vol-002",
138 Attachments: []map[string]interface{}{},
139 AvailabilityZone: "nova",
140 Bootable: "false",
141 ConsistencyGroupID: "",
142 CreatedAt: "2015-09-17T03:32:29.000000",
143 Description: "",
144 Encrypted: false,
145 Metadata: map[string]string{},
146 Multiattach: false,
147 TenantID: "304dc00909ac4d0da6c62d816bcb3459",
148 ReplicationDriverData: "",
149 ReplicationExtendedStatus: "",
150 ReplicationStatus: "disabled",
151 Size: 75,
152 SnapshotID: "",
153 SourceVolID: "",
154 Status: "available",
155 UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
156 VolumeType: "lvmdriver-1",
feiskyda546142015-09-17 12:28:23 +0800157 },
158 }
159
160 th.CheckDeepEquals(t, expected, actual)
161
162}
163
164func TestGet(t *testing.T) {
165 th.SetupHTTP()
166 defer th.TeardownHTTP()
167
feiskycf0c7fe2015-11-05 22:06:17 +0800168 MockGetResponse(t)
feiskyda546142015-09-17 12:28:23 +0800169
170 v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
171 th.AssertNoErr(t, err)
172
173 th.AssertEquals(t, v.Name, "vol-001")
174 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
feiskyda546142015-09-17 12:28:23 +0800175}
176
177func TestCreate(t *testing.T) {
178 th.SetupHTTP()
179 defer th.TeardownHTTP()
180
feiskycf0c7fe2015-11-05 22:06:17 +0800181 MockCreateResponse(t)
feiskyda546142015-09-17 12:28:23 +0800182
feiskycf0c7fe2015-11-05 22:06:17 +0800183 options := &CreateOpts{Size: 75, Name: "vol-001"}
feiskyda546142015-09-17 12:28:23 +0800184 n, err := Create(client.ServiceClient(), options).Extract()
185 th.AssertNoErr(t, err)
186
feiskycf0c7fe2015-11-05 22:06:17 +0800187 th.AssertEquals(t, n.Size, 75)
feiskyda546142015-09-17 12:28:23 +0800188 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
189}
190
191func TestDelete(t *testing.T) {
192 th.SetupHTTP()
193 defer th.TeardownHTTP()
194
feiskycf0c7fe2015-11-05 22:06:17 +0800195 MockDeleteResponse(t)
feiskyda546142015-09-17 12:28:23 +0800196
197 res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
198 th.AssertNoErr(t, res.Err)
199}
200
201func TestUpdate(t *testing.T) {
202 th.SetupHTTP()
203 defer th.TeardownHTTP()
204
feiskycf0c7fe2015-11-05 22:06:17 +0800205 MockUpdateResponse(t)
feiskyda546142015-09-17 12:28:23 +0800206
207 options := UpdateOpts{Name: "vol-002"}
208 v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
209 th.AssertNoErr(t, err)
210 th.CheckEquals(t, "vol-002", v.Name)
211}