forked from cloudfoundry/grootfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurer_linux_test.go
237 lines (197 loc) · 7.16 KB
/
measurer_linux_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
230
231
232
233
234
235
236
237
package store_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"code.cloudfoundry.org/grootfs/store"
"code.cloudfoundry.org/grootfs/store/storefakes"
"code.cloudfoundry.org/lager/lagertest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Measurer", func() {
var (
storePath string
storeMeasurer *store.StoreMeasurer
logger *lagertest.TestLogger
volumeDriver *storefakes.FakeVolumeDriver
unusedVolumeGetter *storefakes.FakeUnusedVolumeGetter
)
BeforeEach(func() {
mountPoint := fmt.Sprintf("/mnt/xfs-%d", GinkgoParallelNode())
var err error
storePath, err = ioutil.TempDir(mountPoint, "")
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(
filepath.Join(storePath, store.VolumesDirName), 0744,
)).To(Succeed())
Expect(os.MkdirAll(
filepath.Join(storePath, store.ImageDirName), 0744,
)).To(Succeed())
volumeDriver = new(storefakes.FakeVolumeDriver)
unusedVolumeGetter = new(storefakes.FakeUnusedVolumeGetter)
storeMeasurer = store.NewStoreMeasurer(storePath, volumeDriver, unusedVolumeGetter)
logger = lagertest.NewTestLogger("store-measurer")
})
AfterEach(func() {
Expect(os.RemoveAll(storePath)).To(Succeed())
})
Describe("TotalVolumesSize", func() {
BeforeEach(func() {
volumeDriver.VolumeSizeReturns(2048, nil)
volumeDriver.VolumesReturns([]string{"sha256:fake1", "sha256:fake2"}, nil)
})
It("measures the size of all layers", func() {
cacheUsage, err := storeMeasurer.TotalVolumesSize(logger)
Expect(err).NotTo(HaveOccurred())
Expect(cacheUsage).To(BeNumerically("==", 4096))
})
Context("when getting volumes returns an error", func() {
BeforeEach(func() {
volumeDriver.VolumesReturns([]string{}, errors.New("failed here"))
})
It("returns the error", func() {
_, err := storeMeasurer.TotalVolumesSize(logger)
Expect(err).To(HaveOccurred())
})
})
Context("when the driver VolumeSize returns an error", func() {
BeforeEach(func() {
volumeDriver.VolumeSizeReturns(0, errors.New("failed here"))
})
It("returns the error", func() {
_, err := storeMeasurer.TotalVolumesSize(logger)
Expect(err).To(HaveOccurred())
})
Context("but it's because a file doesn't exist", func() {
BeforeEach(func() {
volumeDriver.VolumeSizeReturns(0, os.ErrNotExist)
})
It("carries on silently", func() {
_, err := storeMeasurer.TotalVolumesSize(logger)
Expect(err).NotTo(HaveOccurred())
})
})
})
})
Describe("UsedVolumesSize", func() {
BeforeEach(func() {
volumeDriver.VolumeSizeReturns(1024, nil)
unusedVolumeGetter.UnusedVolumesReturns([]string{"sha256:fake1", "sha256:fake2"}, nil)
volumeDriver.VolumesReturns([]string{"sha256:fake1", "sha256:fake2"}, nil)
})
It("measures the size of the used layers", func() {
cacheUsage, err := storeMeasurer.UsedVolumesSize(logger)
Expect(err).NotTo(HaveOccurred())
Expect(cacheUsage).To(BeNumerically("==", 0))
})
Context("when getting total volumes returns an error", func() {
BeforeEach(func() {
volumeDriver.VolumesReturns([]string{}, errors.New("failed here"))
})
It("returns the error", func() {
_, err := storeMeasurer.TotalVolumesSize(logger)
Expect(err).To(HaveOccurred())
})
})
Context("when getting unused volumes returns an error", func() {
BeforeEach(func() {
unusedVolumeGetter.UnusedVolumesReturns([]string{}, errors.New("failed here"))
})
It("returns the error", func() {
_, err := storeMeasurer.UnusedVolumesSize(logger)
Expect(err).To(HaveOccurred())
})
})
})
Describe("UnusedVolumeSize", func() {
BeforeEach(func() {
volumeDriver.VolumeSizeReturns(1024, nil)
unusedVolumeGetter.UnusedVolumesReturns([]string{"sha256:fake1", "sha256:fake2"}, nil)
})
It("measures the size of the unused layers", func() {
cacheUsage, err := storeMeasurer.UnusedVolumesSize(logger)
Expect(err).NotTo(HaveOccurred())
Expect(cacheUsage).To(BeNumerically("==", 2048))
})
Context("when getting volumes returns an error", func() {
BeforeEach(func() {
unusedVolumeGetter.UnusedVolumesReturns([]string{}, errors.New("failed here"))
})
It("returns the error", func() {
_, err := storeMeasurer.UnusedVolumesSize(logger)
Expect(err).To(HaveOccurred())
})
})
Context("when the driver VolumeSize returns an error", func() {
BeforeEach(func() {
volumeDriver.VolumeSizeReturns(0, errors.New("failed here"))
})
It("returns the error", func() {
_, err := storeMeasurer.UnusedVolumesSize(logger)
Expect(err).To(HaveOccurred())
})
})
})
Describe("CommittedQuota", func() {
BeforeEach(func() {
image1Path := filepath.Join(storePath, store.ImageDirName, "my-image-1")
Expect(os.MkdirAll(image1Path, 0744)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(image1Path, "image_quota"), []byte("1024"), 0777)).To(Succeed())
image2Path := filepath.Join(storePath, store.ImageDirName, "my-image-2")
Expect(os.MkdirAll(image2Path, 0744)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(image2Path, "image_quota"), []byte("2048"), 0777)).To(Succeed())
})
It("returns the committed size of the store", func() {
committedSize, err := storeMeasurer.CommittedQuota(logger)
Expect(err).NotTo(HaveOccurred())
Expect(committedSize).To(BeNumerically("==", 3072))
})
It("ignores images without quota", func() {
Expect(os.Remove(filepath.Join(storePath, store.ImageDirName, "my-image-2", "image_quota"))).To(Succeed())
committedSize, err := storeMeasurer.CommittedQuota(logger)
Expect(err).NotTo(HaveOccurred())
Expect(committedSize).To(BeNumerically("==", 1024))
})
It("errors if it cannot read the images dir", func() {
Expect(os.RemoveAll(filepath.Join(storePath, store.ImageDirName))).To(Succeed())
_, err := storeMeasurer.CommittedQuota(logger)
Expect(err).To(HaveOccurred())
})
It("errors when unable to read quota files containing garbage", func() {
erroneousImagePath := filepath.Join(storePath, store.ImageDirName, "erroneous-image")
Expect(os.MkdirAll(erroneousImagePath, 0744)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(erroneousImagePath, "image_quota"), []byte("what!?"), 0777)).To(Succeed())
_, err := storeMeasurer.CommittedQuota(logger)
Expect(err).To(HaveOccurred())
})
It("silently ignores empty quota files", func() {
erroneousImagePath := filepath.Join(storePath, store.ImageDirName, "erroneous-image")
Expect(os.MkdirAll(erroneousImagePath, 0744)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(erroneousImagePath, "image_quota"), []byte(""), 0777)).To(Succeed())
_, err := storeMeasurer.CommittedQuota(logger)
Expect(err).NotTo(HaveOccurred())
})
})
})
func writeFile(path string, size int64) error {
cmd := exec.Command(
"dd", "if=/dev/zero", fmt.Sprintf("of=%s", path),
"bs=1024", fmt.Sprintf("count=%d", size/1024),
)
sess, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
if err != nil {
return err
}
sess.Wait()
out := sess.Buffer().Contents()
exitCode := sess.ExitCode()
if exitCode != 0 {
return fmt.Errorf("du failed with exit code %d: %s", exitCode, string(out))
}
return nil
}