@@ -552,55 +552,102 @@ func TestWithImageConfigArgs(t *testing.T) {
552552
553553func TestDevShmSize (t * testing.T ) {
554554 t .Parallel ()
555- var (
556- s Spec
557- c = containers.Container {ID : t .Name ()}
558- ctx = namespaces .WithNamespace (context .Background (), "test" )
559- )
560555
561- err := populateDefaultUnixSpec (ctx , & s , c .ID )
562- if err != nil {
563- t .Fatal (err )
556+ ss := []Spec {
557+ {
558+ Mounts : []specs.Mount {
559+ {
560+ Destination : "/dev/shm" ,
561+ Type : "tmpfs" ,
562+ Source : "shm" ,
563+ Options : []string {"nosuid" , "noexec" , "nodev" , "mode=1777" },
564+ },
565+ },
566+ },
567+ {
568+ Mounts : []specs.Mount {
569+ {
570+ Destination : "/test/shm" ,
571+ Type : "tmpfs" ,
572+ Source : "shm" ,
573+ Options : []string {"nosuid" , "noexec" , "nodev" , "mode=1777" , "size=65536k" },
574+ },
575+ },
576+ },
577+ {
578+ Mounts : []specs.Mount {
579+ {
580+ Destination : "/test/shm" ,
581+ Type : "tmpfs" ,
582+ Source : "shm" ,
583+ Options : []string {"nosuid" , "noexec" , "nodev" , "mode=1777" , "size=65536k" },
584+ },
585+ {
586+ Destination : "/dev/shm" ,
587+ Type : "tmpfs" ,
588+ Source : "shm" ,
589+ Options : []string {"nosuid" , "noexec" , "nodev" , "mode=1777" , "size=65536k" , "size=131072k" },
590+ },
591+ },
592+ },
564593 }
565594
566595 expected := "1024k"
567- if err := WithDevShmSize (1024 )(nil , nil , nil , & s ); err != nil {
568- t .Fatal (err )
569- }
570- m := getShmMount (& s )
571- if m == nil {
572- t .Fatal ("no shm mount found" )
573- }
574- o := getShmSize (m .Options )
575- if o == "" {
576- t .Fatal ("shm size not specified" )
577- }
578- parts := strings .Split (o , "=" )
579- if len (parts ) != 2 {
580- t .Fatal ("invalid size format" )
581- }
582- size := parts [1 ]
583- if size != expected {
584- t .Fatalf ("size %s not equal %s" , size , expected )
596+ for _ , s := range ss {
597+ if err := WithDevShmSize (1024 )(nil , nil , nil , & s ); err != nil {
598+ if err != ErrNoShmMount {
599+ t .Fatal (err )
600+ }
601+
602+ if getDevShmMount (& s ) == nil {
603+ continue
604+ }
605+ t .Fatal ("excepted nil /dev/shm mount" )
606+ }
607+
608+ m := getDevShmMount (& s )
609+ if m == nil {
610+ t .Fatal ("no shm mount found" )
611+ }
612+ size , err := getShmSize (m .Options )
613+ if err != nil {
614+ t .Fatal (err )
615+ }
616+ if size != expected {
617+ t .Fatalf ("size %s not equal %s" , size , expected )
618+ }
585619 }
586620}
587621
588- func getShmMount (s * Spec ) * specs.Mount {
622+ func getDevShmMount (s * Spec ) * specs.Mount {
589623 for _ , m := range s .Mounts {
590- if m .Source == "shm" && m .Type == "tmpfs" {
624+ if filepath . Clean ( m . Destination ) == "/dev/shm" && m .Source == "shm" && m .Type == "tmpfs" {
591625 return & m
592626 }
593627 }
594628 return nil
595629}
596630
597- func getShmSize (opts []string ) string {
631+ func getShmSize (opts []string ) (string , error ) {
632+ // linux will use the last size option
633+ var so string
598634 for _ , o := range opts {
599635 if strings .HasPrefix (o , "size=" ) {
600- return o
636+ if so != "" {
637+ return "" , errors .New ("contains multiple size options" )
638+ }
639+ so = o
601640 }
602641 }
603- return ""
642+ if so == "" {
643+ return "" , errors .New ("shm size not specified" )
644+ }
645+
646+ parts := strings .Split (so , "=" )
647+ if len (parts ) != 2 {
648+ return "" , errors .New ("invalid size format" )
649+ }
650+ return parts [1 ], nil
604651}
605652
606653func TestWithoutMounts (t * testing.T ) {
0 commit comments