blob: 436cfdce2251a318db1c20a801a2b7000b2039bc [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"
Jon Perritt12395212016-02-24 10:41:17 -06005 "time"
Jon Perritt6d5561b2014-10-01 21:42:15 -05006
Jon Perritt12395212016-02-24 10:41:17 -06007 "github.com/gophercloud/gophercloud"
Jon Perritt27249f42016-02-18 10:35:59 -06008 fixtures "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes/testing"
9 "github.com/gophercloud/gophercloud/pagination"
10 th "github.com/gophercloud/gophercloud/testhelper"
11 "github.com/gophercloud/gophercloud/testhelper/client"
Jon Perritt6d5561b2014-10-01 21:42:15 -050012)
13
Jon Perritt6d5561b2014-10-01 21:42:15 -050014func TestList(t *testing.T) {
15 th.SetupHTTP()
16 defer th.TeardownHTTP()
17
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070018 fixtures.MockListResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050019
Jon Perritt6d5561b2014-10-01 21:42:15 -050020 count := 0
Jamie Hannaford449c4ac2014-10-21 09:58:02 +020021
Ash Wilson407cfa32014-10-22 09:21:37 -040022 List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt6d5561b2014-10-01 21:42:15 -050023 count++
24 actual, err := ExtractVolumes(page)
25 if err != nil {
26 t.Errorf("Failed to extract volumes: %v", err)
27 return false, err
28 }
29
30 expected := []Volume{
31 Volume{
32 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
33 Name: "vol-001",
34 },
35 Volume{
36 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
37 Name: "vol-002",
38 },
39 }
40
41 th.CheckDeepEquals(t, expected, actual)
42
43 return true, nil
44 })
45
46 if count != 1 {
47 t.Errorf("Expected 1 page, got %d", count)
48 }
49}
50
Jon Perritte7017d62015-02-18 10:53:53 -070051func TestListAll(t *testing.T) {
52 th.SetupHTTP()
53 defer th.TeardownHTTP()
54
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070055 fixtures.MockListResponse(t)
Jon Perritte7017d62015-02-18 10:53:53 -070056
57 allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages()
58 th.AssertNoErr(t, err)
59 actual, err := ExtractVolumes(allPages)
60 th.AssertNoErr(t, err)
61
62 expected := []Volume{
63 Volume{
64 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
65 Name: "vol-001",
66 },
67 Volume{
68 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
69 Name: "vol-002",
70 },
71 }
72
73 th.CheckDeepEquals(t, expected, actual)
74
75}
76
Jon Perritt6d5561b2014-10-01 21:42:15 -050077func TestGet(t *testing.T) {
78 th.SetupHTTP()
79 defer th.TeardownHTTP()
80
Sreekanth Pothanis07400f32015-09-08 00:26:14 -070081 fixtures.MockGetResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050082
Jon Perritt12395212016-02-24 10:41:17 -060083 actual, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050084 th.AssertNoErr(t, err)
85
Jon Perritt12395212016-02-24 10:41:17 -060086 expected := &Volume{
87 Status: "active",
88 Name: "vol-001",
89 Attachments: []map[string]interface{}{
90 {
91 "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
92 "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
93 "volume_id": "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe",
94 "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
95 "host_name": "mitaka",
96 "device": "/",
97 },
98 },
99 AvailabilityZone: "us-east1",
100 Bootable: "false",
101 CreatedAt: gophercloud.JSONRFC3339Milli(time.Date(2012, 2, 14, 20, 53, 07, 0, time.UTC)),
102 Description: "Another volume.",
103 VolumeType: "289da7f8-6440-407c-9fb4-7db01ec49164",
104 SnapshotID: "",
105 SourceVolID: "",
106 Metadata: map[string]string{
107 "contents": "junk",
108 },
109 ID: "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
110 Size: 30,
111 }
112
113 th.AssertDeepEquals(t, expected, actual)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500114}
115
116func TestCreate(t *testing.T) {
117 th.SetupHTTP()
118 defer th.TeardownHTTP()
119
Sreekanth Pothanis07400f32015-09-08 00:26:14 -0700120 fixtures.MockCreateResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500121
Jamie Hannaford59f22072014-10-23 17:00:59 +0200122 options := &CreateOpts{Size: 75}
Ash Wilson407cfa32014-10-22 09:21:37 -0400123 n, err := Create(client.ServiceClient(), options).Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500124 th.AssertNoErr(t, err)
125
Jon Perritt1c2356b2014-10-13 19:56:43 -0500126 th.AssertEquals(t, n.Size, 4)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500127 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
128}
129
130func TestDelete(t *testing.T) {
131 th.SetupHTTP()
132 defer th.TeardownHTTP()
133
Sreekanth Pothanis07400f32015-09-08 00:26:14 -0700134 fixtures.MockDeleteResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500135
Jamie Hannafordce9f9082014-10-27 11:27:12 +0100136 res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
137 th.AssertNoErr(t, res.Err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500138}
Jon Perritt84857132014-10-07 11:59:14 -0500139
140func TestUpdate(t *testing.T) {
141 th.SetupHTTP()
142 defer th.TeardownHTTP()
143
Sreekanth Pothanis07400f32015-09-08 00:26:14 -0700144 fixtures.MockUpdateResponse(t)
Jon Perritt84857132014-10-07 11:59:14 -0500145
Jamie Hannaford449c4ac2014-10-21 09:58:02 +0200146 options := UpdateOpts{Name: "vol-002"}
Ash Wilson407cfa32014-10-22 09:21:37 -0400147 v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
Jon Perritt84857132014-10-07 11:59:14 -0500148 th.AssertNoErr(t, err)
149 th.CheckEquals(t, "vol-002", v.Name)
150}