Skip to content

Commit 9cb82b9

Browse files
authored
Misc test tweaks (mistifyio#82)
Just some minor reworks for readability.
2 parents d014733 + 156bfc9 commit 9cb82b9

File tree

6 files changed

+343
-318
lines changed

6 files changed

+343
-318
lines changed

.github/workflows/formatters.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ if ! nixfmt shell.nix; then
1818
failed=1
1919
fi
2020

21+
if ! rufo Vagrantfile; then
22+
failed=1
23+
fi
24+
2125
if ! git diff | (! grep .); then
2226
failed=1
2327
fi

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bin
2+
go-zfs.test
23
.vagrant
34

45
# added by lint-install

Vagrantfile

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
21
VAGRANTFILE_API_VERSION = "2"
32

43
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
5-
config.vm.box = "ubuntu/trusty64"
6-
config.ssh.forward_agent = true
4+
config.vm.box = "ubuntu/trusty64"
5+
config.ssh.forward_agent = true
76

8-
config.vm.synced_folder ".", "/home/vagrant/go/src/github.com/mistifyio/go-zfs", create: true
7+
config.vm.synced_folder ".", "/home/vagrant/go/src/github.com/mistifyio/go-zfs", create: true
98

10-
config.vm.provision "shell", inline: <<EOF
9+
config.vm.provision "shell", inline: <<EOF
1110
cat << END > /etc/profile.d/go.sh
1211
export GOPATH=\\$HOME/go
1312
export PATH=\\$GOPATH/bin:/usr/local/go/bin:\\$PATH
@@ -30,5 +29,4 @@ Defaults env_keep += "GOPATH"
3029
END
3130
3231
EOF
33-
3432
end

shell.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mkShell {
1919
nodePackages.prettier
2020
python3Packages.pip
2121
python3Packages.setuptools
22+
rufo
2223
shfmt
2324
vagrant
2425
];

test_helpers_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package zfs_test
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"math"
7+
"os"
8+
"path/filepath"
9+
"reflect"
10+
"runtime"
11+
"testing"
12+
"time"
13+
14+
"github.com/mistifyio/go-zfs/v3"
15+
)
16+
17+
func sleep(delay int) {
18+
time.Sleep(time.Duration(delay) * time.Second)
19+
}
20+
21+
func pow2(x int) int64 {
22+
return int64(math.Pow(2, float64(x)))
23+
}
24+
25+
// https://github.com/benbjohnson/testing
26+
// assert fails the test if the condition is false.
27+
func _assert(t *testing.T, condition bool, msg string, v ...interface{}) {
28+
t.Helper()
29+
30+
if !condition {
31+
_, file, line, _ := runtime.Caller(2)
32+
t.Logf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
33+
t.FailNow()
34+
}
35+
}
36+
37+
func assert(t *testing.T, condition bool, msg string, v ...interface{}) {
38+
t.Helper()
39+
_assert(t, condition, msg, v...)
40+
}
41+
42+
// ok fails the test if an err is not nil.
43+
func ok(t *testing.T, err error) {
44+
t.Helper()
45+
_assert(t, err == nil, "unexpected error: %v", err)
46+
}
47+
48+
// nok fails the test if an err is nil.
49+
func nok(t *testing.T, err error) {
50+
t.Helper()
51+
_assert(t, err != nil, "expected error, got nil")
52+
}
53+
54+
// equals fails the test if exp is not equal to act.
55+
func equals(t *testing.T, exp, act interface{}) {
56+
t.Helper()
57+
_assert(t, reflect.DeepEqual(exp, act), "exp: %#v\n\ngot: %#v", exp, act)
58+
}
59+
60+
type cleanUpFunc func()
61+
62+
func (f cleanUpFunc) cleanUp() {
63+
f()
64+
}
65+
66+
// do something like Restorer in github.com/packethost/pkg/internal/testenv/clearer.go
67+
func setupZPool(t *testing.T) cleanUpFunc {
68+
t.Helper()
69+
70+
d, err := ioutil.TempDir("/tmp/", "zfs-test-*")
71+
ok(t, err)
72+
73+
var skipRemoveAll bool
74+
defer func() {
75+
if !skipRemoveAll {
76+
t.Logf("cleaning up")
77+
os.RemoveAll(d)
78+
}
79+
}()
80+
81+
tempfiles := make([]string, 3)
82+
for i := range tempfiles {
83+
f, err := ioutil.TempFile(d, fmt.Sprintf("loop%d", i))
84+
ok(t, err)
85+
86+
ok(t, f.Truncate(pow2(30)))
87+
88+
f.Close()
89+
tempfiles[i] = f.Name()
90+
}
91+
92+
pool, err := zfs.CreateZpool("test", nil, tempfiles...)
93+
ok(t, err)
94+
95+
skipRemoveAll = true
96+
return func() {
97+
ok(t, pool.Destroy())
98+
os.RemoveAll(d)
99+
}
100+
}

0 commit comments

Comments
 (0)