| 1 | module main |
| 2 | |
| 3 | import nspkg.tests.coracle as alpm |
| 4 | import nspkg.engine |
| 5 | import nspkg.tests.signaturefixtures |
| 6 | import nspkg.tests.transactionfixtures |
| 7 | import os |
| 8 | |
| 9 | #include "@DIR/reference_log.h" |
| 10 | |
| 11 | type DownloadSpaceReceiver = fn (voidptr, u32, &u8) |
| 12 | |
| 13 | fn C.v_alpm_reference_log_install(voidptr, DownloadSpaceReceiver, voidptr) voidptr |
| 14 | fn C.v_alpm_reference_log_remove(voidptr, voidptr) |
| 15 | |
| 16 | struct DownloadSpaceTrace { |
| 17 | base string |
| 18 | mut: |
| 19 | lines []string |
| 20 | } |
| 21 | |
| 22 | fn download_space_line(context voidptr, level u32, message string) { |
| 23 | mut trace := unsafe { &DownloadSpaceTrace(context) } |
| 24 | if message.contains(trace.base) && (message.starts_with('loading fsinfo for ') |
| 25 | || message.starts_with('partition ') || message.starts_with('Partition ')) { |
| 26 | trace.lines << '${level}:' + message.replace(trace.base, '<fixture>') |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn reference_download_space_line(context voidptr, level u32, message &u8) { |
| 31 | download_space_line(context, level, unsafe { tos_clone(message) }) |
| 32 | } |
| 33 | |
| 34 | fn native_download_space_line(context voidptr, level engine.LogLevel, message string) { |
| 35 | download_space_line(context, u32(level), message) |
| 36 | } |
| 37 | |
| 38 | fn main() { |
| 39 | run() or { |
| 40 | eprintln(err) |
| 41 | exit(1) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn reference_space(fixture transactionfixtures.Fixture, success bool) ![]string { |
| 46 | handle := alpm.initialize(fixture.root, fixture.db)! |
| 47 | defer { handle.release() or { eprintln(err) } } |
| 48 | handle.option_add_cachedir(fixture.cache)! |
| 49 | handle.option_set_checkspace(1)! |
| 50 | db := handle.register_syncdb('core', 0)! |
| 51 | db.add_server('file://' + fixture.server)! |
| 52 | handle.trans_init(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet | .download_only)! |
| 53 | defer { handle.trans_release() or { eprintln(err) } } |
| 54 | handle.add_pkg(db.get_pkg('app') or { return error('missing C target') })! |
| 55 | handle.trans_prepare()!.free() |
| 56 | mut trace := DownloadSpaceTrace{ base: fixture.base } |
| 57 | adapter := C.v_alpm_reference_log_install(handle, reference_download_space_line, &trace) |
| 58 | defer { C.v_alpm_reference_log_remove(handle, adapter) } |
| 59 | result := handle.trans_commit() or { |
| 60 | assert !success |
| 61 | assert handle.errno() == .disk_space |
| 62 | return trace.lines |
| 63 | } |
| 64 | result.free() |
| 65 | assert success |
| 66 | return trace.lines |
| 67 | } |
| 68 | |
| 69 | fn native_space(fixture transactionfixtures.Fixture, success bool) ![]string { |
| 70 | mut handle := engine.initialize(fixture.root, fixture.db)! |
| 71 | defer { handle.release() or { eprintln(err) } } |
| 72 | handle.add_string_list(.cache_dirs, fixture.cache)! |
| 73 | handle.set_integer_option(.check_space, 1)! |
| 74 | mut db := handle.register_syncdb('core', 0)! |
| 75 | db.add_server('file://' + fixture.server, false)! |
| 76 | handle.start_transaction(.no_deps | .no_conflicts | .no_hooks | .no_scriptlet | .download_only)! |
| 77 | defer { handle.release_transaction() or { eprintln(err) } } |
| 78 | handle.add_package(db.get_pkg('app') or { return error('missing native target') })! |
| 79 | assert handle.prepare_transaction()!.success |
| 80 | mut trace := DownloadSpaceTrace{ base: fixture.base } |
| 81 | handle.set_log_callback(native_download_space_line, &trace)! |
| 82 | handle.commit_transaction() or { |
| 83 | assert !success |
| 84 | assert handle.errno() == .disk_space |
| 85 | assert handle.transaction_state()! == .downloading |
| 86 | return trace.lines |
| 87 | } |
| 88 | assert success |
| 89 | assert handle.transaction_state()! == .downloading |
| 90 | return trace.lines |
| 91 | } |
| 92 | |
| 93 | fn run() ! { |
| 94 | if os.args.len != 3 || os.readlink('/proc/self/ns/mnt')! == os.args[2] { |
| 95 | return error('requires a private mount namespace') |
| 96 | } |
| 97 | signaturefixtures.command('mount', ['--make-rprivate', '/'])! |
| 98 | for linked in [false, true] { |
| 99 | for success in [false, true] { |
| 100 | mut traces := [][]string{} |
| 101 | for native in [false, true] { |
| 102 | base := os.join_path(os.args[1], linked.str(), success.str(), native.str()) |
| 103 | fixture := transactionfixtures.create(base, 'fetch')! |
| 104 | volume := if linked { os.join_path(base, 'cache-volume') } else { fixture.cache } |
| 105 | if linked { |
| 106 | os.mv(fixture.cache, volume)! |
| 107 | os.symlink(volume, fixture.cache)! |
| 108 | } |
| 109 | size := if success { '32m' } else { '8k' } |
| 110 | signaturefixtures.command('mount', ['-t', 'tmpfs', '-o', 'size=' + size, 'tmpfs', |
| 111 | volume])! |
| 112 | if native { |
| 113 | traces << native_space(fixture, success)! |
| 114 | } else { |
| 115 | traces << reference_space(fixture, success)! |
| 116 | } |
| 117 | assert os.is_file(os.join_path(fixture.cache, 'app-2-1.pkg.tar')) == success |
| 118 | assert os.read_file(os.join_path(fixture.root, 'old-payload'))! == 'old' |
| 119 | assert !os.exists(os.join_path(fixture.root, 'usr')) |
| 120 | signaturefixtures.command('umount', [volume])! |
| 121 | } |
| 122 | assert traces[0].len >= 2 |
| 123 | assert traces[1] == traces[0], '${linked}/${success}: ${traces}' |
| 124 | } |
| 125 | } |
| 126 | println('native/C transaction download space verified on constrained filesystems') |
| 127 | } |
| 128 | |