blob: 56b86bd0867a0fd09375a4953253ecfb29d7cbb1 [file] [log] [blame]
Jamie Hannaforddfc1be72014-10-20 18:58:20 +02001package volumes
Jamie Hannaford0b7d0402014-10-21 10:24:35 +02002
3import (
4 "testing"
5
6 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
7 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
12func TestList(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
16 os.MockListResponse(t)
17
18 count := 0
19
20 List(fake.ServiceClient()).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 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
Jamie Hannafordb4151702014-10-23 14:49:14 +020068 n, err := Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 40}}).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) {
76 _, err := Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 1}}).Extract()
77 if err == nil {
78 t.Fatalf("Expected error, got none")
79 }
80
81 _, err = Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 2000}}).Extract()
82 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
93 err := Delete(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
94 th.AssertNoErr(t, err)
95}
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}