github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v3/volumes/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/huaweicloud/golangsdk/openstack/blockstorage/extensions/volumetenants" 8 "github.com/huaweicloud/golangsdk/openstack/blockstorage/v3/volumes" 9 "github.com/huaweicloud/golangsdk/pagination" 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 "github.com/huaweicloud/golangsdk/testhelper/client" 12 ) 13 14 func TestListWithExtensions(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 MockListResponse(t) 19 20 count := 0 21 22 volumes.List(client.ServiceClient(), &volumes.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 23 count++ 24 actual, err := volumes.ExtractVolumes(page) 25 if err != nil { 26 t.Errorf("Failed to extract volumes: %v", err) 27 return false, err 28 } 29 30 expected := []volumes.Volume{ 31 { 32 ID: "289da7f8-6440-407c-9fb4-7db01ec49164", 33 Name: "vol-001", 34 Attachments: []volumes.Attachment{{ 35 ServerID: "83ec2e3b-4321-422b-8706-a84185f52a0a", 36 AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a", 37 AttachedAt: time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC), 38 HostName: "foobar", 39 VolumeID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 40 Device: "/dev/vdc", 41 ID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 42 }}, 43 AvailabilityZone: "nova", 44 Bootable: "false", 45 ConsistencyGroupID: "", 46 CreatedAt: time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC), 47 Description: "", 48 Encrypted: false, 49 Metadata: map[string]string{"foo": "bar"}, 50 Multiattach: false, 51 //TenantID: "304dc00909ac4d0da6c62d816bcb3459", 52 //ReplicationDriverData: "", 53 //ReplicationExtendedStatus: "", 54 ReplicationStatus: "disabled", 55 Size: 75, 56 SnapshotID: "", 57 SourceVolID: "", 58 Status: "available", 59 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 60 VolumeType: "lvmdriver-1", 61 }, 62 { 63 ID: "96c3bda7-c82a-4f50-be73-ca7621794835", 64 Name: "vol-002", 65 Attachments: []volumes.Attachment{}, 66 AvailabilityZone: "nova", 67 Bootable: "false", 68 ConsistencyGroupID: "", 69 CreatedAt: time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC), 70 Description: "", 71 Encrypted: false, 72 Metadata: map[string]string{}, 73 Multiattach: false, 74 //TenantID: "304dc00909ac4d0da6c62d816bcb3459", 75 //ReplicationDriverData: "", 76 //ReplicationExtendedStatus: "", 77 ReplicationStatus: "disabled", 78 Size: 75, 79 SnapshotID: "", 80 SourceVolID: "", 81 Status: "available", 82 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 83 VolumeType: "lvmdriver-1", 84 }, 85 } 86 87 th.CheckDeepEquals(t, expected, actual) 88 89 return true, nil 90 }) 91 92 if count != 1 { 93 t.Errorf("Expected 1 page, got %d", count) 94 } 95 } 96 97 func TestListAllWithExtensions(t *testing.T) { 98 th.SetupHTTP() 99 defer th.TeardownHTTP() 100 101 MockListResponse(t) 102 103 type VolumeWithExt struct { 104 volumes.Volume 105 volumetenants.VolumeTenantExt 106 } 107 108 allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages() 109 th.AssertNoErr(t, err) 110 111 var actual []VolumeWithExt 112 err = volumes.ExtractVolumesInto(allPages, &actual) 113 th.AssertNoErr(t, err) 114 th.AssertEquals(t, 2, len(actual)) 115 th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", actual[0].TenantID) 116 } 117 118 func TestListAll(t *testing.T) { 119 th.SetupHTTP() 120 defer th.TeardownHTTP() 121 122 MockListResponse(t) 123 124 allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages() 125 th.AssertNoErr(t, err) 126 actual, err := volumes.ExtractVolumes(allPages) 127 th.AssertNoErr(t, err) 128 129 expected := []volumes.Volume{ 130 { 131 ID: "289da7f8-6440-407c-9fb4-7db01ec49164", 132 Name: "vol-001", 133 Attachments: []volumes.Attachment{{ 134 ServerID: "83ec2e3b-4321-422b-8706-a84185f52a0a", 135 AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a", 136 AttachedAt: time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC), 137 HostName: "foobar", 138 VolumeID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 139 Device: "/dev/vdc", 140 ID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 141 }}, 142 AvailabilityZone: "nova", 143 Bootable: "false", 144 ConsistencyGroupID: "", 145 CreatedAt: time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC), 146 Description: "", 147 Encrypted: false, 148 Metadata: map[string]string{"foo": "bar"}, 149 Multiattach: false, 150 //TenantID: "304dc00909ac4d0da6c62d816bcb3459", 151 //ReplicationDriverData: "", 152 //ReplicationExtendedStatus: "", 153 ReplicationStatus: "disabled", 154 Size: 75, 155 SnapshotID: "", 156 SourceVolID: "", 157 Status: "available", 158 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 159 VolumeType: "lvmdriver-1", 160 }, 161 { 162 ID: "96c3bda7-c82a-4f50-be73-ca7621794835", 163 Name: "vol-002", 164 Attachments: []volumes.Attachment{}, 165 AvailabilityZone: "nova", 166 Bootable: "false", 167 ConsistencyGroupID: "", 168 CreatedAt: time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC), 169 Description: "", 170 Encrypted: false, 171 Metadata: map[string]string{}, 172 Multiattach: false, 173 //TenantID: "304dc00909ac4d0da6c62d816bcb3459", 174 //ReplicationDriverData: "", 175 //ReplicationExtendedStatus: "", 176 ReplicationStatus: "disabled", 177 Size: 75, 178 SnapshotID: "", 179 SourceVolID: "", 180 Status: "available", 181 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 182 VolumeType: "lvmdriver-1", 183 }, 184 } 185 186 th.CheckDeepEquals(t, expected, actual) 187 188 } 189 190 func TestGet(t *testing.T) { 191 th.SetupHTTP() 192 defer th.TeardownHTTP() 193 194 MockGetResponse(t) 195 196 v, err := volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() 197 th.AssertNoErr(t, err) 198 199 th.AssertEquals(t, v.Name, "vol-001") 200 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 201 } 202 203 func TestCreate(t *testing.T) { 204 th.SetupHTTP() 205 defer th.TeardownHTTP() 206 207 MockCreateResponse(t) 208 209 options := &volumes.CreateOpts{Size: 75, Name: "vol-001"} 210 n, err := volumes.Create(client.ServiceClient(), options).Extract() 211 th.AssertNoErr(t, err) 212 213 th.AssertEquals(t, n.Size, 75) 214 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 215 } 216 217 func TestDelete(t *testing.T) { 218 th.SetupHTTP() 219 defer th.TeardownHTTP() 220 221 MockDeleteResponse(t) 222 223 res := volumes.Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") 224 th.AssertNoErr(t, res.Err) 225 } 226 227 func TestUpdate(t *testing.T) { 228 th.SetupHTTP() 229 defer th.TeardownHTTP() 230 231 MockUpdateResponse(t) 232 233 options := volumes.UpdateOpts{Name: "vol-002"} 234 v, err := volumes.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() 235 th.AssertNoErr(t, err) 236 th.CheckEquals(t, "vol-002", v.Name) 237 } 238 239 func TestGetWithExtensions(t *testing.T) { 240 th.SetupHTTP() 241 defer th.TeardownHTTP() 242 243 MockGetResponse(t) 244 245 var s struct { 246 volumes.Volume 247 volumetenants.VolumeTenantExt 248 } 249 err := volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&s) 250 th.AssertNoErr(t, err) 251 th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", s.TenantID) 252 253 err = volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(s) 254 if err == nil { 255 t.Errorf("Expected error when providing non-pointer struct") 256 } 257 }