-
Notifications
You must be signed in to change notification settings - Fork 332
/
test_utils.go
143 lines (122 loc) · 2.75 KB
/
test_utils.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package tablediff
import (
"context"
"errors"
"strconv"
"time"
"github.com/treeverse/lakefs/pkg/plugins/internal"
)
var ErrNotFound = errors.New("plugin not found")
var ErrDiffFailed = errors.New("failed")
const (
diffType = "aDiff"
ControllerTestPlugin = "controller"
PluginPath = "plugin_path"
PluginVersion = "plugin_version"
)
type MockHandler struct {
differs map[string]Differ
}
func (mh *MockHandler) RegisterPlugin(name string, pp internal.HCPluginProperties) {
mh.differs[name] = TestDiffer{pp}
}
func (mh *MockHandler) LoadPluginClient(name string) (Differ, func(), error) {
d, ok := mh.differs[name]
if !ok {
return nil, nil, ErrNotFound
}
return d, nil, nil
}
func (mh *MockHandler) Plugins() []string {
ps := make([]string, 0, len(mh.differs))
for k := range mh.differs {
ps = append(ps, k)
}
return ps
}
func NewMockHandler() *MockHandler {
mh := MockHandler{
differs: make(map[string]Differ),
}
mh.differs[ControllerTestPlugin] = ControllerTestDiffer{}
return &mh
}
type TestDiffer struct {
props internal.HCPluginProperties
}
func (td TestDiffer) Diff(ctx context.Context, p Params) (Response, error) {
e := ErrorFromContext(ctx)
if e != nil {
return Response{}, e
}
r := Response{
DiffType: DiffTypeChanged,
Diffs: []DiffEntry{
{
ID: "v0",
Timestamp: time.Unix(0, 0),
Operation: "op",
OperationContent: map[string]string{
PluginPath: td.props.ID.ExecutableLocation,
PluginVersion: strconv.Itoa(int(td.props.ID.ProtocolVersion)),
},
},
},
}
return r, nil
}
type ControllerTestDiffer struct{}
func (ctd ControllerTestDiffer) Diff(ctx context.Context, p Params) (Response, error) {
left := p.TablePaths.Left.Ref
right := p.TablePaths.Right.Ref
notfound := "notfound"
if left == notfound && right == notfound {
return Response{}, ErrTableNotFound
}
var ct = DiffTypeChanged
switch {
case left == "dropped":
ct = DiffTypeDropped
case right == "created":
ct = DiffTypeCreated
}
var diffs []DiffEntry
if ct != DiffTypeDropped {
diffs = generateDiffs()
}
r := Response{
DiffType: ct,
Diffs: diffs,
}
return r, nil
}
func generateDiffs() []DiffEntry {
return []DiffEntry{
{
ID: "v0",
Timestamp: time.Unix(0, 0),
Operation: "op",
OperationContent: map[string]string{
"errKey": "value",
},
},
}
}
func NewMockService() *Service {
return &Service{
pluginHandler: NewMockHandler(),
closeFunctions: nil,
}
}
type keyType string
const errKey keyType = "error"
func ContextWithError(ctx context.Context, e error) context.Context {
return context.WithValue(ctx, errKey, e)
}
func ErrorFromContext(ctx context.Context) error {
e, ok := ctx.Value(errKey).(error)
if !ok {
return nil
}
return e
}