-
Notifications
You must be signed in to change notification settings - Fork 342
/
adapter.go
89 lines (76 loc) · 2.16 KB
/
adapter.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
package adapter_test
import (
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/block/local"
"github.com/treeverse/lakefs/pkg/block/mem"
)
func TestAdapter(t *testing.T, name string, params interface{}) {
a := MakeAdapterByName(t, name, params)
t.Run("Adapter_Walk", func(t *testing.T) { testAdapterWalk(t, a) })
}
func testAdapterWalk(t *testing.T, a block.Adapter) {
ctx := context.Background()
itemsNum := 10
namespace := getStorageNamespace(a)
objects := setupSampleData(t, ctx, a, itemsNum)
index := 0
_ = a.Walk(ctx, block.WalkOpts{StorageNamespace: namespace}, func(id string) error {
require.Equal(t, fmt.Sprintf("%s:%s", namespace, objects[index].Identifier), id)
index += 1
return nil
})
}
func setupSampleData(t *testing.T, ctx context.Context, a block.Adapter, items int) []block.ObjectPointer {
t.Helper()
entries := make([]block.ObjectPointer, 0, items)
for i := 0; i < items; i++ {
contents := fmt.Sprintf("file_%06d", i)
obj := block.ObjectPointer{
StorageNamespace: getStorageNamespace(a),
Identifier: contents,
IdentifierType: block.IdentifierTypeRelative,
}
err := a.Put(ctx, obj, 0, strings.NewReader(contents), block.PutOpts{})
if err != nil {
t.Fatalf("failed to setup data with '%v': %s", obj, err)
}
entries = append(entries, obj)
}
return entries
}
func getStorageNamespace(a block.Adapter) string {
switch a.BlockstoreType() {
case block.BlockstoreTypeMem:
return ""
case block.BlockstoreTypeLocal:
return "local://test"
default:
panic("Missing adapter type")
}
}
func MakeAdapterByName(t *testing.T, name string, params interface{}) block.Adapter {
var a block.Adapter
switch name {
case block.BlockstoreTypeMem:
p := params.([]func(a *mem.Adapter))
a = mem.New(p...)
case block.BlockstoreTypeLocal:
dir, err := os.MkdirTemp("", "testing-local-adapter-*")
if err != nil {
t.Fatalf("failed creating adapter: %s", err)
}
a, err = local.NewAdapter(dir)
if err != nil {
t.Fatalf("failed creating adapter: %s", err)
}
default:
panic("Missing adapter type")
}
return a
}