-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
pinmfs_test.go
179 lines (164 loc) · 4.43 KB
/
pinmfs_test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"fmt"
"strings"
"testing"
"time"
config "github.com/ipfs/go-ipfs-config"
ipld "github.com/ipfs/go-ipld-format"
merkledag "github.com/ipfs/go-merkledag"
"github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
)
type testPinMFSContext struct {
ctx context.Context
cfg *config.Config
err error
}
func (x *testPinMFSContext) Context() context.Context {
return x.ctx
}
func (x *testPinMFSContext) GetConfigNoCache() (*config.Config, error) {
return x.cfg, x.err
}
type testPinMFSNode struct {
err error
}
func (x *testPinMFSNode) RootNode() (ipld.Node, error) {
return merkledag.NewRawNode([]byte{0x01}), x.err
}
func (x *testPinMFSNode) Identity() peer.ID {
return peer.ID("test_id")
}
func (x *testPinMFSNode) PeerHost() host.Host {
return nil
}
var testConfigPollInterval = time.Second
func isErrorSimilar(e1, e2 error) bool {
switch {
case e1 == nil && e2 == nil:
return true
case e1 != nil && e2 == nil:
return false
case e1 == nil && e2 != nil:
return false
default:
return strings.Contains(e1.Error(), e2.Error()) || strings.Contains(e2.Error(), e1.Error())
}
}
func TestPinMFSConfigError(t *testing.T) {
ctx := &testPinMFSContext{
ctx: context.Background(),
cfg: nil,
err: fmt.Errorf("couldn't read config"),
}
node := &testPinMFSNode{}
errCh := make(chan error)
go pinMFSOnChange(testConfigPollInterval, ctx, node, errCh)
if !isErrorSimilar(<-errCh, ctx.err) {
t.Errorf("error did not propagate")
}
if !isErrorSimilar(<-errCh, ctx.err) {
t.Errorf("error did not propagate")
}
}
func TestPinMFSRootNodeError(t *testing.T) {
ctx := &testPinMFSContext{
ctx: context.Background(),
cfg: &config.Config{
Pinning: config.Pinning{},
},
err: nil,
}
node := &testPinMFSNode{
err: fmt.Errorf("cannot create root node"),
}
errCh := make(chan error)
go pinMFSOnChange(testConfigPollInterval, ctx, node, errCh)
if !isErrorSimilar(<-errCh, node.err) {
t.Errorf("error did not propagate")
}
if !isErrorSimilar(<-errCh, node.err) {
t.Errorf("error did not propagate")
}
}
func TestPinMFSService(t *testing.T) {
cfg_invalid_interval := &config.Config{
Pinning: config.Pinning{
RemoteServices: map[string]config.RemotePinningService{
"disabled": {
Policies: config.RemotePinningServicePolicies{
MFS: config.RemotePinningServiceMFSPolicy{
Enable: false,
},
},
},
"invalid_interval": {
Policies: config.RemotePinningServicePolicies{
MFS: config.RemotePinningServiceMFSPolicy{
Enable: true,
RepinInterval: "INVALID_INTERVAL",
},
},
},
},
},
}
cfg_valid_unnamed := &config.Config{
Pinning: config.Pinning{
RemoteServices: map[string]config.RemotePinningService{
"valid_unnamed": {
Policies: config.RemotePinningServicePolicies{
MFS: config.RemotePinningServiceMFSPolicy{
Enable: true,
PinName: "",
RepinInterval: "2s",
},
},
},
},
},
}
cfg_valid_named := &config.Config{
Pinning: config.Pinning{
RemoteServices: map[string]config.RemotePinningService{
"valid_named": {
Policies: config.RemotePinningServicePolicies{
MFS: config.RemotePinningServiceMFSPolicy{
Enable: true,
PinName: "pin_name",
RepinInterval: "2s",
},
},
},
},
},
}
testPinMFSServiceWithError(t, cfg_invalid_interval, "remote pinning service \"invalid_interval\" has invalid MFS.RepinInterval")
testPinMFSServiceWithError(t, cfg_valid_unnamed, "error while listing remote pins: empty response from remote pinning service")
testPinMFSServiceWithError(t, cfg_valid_named, "error while listing remote pins: empty response from remote pinning service")
}
func testPinMFSServiceWithError(t *testing.T, cfg *config.Config, expectedErrorPrefix string) {
goctx, cancel := context.WithCancel(context.Background())
ctx := &testPinMFSContext{
ctx: goctx,
cfg: cfg,
err: nil,
}
node := &testPinMFSNode{
err: nil,
}
errCh := make(chan error)
go pinMFSOnChange(testConfigPollInterval, ctx, node, errCh)
defer cancel()
// first pass through the pinning loop
err := <-errCh
if !strings.Contains((err).Error(), expectedErrorPrefix) {
t.Errorf("expecting error containing %q", expectedErrorPrefix)
}
// second pass through the pinning loop
if !strings.Contains((err).Error(), expectedErrorPrefix) {
t.Errorf("expecting error containing %q", expectedErrorPrefix)
}
}