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
|
package storage
import (
"testing"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
)
func TestHasFreeDiskLocation(t *testing.T) {
testCases := []struct {
name string
isDiskSpaceLow bool
maxVolumeCount int32
currentVolumes int
expected bool
}{
{
name: "low disk space prevents allocation",
isDiskSpaceLow: true,
maxVolumeCount: 10,
currentVolumes: 5,
expected: false,
},
{
name: "normal disk space and available volume count allows allocation",
isDiskSpaceLow: false,
maxVolumeCount: 10,
currentVolumes: 5,
expected: true,
},
{
name: "volume count at max prevents allocation",
isDiskSpaceLow: false,
maxVolumeCount: 2,
currentVolumes: 2,
expected: false,
},
{
name: "volume count over max prevents allocation",
isDiskSpaceLow: false,
maxVolumeCount: 2,
currentVolumes: 3,
expected: false,
},
{
name: "volume count just under max allows allocation",
isDiskSpaceLow: false,
maxVolumeCount: 2,
currentVolumes: 1,
expected: true,
},
{
name: "max volume count is 0 allows allocation",
isDiskSpaceLow: false,
maxVolumeCount: 0,
currentVolumes: 100,
expected: true,
},
{
name: "max volume count is 0 but low disk space prevents allocation",
isDiskSpaceLow: true,
maxVolumeCount: 0,
currentVolumes: 100,
expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// setup
diskLocation := &DiskLocation{
volumes: make(map[needle.VolumeId]*Volume),
isDiskSpaceLow: tc.isDiskSpaceLow,
MaxVolumeCount: tc.maxVolumeCount,
}
for i := 0; i < tc.currentVolumes; i++ {
diskLocation.volumes[needle.VolumeId(i+1)] = &Volume{}
}
store := &Store{
Locations: []*DiskLocation{diskLocation},
}
// act
result := store.hasFreeDiskLocation(diskLocation)
// assert
if result != tc.expected {
t.Errorf("Expected hasFreeDiskLocation() = %v; want %v for volumes:%d/%d, lowSpace:%v",
result, tc.expected, len(diskLocation.volumes), diskLocation.MaxVolumeCount, diskLocation.isDiskSpaceLow)
}
})
}
}
|