-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from zimmski/additions
Cleanups, file resource limit helper, environment variable with default value helper, query files of path and subpaths helper
- Loading branch information
Showing
9 changed files
with
129 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,25 @@ | ||
package osutil | ||
|
||
import ( | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func setAndGetSmallRLimit(t *testing.T) syscall.Rlimit { | ||
var limit syscall.Rlimit | ||
|
||
limit.Max = 10 | ||
limit.Cur = 10 | ||
|
||
assert.NoError(t, syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit)) | ||
assert.NoError(t, syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit)) | ||
|
||
return limit | ||
} | ||
|
||
func TestCapture(t *testing.T) { | ||
limit := setAndGetSmallRLimit(t) | ||
|
||
// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks. | ||
for i := 0; i <= int(limit.Cur); i++ { | ||
testCapture(t) | ||
} | ||
assert.NoError(t, SetRLimitFiles(10, func(limit uint64) { | ||
// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks. | ||
for i := 0; i <= int(limit); i++ { | ||
testCapture(t) | ||
} | ||
})) | ||
} | ||
|
||
func TestCaptureWithCGo(t *testing.T) { | ||
limit := setAndGetSmallRLimit(t) | ||
|
||
// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks. | ||
for i := 0; i <= int(limit.Cur); i++ { | ||
testCaptureWithCGo(t) | ||
} | ||
assert.NoError(t, SetRLimitFiles(10, func(limit uint64) { | ||
// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks. | ||
for i := 0; i <= int(limit); i++ { | ||
testCaptureWithCGo(t) | ||
} | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package osutil | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// EnvOrDefault returns the environment variable with the given key, or the default value if the key is not defined. | ||
func EnvOrDefault(key string, defaultValue string) string { | ||
if v, ok := os.LookupEnv(key); ok { | ||
return v | ||
} | ||
|
||
return defaultValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package osutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// FilesRecursive returns all files in a given path and its subpaths. | ||
func FilesRecursive(path string) (files []string, err error) { | ||
var fs []string | ||
|
||
err = filepath.Walk(path, func(path string, f os.FileInfo, err error) error { | ||
if f.IsDir() { | ||
return nil | ||
} | ||
|
||
fs = append(fs, path) | ||
|
||
return err | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return fs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package osutil | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFilesRecursive(t *testing.T) { | ||
path, err := ioutil.TempDir("", "os-util") | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, os.MkdirAll(path+"/a/b", 0750)) | ||
assert.NoError(t, ioutil.WriteFile(path+"/c.txt", []byte("foobar"), 0640)) | ||
assert.NoError(t, ioutil.WriteFile(path+"/a/d.txt", []byte("foobar"), 0640)) | ||
assert.NoError(t, ioutil.WriteFile(path+"/a/b/e.txt", []byte("foobar"), 0640)) | ||
|
||
fs, err := FilesRecursive(path) | ||
assert.NoError(t, err) | ||
|
||
sort.Strings(fs) | ||
|
||
assert.Equal( | ||
t, | ||
[]string{ | ||
path + "/a/b/e.txt", | ||
path + "/a/d.txt", | ||
path + "/c.txt", | ||
}, | ||
fs, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package osutil | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// SetRLimitFiles temporarily changes the file descriptor resource limit while calling the given function. | ||
func SetRLimitFiles(limit uint64, call func(limit uint64)) (err error) { | ||
var tmp syscall.Rlimit | ||
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &tmp); err != nil { | ||
return nil | ||
} | ||
defer func() { | ||
if err == nil { | ||
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &tmp) | ||
} | ||
}() | ||
|
||
if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{ | ||
Cur: limit, | ||
Max: tmp.Max, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
call(limit) | ||
|
||
return nil | ||
} |