@@ -25,6 +25,7 @@ import (
2525 "io/ioutil"
2626 "log"
2727 "os"
28+ "path/filepath"
2829 "reflect"
2930 "runtime"
3031 "strings"
@@ -551,53 +552,100 @@ func TestWithImageConfigArgs(t *testing.T) {
551552
552553func TestDevShmSize (t * testing.T ) {
553554 t .Parallel ()
554- var (
555- s Spec
556- c = containers.Container {ID : t .Name ()}
557- ctx = namespaces .WithNamespace (context .Background (), "test" )
558- )
559555
560- err := populateDefaultUnixSpec (ctx , & s , c .ID )
561- if err != nil {
562- 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+ },
563593 }
564594
565595 expected := "1024k"
566- if err := WithDevShmSize (1024 )(nil , nil , nil , & s ); err != nil {
567- t .Fatal (err )
568- }
569- m := getShmMount (& s )
570- if m == nil {
571- t .Fatal ("no shm mount found" )
572- }
573- o := getShmSize (m .Options )
574- if o == "" {
575- t .Fatal ("shm size not specified" )
576- }
577- parts := strings .Split (o , "=" )
578- if len (parts ) != 2 {
579- t .Fatal ("invalid size format" )
580- }
581- size := parts [1 ]
582- if size != expected {
583- 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+ }
584619 }
585620}
586621
587- func getShmMount (s * Spec ) * specs.Mount {
622+ func getDevShmMount (s * Spec ) * specs.Mount {
588623 for _ , m := range s .Mounts {
589- if m .Source == "shm" && m .Type == "tmpfs" {
624+ if filepath . Clean ( m . Destination ) == "/dev/shm" && m .Source == "shm" && m .Type == "tmpfs" {
590625 return & m
591626 }
592627 }
593628 return nil
594629}
595630
596- func getShmSize (opts []string ) string {
631+ func getShmSize (opts []string ) (string , error ) {
632+ // linux will use the last size option
633+ var so string
597634 for _ , o := range opts {
598635 if strings .HasPrefix (o , "size=" ) {
599- return o
636+ if so != "" {
637+ return "" , errors .New ("contains multiple size options" )
638+ }
639+ so = o
600640 }
601641 }
602- 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
603651}
0 commit comments