blob: 2b9809fc53d05bdd65a3040272998b8fae1115ab [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
Jamie Hannaford8ad8c592014-10-23 16:58:27 +020020 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020021 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
Jamie Hannaford8ad8c592014-10-23 16:58:27 +020044 th.AssertEquals(t, 1, count)
45 th.AssertNoErr(t, err)
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020046}
47
48func TestGet(t *testing.T) {
49 th.SetupHTTP()
50 defer th.TeardownHTTP()
51
52 os.MockGetResponse(t)
53
54 v, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
55 th.AssertNoErr(t, err)
56
57 th.AssertEquals(t, v.Name, "vol-001")
58 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
59}
60
61func TestCreate(t *testing.T) {
62 th.SetupHTTP()
63 defer th.TeardownHTTP()
64
65 os.MockCreateResponse(t)
66
Jamie Hannafordb4151702014-10-23 14:49:14 +020067 n, err := Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 40}}).Extract()
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020068 th.AssertNoErr(t, err)
69
70 th.AssertEquals(t, n.Size, 4)
71 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
72}
73
Jamie Hannafordb4151702014-10-23 14:49:14 +020074func TestSizeRange(t *testing.T) {
75 _, err := Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 1}}).Extract()
76 if err == nil {
77 t.Fatalf("Expected error, got none")
78 }
79
80 _, err = Create(fake.ServiceClient(), CreateOpts{os.CreateOpts{Size: 2000}}).Extract()
81 if err == nil {
82 t.Fatalf("Expected error, got none")
83 }
84}
85
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020086func TestDelete(t *testing.T) {
87 th.SetupHTTP()
88 defer th.TeardownHTTP()
89
90 os.MockDeleteResponse(t)
91
92 err := Delete(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
93 th.AssertNoErr(t, err)
94}
95
96func TestUpdate(t *testing.T) {
97 th.SetupHTTP()
98 defer th.TeardownHTTP()
99
100 os.MockUpdateResponse(t)
101
102 options := &UpdateOpts{Name: "vol-002"}
103 v, err := Update(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
104 th.AssertNoErr(t, err)
105 th.CheckEquals(t, "vol-002", v.Name)
106}