github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/fs/mount/system_test.go (about)

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