src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/unix/unix_test.go (about) 1 //go:build unix 2 3 package unix_test 4 5 import ( 6 "embed" 7 "errors" 8 "testing" 9 10 "golang.org/x/sys/unix" 11 "src.elv.sh/pkg/eval" 12 "src.elv.sh/pkg/eval/evaltest" 13 "src.elv.sh/pkg/eval/vars" 14 unixmod "src.elv.sh/pkg/mods/unix" 15 "src.elv.sh/pkg/testutil" 16 ) 17 18 //go:embed *.elvts 19 var transcripts embed.FS 20 21 func TestTranscripts(t *testing.T) { 22 // Intention is to restore umask after test finishes 23 testutil.Umask(t, 0) 24 evaltest.TestTranscriptsInFS(t, transcripts, 25 "mock-rlimit", mockRlimit, 26 ) 27 } 28 29 func mockRlimit(t *testing.T, ev *eval.Evaler) { 30 testutil.Set(t, unixmod.GetRlimit, func(res int, lim *unix.Rlimit) error { 31 switch res { 32 case unix.RLIMIT_CPU: 33 *lim = unix.Rlimit{Cur: unix.RLIM_INFINITY, Max: unix.RLIM_INFINITY} 34 case unix.RLIMIT_NOFILE: 35 *lim = unix.Rlimit{Cur: 30, Max: 40} 36 case unix.RLIMIT_STACK: 37 return errors.New("fake getrlimit error") 38 } 39 return nil 40 }) 41 42 var cpuCur, cpuMax int 43 testutil.Set(t, unixmod.SetRlimit, func(res int, lim *unix.Rlimit) error { 44 switch res { 45 case unix.RLIMIT_CPU: 46 cpuCur = rlimTToInt(lim.Cur) 47 cpuMax = rlimTToInt(lim.Max) 48 case unix.RLIMIT_NOFILE: 49 return errors.New("fake setrlimit error") 50 } 51 return nil 52 }) 53 54 ev.ExtendGlobal(eval.BuildNs(). 55 AddVar("cpu-cur", vars.FromPtr(&cpuCur)). 56 AddVar("cpu-max", vars.FromPtr(&cpuMax))) 57 } 58 59 func rlimTToInt(r unixmod.RlimT) int { 60 if r == unix.RLIM_INFINITY { 61 return -1 62 } 63 return int(r) 64 }