Add tests

This commit is contained in:
Elara 2023-10-23 17:01:08 -07:00
parent a59cb5b607
commit b130d64a68
1 changed files with 59 additions and 0 deletions

59
fakeroot_test.go Normal file
View File

@ -0,0 +1,59 @@
package fakeroot_test
import (
"errors"
"os/exec"
"syscall"
"testing"
"lure.sh/fakeroot"
)
func TestCommand(t *testing.T) {
cmd, err := fakeroot.Command("whoami")
if err != nil {
t.Errorf("Unexpected error while creating command: %q", err)
}
data, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("Unexpected error while executing command: %q", err)
}
if sdata := string(data); sdata != "root\n" {
t.Errorf("Expected %q, got %q", "root\n", sdata)
}
}
func TestCommandUIDError(t *testing.T) {
cmd := exec.Command("whoami")
cmd.SysProcAttr = &syscall.SysProcAttr{
UidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: 1000,
Size: 1,
},
},
}
err := fakeroot.Apply(cmd)
if !errors.Is(err, fakeroot.ErrRootUIDAlreadyMapped) {
t.Errorf("Expected error %q, got %q", fakeroot.ErrRootUIDAlreadyMapped, err)
}
}
func TestCommandGIDError(t *testing.T) {
cmd := exec.Command("whoami")
cmd.SysProcAttr = &syscall.SysProcAttr{
GidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: 1000,
Size: 1,
},
},
}
err := fakeroot.Apply(cmd)
if !errors.Is(err, fakeroot.ErrRootGIDAlreadyMapped) {
t.Errorf("Expected error %q, got %q", fakeroot.ErrRootGIDAlreadyMapped, err)
}
}