blob: 11b950ea2feab39ced85d887e4975690b13b0545 [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
48func TestGet(t *testing.T) {
49 th.SetupHTTP()
50 defer th.TeardownHTTP()
51
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020052 MockGetResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050053
Ash Wilson407cfa32014-10-22 09:21:37 -040054 v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050055 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
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020065 MockCreateResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050066
Jamie Hannaford59f22072014-10-23 17:00:59 +020067 options := &CreateOpts{Size: 75}
Ash Wilson407cfa32014-10-22 09:21:37 -040068 n, err := Create(client.ServiceClient(), options).Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050069 th.AssertNoErr(t, err)
70
Jon Perritt1c2356b2014-10-13 19:56:43 -050071 th.AssertEquals(t, n.Size, 4)
Jon Perritt6d5561b2014-10-01 21:42:15 -050072 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
73}
74
75func TestDelete(t *testing.T) {
76 th.SetupHTTP()
77 defer th.TeardownHTTP()
78
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020079 MockDeleteResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050080
Ash Wilson407cfa32014-10-22 09:21:37 -040081 err := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
Jon Perritt57ba7632014-10-02 20:32:22 -050082 th.AssertNoErr(t, err)
Jon Perritt6d5561b2014-10-01 21:42:15 -050083}
Jon Perritt84857132014-10-07 11:59:14 -050084
85func TestUpdate(t *testing.T) {
86 th.SetupHTTP()
87 defer th.TeardownHTTP()
88
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020089 MockUpdateResponse(t)
Jon Perritt84857132014-10-07 11:59:14 -050090
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020091 options := UpdateOpts{Name: "vol-002"}
Ash Wilson407cfa32014-10-22 09:21:37 -040092 v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
Jon Perritt84857132014-10-07 11:59:14 -050093 th.AssertNoErr(t, err)
94 th.CheckEquals(t, "vol-002", v.Name)
95}