forked from probe-lab/zikade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
216 lines (182 loc) · 5.66 KB
/
config_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package zikade
import (
"testing"
"time"
"github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig_Validate(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
cfg := DefaultConfig()
assert.NoError(t, cfg.Validate())
})
t.Run("invalid mode", func(t *testing.T) {
cfg := DefaultConfig()
cfg.Mode = "invalid"
assert.Error(t, cfg.Validate())
})
t.Run("nil Query configuration", func(t *testing.T) {
cfg := DefaultConfig()
cfg.Query = nil
assert.Error(t, cfg.Validate())
})
t.Run("invalid Query configuration", func(t *testing.T) {
cfg := DefaultConfig()
cfg.Query.Concurrency = -1
assert.Error(t, cfg.Validate())
})
t.Run("empty protocol", func(t *testing.T) {
cfg := DefaultConfig()
cfg.ProtocolID = ""
assert.Error(t, cfg.Validate())
})
t.Run("nil logger", func(t *testing.T) {
cfg := DefaultConfig()
cfg.Logger = nil
assert.Error(t, cfg.Validate())
})
t.Run("0 stream idle timeout", func(t *testing.T) {
cfg := DefaultConfig()
cfg.TimeoutStreamIdle = time.Duration(0)
assert.Error(t, cfg.Validate())
})
t.Run("negative stream idle timeout", func(t *testing.T) {
cfg := DefaultConfig()
cfg.TimeoutStreamIdle = time.Duration(-1)
assert.Error(t, cfg.Validate())
})
t.Run("incompatible backends with ipfs protocol", func(t *testing.T) {
// When we're using the IPFS protocol, we always require support
// for ipns, pk, and provider records.
// If the Backends map is empty and the IPFS protocol is configured,
// we automatically populate the DHT backends for these record
// types.
cfg := DefaultConfig()
cfg.ProtocolID = ProtocolIPFS
cfg.Backends["another"] = &RecordBackend{}
assert.Error(t, cfg.Validate())
})
t.Run("additional backends for ipfs protocol", func(t *testing.T) {
cfg := DefaultConfig()
cfg.ProtocolID = ProtocolIPFS
cfg.Backends[namespaceProviders] = &RecordBackend{}
cfg.Backends[namespaceIPNS] = &RecordBackend{}
cfg.Backends[namespacePublicKey] = &RecordBackend{}
cfg.Backends["another"] = &RecordBackend{}
assert.Error(t, cfg.Validate())
})
t.Run("backends for ipfs protocol (public key missing)", func(t *testing.T) {
cfg := DefaultConfig()
cfg.ProtocolID = ProtocolIPFS
cfg.Backends[namespaceProviders] = &RecordBackend{}
cfg.Backends[namespaceIPNS] = &RecordBackend{}
cfg.Backends["another"] = &RecordBackend{}
assert.Error(t, cfg.Validate())
})
t.Run("backends for ipfs protocol (ipns missing)", func(t *testing.T) {
cfg := DefaultConfig()
cfg.ProtocolID = ProtocolIPFS
cfg.Backends[namespaceProviders] = &RecordBackend{}
cfg.Backends["another"] = &RecordBackend{}
cfg.Backends[namespacePublicKey] = &RecordBackend{}
assert.Error(t, cfg.Validate())
})
t.Run("backends for ipfs protocol (providers missing)", func(t *testing.T) {
cfg := DefaultConfig()
cfg.ProtocolID = ProtocolIPFS
cfg.Backends["another"] = &RecordBackend{}
cfg.Backends[namespaceIPNS] = &RecordBackend{}
cfg.Backends[namespacePublicKey] = &RecordBackend{}
assert.Error(t, cfg.Validate())
})
t.Run("nil address filter", func(t *testing.T) {
cfg := DefaultConfig()
cfg.AddressFilter = nil
assert.Error(t, cfg.Validate())
})
t.Run("nil meter provider", func(t *testing.T) {
cfg := DefaultConfig()
cfg.MeterProvider = nil
assert.Error(t, cfg.Validate())
})
t.Run("nil tracer provider", func(t *testing.T) {
cfg := DefaultConfig()
cfg.TracerProvider = nil
assert.Error(t, cfg.Validate())
})
t.Run("nil clock", func(t *testing.T) {
cfg := DefaultConfig()
cfg.Clock = nil
assert.Error(t, cfg.Validate())
})
t.Run("zero bucket size", func(t *testing.T) {
cfg := DefaultConfig()
cfg.BucketSize = 0
assert.Error(t, cfg.Validate())
})
t.Run("empty bootstrap peers", func(t *testing.T) {
cfg := DefaultConfig()
cfg.BootstrapPeers = []peer.AddrInfo{}
assert.Error(t, cfg.Validate())
})
t.Run("bootstrap peers no addresses", func(t *testing.T) {
cfg := DefaultConfig()
cfg.BootstrapPeers = []peer.AddrInfo{
{ID: newPeerID(t), Addrs: []ma.Multiaddr{}},
}
assert.Error(t, cfg.Validate())
})
t.Run("bootstrap peers mixed no addresses", func(t *testing.T) {
cfg := DefaultConfig()
maddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
require.NoError(t, err)
cfg.BootstrapPeers = []peer.AddrInfo{
{ID: newPeerID(t), Addrs: []ma.Multiaddr{}},
{ID: newPeerID(t), Addrs: []ma.Multiaddr{maddr}},
}
assert.Error(t, cfg.Validate()) // still an error
})
}
func TestQueryConfig_Validate(t *testing.T) {
t.Run("default is valid", func(t *testing.T) {
cfg := DefaultQueryConfig()
assert.NoError(t, cfg.Validate())
})
t.Run("concurrency positive", func(t *testing.T) {
cfg := DefaultQueryConfig()
cfg.Concurrency = 0
assert.Error(t, cfg.Validate())
cfg.Concurrency = -1
assert.Error(t, cfg.Validate())
})
t.Run("timeout positive", func(t *testing.T) {
cfg := DefaultQueryConfig()
cfg.Timeout = 0
assert.Error(t, cfg.Validate())
cfg.Timeout = -1
assert.Error(t, cfg.Validate())
})
t.Run("request concurrency positive", func(t *testing.T) {
cfg := DefaultQueryConfig()
cfg.RequestConcurrency = 0
assert.Error(t, cfg.Validate())
cfg.RequestConcurrency = -1
assert.Error(t, cfg.Validate())
})
t.Run("request timeout positive", func(t *testing.T) {
cfg := DefaultQueryConfig()
cfg.RequestTimeout = 0
assert.Error(t, cfg.Validate())
cfg.RequestTimeout = -1
assert.Error(t, cfg.Validate())
})
t.Run("negative default quorum", func(t *testing.T) {
cfg := DefaultQueryConfig()
cfg.DefaultQuorum = 0
assert.NoError(t, cfg.Validate())
cfg.DefaultQuorum = -1
assert.Error(t, cfg.Validate())
})
}