1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package mount
import (
"fmt"
"syscall"
"testing"
"github.com/sylabs/singularity/v4/internal/pkg/test"
)
func TestSystem(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
points := &Points{}
points.AddBind(BindsTag, "/etc/hosts", "/etc/hosts", syscall.MS_BIND|syscall.MS_REC)
system := &System{
Points: points,
}
before := false
after := false
mnt := false
//nolint:unparam
mountFn := func(point *Point, system *System) error {
mnt = true
return nil
}
beforeHook := func(system *System) error {
before = true
tag := system.CurrentTag()
if tag != BindsTag {
return fmt.Errorf("bad tag returned: %s instead of %s", tag, BindsTag)
}
return nil
}
afterHook := func(system *System) error {
after = true
if system.Mount == nil {
system.Mount = mountFn
}
tag := system.CurrentTag()
if tag != BindsTag {
return fmt.Errorf("bad tag returned: %s instead of %s", tag, BindsTag)
}
return nil
}
if err := system.RunBeforeTag("fakeTag", beforeHook); err == nil {
t.Errorf("RunBeforeTag should have failed with unauthorized tag")
}
if err := system.RunAfterTag("fakeTag", afterHook); err == nil {
t.Errorf("RunAfterTag should have failed with unauthorized tag")
}
if err := system.RunBeforeTag(BindsTag, beforeHook); err != nil {
t.Error(err)
}
if err := system.RunAfterTag(BindsTag, afterHook); err != nil {
t.Error(err)
}
if err := system.MountAll(); err != nil {
t.Error(err)
}
if before == false {
t.Errorf("beforeHook wasn't executed")
}
if after == false {
t.Errorf("afterHook wasn't executed")
}
if err := system.MountAll(); err != nil {
t.Error(err)
}
if mnt == false {
t.Errorf("mountFn wasn't executed")
}
}
|