| 1 | module coracle |
| 2 | |
| 3 | import nspkg.engine |
| 4 | import os |
| 5 | |
| 6 | struct RemovalExecutionTrace { |
| 7 | root string |
| 8 | mut: |
| 9 | items []string |
| 10 | reference_handle &Handle = unsafe { nil } |
| 11 | native_handle &engine.Handle = unsafe { nil } |
| 12 | interrupt bool |
| 13 | } |
| 14 | |
| 15 | fn reference_removal_execution_event(context voidptr, event &Event) { |
| 16 | mut trace := unsafe { &RemovalExecutionTrace(context) } |
| 17 | match unsafe { event.type_ } { |
| 18 | .transaction_start { trace.items << 'transaction:start' } |
| 19 | .transaction_done { trace.items << 'transaction:done' } |
| 20 | .package_operation_start { |
| 21 | trace.items << 'remove:start:${unsafe { event.package_operation.oldpkg }.get_name()}' |
| 22 | if trace.interrupt { |
| 23 | trace.reference_handle.trans_interrupt() or { assert false, err.msg() } |
| 24 | } |
| 25 | } |
| 26 | .package_operation_done { |
| 27 | trace.items << 'remove:done:${unsafe { event.package_operation.oldpkg }.get_name()}' |
| 28 | } |
| 29 | .pacsave_created { |
| 30 | path := unsafe { cstring_to_vstring(event.pacsave_created.file) } |
| 31 | trace.items << 'pacsave:${path[trace.root.len..]}' |
| 32 | } |
| 33 | else {} |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn native_removal_execution_event(context voidptr, event engine.CommitEvent) { |
| 38 | mut trace := unsafe { &RemovalExecutionTrace(context) } |
| 39 | match event.kind { |
| 40 | .transaction_start { trace.items << 'transaction:start' } |
| 41 | .transaction_done { trace.items << 'transaction:done' } |
| 42 | .package_remove_start { |
| 43 | trace.items << 'remove:start:${event.old_package.name}' |
| 44 | if trace.interrupt { |
| 45 | assert trace.native_handle.interrupt_transaction_async() == 0 |
| 46 | } |
| 47 | } |
| 48 | .package_remove_done { trace.items << 'remove:done:${event.old_package.name}' } |
| 49 | .pacsave_created { trace.items << 'pacsave:${event.file[trace.root.len..]}' } |
| 50 | else {} |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn reference_removal_execution_progress(context voidptr, kind Progress, name &u8, percent i32, total uint, current uint) { |
| 55 | if kind != .remove_start { return } |
| 56 | mut trace := unsafe { &RemovalExecutionTrace(context) } |
| 57 | trace.items << 'progress:${unsafe { cstring_to_vstring(name) }}:${percent}:${total}:${current}' |
| 58 | } |
| 59 | |
| 60 | fn native_removal_execution_progress(context voidptr, event engine.CommitProgress) { |
| 61 | mut trace := unsafe { &RemovalExecutionTrace(context) } |
| 62 | trace.items << 'progress:${event.package}:${event.percent}:${event.total}:${event.current}' |
| 63 | } |
| 64 | |
| 65 | fn removal_execution_fixture(base string, scenario string) ! { |
| 66 | root := os.join_path(base, 'root') |
| 67 | db := os.join_path(base, 'db', 'local') |
| 68 | os.mkdir_all(os.join_path(root, 'etc'))! |
| 69 | os.mkdir_all(os.join_path(root, 'usr', 'bin'))! |
| 70 | os.mkdir_all(os.join_path(db, 'app-1-1'))! |
| 71 | os.write_file(os.join_path(db, 'ALPM_DB_VERSION'), '9\n')! |
| 72 | config := os.join_path(root, 'etc', 'app.conf') |
| 73 | os.write_file(config, 'original')! |
| 74 | hash := compute_md5sum(config) or { return error('cannot hash fixture') } |
| 75 | mut backups := '%BACKUP%\netc/app.conf\t${if scenario == 'empty-hash' { '' } else { hash }}\n\n' |
| 76 | if scenario == 'no-backup' { backups = '' } |
| 77 | if scenario in ['modified', 'nosave', 'rotation', 'empty-hash', 'dangling-pacsave', 'cyclic-pacsave'] { |
| 78 | os.write_file(config, 'modified')! |
| 79 | } |
| 80 | if scenario == 'missing' { os.rm(config)! } |
| 81 | if scenario in ['backup-symlink', 'broken-config', 'cyclic-config'] { |
| 82 | os.rm(config)! |
| 83 | target := match scenario { |
| 84 | 'backup-symlink' { 'target' } |
| 85 | 'broken-config' { 'missing' } |
| 86 | else { 'app.conf' } |
| 87 | } |
| 88 | os.symlink(target, config)! |
| 89 | if scenario == 'backup-symlink' { |
| 90 | os.write_file(os.join_path(root, 'etc', 'target'), 'modified')! |
| 91 | } |
| 92 | } |
| 93 | os.write_file(os.join_path(root, 'usr', 'bin', 'app'), 'executable')! |
| 94 | mut files := ['etc/', 'etc/app.conf', 'usr/', 'usr/bin/', 'usr/bin/app'] |
| 95 | if scenario == 'symlink' { |
| 96 | os.mkdir_all(os.join_path(root, 'target'))! |
| 97 | os.symlink('target', os.join_path(root, 'link'))! |
| 98 | files << 'link/' |
| 99 | } |
| 100 | if scenario == 'merged' { |
| 101 | os.mkdir_all(os.join_path(root, 'usr', 'lib'))! |
| 102 | os.write_file(os.join_path(root, 'usr', 'lib', 'app'), 'library')! |
| 103 | os.symlink('usr/lib', os.join_path(root, 'lib'))! |
| 104 | files << ['lib/', 'lib/app'] |
| 105 | } |
| 106 | if scenario == 'rotation' { |
| 107 | for suffix in ['.pacsave', '.pacsave.1', '.pacsave.3', '.pacsave.03'] { |
| 108 | os.write_file(config + suffix, suffix)! |
| 109 | } |
| 110 | } |
| 111 | if scenario == 'dangling-pacsave' { os.symlink('missing', config + '.pacsave')! } |
| 112 | if scenario == 'cyclic-pacsave' { os.symlink('app.conf.pacsave', config + '.pacsave')! } |
| 113 | files.sort() |
| 114 | os.write_file(os.join_path(db, 'app-1-1', 'desc'), '%GROUPS%\ntest\n\n')! |
| 115 | os.write_file(os.join_path(db, 'app-1-1', 'files'), '%FILES%\n${files.join('\n')}\n\n${backups}')! |
| 116 | if scenario == 'shared' { |
| 117 | os.mkdir_all(os.join_path(db, 'keeper-1-1'))! |
| 118 | os.write_file(os.join_path(db, 'keeper-1-1', 'desc'), '%GROUPS%\ntest\n\n')! |
| 119 | os.write_file(os.join_path(db, 'keeper-1-1', 'files'), '%FILES%\netc/\n\n')! |
| 120 | } |
| 121 | if scenario == 'interrupt' { |
| 122 | os.write_file(os.join_path(root, 'usr', 'bin', 'later'), 'later')! |
| 123 | os.mkdir_all(os.join_path(db, 'later-1-1'))! |
| 124 | os.write_file(os.join_path(db, 'later-1-1', 'desc'), '%GROUPS%\ntest\n\n')! |
| 125 | os.write_file(os.join_path(db, 'later-1-1', 'files'), '%FILES%\nusr/\nusr/bin/\nusr/bin/later\n\n')! |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | fn removal_tree_snapshot(root string, path string, mut result map[string]string) ! { |
| 130 | for name in os.ls(path)! { |
| 131 | full := os.join_path(path, name) |
| 132 | relative := full[root.len..] |
| 133 | if os.is_link(full) { |
| 134 | result[relative] = 'link:' + os.readlink(full)! |
| 135 | } else if os.is_dir(full) { |
| 136 | result[relative] = 'dir' |
| 137 | removal_tree_snapshot(root, full, mut result)! |
| 138 | } else { |
| 139 | result[relative] = 'file:' + os.read_file(full)! |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fn reference_execute_removal(handle &Handle, packages []&Package, flags TransFlag) ! { |
| 145 | handle.trans_init(flags)! |
| 146 | defer { handle.trans_release() or { eprintln(err) } } |
| 147 | for pkg in packages { handle.remove_pkg(pkg)! } |
| 148 | prepared := handle.trans_prepare()! |
| 149 | prepared.free() |
| 150 | committed := handle.trans_commit()! |
| 151 | committed.free() |
| 152 | } |
| 153 | |
| 154 | fn native_execute_removal(mut handle engine.Handle, packages []&engine.Package, flags engine.TransactionFlag) ! { |
| 155 | handle.start_transaction(flags)! |
| 156 | defer { handle.release_transaction() or { eprintln(err) } } |
| 157 | for pkg in packages { handle.remove_package(pkg)! } |
| 158 | assert handle.prepare_transaction()!.success |
| 159 | handle.commit_transaction()! |
| 160 | assert handle.transaction_state()! == .committed |
| 161 | } |
| 162 | |
| 163 | fn test_native_removal_commit_files_backups_events_and_progress_match_libalpm() ! { |
| 164 | dir := os.join_path(os.vtmp_dir(), 'alpm-removal-execution-${os.getpid()}') |
| 165 | os.mkdir_all(dir)! |
| 166 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 167 | for scenario in ['default', 'unchanged', 'modified', 'empty-hash', 'no-backup', 'nosave', |
| 168 | 'noupgrade', 'negated', 'noextract', 'missing', 'shared', 'symlink', 'merged', 'rotation', |
| 169 | 'dangling-pacsave', 'cyclic-pacsave', 'backup-symlink', 'broken-config', 'cyclic-config', |
| 170 | 'db-only', 'interrupt'] { |
| 171 | base := os.join_path(dir, scenario) |
| 172 | old_base := os.join_path(base, 'old') |
| 173 | native_base := os.join_path(base, 'native') |
| 174 | removal_execution_fixture(old_base, scenario)! |
| 175 | removal_execution_fixture(native_base, scenario)! |
| 176 | old := initialize(os.join_path(old_base, 'root'), os.join_path(old_base, 'db'))! |
| 177 | defer { old.release() or { eprintln(err) } } |
| 178 | mut native := engine.initialize(os.join_path(native_base, 'root'), os.join_path(native_base, 'db'))! |
| 179 | defer { native.release() or { eprintln(err) } } |
| 180 | mut expected := &RemovalExecutionTrace{ root: old.option_get_root(), reference_handle: old, interrupt: scenario == 'interrupt' } |
| 181 | mut actual := &RemovalExecutionTrace{ root: native.root, native_handle: native, interrupt: scenario == 'interrupt' } |
| 182 | old.option_set_eventcb(reference_removal_execution_event, expected)! |
| 183 | old.option_set_progresscb(reference_removal_execution_progress, expected)! |
| 184 | native.set_commit_callback(native_removal_execution_event, actual)! |
| 185 | native.set_commit_progress_callback(native_removal_execution_progress, actual)! |
| 186 | if scenario in ['noupgrade', 'negated'] { |
| 187 | old.option_add_noupgrade('etc/*')! |
| 188 | native.add_string_list(.no_upgrade, 'etc/*')! |
| 189 | if scenario == 'negated' { |
| 190 | old.option_add_noupgrade('!etc/app.conf')! |
| 191 | native.add_string_list(.no_upgrade, '!etc/app.conf')! |
| 192 | } |
| 193 | } |
| 194 | if scenario == 'noextract' { |
| 195 | old.option_add_no_extract('etc/*')! |
| 196 | native.add_string_list(.no_extract, 'etc/*')! |
| 197 | } |
| 198 | old_db := old.get_localdb()! |
| 199 | mut db := native.local_database()! |
| 200 | old_pkg := old_db.get_pkg('app') or { return error('missing reference package') } |
| 201 | pkg := db.get_pkg('app') or { return error('missing native package') } |
| 202 | mut flags := TransFlag.no_hooks | .no_scriptlet |
| 203 | if scenario == 'default' { flags = trans_flag_0 } |
| 204 | if scenario == 'nosave' { flags |= .no_save } |
| 205 | if scenario == 'db-only' { flags |= .db_only } |
| 206 | mut old_targets := [old_pkg] |
| 207 | mut targets := [pkg] |
| 208 | if scenario == 'interrupt' { |
| 209 | old_targets << old_db.get_pkg('later') or { return error('missing later package') } |
| 210 | targets << db.get_pkg('later') or { return error('missing later package') } |
| 211 | flags |= .no_deps |
| 212 | } |
| 213 | reference_execute_removal(old, old_targets, flags)! |
| 214 | native_execute_removal(mut native, targets, unsafe { engine.TransactionFlag(flags) })! |
| 215 | assert actual.items == expected.items, '${scenario}: ${actual.items} != ${expected.items}' |
| 216 | mut expected_files := map[string]string{} |
| 217 | mut actual_files := map[string]string{} |
| 218 | removal_tree_snapshot(old_base + '/', old_base, mut expected_files)! |
| 219 | removal_tree_snapshot(native_base + '/', native_base, mut actual_files)! |
| 220 | assert actual_files == expected_files, '${scenario}: ${actual_files} != ${expected_files}' |
| 221 | assert db.get_pkg('app') == none |
| 222 | assert !os.exists(native.lockfile) |
| 223 | } |
| 224 | } |
| 225 | |