blob: c484cf08df3a2ffbc37e19e356cfcb2540352c09 [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
Jon Perritt6d5561b2014-10-01 21:42:15 -05006 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilson407cfa32014-10-22 09:21:37 -04008 "github.com/rackspace/gophercloud/testhelper/client"
Jon Perritt6d5561b2014-10-01 21:42:15 -05009)
10
Jon Perritt6d5561b2014-10-01 21:42:15 -050011func TestList(t *testing.T) {
12 th.SetupHTTP()
13 defer th.TeardownHTTP()
14
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020015 MockListResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050016
Jon Perritt6d5561b2014-10-01 21:42:15 -050017 count := 0
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020018
Ash Wilson407cfa32014-10-22 09:21:37 -040019 List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt6d5561b2014-10-01 21:42:15 -050020 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{
28 Volume{
29 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
30 Name: "vol-001",
31 },
32 Volume{
33 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
34 Name: "vol-002",
35 },
36 }
37
38 th.CheckDeepEquals(t, expected, actual)
39
40 return true, nil
41 })
42
43 if count != 1 {
44 t.Errorf("Expected 1 page, got %d", count)
45 }
46}
47
Jon Perritte7017d62015-02-18 10:53:53 -070048func TestListAll(t *testing.T) {
49 th.SetupHTTP()
50 defer th.TeardownHTTP()
51
52 MockListResponse(t)
53
54 allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages()
55 th.AssertNoErr(t, err)
56 actual, err := ExtractVolumes(allPages)
57 th.AssertNoErr(t, err)
58
59 expected := []Volume{
60 Volume{
61 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
62 Name: "vol-001",
63 },
64 Volume{
65 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
66 Name: "vol-002",
67 },
68 }
69
70 th.CheckDeepEquals(t, expected, actual)
71
72}
73
Jon Perritt6d5561b2014-10-01 21:42:15 -050074func TestGet(t *testing.T) {
75 th.SetupHTTP()
76 defer th.TeardownHTTP()
77
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020078 MockGetResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050079
Ash Wilson407cfa32014-10-22 09:21:37 -040080 v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050081 th.AssertNoErr(t, err)
82
83 th.AssertEquals(t, v.Name, "vol-001")
84 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
Joe Topjianb75567d2015-02-06 04:23:16 +000085 th.AssertEquals(t, v.Attachments[0]["device"], "/dev/vde")
Jon Perritt6d5561b2014-10-01 21:42:15 -050086}
87
88func TestCreate(t *testing.T) {
89 th.SetupHTTP()
90 defer th.TeardownHTTP()
91
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020092 MockCreateResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050093
Jamie Hannaford59f22072014-10-23 17:00:59 +020094 options := &CreateOpts{Size: 75}
Ash Wilson407cfa32014-10-22 09:21:37 -040095 n, err := Create(client.ServiceClient(), options).Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050096 th.AssertNoErr(t, err)
97
Jon Perritt1c2356b2014-10-13 19:56:43 -050098 th.AssertEquals(t, n.Size, 4)
Jon Perritt6d5561b2014-10-01 21:42:15 -050099 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
100}
101
102func TestDelete(t *testing.T) {
103 th.SetupHTTP()
104 defer th.TeardownHTTP()
105
Jamie Hannaford449c4ac2014-10-21 09:58:02 +0200106 MockDeleteResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500107
Jamie Hannafordce9f9082014-10-27 11:27:12 +0100108 res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
109 th.AssertNoErr(t, res.Err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500110}
Jon Perritt84857132014-10-07 11:59:14 -0500111
112func TestUpdate(t *testing.T) {
113 th.SetupHTTP()
114 defer th.TeardownHTTP()
115
Jamie Hannaford449c4ac2014-10-21 09:58:02 +0200116 MockUpdateResponse(t)
Jon Perritt84857132014-10-07 11:59:14 -0500117
Jamie Hannaford449c4ac2014-10-21 09:58:02 +0200118 options := UpdateOpts{Name: "vol-002"}
Ash Wilson407cfa32014-10-22 09:21:37 -0400119 v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
Jon Perritt84857132014-10-07 11:59:14 -0500120 th.AssertNoErr(t, err)
121 th.CheckEquals(t, "vol-002", v.Name)
122}