File: open_linux_test.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (33 lines) | stat: -rw-r--r-- 993 bytes parent folder | download
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")
}