-
Notifications
You must be signed in to change notification settings - Fork 0
/
storager_list.go
113 lines (90 loc) · 2.14 KB
/
storager_list.go
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
106
107
108
109
110
111
112
113
package tests
import (
"errors"
"fmt"
"io"
"math/rand"
"github.com/google/uuid"
"github.com/stretchr/testify/suite"
ps "github.com/fastone-open/go-storage/pairs"
"github.com/fastone-open/go-storage/pkg/randbytes"
"github.com/fastone-open/go-storage/types"
)
type storageListSuite struct {
suite.Suite
p *StorageSuite
base string
length int
paths []string
}
func (s *storageListSuite) SetupTest() {
size := rand.Int63n(256)
s.length = rand.Intn(16)
s.base = uuid.NewString()
s.paths = make([]string, s.length)
for i := 0; i < s.length; i++ {
s.paths[i] = fmt.Sprintf("%s/%s", s.base, uuid.NewString())
_, err := s.p.store.Write(s.paths[i],
io.LimitReader(randbytes.NewRand(), size), size)
s.NoError(err)
}
}
func (s *storageListSuite) TearDownTest() {
for i := 0; i < s.length; i++ {
err := s.p.store.Delete(s.paths[i])
s.NoError(err)
}
}
func (s *storageListSuite) TestList() {
it, err := s.p.store.List(s.base, ps.WithListMode(types.ListModeDir))
s.NoError(err)
s.NotNil(it)
paths := make([]string, 0)
for {
o, err := it.Next()
if errors.Is(err, types.IterateDone) {
break
}
s.NoError(err)
paths = append(paths, o.Path)
}
s.ElementsMatch(s.paths, paths)
}
func (s *storageListSuite) TestListWithoutListMode() {
it, err := s.p.store.List(s.base)
s.NoError(err)
s.NotNil(it)
paths := make([]string, 0)
for {
o, err := it.Next()
if errors.Is(err, types.IterateDone) {
break
}
s.NoError(err)
paths = append(paths, o.Path)
}
s.ElementsMatch(s.paths, paths)
}
func (s *storageListSuite) TestListEmptyDir() {
if !s.p.store.Features().CreateDir {
s.T().Skipf("store doesn't support CreateDir, skip TestListEmptyDir.")
}
path := uuid.New().String()
virtualDir := s.p.store.Features().VirtualDir
if !virtualDir {
_, err := s.p.store.CreateDir(path)
s.NoError(err)
}
defer func(isVirtualDir bool) {
if !isVirtualDir {
err := s.p.store.Delete(path, ps.WithObjectMode(types.ModeDir))
s.NoError(err)
}
}(virtualDir)
it, err := s.p.store.List(path, ps.WithListMode(types.ListModeDir))
s.NoError(err)
s.NotNil(it)
o, err := it.Next()
s.ErrorIs(err, types.IterateDone)
s.Nil(o)
}