-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
175 lines (136 loc) · 3.58 KB
/
api_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package extremofile
import (
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testDirPrefix = "test-extremofile-delete-this-"
type tenv struct {
t testing.TB
tmpdir string
e *efile
w io.Writer
}
func newTest(t testing.TB) *tenv {
tmpdir, err := ioutil.TempDir("", testDirPrefix)
require.NoError(t, err)
data, e, err := Open(tmpdir)
require.NoError(t, err)
require.NotNil(t, e)
require.Nil(t, data)
return &tenv{t: t, tmpdir: tmpdir, e: e.(*efile), w: e}
}
func TestCreateDir(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", testDirPrefix)
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
dir := filepath.Join(tmpdir, "sub", "dir")
data, e, err := Open(dir)
require.NoError(t, err)
require.NotNil(t, e)
assert.Nil(t, data)
data = []byte("test data")
_, werr := e.Write(data)
require.NoError(t, werr)
rdata, _, rerr := Open(dir)
require.NoError(t, rerr)
assert.Equal(t, data, rdata)
}
func TestOpenEmpty(t *testing.T) {
t.Parallel()
env := newTest(t)
defer os.RemoveAll(env.tmpdir)
}
func TestWriteReadSame(t *testing.T) {
t.Parallel()
env := newTest(t)
defer os.RemoveAll(env.tmpdir)
data := []byte("test data")
_, werr := env.e.Write(data)
require.NoError(t, werr)
rdata, _, rerr := Open(env.tmpdir)
require.NoError(t, rerr)
assert.Equal(t, data, rdata)
}
func TestMainCorruptBackupOK(t *testing.T) {
t.Parallel()
env := newTest(t)
defer os.RemoveAll(env.tmpdir)
data := []byte("test data")
_, werr := env.e.Write(data)
require.NoError(t, werr)
corrupt(t, env.e.pathMain, 1)
rdata, _, rerr := Open(env.tmpdir)
assertErrorCorrupt(t, rerr, true)
assertErrorCritical(t, rerr, false)
assert.Equal(t, data, rdata)
}
func TestMainCorruptMissingBackup(t *testing.T) {
t.Parallel()
env := newTest(t)
defer os.RemoveAll(env.tmpdir)
data := []byte("test data")
_, werr := env.e.Write(data)
require.NoError(t, werr)
corrupt(t, env.e.pathMain, 1)
os.Remove(env.e.pathBackup)
rdata, _, rerr := Open(env.tmpdir)
assert.Nil(t, rdata)
assert.Error(t, rerr)
assertErrorCorrupt(t, rerr, true)
assertErrorCritical(t, rerr, true)
}
func TestMainMissingBackupCorrupt(t *testing.T) {
t.Parallel()
env := newTest(t)
defer os.RemoveAll(env.tmpdir)
data := []byte("test data")
_, werr := env.e.Write(data)
require.NoError(t, werr)
os.Remove(env.e.pathMain)
corrupt(t, env.e.pathBackup, 1)
rdata, _, rerr := Open(env.tmpdir)
assert.Nil(t, rdata)
assert.Error(t, rerr)
assertErrorCorrupt(t, rerr, true)
assertErrorCritical(t, rerr, true)
}
func TestBothCorrupt(t *testing.T) {
t.Parallel()
env := newTest(t)
defer os.RemoveAll(env.tmpdir)
data := []byte("test data")
_, werr := env.e.Write(data)
require.NoError(t, werr)
corrupt(t, env.e.pathMain, 1)
corrupt(t, env.e.pathBackup, 1)
rdata, _, rerr := Open(env.tmpdir)
assert.Nil(t, rdata)
assertErrorCorrupt(t, rerr, true)
assertErrorCritical(t, rerr, true)
}
func assertErrorCorrupt(t testing.TB, e error, expect bool) {
assert.Error(t, e)
assert.Equal(t, expect, IsCorrupt(e), "IsCorrupt type=%#v err=%v", e, e)
}
func assertErrorCritical(t testing.TB, e error, expect bool) {
assert.Error(t, e)
assert.Equal(t, expect, IsCritical(e), "IsCritical type=%#v err=%v", e, e)
}
func corrupt(t testing.TB, path string, num uint32) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
randbs := make([]byte, num)
rnd.Read(randbs)
f, err := os.OpenFile(path, os.O_WRONLY, 0)
require.NoError(t, err)
_, err = f.WriteAt(randbs, 0)
require.NoError(t, err)
f.Close()
}