feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | th "github.com/rackspace/gophercloud/testhelper" |
| 8 | "github.com/rackspace/gophercloud/testhelper/client" |
| 9 | ) |
| 10 | |
| 11 | func TestList(t *testing.T) { |
| 12 | th.SetupHTTP() |
| 13 | defer th.TeardownHTTP() |
| 14 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 15 | MockListResponse(t) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 16 | |
| 17 | count := 0 |
| 18 | |
| 19 | List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 20 | 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{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 28 | { |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 29 | ID: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 30 | Name: "vol-001", |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 31 | Attachments: []map[string]interface{}{{ |
| 32 | "AttachmentID": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa", |
| 33 | "ID": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf", |
| 34 | "VolumeID": "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 35 | "ServerID": "d1c4788b-9435-42e2-9b81-29f3be1cd01f", |
| 36 | "HostName": "stack", |
| 37 | "Device": "/dev/vdc", |
| 38 | }}, |
| 39 | AvailabilityZone: "nova", |
| 40 | Bootable: "false", |
| 41 | ConsistencyGroupID: "", |
| 42 | CreatedAt: "2015-09-17T03:35:03.000000", |
| 43 | Description: "", |
| 44 | Encrypted: false, |
| 45 | Metadata: map[string]string{"foo": "bar"}, |
| 46 | Multiattach: false, |
| 47 | TenantID: "304dc00909ac4d0da6c62d816bcb3459", |
| 48 | ReplicationDriverData: "", |
| 49 | ReplicationExtendedStatus: "", |
| 50 | ReplicationStatus: "disabled", |
| 51 | Size: 75, |
| 52 | SnapshotID: "", |
| 53 | SourceVolID: "", |
| 54 | Status: "available", |
| 55 | UserID: "ff1ce52c03ab433aaba9108c2e3ef541", |
| 56 | VolumeType: "lvmdriver-1", |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 57 | }, |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 58 | { |
| 59 | ID: "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 60 | Name: "vol-002", |
| 61 | Attachments: []map[string]interface{}{}, |
| 62 | AvailabilityZone: "nova", |
| 63 | Bootable: "false", |
| 64 | ConsistencyGroupID: "", |
| 65 | CreatedAt: "2015-09-17T03:32:29.000000", |
| 66 | Description: "", |
| 67 | Encrypted: false, |
| 68 | Metadata: map[string]string{}, |
| 69 | Multiattach: false, |
| 70 | TenantID: "304dc00909ac4d0da6c62d816bcb3459", |
| 71 | ReplicationDriverData: "", |
| 72 | ReplicationExtendedStatus: "", |
| 73 | ReplicationStatus: "disabled", |
| 74 | Size: 75, |
| 75 | SnapshotID: "", |
| 76 | SourceVolID: "", |
| 77 | Status: "available", |
| 78 | UserID: "ff1ce52c03ab433aaba9108c2e3ef541", |
| 79 | VolumeType: "lvmdriver-1", |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 80 | }, |
| 81 | } |
| 82 | |
| 83 | th.CheckDeepEquals(t, expected, actual) |
| 84 | |
| 85 | return true, nil |
| 86 | }) |
| 87 | |
| 88 | if count != 1 { |
| 89 | t.Errorf("Expected 1 page, got %d", count) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestListAll(t *testing.T) { |
| 94 | th.SetupHTTP() |
| 95 | defer th.TeardownHTTP() |
| 96 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 97 | MockListResponse(t) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 98 | |
| 99 | allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages() |
| 100 | th.AssertNoErr(t, err) |
| 101 | actual, err := ExtractVolumes(allPages) |
| 102 | th.AssertNoErr(t, err) |
| 103 | |
| 104 | expected := []Volume{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 105 | { |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 106 | ID: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 107 | Name: "vol-001", |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 108 | Attachments: []map[string]interface{}{{ |
| 109 | "AttachmentID": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa", |
| 110 | "ID": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf", |
| 111 | "VolumeID": "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 112 | "ServerID": "d1c4788b-9435-42e2-9b81-29f3be1cd01f", |
| 113 | "HostName": "stack", |
| 114 | "Device": "/dev/vdc", |
| 115 | }}, |
| 116 | AvailabilityZone: "nova", |
| 117 | Bootable: "false", |
| 118 | ConsistencyGroupID: "", |
| 119 | CreatedAt: "2015-09-17T03:35:03.000000", |
| 120 | Description: "", |
| 121 | Encrypted: false, |
| 122 | Metadata: map[string]string{"foo": "bar"}, |
| 123 | Multiattach: false, |
| 124 | TenantID: "304dc00909ac4d0da6c62d816bcb3459", |
| 125 | ReplicationDriverData: "", |
| 126 | ReplicationExtendedStatus: "", |
| 127 | ReplicationStatus: "disabled", |
| 128 | Size: 75, |
| 129 | SnapshotID: "", |
| 130 | SourceVolID: "", |
| 131 | Status: "available", |
| 132 | UserID: "ff1ce52c03ab433aaba9108c2e3ef541", |
| 133 | VolumeType: "lvmdriver-1", |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 134 | }, |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 135 | { |
| 136 | ID: "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 137 | Name: "vol-002", |
| 138 | Attachments: []map[string]interface{}{}, |
| 139 | AvailabilityZone: "nova", |
| 140 | Bootable: "false", |
| 141 | ConsistencyGroupID: "", |
| 142 | CreatedAt: "2015-09-17T03:32:29.000000", |
| 143 | Description: "", |
| 144 | Encrypted: false, |
| 145 | Metadata: map[string]string{}, |
| 146 | Multiattach: false, |
| 147 | TenantID: "304dc00909ac4d0da6c62d816bcb3459", |
| 148 | ReplicationDriverData: "", |
| 149 | ReplicationExtendedStatus: "", |
| 150 | ReplicationStatus: "disabled", |
| 151 | Size: 75, |
| 152 | SnapshotID: "", |
| 153 | SourceVolID: "", |
| 154 | Status: "available", |
| 155 | UserID: "ff1ce52c03ab433aaba9108c2e3ef541", |
| 156 | VolumeType: "lvmdriver-1", |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 157 | }, |
| 158 | } |
| 159 | |
| 160 | th.CheckDeepEquals(t, expected, actual) |
| 161 | |
| 162 | } |
| 163 | |
| 164 | func TestGet(t *testing.T) { |
| 165 | th.SetupHTTP() |
| 166 | defer th.TeardownHTTP() |
| 167 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 168 | MockGetResponse(t) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 169 | |
| 170 | v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
| 171 | th.AssertNoErr(t, err) |
| 172 | |
| 173 | th.AssertEquals(t, v.Name, "vol-001") |
| 174 | th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | func TestCreate(t *testing.T) { |
| 178 | th.SetupHTTP() |
| 179 | defer th.TeardownHTTP() |
| 180 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 181 | MockCreateResponse(t) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 182 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 183 | options := &CreateOpts{Size: 75, Name: "vol-001"} |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 184 | n, err := Create(client.ServiceClient(), options).Extract() |
| 185 | th.AssertNoErr(t, err) |
| 186 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 187 | th.AssertEquals(t, n.Size, 75) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 188 | th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 189 | } |
| 190 | |
| 191 | func TestDelete(t *testing.T) { |
| 192 | th.SetupHTTP() |
| 193 | defer th.TeardownHTTP() |
| 194 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 195 | MockDeleteResponse(t) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 196 | |
| 197 | res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 198 | th.AssertNoErr(t, res.Err) |
| 199 | } |
| 200 | |
| 201 | func TestUpdate(t *testing.T) { |
| 202 | th.SetupHTTP() |
| 203 | defer th.TeardownHTTP() |
| 204 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 205 | MockUpdateResponse(t) |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 206 | |
| 207 | options := UpdateOpts{Name: "vol-002"} |
| 208 | v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() |
| 209 | th.AssertNoErr(t, err) |
| 210 | th.CheckEquals(t, "vol-002", v.Name) |
| 211 | } |