nspkg / engine / space_test.v
62 lines · 57 sloc · 2.1 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import os
4
5struct SpaceFailureTrace {
6mut:
7 lines []string
8}
9
10fn space_failure_log(context voidptr, level LogLevel, message string) {
11 mut trace := unsafe { &SpaceFailureTrace(context) }
12 trace.lines << '${u32(level)}:${message}'
13}
14
15fn test_unavailable_filesystem_logs_once_without_replacing_previous_error() ! {
16 base := os.join_path(os.vtmp_dir(), 'alpm-space-failure-${os.getpid()}')
17 os.mkdir_all(base)!
18 defer { os.rmdir_all(base) or { eprintln(err) } }
19 mut handle := initialize('/', base)!
20 defer { handle.release() or { eprintln(err) } }
21 mut trace := SpaceFailureTrace{}
22 handle.set_log_callback(space_failure_log, &trace)!
23 if _ := handle.register_syncdb('', 0) {
24 assert false
25 }
26 mut mount := InstallationMount{ path: os.join_path(base, 'absent') }
27 assert !handle.load_installation_mount(mut mount)
28 assert mount.loaded && mount.failed
29 assert handle.errno() == .wrong_args
30 assert trace.lines == ['2:could not get filesystem information for ${mount.path}: No such file or directory\n']
31 assert !handle.load_installation_mount(mut mount)
32 assert trace.lines.len == 1
33 assert handle.errno() == .wrong_args
34}
35
36fn test_partition_space_threshold_and_overflow_guard() ! {
37 base := os.join_path(os.vtmp_dir(), 'alpm-space-threshold-${os.getpid()}')
38 os.mkdir_all(base)!
39 defer { os.rmdir_all(base) or { eprintln(err) } }
40 mut handle := initialize('/', base)!
41 defer { handle.release() or { eprintln(err) } }
42 mut trace := SpaceFailureTrace{}
43 handle.set_log_callback(space_failure_log, &trace)!
44 mut mount := InstallationMount{
45 path: '/fixture'
46 info: C.v_alpm_space_info{ block_size: 4096, blocks: 10000, available: 506 }
47 maximum_needed: 5
48 }
49 assert !handle.space_mount_full(mount)!
50 mount.info.available--
51 assert handle.space_mount_full(mount)!
52 assert trace.lines == [
53 '4:partition /fixture, needed 5, cushion 501, free 506\n',
54 '4:partition /fixture, needed 5, cushion 501, free 505\n',
55 '1:Partition /fixture too full: 506 blocks needed, 505 blocks free\n',
56 ]
57 mount.maximum_needed = max_i64
58 if _ := handle.space_mount_full(mount) {
59 assert false
60 }
61 assert handle.errno() == .disk_space
62}
63