nspkg / tests / coracle / install_commit_compatibility_test.v
220 lines · 211 sloc · 9.96 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module coracle
2
3import nspkg.engine
4import nspkg.tests.installfixtures
5import os
6
7struct InstallExecutionTrace {
8 root string
9 interrupt bool
10mut:
11 items []string
12 reference &Handle = unsafe { nil }
13 native &engine.Handle = unsafe { nil }
14}
15
16fn install_reference_event(context voidptr, event &Event) {
17 mut trace := unsafe { &InstallExecutionTrace(context) }
18 type_ := unsafe { event.type_ }
19 match type_ {
20 .package_operation_start, .package_operation_done {
21 item := unsafe { event.package_operation }
22 old := if item.oldpkg == unsafe { nil } {
23 ''
24 } else {
25 item.oldpkg.get_name() + ':' + item.oldpkg.get_version()
26 }
27 new := if item.newpkg == unsafe { nil } {
28 ''
29 } else {
30 item.newpkg.get_name() + ':' + item.newpkg.get_version() + ':' + u32(item.newpkg.get_reason()).str()
31 }
32 trace.items << '${type_}:${item.operation}:${old}:${new}'
33 if trace.interrupt && type_ == .package_operation_start && item.operation != .remove {
34 trace.reference.trans_interrupt() or { assert false, err.msg() }
35 }
36 }
37 .pacnew_created {
38 item := unsafe { event.pacnew_created }
39 trace.items << 'pacnew:${item.from_noupgrade}:${unsafe { cstring_to_vstring(item.file) }[trace.root.len..]}'
40 }
41 .pacsave_created {
42 trace.items << 'pacsave:${unsafe { cstring_to_vstring(event.pacsave_created.file) }[trace.root.len..]}'
43 }
44 .transaction_start, .transaction_done, .fileconflicts_start, .fileconflicts_done,
45 .keyring_start, .keyring_done, .integrity_start, .integrity_done, .load_start, .load_done,
46 .diskspace_start, .diskspace_done {
47 trace.items << type_.str()
48 }
49 else {}
50 }
51}
52
53fn install_native_event(context voidptr, event engine.CommitEvent) {
54 mut trace := unsafe { &InstallExecutionTrace(context) }
55 match event.kind {
56 .package_install_start, .package_install_done, .package_remove_start, .package_remove_done {
57 old := if event.old_package == unsafe { nil } {
58 ''
59 } else {
60 event.old_package.name + ':' + event.old_package.version
61 }
62 new := if event.new_package == unsafe { nil } {
63 ''
64 } else {
65 event.new_package.name + ':' + event.new_package.version + ':' + u32(event.new_package.reason).str()
66 }
67 phase := if event.kind in [.package_install_start, .package_remove_start] {
68 'package_operation_start'
69 } else {
70 'package_operation_done'
71 }
72 trace.items << '${phase}:${event.operation}:${old}:${new}'
73 if trace.interrupt && event.kind == .package_install_start {
74 assert trace.native.interrupt_transaction_async() == 0
75 }
76 }
77 .pacnew_created {
78 trace.items << 'pacnew:${if event.from_no_upgrade { 1 } else { 0 }}:${event.file[trace.root.len..]}'
79 }
80 .pacsave_created { trace.items << 'pacsave:${event.file[trace.root.len..]}' }
81 .file_conflicts_start { trace.items << 'fileconflicts_start' }
82 .file_conflicts_done { trace.items << 'fileconflicts_done' }
83 else { trace.items << event.kind.str() }
84 }
85}
86
87fn install_reference_progress(context voidptr, kind Progress, name &u8, percent i32, total uint, current uint) {
88 mut trace := unsafe { &InstallExecutionTrace(context) }
89 trace.items << 'progress:${kind.str().all_before('_start')}:${unsafe { cstring_to_vstring(name) }}:${percent}:${total}:${current}'
90}
91
92fn install_native_progress(context voidptr, item engine.CommitProgress) {
93 mut trace := unsafe { &InstallExecutionTrace(context) }
94 kind := if item.kind == .file_conflicts { 'conflicts' } else { item.kind.str() }
95 trace.items << 'progress:${kind}:${item.package}:${item.percent}:${item.total}:${item.current}'
96}
97
98fn install_reference(fixture installfixtures.Fixture, scenario installfixtures.Scenario) ![]string {
99 handle := initialize(fixture.root, fixture.db)!
100 defer { handle.release() or { eprintln(err) } }
101 handle.option_add_cachedir(fixture.cache)!
102 mut trace := &InstallExecutionTrace{ root: handle.option_get_root(), reference: handle, interrupt: scenario.interrupt }
103 handle.option_set_eventcb(install_reference_event, trace)!
104 handle.option_set_progresscb(install_reference_progress, trace)!
105 for pattern in scenario.no_upgrade { handle.option_add_noupgrade(pattern)! }
106 for pattern in scenario.no_extract { handle.option_add_no_extract(pattern)! }
107 handle.option_set_checkspace(if scenario.check_space { 1 } else { 0 })!
108 mut flags := TransFlag.no_deps | .no_conflicts | .no_hooks | .no_scriptlet
109 if scenario.db_only { flags.set(.db_only) }
110 if scenario.no_save { flags.set(.no_save) }
111 if scenario.all_deps { flags.set(.all_deps) }
112 if scenario.all_explicit { flags.set(.all_explicit) }
113 handle.trans_init(flags)!
114 defer { handle.trans_release() or { eprintln(err) } }
115 if scenario.remove_victim {
116 db := handle.get_localdb()!
117 handle.remove_pkg(db.get_pkg('victim') or { return error('missing victim') })!
118 }
119 for path in fixture.archives {
120 handle.add_pkg(handle.pkg_load(path, if scenario.partial { 0 } else { 1 }, 0)!)!
121 }
122 handle.trans_prepare()!.free()
123 handle.trans_commit()!.free()
124 return trace.items.clone()
125}
126
127fn install_native(fixture installfixtures.Fixture, scenario installfixtures.Scenario) ![]string {
128 mut handle := engine.initialize(fixture.root, fixture.db)!
129 defer { handle.release() or { eprintln(err) } }
130 handle.add_string_list(.cache_dirs, fixture.cache)!
131 mut trace := &InstallExecutionTrace{ root: handle.root, native: handle, interrupt: scenario.interrupt }
132 handle.set_commit_callback(install_native_event, trace)!
133 handle.set_commit_progress_callback(install_native_progress, trace)!
134 for pattern in scenario.no_upgrade { handle.add_string_list(.no_upgrade, pattern)! }
135 for pattern in scenario.no_extract { handle.add_string_list(.no_extract, pattern)! }
136 handle.set_integer_option(.check_space, if scenario.check_space { 1 } else { 0 })!
137 mut flags := engine.TransactionFlag.no_deps | .no_conflicts | .no_hooks | .no_scriptlet
138 if scenario.db_only { flags.set(.db_only) }
139 if scenario.no_save { flags.set(.no_save) }
140 if scenario.all_deps { flags.set(.all_deps) }
141 if scenario.all_explicit { flags.set(.all_explicit) }
142 handle.start_transaction(flags)!
143 defer { handle.release_transaction() or { eprintln(err) } }
144 if scenario.remove_victim {
145 mut db := handle.local_database()!
146 handle.remove_package(db.get_pkg('victim') or { return error('missing victim') })!
147 }
148 for path in fixture.archives {
149 handle.add_package(handle.load_package(path, !scenario.partial, 0)!)!
150 }
151 assert handle.prepare_transaction()!.success
152 handle.commit_transaction()!
153 assert handle.transaction_state()! == .committed
154 mut db := handle.local_database()!
155 mut installed := db.get_pkg('app') or { return error('missing installed package') }
156 assert installed.version == scenario.version
157 assert installed.origin == .local_db
158 assert voidptr(installed.owner_database()!) == voidptr(db)
159 assert installed.install_date == 1700000000
160 return trace.items.clone()
161}
162
163fn test_native_install_upgrade_backups_database_and_progress_match_libalpm() ! {
164 dir := os.join_path(os.vtmp_dir(), 'alpm-install-execution-${os.getpid()}')
165 defer { os.rmdir_all(dir) or { eprintln(err) } }
166 previous := os.getenv_opt('SOURCE_DATE_EPOCH')
167 assert os.setenv('SOURCE_DATE_EPOCH', '1700000000', true) == 0
168 defer {
169 if value := previous {
170 os.setenv('SOURCE_DATE_EPOCH', value, true)
171 } else {
172 os.unsetenv('SOURCE_DATE_EPOCH')
173 }
174 }
175 mut cases := []installfixtures.Scenario{}
176 for local in ['original', 'incoming', 'custom'] {
177 for incoming in ['original', 'incoming', 'custom'] {
178 for pacnew in [false, true] {
179 cases << installfixtures.Scenario{ name: 'hash-${local}-${incoming}-${pacnew}', local_text: local, incoming: incoming, pacnew: pacnew }
180 }
181 }
182 }
183 cases << [
184 installfixtures.Scenario{ name: 'fresh', installed: false, missing_local: true },
185 installfixtures.Scenario{ name: 'fresh-unowned', installed: false },
186 installfixtures.Scenario{ name: 'reinstall', version: '1-1' },
187 installfixtures.Scenario{ name: 'downgrade', version: '0.5-1' },
188 installfixtures.Scenario{ name: 'missing-local', missing_local: true },
189 installfixtures.Scenario{ name: 'missing-hash', missing_hash: true },
190 installfixtures.Scenario{ name: 'add-backup', old_backup: false },
191 installfixtures.Scenario{ name: 'drop-backup', new_backup: false, local_text: 'custom' },
192 installfixtures.Scenario{ name: 'drop-backup-nosave', new_backup: false, local_text: 'custom', no_save: true },
193 installfixtures.Scenario{ name: 'local-symlink', local_symlink: true, local_text: 'custom' },
194 installfixtures.Scenario{ name: 'noupgrade', no_upgrade: ['etc/*'] },
195 installfixtures.Scenario{ name: 'noupgrade-negated', no_upgrade: ['etc/*', '!etc/app.conf'] },
196 installfixtures.Scenario{ name: 'noextract', no_extract: ['etc/*'] },
197 installfixtures.Scenario{ name: 'noextract-negated', no_extract: ['etc/*', '!etc/app.conf'] },
198 installfixtures.Scenario{ name: 'db-only', db_only: true },
199 installfixtures.Scenario{ name: 'all-deps', all_deps: true },
200 installfixtures.Scenario{ name: 'all-explicit', all_explicit: true },
201 installfixtures.Scenario{ name: 'both-reasons', all_deps: true, all_explicit: true },
202 installfixtures.Scenario{ name: 'check-space', check_space: true },
203 installfixtures.Scenario{ name: 'db-only-space', db_only: true, check_space: true },
204 installfixtures.Scenario{ name: 'two-targets', second: true },
205 installfixtures.Scenario{ name: 'interrupt-last', interrupt: true },
206 installfixtures.Scenario{ name: 'interrupt-first', second: true, interrupt: true },
207 installfixtures.Scenario{ name: 'remove-victim', remove_victim: true },
208 installfixtures.Scenario{ name: 'mtree', mtree: true },
209 ]
210 for scenario in cases {
211 base := os.join_path(dir, scenario.name)
212 reference := installfixtures.create(os.join_path(base, 'reference'), scenario)!
213 native := installfixtures.create(os.join_path(base, 'native'), scenario)!
214 expected := install_reference(reference, scenario)!
215 actual := install_native(native, scenario)!
216 assert actual == expected, '${scenario.name}\nnative: ${actual}\nreference: ${expected}'
217 assert installfixtures.snapshot(native.root)! == installfixtures.snapshot(reference.root)!, scenario.name
218 assert installfixtures.snapshot(native.db)! == installfixtures.snapshot(reference.db)!, scenario.name
219 }
220}
221