@@ -30,7 +30,7 @@ import (
30
30
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
31
31
)
32
32
33
- func TestTestParseFlagPWithPlatformSpec (t *testing.T) {
33
+ func TestParseFlagPWithPlatformSpec (t *testing.T) {
34
34
if runtime.GOOS != "linux" || rootlessutil.IsRootless() {
35
35
t.Skip("no non-Linux platform or rootless mode in Linux are not supported yet")
36
36
}
@@ -232,10 +232,10 @@ func TestParseFlagP(t *testing.T) {
232
232
s string
233
233
}
234
234
tests := []struct {
235
- name string
236
- args args
237
- want []cni.PortMapping
238
- wantErr bool
235
+ name string
236
+ args args
237
+ want []cni.PortMapping
238
+ wantErrMsg string
239
239
}{
240
240
{
241
241
name: "normal",
@@ -250,7 +250,7 @@ func TestParseFlagP(t *testing.T) {
250
250
HostIP: "127.0.0.1",
251
251
},
252
252
},
253
- wantErr: false ,
253
+ wantErrMsg: "" ,
254
254
},
255
255
{
256
256
name: "with port range",
@@ -271,15 +271,15 @@ func TestParseFlagP(t *testing.T) {
271
271
HostIP: "127.0.0.1",
272
272
},
273
273
},
274
- wantErr: false ,
274
+ wantErrMsg: "" ,
275
275
},
276
276
{
277
277
name: "with wrong port range",
278
278
args: args{
279
279
s: "127.0.0.1:3000-3001:8080-8082/tcp",
280
280
},
281
- want: nil,
282
- wantErr: true ,
281
+ want: nil,
282
+ wantErrMsg: "invalid ranges specified for container and host Ports: 8080-8082 and 3000-3001" ,
283
283
},
284
284
{
285
285
name: "without host ip",
@@ -294,7 +294,7 @@ func TestParseFlagP(t *testing.T) {
294
294
HostIP: "0.0.0.0",
295
295
},
296
296
},
297
- wantErr: false ,
297
+ wantErrMsg: "" ,
298
298
},
299
299
{
300
300
name: "without protocol",
@@ -309,7 +309,7 @@ func TestParseFlagP(t *testing.T) {
309
309
HostIP: "0.0.0.0",
310
310
},
311
311
},
312
- wantErr: false ,
312
+ wantErrMsg: "" ,
313
313
},
314
314
{
315
315
name: "with protocol udp",
@@ -324,10 +324,10 @@ func TestParseFlagP(t *testing.T) {
324
324
HostIP: "0.0.0.0",
325
325
},
326
326
},
327
- wantErr: false ,
327
+ wantErrMsg: "" ,
328
328
},
329
329
{
330
- name: "with protocol udp ",
330
+ name: "with protocol sctp ",
331
331
args: args{
332
332
s: "3000:8080/sctp",
333
333
},
@@ -339,7 +339,7 @@ func TestParseFlagP(t *testing.T) {
339
339
HostIP: "0.0.0.0",
340
340
},
341
341
},
342
- wantErr: false ,
342
+ wantErrMsg: "" ,
343
343
},
344
344
{
345
345
name: "with ipv6 host ip",
@@ -354,86 +354,82 @@ func TestParseFlagP(t *testing.T) {
354
354
HostIP: "::0",
355
355
},
356
356
},
357
- wantErr: false ,
357
+ wantErrMsg: "" ,
358
358
},
359
359
{
360
360
name: "with invalid protocol",
361
361
args: args{
362
362
s: "3000:8080/invalid",
363
363
},
364
- want: nil,
365
- wantErr: true ,
364
+ want: nil,
365
+ wantErrMsg: `invalid protocol "invalid"` ,
366
366
},
367
367
{
368
368
name: "multiple colon",
369
369
args: args{
370
370
s: "127.0.0.1:3000:0.0.0.0:8080",
371
371
},
372
- want: nil,
373
- wantErr: true ,
372
+ want: nil,
373
+ wantErrMsg: "invalid hostPort: 127.0.0.1:3000:0.0.0.0" ,
374
374
},
375
375
{
376
376
name: "multiple slash",
377
377
args: args{
378
378
s: "127.0.0.1:3000:8080/tcp/",
379
379
},
380
- want: nil,
381
- wantErr: true ,
380
+ want: nil,
381
+ wantErrMsg: `failed to parse "127.0.0.1:3000:8080/tcp/", unexpected slashes` ,
382
382
},
383
383
{
384
384
name: "invalid ip",
385
385
args: args{
386
386
s: "127.0.0.256:3000:8080/tcp",
387
387
},
388
- want: nil,
389
- wantErr: true ,
388
+ want: nil,
389
+ wantErrMsg: "invalid ip address: 127.0.0.256" ,
390
390
},
391
391
{
392
392
name: "large port",
393
393
args: args{
394
394
s: "3000:65536",
395
395
},
396
- want: nil,
397
- wantErr: true ,
396
+ want: nil,
397
+ wantErrMsg: "invalid containerPort: 65536" ,
398
398
},
399
399
{
400
400
name: "blank",
401
401
args: args{
402
402
s: "",
403
403
},
404
- want: nil,
405
- wantErr: true ,
404
+ want: nil,
405
+ wantErrMsg: "no port specified: " ,
406
406
},
407
407
}
408
408
for _, tt := range tests {
409
409
t.Run(tt.name, func(t *testing.T) {
410
410
got, err := ParseFlagP(tt.args.s)
411
- t.Log(err)
412
- if (err != nil) != tt.wantErr {
413
- t.Errorf("ParseFlagP() error = %v, wantErr %v", err, tt.wantErr)
414
- return
411
+ if tt.wantErrMsg == "" {
412
+ assert.NilError(t, err)
413
+ } else {
414
+ assert.Error(t, err, tt.wantErrMsg)
415
415
}
416
416
if !reflect.DeepEqual(got, tt.want) {
417
- if len(got) == len(tt.want) {
418
- if len(got) > 1 {
419
- var hostPorts []int32
420
- var containerPorts []int32
421
- for _, value := range got {
422
- hostPorts = append(hostPorts, value.HostPort)
423
- containerPorts = append(containerPorts, value.ContainerPort)
424
- }
425
- sort.Slice(hostPorts, func(i, j int) bool {
426
- return i < j
427
- })
428
- sort.Slice(containerPorts, func(i, j int) bool {
429
- return i < j
430
- })
431
- if (hostPorts[len(hostPorts)-1] - hostPorts[0]) != (containerPorts[len(hostPorts)-1] - containerPorts[0]) {
432
- t.Errorf("ParseFlagP() = %v, want %v", got, tt.want)
433
- }
417
+ assert.Equal(t, len(got), len(tt.want))
418
+ if len(got) > 0 {
419
+ sort.Slice(got, func(i, j int) bool {
420
+ return got[i].HostPort < got[j].HostPort
421
+ })
422
+ assert.Equal(
423
+ t,
424
+ got[len(got)-1].HostPort-got[0].HostPort,
425
+ got[len(got)-1].ContainerPort-got[0].ContainerPort,
426
+ )
427
+ for i := range len(got) {
428
+ assert.Equal(t, got[i].HostPort, tt.want[i].HostPort)
429
+ assert.Equal(t, got[i].ContainerPort, tt.want[i].ContainerPort)
430
+ assert.Equal(t, got[i].Protocol, tt.want[i].Protocol)
431
+ assert.Equal(t, got[i].HostIP, tt.want[i].HostIP)
434
432
}
435
- } else {
436
- t.Errorf("ParseFlagP() = %v, want %v", got, tt.want)
437
433
}
438
434
}
439
435
})
0 commit comments