github.com/grafana/pyroscope@v1.18.0/pkg/objstore/providers/filesystem/bucket_client_test.go (about) 1 package filesystem 2 3 import ( 4 "bytes" 5 "context" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 "github.com/thanos-io/objstore" 10 ) 11 12 type testIterCase struct { 13 prefix string 14 expected []string 15 options []objstore.IterOption 16 } 17 18 func (t testIterCase) name() string { 19 p := new(objstore.IterParams) 20 for _, opt := range t.options { 21 opt.Apply(p) 22 } 23 if p.Recursive { 24 return t.prefix + "#recursive" 25 } 26 return t.prefix 27 } 28 29 func TestIter(t *testing.T) { 30 bkt, err := NewBucket(t.TempDir()) 31 require.NoError(t, err) 32 defer bkt.Close() 33 34 buff := bytes.NewBufferString("foo") 35 require.NoError(t, bkt.Upload(context.Background(), "foo/bar/buz1", buff)) 36 require.NoError(t, bkt.Upload(context.Background(), "foo/bar/buz2", buff)) 37 require.NoError(t, bkt.Upload(context.Background(), "foo/ba/buzz3", buff)) 38 require.NoError(t, bkt.Upload(context.Background(), "foo/buzz4", buff)) 39 require.NoError(t, bkt.Upload(context.Background(), "foo/buzz5", buff)) 40 require.NoError(t, bkt.Upload(context.Background(), "foo6", buff)) 41 42 for _, tc := range []testIterCase{ 43 { 44 prefix: "foo/", 45 expected: []string{"foo/ba/", "foo/bar/", "foo/buzz4", "foo/buzz5"}, 46 options: []objstore.IterOption{}, 47 }, 48 { 49 prefix: "foo/", 50 expected: []string{"foo/ba/buzz3", "foo/bar/buz1", "foo/bar/buz2", "foo/buzz4", "foo/buzz5"}, 51 options: []objstore.IterOption{objstore.WithRecursiveIter()}, 52 }, 53 { 54 prefix: "foo/ba", 55 expected: []string{"foo/ba/buzz3"}, 56 options: []objstore.IterOption{objstore.WithRecursiveIter()}, 57 }, 58 { 59 prefix: "foo/ba/", 60 expected: []string{"foo/ba/buzz3"}, 61 options: []objstore.IterOption{objstore.WithRecursiveIter()}, 62 }, 63 { 64 prefix: "foo/b", 65 options: []objstore.IterOption{objstore.WithRecursiveIter()}, 66 }, 67 { 68 prefix: "foo", 69 expected: []string{"foo/ba/", "foo/bar/", "foo/buzz4", "foo/buzz5"}, 70 options: []objstore.IterOption{}, 71 }, 72 { 73 prefix: "foo", 74 expected: []string{"foo/ba/buzz3", "foo/bar/buz1", "foo/bar/buz2", "foo/buzz4", "foo/buzz5"}, 75 options: []objstore.IterOption{objstore.WithRecursiveIter()}, 76 }, 77 { 78 prefix: "fo", 79 options: []objstore.IterOption{}, 80 }, 81 { 82 prefix: "fo", 83 options: []objstore.IterOption{objstore.WithRecursiveIter()}, 84 }, 85 { 86 prefix: "", 87 expected: []string{"foo/", "foo6"}, 88 options: []objstore.IterOption{}, 89 }, 90 { 91 prefix: "", 92 expected: []string{"foo/ba/buzz3", "foo/bar/buz1", "foo/bar/buz2", "foo/buzz4", "foo/buzz5", "foo6"}, 93 options: []objstore.IterOption{objstore.WithRecursiveIter()}, 94 }, 95 } { 96 t.Run(tc.name(), func(t *testing.T) { 97 var keys []string 98 err = bkt.Iter(context.Background(), tc.prefix, func(key string) error { 99 keys = append(keys, key) 100 return nil 101 }, tc.options...) 102 require.NoError(t, err) 103 require.Equal(t, tc.expected, keys) 104 }) 105 } 106 }