| 1 | module coracle |
| 2 | |
| 3 | import nspkg.engine |
| 4 | import os |
| 5 | |
| 6 | struct PreparationTrace { |
| 7 | mut: |
| 8 | events []string |
| 9 | } |
| 10 | |
| 11 | fn reference_preparation_event(context voidptr, event &Event) { |
| 12 | mut trace := unsafe { &PreparationTrace(context) } |
| 13 | match unsafe { event.type_ } { |
| 14 | .checkdeps_start { trace.events << 'check:start' } |
| 15 | .checkdeps_done { trace.events << 'check:done' } |
| 16 | .optdep_removal { |
| 17 | removed := unsafe { event.optdep_removal } |
| 18 | trace.events << 'optional:${removed.pkg.get_name()}:${removed.optdep.compute_string()}' |
| 19 | } |
| 20 | else { |
| 21 | assert false, 'unexpected preparation event' |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn native_preparation_event(context voidptr, event engine.PreparationEvent) { |
| 27 | mut trace := unsafe { &PreparationTrace(context) } |
| 28 | match event.kind { |
| 29 | .check_dependencies_start { trace.events << 'check:start' } |
| 30 | .check_dependencies_done { trace.events << 'check:done' } |
| 31 | .optional_dependency_removed { |
| 32 | trace.events << 'optional:${event.package.name}:${event.dependency.compute_string()}' |
| 33 | } |
| 34 | else { |
| 35 | assert false, 'unexpected preparation event' |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn removal_fixture(path string, name string, depends []string, provides []string, optional []string, reason int) ! { |
| 41 | os.mkdir_all(os.join_path(path, 'local', '${name}-1-1'))! |
| 42 | text := '%REASON%\n${reason}\n\n%DEPENDS%\n${depends.join('\n')}\n\n%PROVIDES%\n${provides.join('\n')}\n\n%OPTDEPENDS%\n${optional.join('\n')}\n\n' |
| 43 | os.write_file(os.join_path(path, 'local', '${name}-1-1', 'desc'), text)! |
| 44 | os.write_file(os.join_path(path, 'local', '${name}-1-1', 'files'), '')! |
| 45 | } |
| 46 | |
| 47 | fn test_native_removal_preparation_matches_libalpm_flag_matrix() ! { |
| 48 | dir := os.join_path(os.vtmp_dir(), 'alpm-removal-prepare-parity-${os.getpid()}') |
| 49 | os.mkdir_all(os.join_path(dir, 'local'))! |
| 50 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 51 | os.write_file(os.join_path(dir, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 52 | removal_fixture(dir, 'base', [], ['virtual=2'], [], 1)! |
| 53 | removal_fixture(dir, 'shared', ['base'], [], [], 1)! |
| 54 | removal_fixture(dir, 'top', ['shared'], [], [], 0)! |
| 55 | removal_fixture(dir, 'consumer', ['virtual>=2'], [], [], 0)! |
| 56 | removal_fixture(dir, 'obsolete', ['virtual>=3'], [], [], 0)! |
| 57 | removal_fixture(dir, 'broken', ['already-missing'], [], [], 0)! |
| 58 | removal_fixture(dir, 'optional', [], [], ['virtual>=2: integration', 'leaf'], 0)! |
| 59 | removal_fixture(dir, 'leaf', [], [], [], 1)! |
| 60 | removal_fixture(dir, 'cycle-a', ['cycle-b'], [], [], 1)! |
| 61 | removal_fixture(dir, 'cycle-b', ['cycle-a'], [], [], 1)! |
| 62 | removal_fixture(dir, 'cycle-top', ['cycle-a'], [], [], 0)! |
| 63 | old := initialize('/', dir)! |
| 64 | defer { old.release() or { eprintln(err) } } |
| 65 | refdb := old.get_localdb()! |
| 66 | mut native := engine.initialize('/', dir)! |
| 67 | defer { native.release() or { eprintln(err) } } |
| 68 | mut db := native.local_database()! |
| 69 | mut ref_trace := &PreparationTrace{} |
| 70 | mut native_trace := &PreparationTrace{} |
| 71 | old.option_set_eventcb(reference_preparation_event, ref_trace)! |
| 72 | native.set_preparation_callback(native_preparation_event, native_trace)! |
| 73 | flag_bits := [C.ALPM_TRANS_FLAG_NODEPS, C.ALPM_TRANS_FLAG_CASCADE, C.ALPM_TRANS_FLAG_RECURSE, |
| 74 | C.ALPM_TRANS_FLAG_RECURSEALL, C.ALPM_TRANS_FLAG_UNNEEDED, C.ALPM_TRANS_FLAG_NODEPVERSION] |
| 75 | for combination in 0 .. 64 { |
| 76 | mut flags := int(C.ALPM_TRANS_FLAG_NOLOCK) |
| 77 | for bit, mask in flag_bits { |
| 78 | if combination & (1 << bit) != 0 { flags |= mask } |
| 79 | } |
| 80 | for targets in [['top'], ['base'], ['leaf'], ['base', 'shared'], ['cycle-top']] { |
| 81 | assert C.oracle_alpm_trans_init(old, flags) == 0 |
| 82 | native.start_transaction(unsafe { engine.TransactionFlag(flags) })! |
| 83 | for name in targets { |
| 84 | old.remove_pkg(refdb.get_pkg(name) or { return error('missing C target') })! |
| 85 | native.remove_package(db.get_pkg(name) or { return error('missing native target') })! |
| 86 | } |
| 87 | ref_trace.events = []string{} |
| 88 | native_trace.events = []string{} |
| 89 | problems, problem := old.trans_prepare_problems() |
| 90 | want_kind := old.errno() |
| 91 | result := native.prepare_transaction()! |
| 92 | assert result.success == (problem == none), '${combination}: ${targets}' |
| 93 | if !result.success { |
| 94 | assert int(result.kind) == int(want_kind) |
| 95 | assert result.missing.map('${it.target}|${it.dependency.compute_string()}|${it.causing_package}') == problems.map('${it.target}|${it.depend}|${it.causing}') |
| 96 | assert native.transaction_state()! == .initialized |
| 97 | } else { |
| 98 | assert native.transaction_state()! == .prepared |
| 99 | } |
| 100 | assert native.transaction_remove()!.map(it.name) == old.trans_get_remove().pkgs().map(it.get_name()), '${combination}: ${targets}' |
| 101 | assert native_trace.events == ref_trace.events, '${combination}: ${targets}' |
| 102 | native.release_transaction()! |
| 103 | old.trans_release()! |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | fn test_native_and_libalpm_agree_on_lock_ownership() ! { |
| 109 | dir := os.join_path(os.vtmp_dir(), 'alpm-lock-parity-${os.getpid()}') |
| 110 | os.mkdir_all(dir)! |
| 111 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 112 | old := initialize('/', dir)! |
| 113 | defer { old.release() or { eprintln(err) } } |
| 114 | mut native := engine.initialize('/', dir)! |
| 115 | defer { native.release() or { eprintln(err) } } |
| 116 | old.trans_init(trans_flag_0)! |
| 117 | if _ := native.start_transaction(engine.transaction_flag_0) { |
| 118 | assert false |
| 119 | } |
| 120 | assert native.errno() == .handle_lock |
| 121 | if _ := native.transaction_remove() { |
| 122 | assert false, 'a failed start cannot leave a transaction' |
| 123 | } |
| 124 | assert old.unlock_async() == 0 |
| 125 | assert old.unlock_async() == 0 |
| 126 | native.start_transaction(engine.transaction_flag_0)! |
| 127 | old.trans_release()! |
| 128 | assert os.exists(native.lockfile), 'the old owner must not unlink the new owner’s lock' |
| 129 | native.unlock()! |
| 130 | old.trans_init(trans_flag_0)! |
| 131 | native.release_transaction()! |
| 132 | assert os.exists(native.lockfile) |
| 133 | old.trans_release()! |
| 134 | assert !os.exists(native.lockfile) |
| 135 | old.trans_init(.nolock)! |
| 136 | native.start_transaction(.nolock)! |
| 137 | assert native.prepare_transaction()!.success |
| 138 | old.trans_prepare()! |
| 139 | if _ := old.trans_interrupt() { |
| 140 | assert false |
| 141 | } |
| 142 | if _ := native.interrupt_transaction() { |
| 143 | assert false |
| 144 | } |
| 145 | assert int(native.errno()) == int(old.errno()) |
| 146 | native.release_transaction()! |
| 147 | old.trans_release()! |
| 148 | assert unsafe { (&Handle(nil)).unlock_async() } == -1 |
| 149 | } |
| 150 | |