@@ -30,7 +30,7 @@ import (
3030 "github.com/containerd/nerdctl/v2/pkg/rootlessutil"
3131)
3232
33- func TestTestParseFlagPWithPlatformSpec (t * testing.T ) {
33+ func TestParseFlagPWithPlatformSpec (t * testing.T ) {
3434 if runtime .GOOS != "linux" || rootlessutil .IsRootless () {
3535 t .Skip ("no non-Linux platform or rootless mode in Linux are not supported yet" )
3636 }
@@ -232,10 +232,10 @@ func TestParseFlagP(t *testing.T) {
232232 s string
233233 }
234234 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
239239 }{
240240 {
241241 name : "normal" ,
@@ -250,7 +250,7 @@ func TestParseFlagP(t *testing.T) {
250250 HostIP : "127.0.0.1" ,
251251 },
252252 },
253- wantErr : false ,
253+ wantErrMsg : "" ,
254254 },
255255 {
256256 name : "with port range" ,
@@ -271,15 +271,15 @@ func TestParseFlagP(t *testing.T) {
271271 HostIP : "127.0.0.1" ,
272272 },
273273 },
274- wantErr : false ,
274+ wantErrMsg : "" ,
275275 },
276276 {
277277 name : "with wrong port range" ,
278278 args : args {
279279 s : "127.0.0.1:3000-3001:8080-8082/tcp" ,
280280 },
281- want : nil ,
282- wantErr : true ,
281+ want : nil ,
282+ wantErrMsg : "invalid ranges specified for container and host Ports: 8080-8082 and 3000-3001" ,
283283 },
284284 {
285285 name : "without host ip" ,
@@ -294,7 +294,7 @@ func TestParseFlagP(t *testing.T) {
294294 HostIP : "0.0.0.0" ,
295295 },
296296 },
297- wantErr : false ,
297+ wantErrMsg : "" ,
298298 },
299299 {
300300 name : "without protocol" ,
@@ -309,7 +309,7 @@ func TestParseFlagP(t *testing.T) {
309309 HostIP : "0.0.0.0" ,
310310 },
311311 },
312- wantErr : false ,
312+ wantErrMsg : "" ,
313313 },
314314 {
315315 name : "with protocol udp" ,
@@ -324,10 +324,10 @@ func TestParseFlagP(t *testing.T) {
324324 HostIP : "0.0.0.0" ,
325325 },
326326 },
327- wantErr : false ,
327+ wantErrMsg : "" ,
328328 },
329329 {
330- name : "with protocol udp " ,
330+ name : "with protocol sctp " ,
331331 args : args {
332332 s : "3000:8080/sctp" ,
333333 },
@@ -339,7 +339,7 @@ func TestParseFlagP(t *testing.T) {
339339 HostIP : "0.0.0.0" ,
340340 },
341341 },
342- wantErr : false ,
342+ wantErrMsg : "" ,
343343 },
344344 {
345345 name : "with ipv6 host ip" ,
@@ -354,86 +354,82 @@ func TestParseFlagP(t *testing.T) {
354354 HostIP : "::0" ,
355355 },
356356 },
357- wantErr : false ,
357+ wantErrMsg : "" ,
358358 },
359359 {
360360 name : "with invalid protocol" ,
361361 args : args {
362362 s : "3000:8080/invalid" ,
363363 },
364- want : nil ,
365- wantErr : true ,
364+ want : nil ,
365+ wantErrMsg : `invalid protocol "invalid"` ,
366366 },
367367 {
368368 name : "multiple colon" ,
369369 args : args {
370370 s : "127.0.0.1:3000:0.0.0.0:8080" ,
371371 },
372- want : nil ,
373- wantErr : true ,
372+ want : nil ,
373+ wantErrMsg : "invalid hostPort: 127.0.0.1:3000:0.0.0.0" ,
374374 },
375375 {
376376 name : "multiple slash" ,
377377 args : args {
378378 s : "127.0.0.1:3000:8080/tcp/" ,
379379 },
380- want : nil ,
381- wantErr : true ,
380+ want : nil ,
381+ wantErrMsg : `failed to parse "127.0.0.1:3000:8080/tcp/", unexpected slashes` ,
382382 },
383383 {
384384 name : "invalid ip" ,
385385 args : args {
386386 s : "127.0.0.256:3000:8080/tcp" ,
387387 },
388- want : nil ,
389- wantErr : true ,
388+ want : nil ,
389+ wantErrMsg : "invalid ip address: 127.0.0.256" ,
390390 },
391391 {
392392 name : "large port" ,
393393 args : args {
394394 s : "3000:65536" ,
395395 },
396- want : nil ,
397- wantErr : true ,
396+ want : nil ,
397+ wantErrMsg : "invalid containerPort: 65536" ,
398398 },
399399 {
400400 name : "blank" ,
401401 args : args {
402402 s : "" ,
403403 },
404- want : nil ,
405- wantErr : true ,
404+ want : nil ,
405+ wantErrMsg : "no port specified: " ,
406406 },
407407 }
408408 for _ , tt := range tests {
409409 t .Run (tt .name , func (t * testing.T ) {
410410 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 )
415415 }
416416 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 )
434432 }
435- } else {
436- t .Errorf ("ParseFlagP() = %v, want %v" , got , tt .want )
437433 }
438434 }
439435 })
0 commit comments