This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
137 lines (107 loc) · 3.7 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//go:build linux
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/fuweid/go-dmflakey/contrib/utils"
"github.com/fuweid/go-dmflakey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
)
func TestMain(m *testing.M) {
requires()
os.Exit(m.Run())
}
func requires() {
if os.Getuid() != 0 {
fmt.Fprintln(os.Stderr, "This test must be run as root.")
os.Exit(1)
}
_, err := exec.LookPath("bbolt")
if err != nil {
fmt.Fprintln(os.Stderr, "This test requires bbolt command")
os.Exit(1)
}
}
// TestDropWritesDuringBench is used to drop_writes during bbolt bench. It's
// used to cause data loss during power failure.
func TestDropWritesDuringBench(t *testing.T) {
flakey, root := initFlakeyDevice(t, dmflakey.FSTypeEXT4)
require.NoError(t, utils.Mount(root, flakey.DevicePath(), ""))
dbPath := filepath.Join(root, "boltdb")
// NOTE: Creates database with large size to make sure there is no file's
// The flakey device handles IO in block layer, which means that it's
// unlikely to distinguish the content between file's and filesystem's
// metadata. So, using large size is to ensure that there is no fsync
// syscall called from bbolt-bench command. All the data synced is
// triggered by fdatasync. So, we can ensure all the dropped data is
// from file. Otherwise, it's easy to break filesystem.
t.Logf("Init empty bbolt database with 128 MiB")
initEmptyBoltdb(t, dbPath)
// Ensure all the data has been persisted in the device.
t.Logf("Ensure the empty boltdb data persisted in the flakey device")
utils.SyncFS(dbPath)
t.Logf("Start to run bbolt-bench")
cmdCh, waitCh := make(chan *exec.Cmd), make(chan error)
// run bbolt bench
go func() {
defer close(waitCh)
args := []string{"bbolt", "bench",
"-work", // keep the database
"-path", dbPath,
"-count=1000000000",
"-batch-size=5", // separate total count into multiple truncation
}
cmd := exec.Command(args[0], args[1:]...)
cmdCh <- cmd
require.NoError(t, cmd.Start(), "start bbolt (args: %v)", args[1:])
waitCh <- cmd.Wait()
}()
cmd := <-cmdCh
t.Logf("Drop all the write IOs after 3 seconds")
time.Sleep(3 * time.Second)
require.NoError(t, flakey.DropWrites())
t.Logf("Let bbolt-bench run with DropWrites mode for 3 seconds")
time.Sleep(3 * time.Second)
t.Logf("Start to allow all the write IOs for 2 seconds")
require.NoError(t, flakey.AllowWrites())
time.Sleep(2 * time.Second)
t.Logf("Kill the bbolt process and simulate power failure")
cmd.Process.Kill()
require.Error(t, <-waitCh)
require.NoError(t, utils.SimulatePowerFailure(flakey, root))
t.Logf("Invoke bbolt check to verify data")
output, err := exec.Command("bbolt", "check", dbPath).CombinedOutput()
require.NoError(t, err, "bbolt check output: %s", string(output))
}
// initEmptyBoltdb inits empty boltdb with 128 MiB.
func initEmptyBoltdb(t *testing.T, dbPath string) {
_, err := os.Stat(dbPath)
require.True(t, errors.Is(err, os.ErrNotExist))
db, err := bbolt.Open(dbPath, 0600, nil)
require.NoError(t, err)
require.NoError(t, db.Close())
dbFd, err := os.OpenFile(dbPath, os.O_RDWR, 0600)
require.NoError(t, err)
defer dbFd.Close()
require.NoError(t, dbFd.Truncate(128*1024*1024))
require.NoError(t, dbFd.Sync())
}
func initFlakeyDevice(t *testing.T, fsType dmflakey.FSType) (_ dmflakey.Flakey, root string) {
tmpDir := t.TempDir()
target := filepath.Join(tmpDir, "root")
require.NoError(t, os.MkdirAll(target, 0600))
flakey, err := dmflakey.InitFlakey("boltdb", tmpDir, fsType)
require.NoError(t, err, "init flakey")
t.Cleanup(func() {
assert.NoError(t, utils.Unmount(target))
assert.NoError(t, flakey.Teardown())
})
return flakey, target
}