blob: b6831f211489d721152d242b1d8f8912684cdcde [file] [log] [blame]
Jamie Hannaforddfc1be72014-10-20 18:58:20 +02001package volumes
Jamie Hannaford0b7d0402014-10-21 10:24:35 +02002
3import (
4 "testing"
5
Sreekanth Pothanis07400f32015-09-08 00:26:14 -07006 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
7 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes/testing"
Jamie Hannaford0b7d0402014-10-21 10:24:35 +02008 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 os.MockListResponse(t)
18
19 count := 0
20
Jamie Hannaford8ad8c592014-10-23 16:58:27 +020021 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020022 count++
23 actual, err := ExtractVolumes(page)
24 if err != nil {
25 t.Errorf("Failed to extract volumes: %v", err)
26 return false, err
27 }
28
29 expected := []Volume{
30 Volume{
31 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
32 Name: "vol-001",
33 },
34 Volume{
35 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
36 Name: "vol-002",
37 },
38 }
39
40 th.CheckDeepEquals(t, expected, actual)
41
42 return true, nil
43 })
44
Jamie Hannaford8ad8c592014-10-23 16:58:27 +020045 th.AssertEquals(t, 1, count)
46 th.AssertNoErr(t, err)
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020047}
48
49func TestGet(t *testing.T) {
50 th.SetupHTTP()
51 defer th.TeardownHTTP()
52
53 os.MockGetResponse(t)
54
55 v, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
56 th.AssertNoErr(t, err)
57
58 th.AssertEquals(t, v.Name, "vol-001")
59 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
60}
61
62func TestCreate(t *testing.T) {
63 th.SetupHTTP()
64 defer th.TeardownHTTP()
65
66 os.MockCreateResponse(t)
67
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070068 n, err := Create(fake.ServiceClient(), CreateOpts{volumes.CreateOpts{Size: 75}}).Extract()
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020069 th.AssertNoErr(t, err)
70
71 th.AssertEquals(t, n.Size, 4)
72 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
73}
74
Jamie Hannafordb4151702014-10-23 14:49:14 +020075func TestSizeRange(t *testing.T) {
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070076 _, err := Create(fake.ServiceClient(), CreateOpts{volumes.CreateOpts{Size: 1}}).Extract()
Jamie Hannafordb4151702014-10-23 14:49:14 +020077 if err == nil {
78 t.Fatalf("Expected error, got none")
79 }
80
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070081 _, err = Create(fake.ServiceClient(), CreateOpts{volumes.CreateOpts{Size: 2000}}).Extract()
Jamie Hannafordb4151702014-10-23 14:49:14 +020082 if err == nil {
83 t.Fatalf("Expected error, got none")
84 }
85}
86
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020087func TestDelete(t *testing.T) {
88 th.SetupHTTP()
89 defer th.TeardownHTTP()
90
91 os.MockDeleteResponse(t)
92
Jamie Hannafordf6398642014-10-27 11:40:15 +010093 res := Delete(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
94 th.AssertNoErr(t, res.Err)
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020095}
96
97func TestUpdate(t *testing.T) {
98 th.SetupHTTP()
99 defer th.TeardownHTTP()
100
101 os.MockUpdateResponse(t)
102
103 options := &UpdateOpts{Name: "vol-002"}
104 v, err := Update(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
105 th.AssertNoErr(t, err)
106 th.CheckEquals(t, "vol-002", v.Name)
107}