@@ -19,6 +19,7 @@ package main
19
19
import (
20
20
"fmt"
21
21
"io"
22
+ "net"
22
23
"regexp"
23
24
"runtime"
24
25
"strings"
@@ -491,3 +492,73 @@ func TestRunContainerWithMACAddress(t *testing.T) {
491
492
}
492
493
}
493
494
}
495
+
496
+ func TestRunContainerWithStaticIP6 (t * testing.T ) {
497
+ if rootlessutil .IsRootless () {
498
+ t .Skip ("Static IP6 assignment is not supported rootless mode yet." )
499
+ }
500
+ networkName := "test-network"
501
+ networkSubnet := "2001:db8:5::/64"
502
+ _ , subnet , err := net .ParseCIDR (networkSubnet )
503
+ assert .Assert (t , err == nil )
504
+ base := testutil .NewBaseWithIPv6Compatible (t )
505
+ base .Cmd ("network" , "create" , networkName , "--subnet" , networkSubnet , "--ipv6" ).AssertOK ()
506
+ t .Cleanup (func () {
507
+ base .Cmd ("network" , "rm" , networkName ).Run ()
508
+ })
509
+ testCases := []struct {
510
+ ip string
511
+ shouldSuccess bool
512
+ checkTheIPAddress bool
513
+ }{
514
+ {
515
+ ip : "" ,
516
+ shouldSuccess : true ,
517
+ checkTheIPAddress : false ,
518
+ },
519
+ {
520
+ ip : "2001:db8:5::6" ,
521
+ shouldSuccess : true ,
522
+ checkTheIPAddress : true ,
523
+ },
524
+ {
525
+ ip : "2001:db8:4::6" ,
526
+ shouldSuccess : false ,
527
+ checkTheIPAddress : false ,
528
+ },
529
+ }
530
+ tID := testutil .Identifier (t )
531
+ for i , tc := range testCases {
532
+ i := i
533
+ tc := tc
534
+ tcName := fmt .Sprintf ("%+v" , tc )
535
+ t .Run (tcName , func (t * testing.T ) {
536
+ testContainerName := fmt .Sprintf ("%s-%d" , tID , i )
537
+ base := testutil .NewBaseWithIPv6Compatible (t )
538
+ args := []string {
539
+ "run" , "--rm" , "--name" , testContainerName , "--network" , networkName ,
540
+ }
541
+ if tc .ip != "" {
542
+ args = append (args , "--ip6" , tc .ip )
543
+ }
544
+ args = append (args , []string {testutil .NginxAlpineImage , "ip" , "addr" , "show" , "dev" , "eth0" }... )
545
+ cmd := base .Cmd (args ... )
546
+ if ! tc .shouldSuccess {
547
+ cmd .AssertFail ()
548
+ return
549
+ }
550
+ cmd .AssertOutWithFunc (func (stdout string ) error {
551
+ ip := findIPv6 (stdout )
552
+ if ! subnet .Contains (ip ) {
553
+ return fmt .Errorf ("expected subnet %s include ip %s" , subnet , ip )
554
+ }
555
+ if tc .checkTheIPAddress {
556
+ if ip .String () != tc .ip {
557
+ return fmt .Errorf ("expected ip %s, got %s" , tc .ip , ip )
558
+ }
559
+ }
560
+ return nil
561
+ })
562
+ })
563
+ }
564
+ }
0 commit comments