tests: respect TMPDIR if set

Setting TMPDIR now allows to run the tests against
a directory of your choice, making it easier to test
different filesystems.
This commit is contained in:
Jakob Unterwurzacher 2018-10-10 22:24:20 +02:00
parent e4f1a32a9a
commit 5a1ebdb4f7
3 changed files with 22 additions and 15 deletions

View File

@ -1,10 +1,16 @@
#!/bin/bash #!/bin/bash
if [[ -z $TMPDIR ]]; then
TMPDIR=/tmp
else
echo "Using TMPDIR=$TMPDIR"
fi
set -eu set -eu
cd "$(dirname "$0")" cd "$(dirname "$0")"
MYNAME=$(basename "$0") MYNAME=$(basename "$0")
TESTDIR=/tmp/gocryptfs-test-parent TESTDIR=$TMPDIR/gocryptfs-test-parent
mkdir -p $TESTDIR mkdir -p $TESTDIR
LOCKFILE=$TESTDIR/$MYNAME.lock LOCKFILE=$TESTDIR/$MYNAME.lock

View File

@ -21,8 +21,9 @@ import (
"github.com/rfjakob/gocryptfs/internal/nametransform" "github.com/rfjakob/gocryptfs/internal/nametransform"
) )
// TmpDir will be created inside this directory // TmpDir will be created inside this directory, set in init() to
var testParentDir = "/tmp/gocryptfs-test-parent" // $TMPDIR/gocryptfs-test-parent .
var testParentDir = ""
// GocryptfsBinary is the assumed path to the gocryptfs build. // GocryptfsBinary is the assumed path to the gocryptfs build.
const GocryptfsBinary = "../../gocryptfs" const GocryptfsBinary = "../../gocryptfs"
@ -43,11 +44,11 @@ var DefaultPlainDir string
// DefaultCipherDir is TmpDir + "/default-cipher" // DefaultCipherDir is TmpDir + "/default-cipher"
var DefaultCipherDir string var DefaultCipherDir string
// SwitchTestParentDir changes testParentDir. This is used when you want // SwitchTMPDIR changes TMPDIR and hence the directory the test are performed in.
// to perform tests on a special filesystem. For example, the xattr tests // This is used when you want to perform tests on a special filesystem. The
// cannot run on tmpfs and use /var/tmp instead of /tmp. // xattr tests cannot run on tmpfs and use /var/tmp instead of /tmp.
func SwitchTestParentDir(newDir string) { func SwitchTMPDIR(newDir string) {
testParentDir = newDir os.Setenv("TMPDIR", newDir)
doInit() doInit()
} }
@ -58,6 +59,7 @@ func init() {
func doInit() { func doInit() {
X255 = string(bytes.Repeat([]byte("X"), 255)) X255 = string(bytes.Repeat([]byte("X"), 255))
testParentDir := os.TempDir() + "/gocryptfs-test-parent"
os.MkdirAll(testParentDir, 0700) os.MkdirAll(testParentDir, 0700)
var err error var err error
TmpDir, err = ioutil.TempDir(testParentDir, "") TmpDir, err = ioutil.TempDir(testParentDir, "")

View File

@ -16,16 +16,15 @@ import (
"github.com/rfjakob/gocryptfs/tests/test_helpers" "github.com/rfjakob/gocryptfs/tests/test_helpers"
) )
// On modern Linux distributions, /tmp may be on tmpfs,
// which does not support user xattrs. Try /var/tmp instead.
var alternateTestParentDir = "/var/tmp/gocryptfs-xattr-test-parent"
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
if !xattrSupported(test_helpers.TmpDir) { // On modern Linux distributions, /tmp may be on tmpfs,
test_helpers.SwitchTestParentDir(alternateTestParentDir) // which does not support user xattrs. Try /var/tmp instead
if !xattrSupported(test_helpers.TmpDir) && os.TempDir() == "/tmp" {
fmt.Printf("Switching from /tmp to /var/tmp for xattr tests\n")
test_helpers.SwitchTMPDIR("/var/tmp")
} }
if !xattrSupported(test_helpers.TmpDir) { if !xattrSupported(test_helpers.TmpDir) {
fmt.Printf("xattrs not supported on %q", test_helpers.TmpDir) fmt.Printf("xattrs not supported on %q\n", test_helpers.TmpDir)
os.Exit(1) os.Exit(1)
} }
test_helpers.ResetTmpDir(true) test_helpers.ResetTmpDir(true)