| 1 | module coracle |
| 2 | |
| 3 | import nspkg as facade |
| 4 | import nspkg.tests.installfixtures |
| 5 | import os |
| 6 | |
| 7 | struct PublicInstallTrace { |
| 8 | root string |
| 9 | logfile string |
| 10 | interrupt bool |
| 11 | mut: |
| 12 | items []string |
| 13 | action_count int |
| 14 | reference &Handle = unsafe { nil } |
| 15 | native &facade.Handle = unsafe { nil } |
| 16 | } |
| 17 | |
| 18 | fn public_install_reference_event(context voidptr, event &Event) { |
| 19 | mut trace := unsafe { &PublicInstallTrace(context) } |
| 20 | trace.capture_actions() |
| 21 | type_ := unsafe { event.type_ } |
| 22 | match type_ { |
| 23 | .package_operation_start, .package_operation_done { |
| 24 | item := unsafe { event.package_operation } |
| 25 | old := if item.oldpkg == unsafe { nil } { |
| 26 | '' |
| 27 | } else { |
| 28 | item.oldpkg.get_name() + ':' + item.oldpkg.get_version() |
| 29 | } |
| 30 | new := if item.newpkg == unsafe { nil } { |
| 31 | '' |
| 32 | } else { |
| 33 | item.newpkg.get_name() + ':' + item.newpkg.get_version() + ':' + u32(item.newpkg.get_reason()).str() |
| 34 | } |
| 35 | trace.items << '${type_}:${item.operation}:${old}:${new}' |
| 36 | if trace.interrupt && type_ == .package_operation_start && item.operation != .remove { |
| 37 | trace.reference.trans_interrupt() or { assert false, err.msg() } |
| 38 | } |
| 39 | } |
| 40 | .pacnew_created { |
| 41 | item := unsafe { event.pacnew_created } |
| 42 | trace.items << 'pacnew:${item.from_noupgrade}:${unsafe { cstring_to_vstring(item.file) }[trace.root.len..]}' |
| 43 | } |
| 44 | .pacsave_created { |
| 45 | trace.items << 'pacsave:${unsafe { cstring_to_vstring(event.pacsave_created.file) }[trace.root.len..]}' |
| 46 | } |
| 47 | .transaction_start, .transaction_done, .fileconflicts_start, .fileconflicts_done, |
| 48 | .keyring_start, .keyring_done, .integrity_start, .integrity_done, .load_start, .load_done, |
| 49 | .diskspace_start, .diskspace_done { |
| 50 | trace.items << type_.str() |
| 51 | } |
| 52 | else {} |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn public_install_native_event(context voidptr, event &facade.Event) { |
| 57 | mut trace := unsafe { &PublicInstallTrace(context) } |
| 58 | trace.capture_actions() |
| 59 | type_ := unsafe { event.type_ } |
| 60 | match type_ { |
| 61 | .package_operation_start, .package_operation_done { |
| 62 | item := unsafe { event.package_operation } |
| 63 | old := if item.oldpkg == unsafe { nil } { |
| 64 | '' |
| 65 | } else { |
| 66 | item.oldpkg.get_name() + ':' + item.oldpkg.get_version() |
| 67 | } |
| 68 | new := if item.newpkg == unsafe { nil } { |
| 69 | '' |
| 70 | } else { |
| 71 | item.newpkg.get_name() + ':' + item.newpkg.get_version() + ':' + u32(item.newpkg.get_reason()).str() |
| 72 | } |
| 73 | trace.items << '${type_}:${item.operation}:${old}:${new}' |
| 74 | if trace.interrupt && type_ == .package_operation_start && item.operation != .remove { |
| 75 | trace.native.trans_interrupt() or { assert false, err.msg() } |
| 76 | } |
| 77 | } |
| 78 | .pacnew_created { |
| 79 | item := unsafe { event.pacnew_created } |
| 80 | trace.items << 'pacnew:${item.from_noupgrade}:${unsafe { cstring_to_vstring(item.file) }[trace.root.len..]}' |
| 81 | } |
| 82 | .pacsave_created { |
| 83 | trace.items << 'pacsave:${unsafe { cstring_to_vstring(event.pacsave_created.file) }[trace.root.len..]}' |
| 84 | } |
| 85 | .transaction_start, .transaction_done, .fileconflicts_start, .fileconflicts_done, |
| 86 | .keyring_start, .keyring_done, .integrity_start, .integrity_done, .load_start, .load_done, |
| 87 | .diskspace_start, .diskspace_done { |
| 88 | trace.items << type_.str() |
| 89 | } |
| 90 | else {} |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | fn public_install_reference_progress(context voidptr, kind Progress, name &u8, percent i32, total uint, current uint) { |
| 95 | mut trace := unsafe { &PublicInstallTrace(context) } |
| 96 | trace.capture_actions() |
| 97 | trace.items << 'progress:${kind.str().all_before('_start')}:${unsafe { cstring_to_vstring(name) }}:${percent}:${total}:${current}' |
| 98 | } |
| 99 | |
| 100 | fn public_install_native_progress(context voidptr, kind facade.Progress, name &u8, percent i32, total uint, current uint) { |
| 101 | mut trace := unsafe { &PublicInstallTrace(context) } |
| 102 | trace.capture_actions() |
| 103 | trace.items << 'progress:${kind.str().all_before('_start')}:${unsafe { cstring_to_vstring(name) }}:${percent}:${total}:${current}' |
| 104 | } |
| 105 | |
| 106 | fn public_install_reference(fixture installfixtures.Fixture, scenario installfixtures.Scenario, download_only bool) ![]string { |
| 107 | handle := initialize(fixture.root, fixture.db)! |
| 108 | defer { handle.release() or { eprintln(err) } } |
| 109 | handle.option_add_cachedir(fixture.cache)! |
| 110 | logfile := os.join_path(os.dir(fixture.db), 'actions.log') |
| 111 | handle.option_set_logfile(logfile)! |
| 112 | mut trace := &PublicInstallTrace{ root: handle.option_get_root(), logfile: logfile, reference: handle, interrupt: scenario.interrupt } |
| 113 | handle.option_set_eventcb(public_install_reference_event, trace)! |
| 114 | handle.option_set_progresscb(public_install_reference_progress, trace)! |
| 115 | for pattern in scenario.no_upgrade { handle.option_add_noupgrade(pattern)! } |
| 116 | for pattern in scenario.no_extract { handle.option_add_no_extract(pattern)! } |
| 117 | handle.option_set_checkspace(if scenario.check_space { 1 } else { 0 })! |
| 118 | mut flags := TransFlag.no_deps | .no_conflicts | .no_hooks | .no_scriptlet |
| 119 | if scenario.db_only { flags.set(.db_only) } |
| 120 | if download_only { flags.set(.download_only) } |
| 121 | if scenario.no_save { flags.set(.no_save) } |
| 122 | if scenario.all_deps { flags.set(.all_deps) } |
| 123 | if scenario.all_explicit { flags.set(.all_explicit) } |
| 124 | handle.trans_init(flags)! |
| 125 | defer { handle.trans_release() or { eprintln(err) } } |
| 126 | if scenario.remove_victim { |
| 127 | db := handle.get_localdb()! |
| 128 | handle.remove_pkg(db.get_pkg('victim') or { return error('missing victim') })! |
| 129 | } |
| 130 | for path in fixture.archives { |
| 131 | handle.add_pkg(handle.pkg_load(path, if scenario.partial { 0 } else { 1 }, 0)!)! |
| 132 | } |
| 133 | handle.trans_prepare()!.free() |
| 134 | handle.trans_commit()!.free() |
| 135 | trace.capture_actions() |
| 136 | return trace.items.clone() |
| 137 | } |
| 138 | |
| 139 | fn public_install_native(fixture installfixtures.Fixture, scenario installfixtures.Scenario, download_only bool) ![]string { |
| 140 | handle := facade.initialize(fixture.root, fixture.db)! |
| 141 | defer { handle.release() or { eprintln(err) } } |
| 142 | handle.option_add_cachedir(fixture.cache)! |
| 143 | logfile := os.join_path(os.dir(fixture.db), 'actions.log') |
| 144 | handle.option_set_logfile(logfile)! |
| 145 | mut trace := &PublicInstallTrace{ root: handle.option_get_root(), logfile: logfile, native: handle, interrupt: scenario.interrupt } |
| 146 | handle.option_set_eventcb(public_install_native_event, trace)! |
| 147 | handle.option_set_progresscb(public_install_native_progress, trace)! |
| 148 | for pattern in scenario.no_upgrade { handle.option_add_noupgrade(pattern)! } |
| 149 | for pattern in scenario.no_extract { handle.option_add_no_extract(pattern)! } |
| 150 | handle.option_set_checkspace(if scenario.check_space { 1 } else { 0 })! |
| 151 | mut flags := facade.TransFlag.no_deps | .no_conflicts | .no_hooks | .no_scriptlet |
| 152 | if scenario.db_only { flags.set(.db_only) } |
| 153 | if download_only { flags.set(.download_only) } |
| 154 | if scenario.no_save { flags.set(.no_save) } |
| 155 | if scenario.all_deps { flags.set(.all_deps) } |
| 156 | if scenario.all_explicit { flags.set(.all_explicit) } |
| 157 | handle.trans_init(flags)! |
| 158 | defer { handle.trans_release() or { eprintln(err) } } |
| 159 | if scenario.remove_victim { |
| 160 | db := handle.get_localdb()! |
| 161 | handle.remove_pkg(db.get_pkg('victim') or { return error('missing victim') })! |
| 162 | } |
| 163 | for path in fixture.archives { |
| 164 | handle.add_pkg(handle.pkg_load(path, if scenario.partial { 0 } else { 1 }, 0)!)! |
| 165 | } |
| 166 | handle.trans_prepare()!.free() |
| 167 | handle.trans_commit()!.free() |
| 168 | trace.capture_actions() |
| 169 | return trace.items.clone() |
| 170 | } |
| 171 | |
| 172 | fn test_public_install_upgrade_backups_database_and_progress_match_libalpm() ! { |
| 173 | dir := os.join_path(os.vtmp_dir(), 'alpm-public-install-execution-${os.getpid()}') |
| 174 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 175 | previous := os.getenv_opt('SOURCE_DATE_EPOCH') |
| 176 | assert os.setenv('SOURCE_DATE_EPOCH', '1700000000', true) == 0 |
| 177 | defer { |
| 178 | if value := previous { |
| 179 | os.setenv('SOURCE_DATE_EPOCH', value, true) |
| 180 | } else { |
| 181 | os.unsetenv('SOURCE_DATE_EPOCH') |
| 182 | } |
| 183 | } |
| 184 | mut cases := []installfixtures.Scenario{} |
| 185 | for local in ['original', 'incoming', 'custom'] { |
| 186 | for incoming in ['original', 'incoming', 'custom'] { |
| 187 | for pacnew in [false, true] { |
| 188 | cases << installfixtures.Scenario{ name: 'hash-${local}-${incoming}-${pacnew}', local_text: local, incoming: incoming, pacnew: pacnew } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | cases << [ |
| 193 | installfixtures.Scenario{ name: 'fresh', installed: false, missing_local: true }, |
| 194 | installfixtures.Scenario{ name: 'fresh-unowned', installed: false }, |
| 195 | installfixtures.Scenario{ name: 'reinstall', version: '1-1' }, |
| 196 | installfixtures.Scenario{ name: 'downgrade', version: '0.5-1' }, |
| 197 | installfixtures.Scenario{ name: 'missing-local', missing_local: true }, |
| 198 | installfixtures.Scenario{ name: 'missing-hash', missing_hash: true }, |
| 199 | installfixtures.Scenario{ name: 'add-backup', old_backup: false }, |
| 200 | installfixtures.Scenario{ name: 'drop-backup', new_backup: false, local_text: 'custom' }, |
| 201 | installfixtures.Scenario{ name: 'drop-backup-nosave', new_backup: false, local_text: 'custom', no_save: true }, |
| 202 | installfixtures.Scenario{ name: 'local-symlink', local_symlink: true, local_text: 'custom' }, |
| 203 | installfixtures.Scenario{ name: 'noupgrade', no_upgrade: ['etc/*'] }, |
| 204 | installfixtures.Scenario{ name: 'noupgrade-negated', no_upgrade: ['etc/*', '!etc/app.conf'] }, |
| 205 | installfixtures.Scenario{ name: 'noextract', no_extract: ['etc/*'] }, |
| 206 | installfixtures.Scenario{ name: 'noextract-negated', no_extract: ['etc/*', '!etc/app.conf'] }, |
| 207 | installfixtures.Scenario{ name: 'db-only', db_only: true }, |
| 208 | installfixtures.Scenario{ name: 'all-deps', all_deps: true }, |
| 209 | installfixtures.Scenario{ name: 'all-explicit', all_explicit: true }, |
| 210 | installfixtures.Scenario{ name: 'both-reasons', all_deps: true, all_explicit: true }, |
| 211 | installfixtures.Scenario{ name: 'check-space', check_space: true }, |
| 212 | installfixtures.Scenario{ name: 'db-only-space', db_only: true, check_space: true }, |
| 213 | installfixtures.Scenario{ name: 'two-targets', second: true }, |
| 214 | installfixtures.Scenario{ name: 'interrupt-last', interrupt: true }, |
| 215 | installfixtures.Scenario{ name: 'interrupt-first', second: true, interrupt: true }, |
| 216 | installfixtures.Scenario{ name: 'remove-victim', remove_victim: true }, |
| 217 | installfixtures.Scenario{ name: 'mtree', mtree: true }, |
| 218 | installfixtures.Scenario{ name: 'directory-mode' }, |
| 219 | ] |
| 220 | for scenario in cases { |
| 221 | base := os.join_path(dir, scenario.name) |
| 222 | reference := installfixtures.create(os.join_path(base, 'reference'), scenario)! |
| 223 | native := installfixtures.create(os.join_path(base, 'native'), scenario)! |
| 224 | if scenario.name == 'directory-mode' { |
| 225 | os.chmod(os.join_path(reference.root, 'usr/bin'), 0o700)! |
| 226 | os.chmod(os.join_path(native.root, 'usr/bin'), 0o700)! |
| 227 | } |
| 228 | expected := public_install_reference(reference, scenario, false)! |
| 229 | actual := public_install_native(native, scenario, false)! |
| 230 | assert actual == expected, '${scenario.name}\nnative: ${actual}\nreference: ${expected}' |
| 231 | assert installfixtures.snapshot(native.root)! == installfixtures.snapshot(reference.root)!, scenario.name |
| 232 | assert installfixtures.snapshot(native.db)! == installfixtures.snapshot(reference.db)!, scenario.name |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | fn (mut trace PublicInstallTrace) capture_actions() { |
| 237 | if !os.exists(trace.logfile) { return } |
| 238 | lines := os.read_lines(trace.logfile) or { |
| 239 | assert false, err.msg() |
| 240 | return |
| 241 | } |
| 242 | assert lines.len >= trace.action_count |
| 243 | for line in lines[trace.action_count..] { |
| 244 | trace.items << 'action:' + line.all_after('] ').replace(trace.root, 'ROOT/') |
| 245 | } |
| 246 | trace.action_count = lines.len |
| 247 | } |
| 248 | |
| 249 | fn test_public_download_only_writes_no_transaction_action_records() ! { |
| 250 | base := os.join_path(os.vtmp_dir(), 'alpm-public-download-only-actions-${os.getpid()}') |
| 251 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 252 | scenario := installfixtures.Scenario{ name: 'download-only' } |
| 253 | reference := installfixtures.create(os.join_path(base, 'reference'), scenario)! |
| 254 | native := installfixtures.create(os.join_path(base, 'native'), scenario)! |
| 255 | before := installfixtures.snapshot(native.root)! |
| 256 | expected := public_install_reference(reference, scenario, true)! |
| 257 | actual := public_install_native(native, scenario, true)! |
| 258 | assert actual == expected |
| 259 | assert !os.exists(os.join_path(os.dir(native.db), 'actions.log')) |
| 260 | assert !os.exists(os.join_path(os.dir(reference.db), 'actions.log')) |
| 261 | assert installfixtures.snapshot(native.root)! == before |
| 262 | assert installfixtures.snapshot(native.root)! == installfixtures.snapshot(reference.root)! |
| 263 | assert installfixtures.snapshot(native.db)! == installfixtures.snapshot(reference.db)! |
| 264 | } |
| 265 | |