github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/tests/integration/inputdir_unix_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package integration 5 6 import ( 7 "fmt" 8 "os" 9 "testing" 10 11 "golang.org/x/sys/unix" 12 "gotest.tools/v3/assert" 13 "gotest.tools/v3/fs" 14 ) 15 16 func setFileUlimit(b uint64) error { 17 ulimit := unix.Rlimit{} 18 err := unix.Getrlimit(unix.RLIMIT_NOFILE, &ulimit) 19 if err != nil { 20 return err 21 } 22 23 ulimit.Cur = b 24 err = unix.Setrlimit(unix.RLIMIT_NOFILE, &ulimit) 25 return err 26 } 27 28 func checkFileUlimit(t *testing.T, b uint64) { 29 ulimit := unix.Rlimit{} 30 err := unix.Getrlimit(unix.RLIMIT_NOFILE, &ulimit) 31 assert.NilError(t, err) 32 assert.Equal(t, b, ulimit.Cur) 33 } 34 35 func TestInputDir_RespectsUlimit(t *testing.T) { 36 numfiles := 32 37 flist := map[string]string{} 38 for i := 0; i < numfiles; i++ { 39 k := fmt.Sprintf("file_%d", i) 40 flist[k] = fmt.Sprintf("hello world %d\n", i) 41 } 42 testdir := fs.NewDir(t, "ulimittestfiles", 43 fs.WithDir("in", fs.WithFiles(flist)), 44 ) 45 defer testdir.Remove() 46 47 // we need another ~11 fds for other various things, so we'd be guaranteed 48 // to hit the limit if we try to have all the input files open 49 // simultaneously 50 setFileUlimit(uint64(numfiles)) 51 defer setFileUlimit(8192) 52 53 // make sure the ulimit is actually set correctly - the capability may not 54 // be available in some environments, and we don't want to pass if we can't 55 // actually test the behavior 56 checkFileUlimit(t, uint64(numfiles)) 57 58 o, e, err := cmd(t, "--input-dir", testdir.Join("in"), 59 "--output-dir", testdir.Join("out")). 60 withDir(testdir.Path()).run() 61 62 setFileUlimit(8192) 63 checkFileUlimit(t, 8192) 64 assertSuccess(t, o, e, err, "") 65 66 files, err := os.ReadDir(testdir.Join("out")) 67 assert.NilError(t, err) 68 assert.Equal(t, numfiles, len(files)) 69 70 for i := 0; i < numfiles; i++ { 71 f := testdir.Join("out", fmt.Sprintf("file_%d", i)) 72 _, err := os.Stat(f) 73 assert.NilError(t, err) 74 75 content, err := os.ReadFile(f) 76 assert.NilError(t, err) 77 expected := fmt.Sprintf("hello world %d\n", i) 78 assert.Equal(t, expected, string(content)) 79 } 80 }