| 1 |
//go:build linux |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright 2021 The Kubernetes Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package mount |
| 20 |
|
| 21 |
import ( |
| 22 |
"fmt" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"k8s.io/utils/exec" |
| 26 |
fakeexec "k8s.io/utils/exec/testing" |
| 27 |
) |
| 28 |
|
| 29 |
func TestGetFileSystemSize(t *testing.T) { |
| 30 |
cmdOutputSuccessBtrfs := `superblock: bytenr=65536, device=/dev/loop0 |
| 31 |
--------------------------------------------------------- |
| 32 |
csum_type 0 (crc32c) |
| 33 |
csum_size 4 |
| 34 |
csum 0x31693b11 [match] |
| 35 |
bytenr 65536 |
| 36 |
flags 0x1 |
| 37 |
( WRITTEN ) |
| 38 |
magic _BHRfS_M [match] |
| 39 |
fsid 3f53c8f7-3c57-4185-bf1d-b305b42cce97 |
| 40 |
metadata_uuid 3f53c8f7-3c57-4185-bf1d-b305b42cce97 |
| 41 |
label |
| 42 |
generation 7 |
| 43 |
root 30441472 |
| 44 |
sys_array_size 129 |
| 45 |
chunk_root_generation 6 |
| 46 |
root_level 0 |
| 47 |
chunk_root 22036480 |
| 48 |
chunk_root_level 0 |
| 49 |
log_root 0 |
| 50 |
log_root_transid 0 |
| 51 |
log_root_level 0 |
| 52 |
total_bytes 1048576000 |
| 53 |
bytes_used 147456 |
| 54 |
sectorsize 4096 |
| 55 |
nodesize 16384 |
| 56 |
leafsize (deprecated) 16384 |
| 57 |
stripesize 4096 |
| 58 |
root_dir 6 |
| 59 |
num_devices 1 |
| 60 |
compat_flags 0x0 |
| 61 |
compat_ro_flags 0x3 |
| 62 |
( FREE_SPACE_TREE | |
| 63 |
FREE_SPACE_TREE_VALID ) |
| 64 |
incompat_flags 0x341 |
| 65 |
( MIXED_BACKREF | |
| 66 |
EXTENDED_IREF | |
| 67 |
SKINNY_METADATA | |
| 68 |
NO_HOLES ) |
| 69 |
cache_generation 0 |
| 70 |
uuid_tree_generation 7 |
| 71 |
dev_item.uuid 987c8423-fba3-4168-9892-560a116feb81 |
| 72 |
dev_item.fsid 3f53c8f7-3c57-4185-bf1d-b305b42cce97 [match] |
| 73 |
dev_item.type 0 |
| 74 |
dev_item.total_bytes 1048576000 |
| 75 |
dev_item.bytes_used 130023424 |
| 76 |
dev_item.io_align 4096 |
| 77 |
dev_item.io_width 4096 |
| 78 |
dev_item.sector_size 4096 |
| 79 |
dev_item.devid 1 |
| 80 |
dev_item.dev_group 0 |
| 81 |
dev_item.seek_speed 0 |
| 82 |
dev_item.bandwidth 0 |
| 83 |
dev_item.generation 0 |
| 84 |
sys_chunk_array[2048]: |
| 85 |
item 0 key (FIRST_CHUNK_TREE CHUNK_ITEM 22020096) |
| 86 |
length 8388608 owner 2 stripe_len 65536 type SYSTEM|DUP |
| 87 |
io_align 65536 io_width 65536 sector_size 4096 |
| 88 |
num_stripes 2 sub_stripes 1 |
| 89 |
stripe 0 devid 1 offset 22020096 |
| 90 |
dev_uuid 987c8423-fba3-4168-9892-560a116feb81 |
| 91 |
stripe 1 devid 1 offset 30408704 |
| 92 |
dev_uuid 987c8423-fba3-4168-9892-560a116feb81 |
| 93 |
backup_roots[4]: |
| 94 |
backup 0: |
| 95 |
backup_tree_root: 30441472 gen: 5 level: 0 |
| 96 |
backup_chunk_root: 22020096 gen: 5 level: 0 |
| 97 |
backup_extent_root: 30474240 gen: 5 level: 0 |
| 98 |
backup_fs_root: 30425088 gen: 5 level: 0 |
| 99 |
backup_dev_root: 30457856 gen: 5 level: 0 |
| 100 |
backup_csum_root: 30490624 gen: 5 level: 0 |
| 101 |
backup_total_bytes: 1048576000 |
| 102 |
backup_bytes_used: 147456 |
| 103 |
backup_num_devices: 1 |
| 104 |
|
| 105 |
backup 1: |
| 106 |
backup_tree_root: 30588928 gen: 6 level: 0 |
| 107 |
backup_chunk_root: 22036480 gen: 6 level: 0 |
| 108 |
backup_extent_root: 30408704 gen: 6 level: 0 |
| 109 |
backup_fs_root: 30425088 gen: 5 level: 0 |
| 110 |
backup_dev_root: 30556160 gen: 6 level: 0 |
| 111 |
backup_csum_root: 30490624 gen: 5 level: 0 |
| 112 |
backup_total_bytes: 1048576000 |
| 113 |
backup_bytes_used: 147456 |
| 114 |
backup_num_devices: 1 |
| 115 |
|
| 116 |
backup 2: |
| 117 |
backup_tree_root: 30441472 gen: 7 level: 0 |
| 118 |
backup_chunk_root: 22036480 gen: 6 level: 0 |
| 119 |
backup_extent_root: 30474240 gen: 7 level: 0 |
| 120 |
backup_fs_root: 30425088 gen: 5 level: 0 |
| 121 |
backup_dev_root: 30457856 gen: 7 level: 0 |
| 122 |
backup_csum_root: 30490624 gen: 5 level: 0 |
| 123 |
backup_total_bytes: 1048576000 |
| 124 |
backup_bytes_used: 147456 |
| 125 |
backup_num_devices: 1 |
| 126 |
|
| 127 |
backup 3: |
| 128 |
backup_tree_root: 30408704 gen: 4 level: 0 |
| 129 |
backup_chunk_root: 1064960 gen: 4 level: 0 |
| 130 |
backup_extent_root: 5341184 gen: 4 level: 0 |
| 131 |
backup_fs_root: 5324800 gen: 3 level: 0 |
| 132 |
backup_dev_root: 5242880 gen: 4 level: 0 |
| 133 |
backup_csum_root: 1130496 gen: 1 level: 0 |
| 134 |
backup_total_bytes: 1048576000 |
| 135 |
backup_bytes_used: 114688 |
| 136 |
backup_num_devices: 1 |
| 137 |
|
| 138 |
` |
| 139 |
cmdOutputNoDataBtrfs := `superblock: bytenr=65536, device=/dev/loop0 |
| 140 |
--------------------------------------------------------- |
| 141 |
csum_type 0 (crc32c) |
| 142 |
csum_size 4 |
| 143 |
csum 0x31693b11 [match] |
| 144 |
bytenr 65536 |
| 145 |
flags 0x1 |
| 146 |
( WRITTEN ) |
| 147 |
magic _BHRfS_M [match] |
| 148 |
fsid 3f53c8f7-3c57-4185-bf1d-b305b42cce97 |
| 149 |
metadata_uuid 3f53c8f7-3c57-4185-bf1d-b305b42cce97 |
| 150 |
label |
| 151 |
generation 7 |
| 152 |
root 30441472 |
| 153 |
sys_array_size 129 |
| 154 |
chunk_root_generation 6 |
| 155 |
root_level 0 |
| 156 |
chunk_root 22036480 |
| 157 |
chunk_root_level 0 |
| 158 |
log_root 0 |
| 159 |
log_root_transid 0 |
| 160 |
log_root_level 0 |
| 161 |
bytes_used 147456 |
| 162 |
nodesize 16384 |
| 163 |
leafsize (deprecated) 16384 |
| 164 |
stripesize 4096 |
| 165 |
root_dir 6 |
| 166 |
num_devices 1 |
| 167 |
compat_flags 0x0 |
| 168 |
compat_ro_flags 0x3 |
| 169 |
( FREE_SPACE_TREE | |
| 170 |
FREE_SPACE_TREE_VALID ) |
| 171 |
incompat_flags 0x341 |
| 172 |
( MIXED_BACKREF | |
| 173 |
EXTENDED_IREF | |
| 174 |
SKINNY_METADATA | |
| 175 |
NO_HOLES ) |
| 176 |
cache_generation 0 |
| 177 |
uuid_tree_generation 7 |
| 178 |
dev_item.uuid 987c8423-fba3-4168-9892-560a116feb81 |
| 179 |
dev_item.fsid 3f53c8f7-3c57-4185-bf1d-b305b42cce97 [match] |
| 180 |
dev_item.type 0 |
| 181 |
dev_item.total_bytes 1048576000 |
| 182 |
dev_item.bytes_used 130023424 |
| 183 |
dev_item.io_align 4096 |
| 184 |
dev_item.io_width 4096 |
| 185 |
dev_item.sector_size 4096 |
| 186 |
dev_item.devid 1 |
| 187 |
dev_item.dev_group 0 |
| 188 |
dev_item.seek_speed 0 |
| 189 |
dev_item.bandwidth 0 |
| 190 |
dev_item.generation 0 |
| 191 |
sys_chunk_array[2048]: |
| 192 |
item 0 key (FIRST_CHUNK_TREE CHUNK_ITEM 22020096) |
| 193 |
length 8388608 owner 2 stripe_len 65536 type SYSTEM|DUP |
| 194 |
io_align 65536 io_width 65536 sector_size 4096 |
| 195 |
num_stripes 2 sub_stripes 1 |
| 196 |
stripe 0 devid 1 offset 22020096 |
| 197 |
dev_uuid 987c8423-fba3-4168-9892-560a116feb81 |
| 198 |
stripe 1 devid 1 offset 30408704 |
| 199 |
dev_uuid 987c8423-fba3-4168-9892-560a116feb81 |
| 200 |
backup_roots[4]: |
| 201 |
backup 0: |
| 202 |
backup_tree_root: 30441472 gen: 5 level: 0 |
| 203 |
backup_chunk_root: 22020096 gen: 5 level: 0 |
| 204 |
backup_extent_root: 30474240 gen: 5 level: 0 |
| 205 |
backup_fs_root: 30425088 gen: 5 level: 0 |
| 206 |
backup_dev_root: 30457856 gen: 5 level: 0 |
| 207 |
backup_csum_root: 30490624 gen: 5 level: 0 |
| 208 |
backup_total_bytes: 1048576000 |
| 209 |
backup_bytes_used: 147456 |
| 210 |
backup_num_devices: 1 |
| 211 |
|
| 212 |
backup 1: |
| 213 |
backup_tree_root: 30588928 gen: 6 level: 0 |
| 214 |
backup_chunk_root: 22036480 gen: 6 level: 0 |
| 215 |
backup_extent_root: 30408704 gen: 6 level: 0 |
| 216 |
backup_fs_root: 30425088 gen: 5 level: 0 |
| 217 |
backup_dev_root: 30556160 gen: 6 level: 0 |
| 218 |
backup_csum_root: 30490624 gen: 5 level: 0 |
| 219 |
backup_total_bytes: 1048576000 |
| 220 |
backup_bytes_used: 147456 |
| 221 |
backup_num_devices: 1 |
| 222 |
|
| 223 |
backup 2: |
| 224 |
backup_tree_root: 30441472 gen: 7 level: 0 |
| 225 |
backup_chunk_root: 22036480 gen: 6 level: 0 |
| 226 |
backup_extent_root: 30474240 gen: 7 level: 0 |
| 227 |
backup_fs_root: 30425088 gen: 5 level: 0 |
| 228 |
backup_dev_root: 30457856 gen: 7 level: 0 |
| 229 |
backup_csum_root: 30490624 gen: 5 level: 0 |
| 230 |
backup_total_bytes: 1048576000 |
| 231 |
backup_bytes_used: 147456 |
| 232 |
backup_num_devices: 1 |
| 233 |
|
| 234 |
backup 3: |
| 235 |
backup_tree_root: 30408704 gen: 4 level: 0 |
| 236 |
backup_chunk_root: 1064960 gen: 4 level: 0 |
| 237 |
backup_extent_root: 5341184 gen: 4 level: 0 |
| 238 |
backup_fs_root: 5324800 gen: 3 level: 0 |
| 239 |
backup_dev_root: 5242880 gen: 4 level: 0 |
| 240 |
backup_csum_root: 1130496 gen: 1 level: 0 |
| 241 |
backup_total_bytes: 1048576000 |
| 242 |
backup_bytes_used: 114688 |
| 243 |
backup_num_devices: 1 |
| 244 |
|
| 245 |
` |
| 246 |
|
| 247 |
testcases := []struct { |
| 248 |
name string |
| 249 |
devicePath string |
| 250 |
blocksize uint64 |
| 251 |
blockCount uint64 |
| 252 |
cmdOutput string |
| 253 |
expectError bool |
| 254 |
fsType string |
| 255 |
}{ |
| 256 |
{ |
| 257 |
name: "success parse btrfs info", |
| 258 |
devicePath: "/dev/test1", |
| 259 |
blocksize: 4096, |
| 260 |
blockCount: 256000, |
| 261 |
cmdOutput: cmdOutputSuccessBtrfs, |
| 262 |
expectError: false, |
| 263 |
fsType: "btrfs", |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "block size not present - btrfs", |
| 267 |
devicePath: "/dev/test1", |
| 268 |
blocksize: 0, |
| 269 |
blockCount: 0, |
| 270 |
cmdOutput: cmdOutputNoDataBtrfs, |
| 271 |
expectError: true, |
| 272 |
fsType: "btrfs", |
| 273 |
}, |
| 274 |
} |
| 275 |
|
| 276 |
for _, test := range testcases { |
| 277 |
t.Run(test.name, func(t *testing.T) { |
| 278 |
fcmd := fakeexec.FakeCmd{ |
| 279 |
CombinedOutputScript: []fakeexec.FakeAction{ |
| 280 |
func() ([]byte, []byte, error) { return []byte(test.cmdOutput), nil, nil }, |
| 281 |
}, |
| 282 |
} |
| 283 |
fexec := &fakeexec.FakeExec{ |
| 284 |
CommandScript: []fakeexec.FakeCommandAction{ |
| 285 |
func(cmd string, args ...string) exec.Cmd { |
| 286 |
return fakeexec.InitFakeCmd(&fcmd, cmd, args...) |
| 287 |
}, |
| 288 |
}, |
| 289 |
} |
| 290 |
resizefs := ResizeFs{exec: fexec} |
| 291 |
|
| 292 |
blockSize, fsSize, err := resizefs.getBtrfsSize(test.devicePath) |
| 293 |
|
| 294 |
if blockSize != test.blocksize { |
| 295 |
t.Fatalf("Parse wrong block size value, expect %d, but got %d", test.blocksize, blockSize) |
| 296 |
} |
| 297 |
if fsSize != test.blocksize*test.blockCount { |
| 298 |
t.Fatalf("Parse wrong fs size value, expect %d, but got %d", test.blocksize*test.blockCount, fsSize) |
| 299 |
} |
| 300 |
if !test.expectError && err != nil { |
| 301 |
t.Fatalf("Expect no error but got %v", err) |
| 302 |
} |
| 303 |
}) |
| 304 |
} |
| 305 |
} |
| 306 |
func TestNeedResize(t *testing.T) { |
| 307 |
testcases := []struct { |
| 308 |
name string |
| 309 |
devicePath string |
| 310 |
deviceMountPath string |
| 311 |
readonly string |
| 312 |
deviceSize string |
| 313 |
extSize string |
| 314 |
cmdOutputFsType string |
| 315 |
expectError bool |
| 316 |
expectResult bool |
| 317 |
}{ |
| 318 |
{ |
| 319 |
name: "True", |
| 320 |
devicePath: "/dev/test1", |
| 321 |
deviceMountPath: "/mnt/test1", |
| 322 |
readonly: "0", |
| 323 |
deviceSize: "2048", |
| 324 |
cmdOutputFsType: "TYPE=ext3", |
| 325 |
extSize: "20", |
| 326 |
expectError: false, |
| 327 |
expectResult: true, |
| 328 |
}, |
| 329 |
{ |
| 330 |
name: "False - needed by size but fs is readonly", |
| 331 |
devicePath: "/dev/test1", |
| 332 |
deviceMountPath: "/mnt/test1", |
| 333 |
readonly: "1", |
| 334 |
deviceSize: "2048", |
| 335 |
cmdOutputFsType: "TYPE=ext3", |
| 336 |
extSize: "20", |
| 337 |
expectError: false, |
| 338 |
expectResult: false, |
| 339 |
}, |
| 340 |
{ |
| 341 |
name: "False - Not needed by size for btrfs", |
| 342 |
devicePath: "/dev/test1", |
| 343 |
deviceMountPath: "/mnt/test1", |
| 344 |
readonly: "0", |
| 345 |
deviceSize: "20", |
| 346 |
cmdOutputFsType: "TYPE=btrfs", |
| 347 |
extSize: "2048", |
| 348 |
expectError: false, |
| 349 |
expectResult: false, |
| 350 |
}, |
| 351 |
{ |
| 352 |
name: "True - needed by size for btrfs", |
| 353 |
devicePath: "/dev/test1", |
| 354 |
deviceMountPath: "/mnt/test1", |
| 355 |
readonly: "0", |
| 356 |
deviceSize: "2048", |
| 357 |
cmdOutputFsType: "TYPE=btrfs", |
| 358 |
extSize: "20", |
| 359 |
expectError: false, |
| 360 |
expectResult: true, |
| 361 |
}, |
| 362 |
{ |
| 363 |
name: "False - Unsupported fs type", |
| 364 |
devicePath: "/dev/test1", |
| 365 |
deviceMountPath: "/mnt/test1", |
| 366 |
readonly: "0", |
| 367 |
deviceSize: "2048", |
| 368 |
extSize: "1", |
| 369 |
cmdOutputFsType: "TYPE=ntfs", |
| 370 |
expectError: true, |
| 371 |
expectResult: false, |
| 372 |
}, |
| 373 |
} |
| 374 |
|
| 375 |
for _, test := range testcases { |
| 376 |
t.Run(test.name, func(t *testing.T) { |
| 377 |
fcmd := fakeexec.FakeCmd{ |
| 378 |
CombinedOutputScript: []fakeexec.FakeAction{ |
| 379 |
func() ([]byte, []byte, error) { return []byte(test.readonly), nil, nil }, |
| 380 |
func() ([]byte, []byte, error) { return []byte(test.cmdOutputFsType), nil, nil }, |
| 381 |
}, |
| 382 |
} |
| 383 |
fexec := &fakeexec.FakeExec{ |
| 384 |
CommandScript: []fakeexec.FakeCommandAction{ |
| 385 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 386 |
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, |
| 387 |
}, |
| 388 |
} |
| 389 |
if test.cmdOutputFsType == "TYPE=btrfs" { |
| 390 |
t.Logf("Adding btrfs size command") |
| 391 |
fcmd.CombinedOutputScript = append(fcmd.CombinedOutputScript, func() ([]byte, []byte, error) { return []byte(test.deviceSize), nil, nil }) |
| 392 |
fcmd.CombinedOutputScript = append(fcmd.CombinedOutputScript, func() ([]byte, []byte, error) { |
| 393 |
return []byte(fmt.Sprintf("sectorsize %s\ntotal_bytes 1\n", test.extSize)), nil, nil |
| 394 |
}) |
| 395 |
|
| 396 |
fexec.CommandScript = append(fexec.CommandScript, func(cmd string, args ...string) exec.Cmd { |
| 397 |
return fakeexec.InitFakeCmd(&fcmd, cmd, args...) |
| 398 |
}) |
| 399 |
fexec.CommandScript = append(fexec.CommandScript, func(cmd string, args ...string) exec.Cmd { |
| 400 |
return fakeexec.InitFakeCmd(&fcmd, cmd, args...) |
| 401 |
}) |
| 402 |
} |
| 403 |
resizefs := ResizeFs{exec: fexec} |
| 404 |
|
| 405 |
needResize, err := resizefs.NeedResize(test.devicePath, test.deviceMountPath) |
| 406 |
if !test.expectError && err != nil { |
| 407 |
t.Fatalf("Expect no error but got %v", err) |
| 408 |
} |
| 409 |
if needResize != test.expectResult { |
| 410 |
t.Fatalf("Expect result is %v but got %v", test.expectResult, needResize) |
| 411 |
} |
| 412 |
}) |
| 413 |
} |
| 414 |
} |