vpac / src / trans_test.v
283 lines · 268 sloc · 9.47 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4import json2
5import os
6
7fn test_split_list() {
8 // repeatable and comma separated, as pacman reads --ignore and friends
9 assert split_list([]) == []
10 assert split_list(['a']) == ['a']
11 assert split_list(['a,b', 'c']) == ['a', 'b', 'c']
12 assert split_list([' a , b ', ',,', '']) == ['a', 'b']
13 assert split_list(['/usr/*,/etc/*']) == ['/usr/*', '/etc/*']
14}
15
16fn test_dep_check_flag() {
17 // pacman's -d skips the version checks, -dd every dependency check;
18 // --nodeps is one -d
19 assert dep_check_flag(false, 0) == alpm.trans_flag_0
20 assert dep_check_flag(true, 0) == alpm.TransFlag.no_depversion
21 assert dep_check_flag(false, 1) == alpm.TransFlag.no_depversion
22 assert dep_check_flag(false, 2) == alpm.TransFlag.no_deps
23 assert dep_check_flag(true, 1) == alpm.TransFlag.no_deps
24 assert dep_check_flag(false, 3) == alpm.TransFlag.no_deps
25}
26
27// trans_sandbox opens a handle on an empty temp db, and points the ipc
28// channel at a file, so what the lifecycle says can be read back
29fn trans_sandbox(name string) !(string, &alpm.Handle) {
30 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_trans_${name}_${os.getpid()}')
31 os.rmdir_all(tmp) or {}
32 os.mkdir_all(os.join_path(tmp, 'root'))!
33 os.mkdir_all(os.join_path(tmp, 'db', 'local'))!
34 os.write_file(os.join_path(tmp, 'db', 'local', 'ALPM_DB_VERSION'), '9\n')!
35 handle := alpm.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')!
36 return tmp, handle
37}
38
39// said runs f with --ui ipc on and the channel in a file, and returns the
40// texts of the messages f sent
41fn said(tmp string, f fn ()) ![]string {
42 path := os.join_path(tmp, 'channel')
43 mut file := os.create(path)!
44 saved_kind, saved_fd := ui_kind, ipc_fd
45 ui_kind, ipc_fd = 'ipc', file.fd
46 f()
47 ui_kind, ipc_fd = saved_kind, saved_fd
48 file.close()
49 mut out := []string{}
50 for line in os.read_lines(path)! {
51 out << json2.decode[json2.Any](line)!.as_map()['text'] or { json2.Any('') }.str()
52 }
53 return out
54}
55
56fn test_trans_cleanup_releases_once() ! {
57 tmp, handle := trans_sandbox('once')!
58 defer { os.rmdir_all(tmp) or {} }
59 defer { handle.release() or {} }
60 lpath := os.join_path(tmp, 'db', 'db.lck')
61 trans_begin(handle, alpm.trans_flag_0)!
62 assert os.exists(lpath)
63 msgs := said(tmp, fn [handle] () {
64 trans_cleanup(handle)
65 // a second cleanup — a defer after an explicit one — is a no-op,
66 // not a release libalpm would refuse
67 trans_cleanup(handle)
68 })!
69 assert msgs.len == 0
70 assert !os.exists(lpath)
71 mut open := txn_open
72 assert open.load() == 0
73}
74
75fn test_trans_cleanup_failure_is_said_and_counts_as_closed() ! {
76 tmp, handle := trans_sandbox('failure')!
77 defer { os.rmdir_all(tmp) or {} }
78 defer { handle.release() or {} }
79 // the process believes in a transaction libalpm does not have: the
80 // release fails, the way it would after libalpm dropped it
81 mut open := txn_open
82 open.store(txn_locked)
83 msgs := said(tmp, fn [handle] () {
84 trans_cleanup(handle)
85 trans_cleanup(handle)
86 })!
87 // said once, and closed: the second cleanup did not try again
88 assert msgs.len == 1
89 assert msgs[0].starts_with('failed to release the transaction: ')
90 assert open.load() == 0
91}
92
93fn test_trans_commit_releases() ! {
94 // a committed transaction lets go of the lock through the commit's own
95 // cleanup, and leaves the deferred one nothing to do. an empty one would
96 // not do: libalpm's prepare returns early on nothing and its commit then
97 // refuses as not prepared, so a package is really removed
98 tmp, handle := trans_sandbox('commit')!
99 defer { os.rmdir_all(tmp) or {} }
100 defer { handle.release() or {} }
101 entry := os.join_path(tmp, 'db', 'local', 'gone-1.0-1')
102 os.mkdir_all(entry)!
103 os.write_file(os.join_path(entry, 'desc'), '%NAME%\ngone\n\n%VERSION%\n1.0-1\n\n')!
104 os.write_file(os.join_path(entry, 'files'), '%FILES%\n\n')!
105 trans_begin(handle, alpm.trans_flag_0)!
106 handle.remove_pkg(handle.find_local_pkg('gone') or { return error('gone is not installed') })!
107 trans_prepare_checked(handle)!
108 trans_commit(handle)!
109 assert !os.exists(os.join_path(tmp, 'db', 'db.lck'))
110 assert !os.exists(entry)
111 mut open := txn_open
112 assert open.load() == 0
113 msgs := said(tmp, fn [handle] () {
114 trans_cleanup(handle)
115 })!
116 assert msgs.len == 0
117}
118
119fn test_trans_options_of_install_and_upgrade() ! {
120 // the same four flags, read the same way from either command line
121 want := TransOptions{
122 overwrite: ['/usr/*']
123 ignore: ['a,b']
124 ignoregroup: ['g']
125 assume_installed: ['sh=5']
126 }
127 assert trans_options(ConfigInstall{
128 overwrite: ['/usr/*']
129 ignore: ['a,b']
130 ignoregroup: ['g']
131 assume_installed: ['sh=5']
132 }) == want
133 assert trans_options(ConfigUpgrade{
134 overwrite: ['/usr/*']
135 ignore: ['a,b']
136 ignoregroup: ['g']
137 assume_installed: ['sh=5']
138 }) == want
139 // and they reach the handle a transaction runs on, lists split
140 tmp, handle := trans_sandbox('options')!
141 defer { os.rmdir_all(tmp) or {} }
142 defer { handle.release() or {} }
143 want.apply(handle)!
144 mut ignored := handle.option_get_ignore_pkgs()
145 assert ignored.strs() == ['a', 'b']
146 mut overwrite := handle.option_get_overwrite_files()
147 assert overwrite.strs() == ['/usr/*']
148 // a spec that names no package is said, rather than assuming nothing
149 for spec in ['=', '=1.0', '>=2'] {
150 TransOptions{
151 assume_installed: [spec]
152 }.apply(handle) or {
153 assert err.msg().contains('names no package'), err.msg()
154 continue
155 }
156 assert false, '--assume-installed ${spec} was taken'
157 }
158}
159
160// killed_child forks, lets the child take the db lock of the sandbox the
161// way take describes, and kills it with SIGTERM through the installed
162// handler; it returns the child's exit status. the child runs V code — the
163// handlers' installation and take, a trans_begin in the tests here — on its
164// copy of the test's memory, and only ever leaves through _exit (the
165// handler's, or the 98 and 99 of a failure), so no defer, no atexit and no
166// test-runner report of the parent's runs a second time in it
167fn killed_child(handle &alpm.Handle, take fn (&alpm.Handle)) int {
168 pid := C.fork()
169 if pid == 0 {
170 install_interrupt_handlers()
171 take(handle)
172 C.kill(C.getpid(), 15)
173 // the handler exits; reaching here is the failure
174 C._exit(99)
175 }
176 mut status := 0
177 C.waitpid(pid, &status, 0)
178 return (status >> 8) & 0xff
179}
180
181fn test_interrupt_lets_go_of_the_lock() ! {
182 tmp, handle := trans_sandbox('interrupt')!
183 defer { os.rmdir_all(tmp) or {} }
184 defer { handle.release() or {} }
185 lpath := os.join_path(tmp, 'db', 'db.lck')
186 // a transaction is open, the lock held: the killed run removes it
187 status := killed_child(handle, fn (h &alpm.Handle) {
188 trans_begin(h, alpm.trans_flag_0) or { C._exit(98) }
189 })
190 assert status == 128 + 15
191 assert !os.exists(lpath)
192}
193
194fn test_interrupt_right_after_the_lock_is_taken() ! {
195 tmp, handle := trans_sandbox('interrupt_window')!
196 defer { os.rmdir_all(tmp) or {} }
197 defer { handle.release() or {} }
198 lpath := os.join_path(tmp, 'db', 'db.lck')
199 // the signal lands between libalpm taking the lock and vpac recording
200 // the transaction: the handle was published before, so the lock goes
201 status := killed_child(handle, fn (h &alpm.Handle) {
202 publish_for_interrupt(h)
203 h.trans_init(alpm.trans_flag_0) or { C._exit(98) }
204 })
205 assert status == 128 + 15
206 assert !os.exists(lpath)
207}
208
209fn test_interrupt_leaves_a_lock_it_does_not_hold() ! {
210 tmp, handle := trans_sandbox('interrupt_other')!
211 defer { os.rmdir_all(tmp) or {} }
212 defer { handle.release() or {} }
213 lpath := os.join_path(tmp, 'db', 'db.lck')
214 // another process holds the lock, and this run only previews (--print
215 // takes none): the killed run leaves the other's lock alone
216 os.write_file(lpath, '')!
217 status := killed_child(handle, fn (h &alpm.Handle) {
218 trans_begin(h, print_flags(true)) or { C._exit(98) }
219 })
220 assert status == 128 + 15
221 assert os.exists(lpath)
222}
223
224fn test_trans_publishes_its_handle_while_open() ! {
225 tmp, handle := trans_sandbox('publish')!
226 defer { os.rmdir_all(tmp) or {} }
227 defer { handle.release() or {} }
228 mut published := interrupt_handle
229 assert published.load() == 0
230 trans_begin(handle, alpm.trans_flag_0)!
231 assert published.load() == u64(voidptr(handle))
232 trans_cleanup(handle)
233 assert published.load() == 0
234 // a begin that fails publishes nothing past its failure: the lock is
235 // someone else's here
236 os.write_file(os.join_path(tmp, 'db', 'db.lck'), '')!
237 if _ := trans_begin(handle, alpm.trans_flag_0) {
238 assert false, 'a held lock was taken'
239 }
240 assert published.load() == 0
241}
242
243// a failed prepare is worded by its kind, pacman's three shapes and
244// libalpm's own text for the rest
245fn test_prepare_problem_lines() {
246 dep := prepare_problem_lines(.unsatisfied_deps, [
247 alpm.DepProblem{
248 target: 'user'
249 depend: 'lib<2.0'
250 causing: 'lib'
251 },
252 alpm.DepProblem{
253 target: 'app'
254 depend: 'gone'
255 },
256 ])
257 assert dep == ['could not satisfy dependencies:',
258 " lib breaks dependency 'lib<2.0' required by user",
259 " unable to satisfy dependency 'gone' required by app"]
260 conflict := prepare_problem_lines(.conflicting_deps, [
261 alpm.DepProblem{
262 target: 'p1'
263 depend: 'p2'
264 causing: 'p2'
265 },
266 alpm.DepProblem{
267 target: 'p1'
268 depend: 'virt<2'
269 causing: 'p3'
270 },
271 ])
272 assert conflict == ['unresolvable package conflicts:', ' p1 and p2 are in conflict',
273 ' p1 and p3 are in conflict (virt<2)']
274 wrong_arch := prepare_problem_lines(.pkg_invalid_arch, [alpm.DepProblem{
275 target: 'foo-1.0-1-riscv64'
276 }])
277 assert wrong_arch.len == 2
278 assert wrong_arch[1] == ' package foo-1.0-1-riscv64 does not have a valid architecture'
279 other := prepare_problem_lines(.trans_dup_target, [alpm.DepProblem{
280 target: 'foo'
281 }])
282 assert other.len == 2 && other[1] == ' foo'
283}
284