-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud_test.go
126 lines (112 loc) · 3.43 KB
/
crud_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
package archiverMedia
import (
"math/rand"
"os"
"strconv"
"testing"
"github.com/fanap-infra/archiverMedia/pkg/virtualMedia"
"github.com/fanap-infra/archiverMedia/pkg/utils"
"github.com/fanap-infra/log"
"github.com/stretchr/testify/assert"
)
func TestVirtualMedia_Remove(t *testing.T) {
homePath, err := os.UserHomeDir()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + fsPath)
_ = utils.DeleteFile(homePath + "/" + headerPath)
eventListener := EventsListener{t: t}
provider := NewProvider()
arch, err := provider.CreateFileSystem(fsID,homePath, fileSizeTest, blockSizeTest, &eventListener, log.GetScope("test"), nil)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.FileExists(homePath+"/"+fsPath))
assert.Equal(t, true, utils.FileExists(homePath+"/"+headerPath))
var testIDs []uint32
var testNames []string
TestSize := 5
MaxID := 1000
var vms []*virtualMedia.VirtualMedia
for i := 0; i < TestSize; i++ {
tmp := uint32(rand.Intn(MaxID))
if utils.ItemExists(testIDs, tmp) {
i = i - 1
continue
}
testIDs = append(testIDs, tmp)
testNames = append(testNames, "test"+strconv.Itoa(i))
vm, err := arch.NewVirtualMediaFile(testIDs[i], testNames[i])
assert.Equal(t, nil, err)
vms = append(vms, vm)
}
for i := 0; i < TestSize; i++ {
_, err := arch.NewVirtualMediaFile(testIDs[i], testNames[i])
assert.NotEqual(t, nil, err)
}
// cna not remove opened virtual files
for i := 0; i < TestSize; i++ {
err := arch.RemoveVirtualMediaFile(testIDs[i])
assert.NotEqual(t, nil, err)
}
for i := 0; i < TestSize/2; i++ {
err := vms[i].Close()
assert.Equal(t, nil, err)
}
for i := 0; i < TestSize; i++ {
if i < TestSize/2 {
err := arch.RemoveVirtualMediaFile(testIDs[i])
assert.Equal(t, nil, err)
} else {
err := arch.RemoveVirtualMediaFile(testIDs[i])
assert.NotEqual(t, nil, err)
err = vms[i].Close()
assert.Equal(t, nil, err)
err = arch.RemoveVirtualMediaFile(testIDs[i])
assert.Equal(t, nil, err)
}
}
err = arch.Close()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + fsPath)
_ = utils.DeleteFile(homePath + "/" + headerPath)
}
func TestVirtualMedia_Open(t *testing.T) {
homePath, err := os.UserHomeDir()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + fsPath)
_ = utils.DeleteFile(homePath + "/" + headerPath)
var eventListener EventsListener
provider := NewProvider()
arch, err := provider.CreateFileSystem(fsID,homePath, fileSizeTest, blockSizeTest, &eventListener,
log.GetScope("test"), nil)
assert.Equal(t, nil, err)
assert.Equal(t, true, utils.FileExists(homePath+"/"+fsPath))
assert.Equal(t, true, utils.FileExists(homePath+"/"+headerPath))
var testIDs []uint32
var testNames []string
TestSize := 1
MaxID := 1000
var vms []*virtualMedia.VirtualMedia
for i := 0; i < TestSize; i++ {
tmp := uint32(rand.Intn(MaxID))
if utils.ItemExists(testIDs, tmp) {
i = i - 1
continue
}
testIDs = append(testIDs, tmp)
testNames = append(testNames, "test"+strconv.Itoa(i))
vm, err := arch.NewVirtualMediaFile(testIDs[i], testNames[i])
assert.Equal(t, nil, err)
vms = append(vms, vm)
}
for i := 0; i < len(vms); i++ {
err := vms[i].Close()
assert.Equal(t, nil, err)
}
for i := 0; i < len(testIDs); i++ {
_, err := arch.OpenVirtualMediaFile(testIDs[i])
assert.Equal(t, nil, err)
}
err = arch.Close()
assert.Equal(t, nil, err)
_ = utils.DeleteFile(homePath + "/" + fsPath)
_ = utils.DeleteFile(homePath + "/" + headerPath)
}