forked from mistifyio/go-zfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Move helpers to a different file
Sucks having to scroll through helpers to get to the tests.
- Loading branch information
Showing
2 changed files
with
100 additions
and
89 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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package zfs_test | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"math" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mistifyio/go-zfs/v3" | ||
) | ||
|
||
func sleep(delay int) { | ||
time.Sleep(time.Duration(delay) * time.Second) | ||
} | ||
|
||
func pow2(x int) int64 { | ||
return int64(math.Pow(2, float64(x))) | ||
} | ||
|
||
// https://github.com/benbjohnson/testing | ||
// assert fails the test if the condition is false. | ||
func _assert(t *testing.T, condition bool, msg string, v ...interface{}) { | ||
t.Helper() | ||
|
||
if !condition { | ||
_, file, line, _ := runtime.Caller(2) | ||
t.Logf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func assert(t *testing.T, condition bool, msg string, v ...interface{}) { | ||
t.Helper() | ||
_assert(t, condition, msg, v...) | ||
} | ||
|
||
// ok fails the test if an err is not nil. | ||
func ok(t *testing.T, err error) { | ||
t.Helper() | ||
_assert(t, err == nil, "unexpected error: %v", err) | ||
} | ||
|
||
// nok fails the test if an err is nil. | ||
func nok(t *testing.T, err error) { | ||
t.Helper() | ||
_assert(t, err != nil, "expected error, got nil") | ||
} | ||
|
||
// equals fails the test if exp is not equal to act. | ||
func equals(t *testing.T, exp, act interface{}) { | ||
t.Helper() | ||
_assert(t, reflect.DeepEqual(exp, act), "exp: %#v\n\ngot: %#v", exp, act) | ||
} | ||
|
||
type cleanUpFunc func() | ||
|
||
func (f cleanUpFunc) cleanUp() { | ||
f() | ||
} | ||
|
||
// do something like Restorer in github.com/packethost/pkg/internal/testenv/clearer.go | ||
func setupZPool(t *testing.T) cleanUpFunc { | ||
t.Helper() | ||
|
||
d, err := ioutil.TempDir("/tmp/", "zfs-test-*") | ||
ok(t, err) | ||
|
||
var skipRemoveAll bool | ||
defer func() { | ||
if !skipRemoveAll { | ||
t.Logf("cleaning up") | ||
os.RemoveAll(d) | ||
} | ||
}() | ||
|
||
tempfiles := make([]string, 3) | ||
for i := range tempfiles { | ||
f, err := ioutil.TempFile(d, fmt.Sprintf("loop%d", i)) | ||
ok(t, err) | ||
|
||
ok(t, f.Truncate(pow2(30))) | ||
|
||
f.Close() | ||
tempfiles[i] = f.Name() | ||
} | ||
|
||
pool, err := zfs.CreateZpool("test", nil, tempfiles...) | ||
ok(t, err) | ||
|
||
skipRemoveAll = true | ||
return func() { | ||
ok(t, pool.Destroy()) | ||
os.RemoveAll(d) | ||
} | ||
} |
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