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
|
package open
import (
"os"
"path/filepath"
"testing"
"strings"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
func TestBindFdToPath(t *testing.T) {
first := t.TempDir()
sampleData := []byte("sample data")
err := os.WriteFile(filepath.Join(first, "testfile"), sampleData, 0o600)
require.NoError(t, err, "writing sample data to first directory")
fd, err := unix.Open(first, unix.O_DIRECTORY, 0)
require.NoError(t, err, "opening descriptor for first directory")
second := t.TempDir()
err = BindFdToPath(uintptr(fd), second)
if strings.Contains(err.Error(), "operation not permitted") {
t.Skipf("Skipping test: %v", err)
}
require.NoError(t, err)
t.Cleanup(func() {
err := unix.Unmount(second, unix.MNT_DETACH)
require.NoError(t, err, "unmounting as part of cleanup")
})
readBack, err := os.ReadFile(filepath.Join(second, "testfile"))
require.NoError(t, err)
require.Equal(t, sampleData, readBack, "expected to read back data via the bind mount")
}
|