@@ -27,6 +27,7 @@ import (
2727 "os"
2828 "reflect"
2929 "runtime"
30+ "strings"
3031 "testing"
3132
3233 "github.com/containerd/containerd/content"
@@ -506,3 +507,56 @@ func TestDropCaps(t *testing.T) {
506507 }
507508 }
508509}
510+
511+ func TestDevShmSize (t * testing.T ) {
512+ t .Parallel ()
513+ var (
514+ s Spec
515+ c = containers.Container {ID : t .Name ()}
516+ ctx = namespaces .WithNamespace (context .Background (), "test" )
517+ )
518+
519+ err := populateDefaultUnixSpec (ctx , & s , c .ID )
520+ if err != nil {
521+ t .Fatal (err )
522+ }
523+
524+ expected := "1024k"
525+ if err := WithDevShmSize (1024 )(nil , nil , nil , & s ); err != nil {
526+ t .Fatal (err )
527+ }
528+ m := getShmMount (& s )
529+ if m == nil {
530+ t .Fatal ("no shm mount found" )
531+ }
532+ o := getShmSize (m .Options )
533+ if o == "" {
534+ t .Fatal ("shm size not specified" )
535+ }
536+ parts := strings .Split (o , "=" )
537+ if len (parts ) != 2 {
538+ t .Fatal ("invalid size format" )
539+ }
540+ size := parts [1 ]
541+ if size != expected {
542+ t .Fatalf ("size %s not equal %s" , size , expected )
543+ }
544+ }
545+
546+ func getShmMount (s * Spec ) * specs.Mount {
547+ for _ , m := range s .Mounts {
548+ if m .Source == "shm" && m .Type == "tmpfs" {
549+ return & m
550+ }
551+ }
552+ return nil
553+ }
554+
555+ func getShmSize (opts []string ) string {
556+ for _ , o := range opts {
557+ if strings .HasPrefix (o , "size=" ) {
558+ return o
559+ }
560+ }
561+ return ""
562+ }
0 commit comments