Skip to content

Commit e1a0f00

Browse files
author
Ma Shimiao
committed
runtimetest: add mounts order validation
Signed-off-by: Ma Shimiao <[email protected]>
1 parent 9e0e42d commit e1a0f00

1 file changed

Lines changed: 66 additions & 12 deletions

File tree

cmd/runtimetest/main.go

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,17 @@ func validateGIDMappings(spec *rspec.Spec) error {
577577
return validateIDMappings(spec.Linux.GIDMappings, "/proc/self/gid_map", "linux.gidMappings")
578578
}
579579

580-
func mountMatch(specMount rspec.Mount, sysMount rspec.Mount) error {
581-
if filepath.Clean(specMount.Destination) != sysMount.Destination {
582-
return fmt.Errorf("mount destination expected: %v, actual: %v", specMount.Destination, sysMount.Destination)
580+
func mountMatch(configMount rspec.Mount, sysMount rspec.Mount) error {
581+
if filepath.Clean(configMount.Destination) != sysMount.Destination {
582+
return fmt.Errorf("mount destination expected: %v, actual: %v", configMount.Destination, sysMount.Destination)
583583
}
584584

585-
if specMount.Type != sysMount.Type {
586-
return fmt.Errorf("mount %v type expected: %v, actual: %v", specMount.Destination, specMount.Type, sysMount.Type)
585+
if configMount.Type != sysMount.Type {
586+
return fmt.Errorf("mount %v type expected: %v, actual: %v", configMount.Destination, configMount.Type, sysMount.Type)
587587
}
588588

589-
if filepath.Clean(specMount.Source) != sysMount.Source {
590-
return fmt.Errorf("mount %v source expected: %v, actual: %v", specMount.Destination, specMount.Source, sysMount.Source)
589+
if filepath.Clean(configMount.Source) != sysMount.Source {
590+
return fmt.Errorf("mount %v source expected: %v, actual: %v", configMount.Destination, configMount.Source, sysMount.Source)
591591
}
592592

593593
return nil
@@ -609,21 +609,71 @@ func validateMountsExist(spec *rspec.Spec) error {
609609
mountsMap[mountInfo.Mountpoint] = append(mountsMap[mountInfo.Mountpoint], m)
610610
}
611611

612-
for _, specMount := range spec.Mounts {
613-
if specMount.Type == "bind" || specMount.Type == "rbind" {
612+
for _, configMount := range spec.Mounts {
613+
if configMount.Type == "bind" || configMount.Type == "rbind" {
614614
// TODO: add bind or rbind check.
615615
continue
616616
}
617617

618618
found := false
619-
for _, sysMount := range mountsMap[filepath.Clean(specMount.Destination)] {
620-
if err := mountMatch(specMount, sysMount); err == nil {
619+
for _, sysMount := range mountsMap[filepath.Clean(configMount.Destination)] {
620+
if err := mountMatch(configMount, sysMount); err == nil {
621621
found = true
622622
break
623623
}
624624
}
625625
if !found {
626-
return fmt.Errorf("Expected mount %v does not exist", specMount)
626+
return fmt.Errorf("Expected mount %v does not exist", configMount)
627+
}
628+
}
629+
630+
return nil
631+
}
632+
633+
func validateMountsOrder(spec *rspec.Spec) error {
634+
if runtime.GOOS == "windows" {
635+
logrus.Warnf("mounts order validation not yet implemented for OS %q", runtime.GOOS)
636+
return nil
637+
}
638+
639+
mountInfos, err := mount.GetMounts()
640+
if err != nil {
641+
return err
642+
}
643+
644+
type mountOrder struct {
645+
Order int
646+
Root string
647+
Dest string
648+
Source string
649+
}
650+
mountsMap := make(map[string][]mountOrder)
651+
for i, mountInfo := range mountInfos {
652+
m := mountOrder{
653+
Order: i,
654+
Root: mountInfo.Root,
655+
Dest: mountInfo.Mountpoint,
656+
Source: mountInfo.Source,
657+
}
658+
mountsMap[mountInfo.Mountpoint] = append(mountsMap[mountInfo.Mountpoint], m)
659+
}
660+
current := -1
661+
for _, configMount := range spec.Mounts {
662+
mounts := mountsMap[configMount.Destination]
663+
for _, mount := range mounts {
664+
source := mount.Source
665+
for _, option := range configMount.Options {
666+
if option == "bind" || option == "rbind" {
667+
source = mount.Root
668+
}
669+
}
670+
if source == configMount.Source {
671+
if current > mount.Order {
672+
return fmt.Errorf("%s is not mounted in order", configMount.Source)
673+
}
674+
current = mount.Order
675+
break
676+
}
627677
}
628678
}
629679

@@ -659,6 +709,10 @@ func run(context *cli.Context) error {
659709
test: validateMountsExist,
660710
description: "mounts",
661711
},
712+
{
713+
test: validateMountsOrder,
714+
description: "mounts order",
715+
},
662716
}
663717

664718
linuxValidations := []validation{

0 commit comments

Comments
 (0)