github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/objectstorage/v1/containers/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 v1 "github.com/vnpaycloud-console/gophercloud/v2/openstack/objectstorage/v1" 9 "github.com/vnpaycloud-console/gophercloud/v2/openstack/objectstorage/v1/containers" 10 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 11 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 12 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 13 ) 14 15 func TestContainerNames(t *testing.T) { 16 for _, tc := range [...]struct { 17 name string 18 containerName string 19 expectedError error 20 }{ 21 { 22 "rejects_a_slash", 23 "one/two", 24 v1.ErrInvalidContainerName{}, 25 }, 26 { 27 "rejects_an_empty_string", 28 "", 29 v1.ErrEmptyContainerName{}, 30 }, 31 } { 32 t.Run(tc.name, func(t *testing.T) { 33 t.Run("create", func(t *testing.T) { 34 th.SetupHTTP() 35 defer th.TeardownHTTP() 36 HandleCreateContainerSuccessfully(t) 37 38 _, err := containers.Create(context.TODO(), fake.ServiceClient(), tc.containerName, nil).Extract() 39 th.CheckErr(t, err, &tc.expectedError) 40 }) 41 t.Run("delete", func(t *testing.T) { 42 th.SetupHTTP() 43 defer th.TeardownHTTP() 44 HandleDeleteContainerSuccessfully(t, WithPath("/")) 45 46 res := containers.Delete(context.TODO(), fake.ServiceClient(), tc.containerName) 47 th.CheckErr(t, res.Err, &tc.expectedError) 48 }) 49 t.Run("update", func(t *testing.T) { 50 th.SetupHTTP() 51 defer th.TeardownHTTP() 52 HandleUpdateContainerSuccessfully(t, WithPath("/")) 53 54 contentType := "text/plain" 55 options := &containers.UpdateOpts{ 56 Metadata: map[string]string{"foo": "bar"}, 57 ContainerWrite: new(string), 58 ContainerRead: new(string), 59 ContainerSyncTo: new(string), 60 ContainerSyncKey: new(string), 61 ContentType: &contentType, 62 } 63 res := containers.Update(context.TODO(), fake.ServiceClient(), tc.containerName, options) 64 th.CheckErr(t, res.Err, &tc.expectedError) 65 }) 66 t.Run("get", func(t *testing.T) { 67 th.SetupHTTP() 68 defer th.TeardownHTTP() 69 HandleGetContainerSuccessfully(t, WithPath("/")) 70 71 res := containers.Get(context.TODO(), fake.ServiceClient(), tc.containerName, nil) 72 _, err := res.ExtractMetadata() 73 th.CheckErr(t, err, &tc.expectedError) 74 75 _, err = res.Extract() 76 th.CheckErr(t, err, &tc.expectedError) 77 }) 78 }) 79 } 80 } 81 82 func TestListContainerInfo(t *testing.T) { 83 th.SetupHTTP() 84 defer th.TeardownHTTP() 85 HandleListContainerInfoSuccessfully(t) 86 87 count := 0 88 err := containers.List(fake.ServiceClient(), &containers.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 89 count++ 90 actual, err := containers.ExtractInfo(page) 91 th.AssertNoErr(t, err) 92 93 th.CheckDeepEquals(t, ExpectedListInfo, actual) 94 95 return true, nil 96 }) 97 th.AssertNoErr(t, err) 98 th.CheckEquals(t, 1, count) 99 } 100 101 func TestListAllContainerInfo(t *testing.T) { 102 th.SetupHTTP() 103 defer th.TeardownHTTP() 104 HandleListContainerInfoSuccessfully(t) 105 106 allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{}).AllPages(context.TODO()) 107 th.AssertNoErr(t, err) 108 actual, err := containers.ExtractInfo(allPages) 109 th.AssertNoErr(t, err) 110 th.CheckDeepEquals(t, ExpectedListInfo, actual) 111 } 112 113 func TestListContainerNames(t *testing.T) { 114 th.SetupHTTP() 115 defer th.TeardownHTTP() 116 HandleListContainerInfoSuccessfully(t) 117 118 count := 0 119 err := containers.List(fake.ServiceClient(), &containers.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 120 count++ 121 actual, err := containers.ExtractNames(page) 122 if err != nil { 123 t.Errorf("Failed to extract container names: %v", err) 124 return false, err 125 } 126 127 th.CheckDeepEquals(t, ExpectedListNames, actual) 128 129 return true, nil 130 }) 131 th.AssertNoErr(t, err) 132 th.CheckEquals(t, 1, count) 133 } 134 135 func TestListAllContainerNames(t *testing.T) { 136 th.SetupHTTP() 137 defer th.TeardownHTTP() 138 HandleListContainerInfoSuccessfully(t) 139 140 allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{}).AllPages(context.TODO()) 141 th.AssertNoErr(t, err) 142 actual, err := containers.ExtractNames(allPages) 143 th.AssertNoErr(t, err) 144 th.CheckDeepEquals(t, ExpectedListNames, actual) 145 } 146 147 func TestListZeroContainerNames(t *testing.T) { 148 th.SetupHTTP() 149 defer th.TeardownHTTP() 150 HandleListZeroContainerNames204(t) 151 152 allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{}).AllPages(context.TODO()) 153 th.AssertNoErr(t, err) 154 actual, err := containers.ExtractNames(allPages) 155 th.AssertNoErr(t, err) 156 th.CheckDeepEquals(t, []string{}, actual) 157 } 158 159 func TestCreateContainer(t *testing.T) { 160 th.SetupHTTP() 161 defer th.TeardownHTTP() 162 HandleCreateContainerSuccessfully(t) 163 164 options := containers.CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}} 165 res := containers.Create(context.TODO(), fake.ServiceClient(), "testContainer", options) 166 th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0]) 167 168 expected := &containers.CreateHeader{ 169 ContentLength: 0, 170 ContentType: "text/html; charset=UTF-8", 171 Date: time.Date(2016, time.August, 17, 19, 25, 43, 0, time.UTC), 172 TransID: "tx554ed59667a64c61866f1-0058b4ba37", 173 } 174 actual, err := res.Extract() 175 th.AssertNoErr(t, err) 176 th.AssertDeepEquals(t, expected, actual) 177 } 178 179 func TestDeleteContainer(t *testing.T) { 180 th.SetupHTTP() 181 defer th.TeardownHTTP() 182 HandleDeleteContainerSuccessfully(t) 183 184 res := containers.Delete(context.TODO(), fake.ServiceClient(), "testContainer") 185 th.AssertNoErr(t, res.Err) 186 } 187 188 func TestBulkDelete(t *testing.T) { 189 th.SetupHTTP() 190 defer th.TeardownHTTP() 191 HandleBulkDeleteSuccessfully(t) 192 193 expected := containers.BulkDeleteResponse{ 194 ResponseStatus: "foo", 195 ResponseBody: "bar", 196 NumberDeleted: 2, 197 Errors: [][]string{}, 198 } 199 200 resp, err := containers.BulkDelete(context.TODO(), fake.ServiceClient(), []string{"testContainer1", "testContainer2"}).Extract() 201 th.AssertNoErr(t, err) 202 th.AssertDeepEquals(t, expected, *resp) 203 } 204 205 func TestUpdateContainer(t *testing.T) { 206 th.SetupHTTP() 207 defer th.TeardownHTTP() 208 HandleUpdateContainerSuccessfully(t) 209 210 contentType := "text/plain" 211 options := &containers.UpdateOpts{ 212 Metadata: map[string]string{"foo": "bar"}, 213 ContainerWrite: new(string), 214 ContainerRead: new(string), 215 ContainerSyncTo: new(string), 216 ContainerSyncKey: new(string), 217 ContentType: &contentType, 218 } 219 res := containers.Update(context.TODO(), fake.ServiceClient(), "testContainer", options) 220 th.AssertNoErr(t, res.Err) 221 } 222 223 func TestGetContainer(t *testing.T) { 224 th.SetupHTTP() 225 defer th.TeardownHTTP() 226 HandleGetContainerSuccessfully(t) 227 228 getOpts := containers.GetOpts{ 229 Newest: true, 230 } 231 res := containers.Get(context.TODO(), fake.ServiceClient(), "testContainer", getOpts) 232 _, err := res.ExtractMetadata() 233 th.AssertNoErr(t, err) 234 235 expected := &containers.GetHeader{ 236 AcceptRanges: "bytes", 237 BytesUsed: 100, 238 ContentType: "application/json; charset=utf-8", 239 Date: time.Date(2016, time.August, 17, 19, 25, 43, 0, time.UTC), 240 ObjectCount: 4, 241 Read: []string{"test"}, 242 TransID: "tx554ed59667a64c61866f1-0057b4ba37", 243 Write: []string{"test2", "user4"}, 244 StoragePolicy: "test_policy", 245 Timestamp: 1471298837.95721, 246 VersionsEnabled: true, 247 SyncKey: "272465181849", 248 SyncTo: "anotherContainer", 249 } 250 actual, err := res.Extract() 251 th.AssertNoErr(t, err) 252 th.AssertDeepEquals(t, expected, actual) 253 } 254 255 func TestUpdateContainerVersioningOff(t *testing.T) { 256 th.SetupHTTP() 257 defer th.TeardownHTTP() 258 HandleUpdateContainerVersioningOff(t) 259 260 contentType := "text/plain" 261 options := &containers.UpdateOpts{ 262 Metadata: map[string]string{"foo": "bar"}, 263 ContainerWrite: new(string), 264 ContainerRead: new(string), 265 ContainerSyncTo: new(string), 266 ContainerSyncKey: new(string), 267 ContentType: &contentType, 268 VersionsEnabled: new(bool), 269 } 270 _, err := containers.Update(context.TODO(), fake.ServiceClient(), "testVersioning", options).Extract() 271 th.AssertNoErr(t, err) 272 } 273 274 func TestUpdateContainerVersioningOn(t *testing.T) { 275 th.SetupHTTP() 276 defer th.TeardownHTTP() 277 HandleUpdateContainerVersioningOn(t) 278 279 iTrue := true 280 contentType := "text/plain" 281 options := &containers.UpdateOpts{ 282 Metadata: map[string]string{"foo": "bar"}, 283 ContainerWrite: new(string), 284 ContainerRead: new(string), 285 ContainerSyncTo: new(string), 286 ContainerSyncKey: new(string), 287 ContentType: &contentType, 288 VersionsEnabled: &iTrue, 289 } 290 _, err := containers.Update(context.TODO(), fake.ServiceClient(), "testVersioning", options).Extract() 291 th.AssertNoErr(t, err) 292 }