7
7
"github.com/ipfs/kubo/config"
8
8
"github.com/ipfs/kubo/test/cli/harness"
9
9
"github.com/libp2p/go-libp2p/core/peer"
10
+ "github.com/libp2p/go-libp2p/core/protocol"
10
11
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
@@ -47,12 +48,12 @@ func TestRcmgr(t *testing.T) {
47
48
t .Run ("swarm limit should fail" , func (t * testing.T ) {
48
49
res := node .RunIPFS ("swarm" , "limit" )
49
50
assert .Equal (t , 1 , res .ExitCode ())
50
- assert .Contains (t , res .Stderr .Lines ()[ 0 ] , "missing ResourceMgr" )
51
+ assert .Contains (t , res .Stderr .String () , "missing ResourceMgr" )
51
52
})
52
53
t .Run ("swarm stats should fail" , func (t * testing.T ) {
53
54
res := node .RunIPFS ("swarm" , "stats" )
54
55
assert .Equal (t , 1 , res .ExitCode ())
55
- assert .Contains (t , res .Stderr .Lines ()[ 0 ] , "missing ResourceMgr" )
56
+ assert .Contains (t , res .Stderr .String () , "missing ResourceMgr" )
56
57
})
57
58
})
58
59
@@ -131,14 +132,8 @@ func TestRcmgr(t *testing.T) {
131
132
assert .Equal (t , resetRes .Stdout .Bytes (), limitRes .Stdout .Bytes ())
132
133
})
133
134
134
- t .Run ("scope is required using reset flags" , func (t * testing.T ) {
135
- res := node .RunIPFS ("swarm" , "limit" , "--reset" )
136
- assert .Equal (t , 1 , res .ExitCode ())
137
- assert .Contains (t , res .Stderr .Lines ()[0 ], `Error: argument "scope" is required` )
138
- })
139
-
140
135
t .Run ("swarm stats works" , func (t * testing.T ) {
141
- res := node .RunIPFS ("swarm" , "stats" , "all" , "--enc=json" )
136
+ res := node .RunIPFS ("swarm" , "stats" )
142
137
require .Equal (t , 0 , res .ExitCode ())
143
138
144
139
stats := rcmgr.PartialLimitConfig {}
@@ -178,46 +173,36 @@ func TestRcmgr(t *testing.T) {
178
173
})
179
174
})
180
175
181
- t .Run ("set the system memory limit while the daemon is running" , func (t * testing.T ) {
182
- node := harness .NewT (t ).NewNode ().Init ().StartDaemon ()
183
- updateLimitsWithFile (t , node , "system" , func (limits * rcmgr.ResourceLimits ) {
184
- limits .Memory = 99998
185
- })
186
-
187
- assert .Equal (t , rcmgr .LimitVal64 (99998 ), node .ReadConfig ().Swarm .ResourceMgr .Limits .System .Memory )
188
-
189
- res := node .RunIPFS ("swarm" , "limit" )
190
- limits := unmarshalLimits (t , res .Stdout .Bytes ())
191
- assert .Equal (t , rcmgr .LimitVal64 (99998 ), limits .System .Memory )
192
- })
193
-
194
176
t .Run ("smoke test transient scope" , func (t * testing.T ) {
195
- node := harness .NewT (t ).NewNode ().Init (). StartDaemon ()
196
- updateLimitsWithFile ( t , node , "transient" , func (limits * rcmgr.ResourceLimits ) {
197
- limits .Memory = 88888
177
+ node := harness .NewT (t ).NewNode ().Init ()
178
+ node . UpdateUserSuppliedRessourceManagerOverrides ( func (overrides * rcmgr.PartialLimitConfig ) {
179
+ overrides . Transient .Memory = 88888
198
180
})
181
+ node .StartDaemon ()
199
182
200
183
res := node .RunIPFS ("swarm" , "limit" )
201
184
limits := unmarshalLimits (t , res .Stdout .Bytes ())
202
185
assert .Equal (t , rcmgr .LimitVal64 (88888 ), limits .Transient .Memory )
203
186
})
204
187
205
188
t .Run ("smoke test service scope" , func (t * testing.T ) {
206
- node := harness .NewT (t ).NewNode ().Init (). StartDaemon ()
207
- updateLimitsWithFile ( t , node , "svc:foo" , func (limits * rcmgr.ResourceLimits ) {
208
- limits . Memory = 77777
189
+ node := harness .NewT (t ).NewNode ().Init ()
190
+ node . UpdateUserSuppliedRessourceManagerOverrides ( func (overrides * rcmgr.PartialLimitConfig ) {
191
+ overrides . Service = map [ string ]rcmgr. ResourceLimits { "foo" : { Memory : 77777 }}
209
192
})
193
+ node .StartDaemon ()
210
194
211
195
res := node .RunIPFS ("swarm" , "limit" )
212
196
limits := unmarshalLimits (t , res .Stdout .Bytes ())
213
197
assert .Equal (t , rcmgr .LimitVal64 (77777 ), limits .Service ["foo" ].Memory )
214
198
})
215
199
216
200
t .Run ("smoke test protocol scope" , func (t * testing.T ) {
217
- node := harness .NewT (t ).NewNode ().Init (). StartDaemon ()
218
- updateLimitsWithFile ( t , node , "proto:foo" , func (limits * rcmgr.ResourceLimits ) {
219
- limits . Memory = 66666
201
+ node := harness .NewT (t ).NewNode ().Init ()
202
+ node . UpdateUserSuppliedRessourceManagerOverrides ( func (overrides * rcmgr.PartialLimitConfig ) {
203
+ overrides . Protocol = map [protocol. ID ]rcmgr. ResourceLimits { "foo" : { Memory : 66666 }}
220
204
})
205
+ node .StartDaemon ()
221
206
222
207
res := node .RunIPFS ("swarm" , "limit" )
223
208
limits := unmarshalLimits (t , res .Stdout .Bytes ())
@@ -227,19 +212,20 @@ func TestRcmgr(t *testing.T) {
227
212
t .Run ("smoke test peer scope" , func (t * testing.T ) {
228
213
validPeerID , err := peer .Decode ("QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" )
229
214
assert .NoError (t , err )
230
- node := harness .NewT (t ).NewNode ().Init (). StartDaemon ()
231
- updateLimitsWithFile ( t , node , "peer:" + validPeerID . Pretty (), func (limits * rcmgr.ResourceLimits ) {
232
- limits . Memory = 66666
215
+ node := harness .NewT (t ).NewNode ().Init ()
216
+ node . UpdateUserSuppliedRessourceManagerOverrides ( func (overrides * rcmgr.PartialLimitConfig ) {
217
+ overrides . Peer = map [peer. ID ]rcmgr. ResourceLimits { validPeerID : { Memory : 55555 }}
233
218
})
219
+ node .StartDaemon ()
234
220
235
- res := node .RunIPFS ("swarm" , "limit" , "peer:" + validPeerID . Pretty (), "--enc=json" )
221
+ res := node .RunIPFS ("swarm" , "limit" )
236
222
limits := unmarshalLimits (t , res .Stdout .Bytes ())
237
- assert .Equal (t , rcmgr .LimitVal64 (66666 ), limits .Peer [validPeerID ].Memory )
223
+ assert .Equal (t , rcmgr .LimitVal64 (55555 ), limits .Peer [validPeerID ].Memory )
238
224
239
225
t .Parallel ()
240
226
241
227
t .Run ("getting limit for invalid peer ID fails" , func (t * testing.T ) {
242
- res := node .RunIPFS ("swarm" , "limit" , "peer:foo" )
228
+ res := node .RunIPFS ("swarm" , "limit" )
243
229
assert .Equal (t , 1 , res .ExitCode ())
244
230
assert .Contains (t , res .Stderr .String (), "invalid peer ID" )
245
231
})
@@ -259,20 +245,20 @@ func TestRcmgr(t *testing.T) {
259
245
// peerID0, peerID1, peerID2 := node0.PeerID(), node1.PeerID(), node2.PeerID()
260
246
peerID1 , peerID2 := node1 .PeerID ().String (), node2 .PeerID ().String ()
261
247
262
- node0 .UpdateConfig (func (cfg * config.Config ) {
248
+ node0 .UpdateConfigAndUserSuppliedRessourceManagerOverrides (func (cfg * config.Config , overrides * rcmgr.PartialLimitConfig ) {
249
+ * overrides = rcmgr.PartialLimitConfig {
250
+ System : rcmgr.ResourceLimits {
251
+ Conns : rcmgr .BlockAllLimit ,
252
+ ConnsInbound : rcmgr .BlockAllLimit ,
253
+ ConnsOutbound : rcmgr .BlockAllLimit ,
254
+ },
255
+ }
263
256
cfg .Swarm .ResourceMgr .Enabled = config .True
264
257
cfg .Swarm .ResourceMgr .Allowlist = []string {"/ip4/0.0.0.0/ipcidr/0/p2p/" + peerID2 }
265
258
})
266
259
267
260
nodes .StartDaemons ()
268
261
269
- // change system limits on node 0
270
- updateLimitsWithFile (t , node0 , "system" , func (limits * rcmgr.ResourceLimits ) {
271
- limits .Conns = rcmgr .BlockAllLimit
272
- limits .ConnsInbound = rcmgr .BlockAllLimit
273
- limits .ConnsOutbound = rcmgr .BlockAllLimit
274
- })
275
-
276
262
t .Parallel ()
277
263
t .Run ("node 0 should fail to connect to node 1" , func (t * testing.T ) {
278
264
res := node0 .Runner .Run (harness.RunRequest {
@@ -307,9 +293,10 @@ func TestRcmgr(t *testing.T) {
307
293
t .Parallel ()
308
294
t .Run ("system conns" , func (t * testing.T ) {
309
295
node := harness .NewT (t ).NewNode ().Init ()
310
- node .UpdateConfig (func (cfg * config.Config ) {
311
- cfg .Swarm .ResourceMgr .Limits = & rcmgr.PartialLimitConfig {}
312
- cfg .Swarm .ResourceMgr .Limits .System .Conns = 128
296
+ node .UpdateConfigAndUserSuppliedRessourceManagerOverrides (func (cfg * config.Config , overrides * rcmgr.PartialLimitConfig ) {
297
+ * overrides = rcmgr.PartialLimitConfig {
298
+ System : rcmgr.ResourceLimits {Conns : 128 },
299
+ }
313
300
cfg .Swarm .ConnMgr .HighWater = config .NewOptionalInteger (128 )
314
301
cfg .Swarm .ConnMgr .LowWater = config .NewOptionalInteger (64 )
315
302
})
@@ -319,9 +306,10 @@ func TestRcmgr(t *testing.T) {
319
306
})
320
307
t .Run ("system conns inbound" , func (t * testing.T ) {
321
308
node := harness .NewT (t ).NewNode ().Init ()
322
- node .UpdateConfig (func (cfg * config.Config ) {
323
- cfg .Swarm .ResourceMgr .Limits = & rcmgr.PartialLimitConfig {}
324
- cfg .Swarm .ResourceMgr .Limits .System .ConnsInbound = 128
309
+ node .UpdateConfigAndUserSuppliedRessourceManagerOverrides (func (cfg * config.Config , overrides * rcmgr.PartialLimitConfig ) {
310
+ * overrides = rcmgr.PartialLimitConfig {
311
+ System : rcmgr.ResourceLimits {ConnsInbound : 128 },
312
+ }
325
313
cfg .Swarm .ConnMgr .HighWater = config .NewOptionalInteger (128 )
326
314
cfg .Swarm .ConnMgr .LowWater = config .NewOptionalInteger (64 )
327
315
})
@@ -331,9 +319,10 @@ func TestRcmgr(t *testing.T) {
331
319
})
332
320
t .Run ("system streams" , func (t * testing.T ) {
333
321
node := harness .NewT (t ).NewNode ().Init ()
334
- node .UpdateConfig (func (cfg * config.Config ) {
335
- cfg .Swarm .ResourceMgr .Limits = & rcmgr.PartialLimitConfig {}
336
- cfg .Swarm .ResourceMgr .Limits .System .Streams = 128
322
+ node .UpdateConfigAndUserSuppliedRessourceManagerOverrides (func (cfg * config.Config , overrides * rcmgr.PartialLimitConfig ) {
323
+ * overrides = rcmgr.PartialLimitConfig {
324
+ System : rcmgr.ResourceLimits {Streams : 128 },
325
+ }
337
326
cfg .Swarm .ConnMgr .HighWater = config .NewOptionalInteger (128 )
338
327
cfg .Swarm .ConnMgr .LowWater = config .NewOptionalInteger (64 )
339
328
})
@@ -343,9 +332,10 @@ func TestRcmgr(t *testing.T) {
343
332
})
344
333
t .Run ("system streams inbound" , func (t * testing.T ) {
345
334
node := harness .NewT (t ).NewNode ().Init ()
346
- node .UpdateConfig (func (cfg * config.Config ) {
347
- cfg .Swarm .ResourceMgr .Limits = & rcmgr.PartialLimitConfig {}
348
- cfg .Swarm .ResourceMgr .Limits .System .StreamsInbound = 128
335
+ node .UpdateConfigAndUserSuppliedRessourceManagerOverrides (func (cfg * config.Config , overrides * rcmgr.PartialLimitConfig ) {
336
+ * overrides = rcmgr.PartialLimitConfig {
337
+ System : rcmgr.ResourceLimits {StreamsInbound : 128 },
338
+ }
349
339
cfg .Swarm .ConnMgr .HighWater = config .NewOptionalInteger (128 )
350
340
cfg .Swarm .ConnMgr .LowWater = config .NewOptionalInteger (64 )
351
341
})
0 commit comments