blob: 75c2bbc59657a789f8fb06d680a5ee0b13366453 [file] [log] [blame]
Jon Perritte747a0f2014-09-29 19:54:55 -05001package volumes
Jon Perritt6d5561b2014-10-01 21:42:15 -05002
3import (
Jon Perritt6d5561b2014-10-01 21:42:15 -05004 "testing"
5
Sreekanth Pothanis07400f32015-09-08 00:26:14 -07006 fixtures "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes/testing"
Jon Perritt6d5561b2014-10-01 21:42:15 -05007 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilson407cfa32014-10-22 09:21:37 -04009 "github.com/rackspace/gophercloud/testhelper/client"
Jon Perritt6d5561b2014-10-01 21:42:15 -050010)
11
Jon Perritt6d5561b2014-10-01 21:42:15 -050012func TestList(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070016 fixtures.MockListResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050017
Jon Perritt6d5561b2014-10-01 21:42:15 -050018 count := 0
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020019
Ash Wilson407cfa32014-10-22 09:21:37 -040020 List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt6d5561b2014-10-01 21:42:15 -050021 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
Jon Perritte7017d62015-02-18 10:53:53 -070049func TestListAll(t *testing.T) {
50 th.SetupHTTP()
51 defer th.TeardownHTTP()
52
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070053 fixtures.MockListResponse(t)
Jon Perritte7017d62015-02-18 10:53:53 -070054
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
Jon Perritt6d5561b2014-10-01 21:42:15 -050075func TestGet(t *testing.T) {
76 th.SetupHTTP()
77 defer th.TeardownHTTP()
78
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070079 fixtures.MockGetResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050080
Ash Wilson407cfa32014-10-22 09:21:37 -040081 v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050082 th.AssertNoErr(t, err)
83
84 th.AssertEquals(t, v.Name, "vol-001")
85 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
Joe Topjianb75567d2015-02-06 04:23:16 +000086 th.AssertEquals(t, v.Attachments[0]["device"], "/dev/vde")
Jon Perritt6d5561b2014-10-01 21:42:15 -050087}
88
89func TestCreate(t *testing.T) {
90 th.SetupHTTP()
91 defer th.TeardownHTTP()
92
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070093 fixtures.MockCreateResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050094
Jamie Hannaford59f22072014-10-23 17:00:59 +020095 options := &CreateOpts{Size: 75}
Ash Wilson407cfa32014-10-22 09:21:37 -040096 n, err := Create(client.ServiceClient(), options).Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050097 th.AssertNoErr(t, err)
98
Jon Perritt1c2356b2014-10-13 19:56:43 -050099 th.AssertEquals(t, n.Size, 4)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500100 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
Sreekanth Pothanis07400f32015-09-08 00:26:14 -0700107 fixtures.MockDeleteResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500108
Jamie Hannafordce9f9082014-10-27 11:27:12 +0100109 res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
110 th.AssertNoErr(t, res.Err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500111}
Jon Perritt84857132014-10-07 11:59:14 -0500112
113func TestUpdate(t *testing.T) {
114 th.SetupHTTP()
115 defer th.TeardownHTTP()
116
Sreekanth Pothanis07400f32015-09-08 00:26:14 -0700117 fixtures.MockUpdateResponse(t)
Jon Perritt84857132014-10-07 11:59:14 -0500118
Jamie Hannaford449c4ac2014-10-21 09:58:02 +0200119 options := UpdateOpts{Name: "vol-002"}
Ash Wilson407cfa32014-10-22 09:21:37 -0400120 v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
Jon Perritt84857132014-10-07 11:59:14 -0500121 th.AssertNoErr(t, err)
122 th.CheckEquals(t, "vol-002", v.Name)
123}