-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprealloc_test.go
204 lines (182 loc) · 5.13 KB
/
prealloc_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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package mongo_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/mongo"
coretesting "github.com/juju/juju/testing"
)
type preallocSuite struct {
coretesting.BaseSuite
}
var _ = gc.Suite(&preallocSuite{})
func (s *preallocSuite) TestOplogSize(c *gc.C) {
type test struct {
hostWordSize int
runtimeGOOS string
availSpace int
expected int
}
tests := []test{{
hostWordSize: 64,
runtimeGOOS: "darwin",
availSpace: 99999,
expected: 183,
}, {
hostWordSize: 64,
runtimeGOOS: "windows",
availSpace: 99999,
expected: 512,
}, {
hostWordSize: 32,
runtimeGOOS: "linux",
availSpace: 48,
expected: 48,
}, {
hostWordSize: 64,
runtimeGOOS: "linux",
availSpace: 1024,
expected: 512,
}, {
hostWordSize: 64,
runtimeGOOS: "linux",
availSpace: 420 * 1024,
expected: 1024,
}, {
hostWordSize: 64,
runtimeGOOS: "linux",
availSpace: 1024 * 1024,
expected: 1024,
}}
var availSpace int
getAvailSpace := func(dir string) (float64, error) {
return float64(availSpace), nil
}
s.PatchValue(mongo.AvailSpace, getAvailSpace)
for i, test := range tests {
c.Logf("test %d: %+v", i, test)
s.PatchValue(mongo.HostWordSize, test.hostWordSize)
s.PatchValue(mongo.RuntimeGOOS, test.runtimeGOOS)
availSpace = test.availSpace
size, err := mongo.DefaultOplogSize("")
c.Check(err, jc.ErrorIsNil)
c.Check(size, gc.Equals, test.expected)
}
}
func (s *preallocSuite) TestFsAvailSpace(c *gc.C) {
output := `Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 8124856 1365292 12345 18% /`
testing.PatchExecutable(c, s, "df", "#!/bin/sh\ncat<<EOF\n"+output+"\nEOF")
mb, err := mongo.FsAvailSpace("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(mb, gc.Equals, float64(12345)/1024)
}
func (s *preallocSuite) TestFsAvailSpaceErrors(c *gc.C) {
tests := []struct {
desc string
output string
err string
}{{
desc: "result is non-numeric",
output: `Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 8124856 1365292 abc 18% /`,
err: `strconv.(ParseInt|Atoi): parsing "abc": invalid syntax`,
}, {
desc: "not enough lines",
output: "abc",
err: `could not determine available space on ""`,
}, {
desc: "not enough fields on second line",
output: "abc\ndef",
err: `could not determine available space on ""`,
}}
for i, test := range tests {
c.Logf("test %d: %s", i, test.desc)
testing.PatchExecutable(c, s, "df", "#!/bin/sh\ncat<<EOF\n"+test.output+"\nEOF")
_, err := mongo.FsAvailSpace("")
c.Check(err, gc.ErrorMatches, test.err)
}
}
func (s *preallocSuite) TestPreallocFileSizes(c *gc.C) {
const MB = 1024 * 1024
tests := []struct {
desc string
size int
result []int
}{{
desc: "zero size, zero files",
size: 0,
result: nil,
}, {
desc: "exactly divides the max chunk size",
size: 1024 * MB,
result: []int{512 * MB, 512 * MB},
}, {
desc: "remainder comes at the beginning",
size: 1025 * MB,
result: []int{1 * MB, 512 * MB, 512 * MB},
}, {
desc: "remaining one byte must be padded out to 4096 bytes",
size: 1024*MB + 1,
result: []int{4096, 512 * MB, 512 * MB},
}}
for i, test := range tests {
c.Logf("test %d: %s", i, test.desc)
sizes := mongo.PreallocFileSizes(test.size)
c.Check(sizes, gc.DeepEquals, test.result)
}
}
func (s *preallocSuite) TestPreallocFiles(c *gc.C) {
dir := c.MkDir()
prefix := filepath.Join(dir, "test.")
err := mongo.PreallocFiles(prefix, 0, 4096, 8192)
c.Assert(err, jc.ErrorIsNil)
zeroes := [8192]byte{}
for i := 0; i < 3; i++ {
filename := fmt.Sprintf("%s%d", prefix, i)
data, err := ioutil.ReadFile(filename)
c.Check(err, jc.ErrorIsNil)
c.Check(data, gc.DeepEquals, zeroes[:i*4096])
}
_, err = os.Stat(prefix + "3")
c.Assert(err, jc.Satisfies, os.IsNotExist)
}
func (s *preallocSuite) TestPreallocFilesErrors(c *gc.C) {
err := mongo.PreallocFiles("", 123)
c.Assert(err, gc.ErrorMatches, `specified size 123 for file "0" is not a multiple of 4096`)
}
func (s *preallocSuite) TestPreallocFilesWriteErrors(c *gc.C) {
dir := c.MkDir()
prefix := filepath.Join(dir, "test.")
err := ioutil.WriteFile(prefix+"0", nil, 0644)
c.Assert(err, jc.ErrorIsNil)
err = ioutil.WriteFile(prefix+"1", nil, 0644)
c.Assert(err, jc.ErrorIsNil)
var called int
s.PatchValue(mongo.PreallocFile, func(filename string, size int) (bool, error) {
var created bool
var err error
called++
if called == 2 {
created = true
err = fmt.Errorf("failed to zero test.1")
}
return created, err
})
err = mongo.PreallocFiles(prefix, 4096, 8192)
c.Assert(err, gc.ErrorMatches, "failed to zero test.1")
// test.0 still exists because we said we didn't
// create it (i.e. it already existed)
_, err = os.Stat(prefix + "0")
c.Assert(err, jc.ErrorIsNil)
// test.1 no longer exists because we said we created
// it, but then failed to write to it.
_, err = os.Stat(prefix + "1")
c.Assert(err, jc.Satisfies, os.IsNotExist)
}