forked from kubernetes/mount-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizefs_linux_test.go
302 lines (289 loc) · 8.43 KB
/
resizefs_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//go:build linux
// +build linux
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mount
import (
"k8s.io/utils/exec"
fakeexec "k8s.io/utils/exec/testing"
"testing"
)
func TestGetFileSystemSize(t *testing.T) {
cmdOutputSuccessXfs :=
`
statfs.f_bsize = 4096
statfs.f_blocks = 1832448
statfs.f_bavail = 1822366
statfs.f_files = 3670016
statfs.f_ffree = 3670012
statfs.f_flags = 0x1020
geom.bsize = 4096
geom.agcount = 4
geom.agblocks = 458752
geom.datablocks = 1835008
geom.rtblocks = 0
geom.rtextents = 0
geom.rtextsize = 1
geom.sunit = 0
geom.swidth = 0
counts.freedata = 1822372
counts.freertx = 0
counts.freeino = 61
counts.allocino = 64
`
cmdOutputNoDataXfs :=
`
statfs.f_bsize = 4096
statfs.f_blocks = 1832448
statfs.f_bavail = 1822366
statfs.f_files = 3670016
statfs.f_ffree = 3670012
statfs.f_flags = 0x1020
geom.agcount = 4
geom.agblocks = 458752
geom.rtblocks = 0
geom.rtextents = 0
geom.rtextsize = 1
geom.sunit = 0
geom.swidth = 0
counts.freedata = 1822372
counts.freertx = 0
counts.freeino = 61
counts.allocino = 64
`
cmdOutputSuccessExt4 :=
`
Filesystem volume name: cloudimg-rootfs
Last mounted on: /
Filesystem UUID: testUUID
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 3840000
Block count: 5242880
Reserved block count: 0
Free blocks: 5514413
Free inodes: 3677492
First block: 0
Block size: 4096
Fragment size: 4096
Group descriptor size: 64
Reserved GDT blocks: 252
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 16000
Inode blocks per group: 1000
Flex block group size: 16
Mount count: 2
Maximum mount count: -1
Check interval: 0 (<none>)
Lifetime writes: 180 GB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 32
Desired extra isize: 32
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: Test Hashing
Journal backup: inode blocks
Checksum type: crc32c
Checksum: 0x57705f62
Journal features: journal_incompat_revoke journal_64bit journal_checksum_v3
Journal size: 64M
Journal length: 16384
Journal sequence: 0x00037109
Journal start: 1
Journal checksum type: crc32c
Journal checksum: 0xb7df3c6e
`
cmdOutputNoDataExt4 :=
`Filesystem volume name: cloudimg-rootfs
Last mounted on: /
Filesystem UUID: testUUID
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 3840000
Reserved block count: 0
Free blocks: 5514413
Free inodes: 3677492
First block: 0
Fragment size: 4096
Group descriptor size: 64
Reserved GDT blocks: 252
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 16000
Inode blocks per group: 1000
Flex block group size: 16
Mount count: 2
Maximum mount count: -1
Check interval: 0 (<none>)
Lifetime writes: 180 GB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 32
Desired extra isize: 32
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: Test Hashing
Journal backup: inode blocks
Checksum type: crc32c
Checksum: 0x57705f62
Journal features: journal_incompat_revoke journal_64bit journal_checksum_v3
Journal size: 64M
Journal length: 16384
Journal sequence: 0x00037109
Journal start: 1
Journal checksum type: crc32c
Journal checksum: 0xb7df3c6e
`
testcases := []struct {
name string
devicePath string
blocksize uint64
blockCount uint64
cmdOutput string
expectError bool
fsType string
}{
{
name: "success parse xfs info",
devicePath: "/dev/test1",
blocksize: 4096,
blockCount: 1835008,
cmdOutput: cmdOutputSuccessXfs,
expectError: false,
fsType: "xfs",
},
{
name: "block size not present - xfs",
devicePath: "/dev/test1",
blocksize: 0,
blockCount: 0,
cmdOutput: cmdOutputNoDataXfs,
expectError: true,
fsType: "xfs",
},
{
name: "success parse ext info",
devicePath: "/dev/test1",
blocksize: 4096,
blockCount: 5242880,
cmdOutput: cmdOutputSuccessExt4,
expectError: false,
fsType: "ext4",
},
{
name: "block size not present - ext4",
devicePath: "/dev/test1",
blocksize: 0,
blockCount: 0,
cmdOutput: cmdOutputNoDataExt4,
expectError: true,
fsType: "ext4",
},
}
for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeAction{
func() ([]byte, []byte, error) { return []byte(test.cmdOutput), nil, nil },
},
}
fexec := fakeexec.FakeExec{
CommandScript: []fakeexec.FakeCommandAction{
func(cmd string, args ...string) exec.Cmd {
return fakeexec.InitFakeCmd(&fcmd, cmd, args...)
},
},
}
resizefs := ResizeFs{exec: &fexec}
var blockSize uint64
var fsSize uint64
var err error
switch test.fsType {
case "xfs":
blockSize, fsSize, err = resizefs.getXFSSize(test.devicePath)
case "ext4":
blockSize, fsSize, err = resizefs.getExtSize(test.devicePath)
}
if blockSize != test.blocksize {
t.Fatalf("Parse wrong block size value, expect %d, but got %d", test.blocksize, blockSize)
}
if fsSize != test.blocksize*test.blockCount {
t.Fatalf("Parse wrong fs size value, expect %d, but got %d", test.blocksize*test.blockCount, fsSize)
}
if !test.expectError && err != nil {
t.Fatalf("Expect no error but got %v", err)
}
})
}
}
func TestNeedResize(t *testing.T) {
testcases := []struct {
name string
devicePath string
deviceMountPath string
deviceSize string
cmdOutputFsType string
expectError bool
expectResult bool
}{
{
name: "False - Unsupported fs type",
devicePath: "/dev/test1",
deviceMountPath: "/mnt/test1",
deviceSize: "2048",
cmdOutputFsType: "TYPE=ntfs",
expectError: true,
expectResult: false,
},
}
for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeAction{
func() ([]byte, []byte, error) { return []byte(test.deviceSize), nil, nil },
func() ([]byte, []byte, error) { return []byte(test.cmdOutputFsType), nil, nil },
},
}
fexec := fakeexec.FakeExec{
CommandScript: []fakeexec.FakeCommandAction{
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
resizefs := ResizeFs{exec: &fexec}
needResize, err := resizefs.NeedResize(test.devicePath, test.deviceMountPath)
if needResize != test.expectResult {
t.Fatalf("Expect result is %v but got %v", test.expectResult, needResize)
}
if !test.expectError && err != nil {
t.Fatalf("Expect no error but got %v", err)
}
})
}
}