-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpDeallocate_test.go
229 lines (192 loc) · 5 KB
/
dumpDeallocate_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bytes"
"golang.org/x/sys/unix"
"io/ioutil"
"os"
"testing"
)
func TestBoolToInt(t *testing.T) {
t.Run("true", func(t *testing.T) {
if returnV := BoolToInt(true); returnV != 1 {
t.Errorf("got '%v'; expected '%v'", returnV, 1)
}
})
t.Run("false", func(t *testing.T) {
if returnV := BoolToInt(false); returnV != 0 {
t.Errorf("got '%v'; expected '%v'", returnV, 0)
}
})
}
func TestFilesystemBlockSize(t *testing.T) {
// create the test file
file, err := os.Open("LICENSE")
if err != nil {
t.Fatal(err)
}
defer file.Close()
defer func() {
if r := recover(); r != nil {
t.Error("Panic: ", r)
}
}()
filesystemBlockSize := FilesystemBlockSize(file)
if filesystemBlockSize <= 0 {
t.Errorf("invalide filesystem block size returned : '%v'", filesystemBlockSize)
}
}
func TestCopyWhileDeallocate(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("Panic : ", r)
}
}()
// get content from LICENSE file
testContent, err := ioutil.ReadFile("LICENSE")
if err != nil {
t.Fatal(err)
}
// create the test file
file, err := ioutil.TempFile(".", "dump-deallocate-TestCopyWhileDeallocate-")
if err != nil {
t.Fatal(err)
}
// keep the file if the test fail
defer func() {
if !t.Failed() {
os.Remove(file.Name())
}
}()
defer file.Close()
// write the test content (LICENSE) on the test file
_, err = file.Write(testContent)
if err != nil {
t.Fatal(err)
}
// sync (just to be sure)
err = file.Sync()
if err != nil {
t.Fatal(err)
}
// seek to the begining of the file
_, err = file.Seek(0, 0)
if err != nil {
t.Fatal(err)
}
// get file stats before the CopyWhileDeallocate
var fileInfoBefore unix.Stat_t
err = unix.Fstat(int(file.Fd()), &fileInfoBefore)
if err != nil {
t.Fatal(err)
}
// buffer should be feed with the content of file (LICENSE)
// and file should be deallocated
outputBuffer := new(bytes.Buffer)
CopyWhileDeallocate(file, outputBuffer)
// check if buffer has been feed with content of file (LICENSE)
if !bytes.Equal(testContent, outputBuffer.Bytes()) {
t.Errorf("content hasn't been copied correctly, see '%s'", file.Name())
}
// check if file now only contain \0
_, err = file.Seek(0, 0)
if err != nil {
t.Fatal(err)
}
var fileNewContent []byte
fileNewContent, err = ioutil.ReadAll(file)
if err != nil {
t.Fatal(err)
}
if bytes.Count(fileNewContent, []byte{0}) != len(fileNewContent) {
t.Errorf("file should only contain \\0, see '%s'", file.Name())
}
// get file stats after the CopyWhileDeallocate
var fileInfoAfter unix.Stat_t
err = unix.Fstat(int(file.Fd()), &fileInfoAfter)
if err != nil {
t.Fatal(err)
}
// check if file has been deallocated
if fileInfoBefore.Size != fileInfoAfter.Size {
t.Errorf("file size, expected: '%v', got '%v', see '%s'", fileInfoBefore.Size, fileInfoAfter.Size, file.Name())
}
if fileInfoBefore.Blocks <= fileInfoAfter.Blocks {
t.Errorf("file blocks, expected: < '%v', got '%v', see '%s'", fileInfoBefore.Blocks, fileInfoAfter.Blocks, file.Name())
}
}
func TestCollapseFileStart(t *testing.T) {
var err error
var file *os.File
var fsBlockSize int64
// check TestCollapse
defer func() {
if r := recover(); r != nil {
t.Error("Panic: ", r)
}
}()
testCollapseErr := TestCollapse()
if testCollapseErr != nil && testCollapseErr != unix.EOPNOTSUPP {
t.Fatal(err)
}
// get fs block size
file, err = ioutil.TempFile(".", "dump-deallocate-TestCollapseFileStart-")
if err != nil {
t.Fatal(err)
}
fsBlockSize = FilesystemBlockSize(file)
err = file.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(file.Name())
if err != nil {
t.Fatal(err)
}
// create function used on each test
createTestFile := func(size int64) {
file, err = ioutil.TempFile(".", "dump-deallocate-TestCollapseFileStart-")
if err != nil {
t.Fatal(err)
}
// mode 0 = Default: allocate disk space
err = unix.Fallocate(int(file.Fd()), 0, 0, size)
if err != nil {
t.Fatal(err)
}
}
var byteActualyDeallocated int64
testCases := []struct {
name string
fileSize int64
bytesToDeallocate int64
expectedV int64
expectedE error
}{
{"1fsb|-1", fsBlockSize, -1, 0, errorZero},
{"1fsb|1", fsBlockSize, 1, 0, errorLessThanOneFsb},
{"2fsb|1fsb", 2 * fsBlockSize, fsBlockSize, fsBlockSize, testCollapseErr},
{"2fsb|1.5fsb", 2 * fsBlockSize, fsBlockSize + fsBlockSize/2, fsBlockSize, testCollapseErr},
}
// check CollapseFileStart
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
createTestFile(tc.fileSize)
defer os.Remove(file.Name())
defer file.Close()
defer func() {
if r := recover(); r != nil {
t.Error("Panic: ", r)
}
}()
byteActualyDeallocated, err = CollapseFileStart(file, tc.bytesToDeallocate)
if err != tc.expectedE {
t.Fatalf("expected error %v, got err: %v", tc.expectedE, err)
}
if byteActualyDeallocated != tc.expectedV {
t.Errorf("expected: %d, got: %d",
tc.expectedV,
byteActualyDeallocated)
}
})
}
}