-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblocks_test.go
158 lines (137 loc) · 5.02 KB
/
blocks_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
package fsEngine
import (
"math/rand"
"os"
"strconv"
"testing"
"github.com/fanap-infra/fsEngine/internal/constants"
"github.com/fanap-infra/fsEngine/pkg/virtualFile"
"github.com/fanap-infra/fsEngine/pkg/utils"
"github.com/fanap-infra/log"
"github.com/stretchr/testify/assert"
)
type EventsListener struct {
t *testing.T
fileID uint32
}
func (el *EventsListener) VirtualFileDeleted(fileID uint32, message string) {
// log.Infov("VirtualFileDeleted event", "fileID", fileID, "message", message,
// "first fileID", el.fileID)
assert.Equal(el.t, el.fileID, fileID)
}
func TestPrepareAndParseBlock(t *testing.T) {
homePath, err := os.UserHomeDir()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + constants.FsPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderBackUpPath)
eventListener := EventsListener{t: t}
fse, err := CreateFileSystem(fsID, homePath, fileSizeTest, blockSizeTest, &eventListener, log.GetScope("test"), nil)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.FsPath))
assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.HeaderPath))
// assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.HeaderBackUpPath))
numberOfTests := 5
blockID := 0
fileIdTest := 5
previousBlock := 0
for i := 0; i < numberOfTests; i++ {
token := make([]byte, uint32(rand.Intn(blockSizeTest)))
_, err := rand.Read(token)
assert.Equal(t, nil, err)
buf, err := fse.prepareBlock(token, uint32(fileIdTest), uint32(previousBlock), uint32(blockID))
assert.Equal(t, nil, err)
buf2, err := fse.parseBlock(buf, uint32(blockID), uint32(fileIdTest))
assert.Equal(t, nil, err)
assert.Equal(t, buf2, token)
blockID++
}
}
func TestPrepareAndParseBlockManually(t *testing.T) {
homePath, err := os.UserHomeDir()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + constants.FsPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderBackUpPath)
eventListener := EventsListener{t: t}
fse, err := CreateFileSystem(fsID, homePath, fileSizeTest, blockSizeTest, &eventListener, log.GetScope("test"), nil)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.FsPath))
assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.HeaderPath))
blockID := 8201
fileIdTest := 2100
previousBlock := 0
// for i := 0; i < numberOfTests; i++ {
token := make([]byte, uint32(rand.Intn(blockSizeTest)))
_, err = rand.Read(token)
assert.Equal(t, nil, err)
buf, err := fse.prepareBlock(token, uint32(fileIdTest), uint32(previousBlock), uint32(blockID))
assert.Equal(t, nil, err)
buf2, err := fse.parseBlock(buf, uint32(blockID), uint32(fileIdTest))
assert.Equal(t, nil, err)
assert.Equal(t, buf2, token)
err = fse.Close()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + constants.FsPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderBackUpPath)
}
func TestFSEngine_NoSpace(t *testing.T) {
homePath, err := os.UserHomeDir()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + constants.FsPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderBackUpPath)
eventListener := EventsListener{t: t}
fse, err := CreateFileSystem(fsID, homePath, fileSizeTest*2, blockSizeTest, &eventListener, log.GetScope("test"), nil)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.FsPath))
assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.HeaderPath))
// assert.Equal(t, true, utils.FileExists(homePath+"/"+constants.HeaderBackUpPath))
MaxID := 1000
numberOfVFs := 5
MaxByteArraySize := int(blockSizeTest * 0.5)
VFSize := (fileSizeTest / numberOfVFs) + blockSizeTest
virtualFiles := make([]*virtualFile.VirtualFile, 0)
bytes := make([][][]byte, numberOfVFs)
vfIDs := make([]uint32, 0)
for i := 0; i < numberOfVFs; i++ {
vfID := uint32(rand.Intn(MaxID))
if utils.ItemExists(vfIDs, vfID) {
i = i - 1
continue
}
vfIDs = append(vfIDs, vfID)
vf, err := fse.NewVirtualFile(vfID, "test"+strconv.Itoa(i))
if assert.Equal(t, nil, err) {
virtualFiles = append(virtualFiles, vf)
}
}
if len(virtualFiles) != numberOfVFs {
return
}
eventListener.fileID = vfIDs[0]
for j, vf := range virtualFiles {
size := 0
for {
token := make([]byte, uint32(1+rand.Intn(MaxByteArraySize)))
m, err := rand.Read(token)
assert.Equal(t, nil, err)
bytes[j] = append(bytes[j], token)
size = size + m
n, err := vf.Write(token)
assert.Equal(t, nil, err)
assert.Equal(t, m, n)
if size > VFSize {
break
}
}
err = vf.Close()
assert.Equal(t, nil, err)
}
err = fse.Close()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + constants.FsPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderPath)
_ = utils.DeleteFile(homePath + "/" + constants.HeaderBackUpPath)
}