From b130d64a68ee5f4ac0b2be3317ad96dd8b51d540 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 23 Oct 2023 17:01:08 -0700 Subject: [PATCH] Add tests --- fakeroot_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 fakeroot_test.go diff --git a/fakeroot_test.go b/fakeroot_test.go new file mode 100644 index 0000000..95b259b --- /dev/null +++ b/fakeroot_test.go @@ -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) + } +}