summaryrefslogtreecommitdiff
path: root/internal/tests/integration/inputdir_unix_test.go
blob: e72625d0b5dd2f3e5c6a5b3430fcb97c4b8b82f3 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//go:build !windows
// +build !windows

package integration

import (
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"testing"

	"golang.org/x/sys/unix"
	"gotest.tools/v3/assert"
	"gotest.tools/v3/fs"
)

func setFileUlimit(b uint64) error {
	ulimit := unix.Rlimit{
		Cur: b,
		Max: math.MaxInt64,
	}
	err := unix.Setrlimit(unix.RLIMIT_NOFILE, &ulimit)
	return err
}

func TestInputDir_RespectsUlimit(t *testing.T) {
	numfiles := 32
	flist := map[string]string{}
	for i := 0; i < numfiles; i++ {
		k := fmt.Sprintf("file_%d", i)
		flist[k] = fmt.Sprintf("hello world %d\n", i)
	}
	testdir := fs.NewDir(t, "ulimittestfiles",
		fs.WithDir("in", fs.WithFiles(flist)),
	)
	defer testdir.Remove()

	// we need another ~11 fds for other various things, so we'd be guaranteed
	// to hit the limit if we try to have all the input files open
	// simultaneously
	setFileUlimit(uint64(numfiles))
	defer setFileUlimit(8192)

	o, e, err := cmd(t, "--input-dir", testdir.Join("in"),
		"--output-dir", testdir.Join("out")).
		withDir(testdir.Path()).run()

	setFileUlimit(8192)
	assertSuccess(t, o, e, err, "")

	files, err := ioutil.ReadDir(testdir.Join("out"))
	assert.NilError(t, err)
	assert.Equal(t, numfiles, len(files))

	for i := 0; i < numfiles; i++ {
		f := testdir.Join("out", fmt.Sprintf("file_%d", i))
		_, err := os.Stat(f)
		assert.NilError(t, err)

		content, err := ioutil.ReadFile(f)
		assert.NilError(t, err)
		expected := fmt.Sprintf("hello world %d\n", i)
		assert.Equal(t, expected, string(content))
	}
}