1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/******************************************************************************
*
* Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package tests
import (
"context"
"fmt"
"strings"
"testing"
"go.xyrillian.de/schwift/v2"
"go.xyrillian.de/schwift/v2/capabilities"
)
func TestBulkDeleteSuccess(t *testing.T) {
testWithAccount(t, func(a *schwift.Account) {
testWithAndWithoutBulkDeleteSupport(func() {
c, err := a.Container("schwift-test-bulkdelete").EnsureExists(context.TODO())
expectSuccess(t, err)
objs, err := createTestObjects(c)
expectSuccess(t, err)
numDeleted, numNotFound, err := c.Account().BulkDelete(context.TODO(), objs, nil, nil)
expectSuccess(t, err)
expectInt(t, numDeleted, len(objs))
expectInt(t, numNotFound, 0)
expectContainerExistence(t, c, true)
numDeleted, numNotFound, err = c.Account().BulkDelete(context.TODO(), objs, nil, nil)
expectSuccess(t, err)
expectInt(t, numDeleted, 0)
expectInt(t, numNotFound, len(objs))
expectContainerExistence(t, c, true)
objs, err = createTestObjects(c)
expectSuccess(t, err)
cs := []*schwift.Container{c}
numDeleted, numNotFound, err = c.Account().BulkDelete(context.TODO(), objs, cs, nil)
expectSuccess(t, err)
expectInt(t, numDeleted, len(objs)+1)
expectInt(t, numNotFound, 0)
expectContainerExistence(t, c, false)
})
})
}
func TestBulkDeleteError(t *testing.T) {
testWithContainer(t, func(c *schwift.Container) {
testWithAndWithoutBulkDeleteSupport(func() {
objs, err := createTestObjects(c)
expectSuccess(t, err)
objs = objs[1:]
cs := []*schwift.Container{c}
// not deleting all objects should lead to 409 Conflict when deleting the Container
// (NOTE: actual Swift returns 400 here although I don't understand why
// even after reading its code)
numDeleted, numNotFound, err := c.Account().BulkDelete(context.TODO(), objs, cs, nil)
expectInt(t, numDeleted, len(objs))
expectInt(t, numNotFound, 0)
t.Logf("err = %#v\n", err)
if err.Error() != "400 Bad Request (+1 object errors)" {
expectError(t, err, "409 Conflict: Conflict (+1 object errors)")
}
expectContainerExistence(t, c, true)
})
})
}
func createTestObjects(c *schwift.Container) ([]*schwift.Object, error) {
var objs []*schwift.Object
for idx := 1; idx <= 5; idx++ {
obj := c.Object(fmt.Sprintf("object%d", idx))
err := obj.Upload(context.TODO(), strings.NewReader("example"), nil, nil)
if err != nil {
return nil, err
}
objs = append(objs, obj)
}
return objs, nil
}
func testWithAndWithoutBulkDeleteSupport(action func()) {
capabilities.AllowBulkDelete = false
action()
capabilities.AllowBulkDelete = true
action()
}
|