vpac / src / pkgbuild_install_test.v
361 lines · 345 sloc · 13.64 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module main
2
3import os
4import testdb
5
6fn test_dep_name() {
7 cases := {
8 'glibc': 'glibc'
9 'glibc>=2.27': 'glibc'
10 'foo<1.0': 'foo'
11 'foo<=1.0': 'foo'
12 'foo=1.0-2': 'foo'
13 'foo>1:2.0': 'foo'
14 'gcc-libs': 'gcc-libs'
15 'python>=3.12': 'python'
16 'libfoo.so=1-64': 'libfoo.so'
17 // stray spaces, as a published dependency list occasionally spells
18 ' glibc >=2.27 ': 'glibc'
19 ' glibc ': 'glibc'
20 'git >2.0': 'git'
21 }
22 for dep, want in cases {
23 assert dep_name(dep) == want, dep
24 }
25}
26
27fn test_review() ! {
28 old := color_flag
29 defer { color_flag = old }
30 color_flag = 'never'
31 dir := os.join_path(os.vtmp_dir(), 'vpac_test_review_${os.getpid()}')
32 os.mkdir_all(os.join_path(dir, '.git'))!
33 defer { os.rmdir_all(dir) or {} }
34 os.write_file(os.join_path(dir, 'PKGBUILD'), 'pkgname=foo\n')!
35 os.write_file(os.join_path(dir, 'foo.install'), 'post_install() { :; }')!
36 os.write_file(os.join_path(dir, 'a.patch'), '')!
37
38 // the PKGBUILD first, then the .install scripts, then every name but .git
39 mut r := read_review('foo', dir)!
40 assert r.files.map(it.name) == ['PKGBUILD', 'foo.install']
41 assert r.listing == ['PKGBUILD', 'a.patch', 'foo.install']
42 r.warnings = pkgbuild_warnings(PackagePkgbuild{ name: 'foo', maintainer: 'me', num_votes: 1 })
43 assert review_text(r, 'review foo') == [
44 'warning: maintainer: me, votes: 1',
45 'review foo',
46 'PKGBUILD',
47 'pkgname=foo\n',
48 'foo.install',
49 'post_install() { :; }',
50 'files in the package',
51 'PKGBUILD a.patch foo.install',
52 ].join('\n')
53}
54
55fn test_makepkg_line() ! {
56 // the dir is one word to the shell whatever it is called; the build
57 // command is the config's text, run in it
58 dir := os.join_path(os.vtmp_dir(), "vpac test's \$dir `x` ${os.getpid()}")
59 os.mkdir_all(dir)!
60 defer { os.rmdir_all(dir) or {} }
61 saved := pkgbuild_makepkg
62 defer { pkgbuild_makepkg = saved }
63 pkgbuild_makepkg = 'pwd -P'
64 res := os.exec(['sh', '-c', makepkg_line(dir)])
65 assert res.exit_code == 0, res.output
66 assert res.output.trim_right('\n') == os.real_path(dir)
67 // the default is makepkg's own, forcing a rebuild of what is there
68 reset_config()
69 assert makepkg_line('/x').ends_with(' && makepkg -f')
70}
71
72fn test_build_dir_takes_a_plain_base_only() ! {
73 // the dir is removed before the clone, so a base that climbs out of the
74 // build root is refused before any path is made of it
75 saved := pkgbuild_dir
76 defer { pkgbuild_dir = saved }
77 pkgbuild_dir = '/var/tmp/vpac-builds'
78 assert pkgbuild_build_dir('u', 'foo-git')! == '/var/tmp/vpac-builds/foo-git'
79 for bad in ['', '.', '..', '../x', 'a/b', '/etc', '-rf'] {
80 if d := pkgbuild_build_dir('u', bad) {
81 assert false, 'accepted "${bad}" as ${d}'
82 }
83 }
84}
85
86fn test_packagelist_files() ! {
87 dir := os.join_path(os.vtmp_dir(), 'vpac_test_pkglist_${os.getpid()}')
88 os.mkdir_all(dir)!
89 defer { os.rmdir_all(dir) or {} }
90 built := os.join_path(dir, 'foo-1.0-1-any.pkg.tar')
91 os.write_file(built, '')!
92 // makepkg names every package it can make, built or not: a debug package
93 // that was not made, and blank or odd lines, are not archives
94 out := '${built}\n${dir}/foo-debug-1.0-1-any.pkg.tar\n\n ${built} \nwarning: x\n'
95 assert packagelist_files(out) == [built, built]
96}
97
98fn test_archives_in() ! {
99 dir := os.join_path(os.vtmp_dir(), 'vpac_test_archives_in_${os.getpid()}')
100 os.mkdir_all(dir)!
101 defer { os.rmdir_all(dir) or {} }
102 for f in ['foo-1.0-1-any.pkg.tar', 'bar-2-1-x86_64.pkg.tar.zst', 'bar-2-1-x86_64.pkg.tar.zst.sig',
103 'PKGBUILD', 'notes.tar'] {
104 os.write_file(os.join_path(dir, f), '')!
105 }
106 native := os.join_path(dir, '.vpac-private')
107 os.mkdir(native)!
108 os.write_file(os.join_path(native, 'split-2-1-any.pkg.tar.zst'), '')!
109 mut got := archives_in(dir)!.map(os.base(it))
110 got.sort()
111 // an uncompressed archive counts, a signature and other files do not
112 assert got == ['bar-2-1-x86_64.pkg.tar.zst', 'foo-1.0-1-any.pkg.tar', 'split-2-1-any.pkg.tar.zst']
113}
114
115fn test_makepkg_packagelist_names_what_is_built() ! {
116 if !os.exists_in_system_path('makepkg') {
117 // no makepkg on this system: the assumption cannot be checked here
118 return
119 }
120 if os.geteuid() == 0 {
121 // makepkg refuses root
122 return
123 }
124 dir := os.join_path(os.vtmp_dir(), 'vpac_test_packagelist_${os.getpid()}')
125 out_dir := os.join_path(dir, 'out')
126 os.mkdir_all(out_dir)!
127 defer { os.rmdir_all(dir) or {} }
128 os.write_file(os.join_path(dir, 'PKGBUILD'), [
129 'pkgbase=split',
130 'pkgname=(split-a split-b)',
131 'pkgver=1.0',
132 'pkgrel=1',
133 "arch=('any')",
134 'package_split-a() { :; }',
135 'package_split-b() { :; }',
136 ].join('\n') + '\n')!
137 // an archive an older build left in the checkout: not this build's
138 os.write_file(os.join_path(dir, 'split-a-0.9-1-any.pkg.tar.zst'), '')!
139 saved := pkgbuild_makepkg
140 defer { pkgbuild_makepkg = saved }
141 pkgbuild_makepkg = 'makepkg -f'
142 // the line built_packages runs, as this user rather than through runuser:
143 // PKGDEST and PKGEXT as a makepkg.conf would set them
144 res := os.exec(['env', 'PKGDEST=${out_dir}', 'PKGEXT=.pkg.tar', 'sh', '-c',
145 '${makepkg_line(dir)} --packagelist'])
146 assert res.exit_code == 0, res.output
147 listed := res.output.split_into_lines().map(it.trim_space()).filter(it.starts_with('/'))
148 assert listed == ['${out_dir}/split-a-1.0-1-any.pkg.tar', '${out_dir}/split-b-1.0-1-any.pkg.tar'], res.output
149 // nothing built yet: nothing to install, the stale archive not among it
150 assert packagelist_files(res.output) == []
151 // built: exactly this version's two, from PKGDEST, uncompressed
152 for f in listed {
153 os.write_file(f, '')!
154 }
155 assert packagelist_files(res.output) == listed
156}
157
158fn test_run_process_drains_both_pipes() ! {
159 // more on stderr than a pipe holds, before stdout says anything: a
160 // reader of stdout alone would wait on a child that waits on it
161 out, code := run_process(['sh', '-c', 'head -c 300000 /dev/zero >&2; echo ok; exit 3'],
162 true)!
163 assert out == 'ok\n'
164 assert code == 3
165 _, plain := run_process(['true'], false)!
166 assert plain == 0
167 if _, _ := run_process(['vpac-no-such-tool'], false) {
168 assert false
169 }
170}
171
172fn test_built_install_config_carries_the_command_line() {
173 outer := ConfigInstall{
174 noconfirm: true
175 asexplicit: true
176 dbonly: true
177 nodeps: true
178 nodeps_level: 2
179 overwrite: ['usr/*']
180 assume_installed: ['x=1']
181 ignore: ['held']
182 ignoregroup: ['grp']
183 no_upgrade_deps: true
184 disable_timeout: true
185 pkgbuild: true
186 no_pkgbuild_confirm: true
187 needed: true
188 query: ['the-target']
189 }
190 target := built_install_config(outer, ['/b/a.pkg.tar.zst', '/b/b.pkg.tar.zst'], true)
191 // the archives are the targets, not the name that was asked for
192 assert target.query == ['/b/a.pkg.tar.zst', '/b/b.pkg.tar.zst']
193 assert target.asexplicit && !target.asdeps
194 assert target.ignore == ['held'] && target.ignoregroup == ['grp']
195 assert target.no_upgrade_deps && target.disable_timeout && target.noconfirm
196 assert target.dbonly && target.nodeps && target.nodeps_level == 2
197 assert target.overwrite == ['usr/*'] && target.assume_installed == ['x=1']
198 // the outer run's own flags stay behind: an archive is not looked up in
199 // the pkgbuild repo, and --needed was settled before the build
200 assert !target.pkgbuild && !target.no_pkgbuild_confirm && !target.needed
201 // a dependency node is a dependency whatever the target was to be
202 dep := built_install_config(outer, ['/b/dep.pkg.tar.zst'], false)
203 assert dep.asdeps && !dep.asexplicit
204 assert dep.ignore == ['held'] && dep.no_upgrade_deps
205 // and nothing appears that the command line did not say
206 plain := built_install_config(ConfigInstall{}, [], true)
207 assert plain == ConfigInstall{}
208}
209
210// node_db writes a local db for the node decisions and points the config at it
211fn node_db(name string, pkgs []testdb.Pkg) !string {
212 tmp := os.join_path(os.vtmp_dir(), 'vpac_test_node_${name}_${os.getpid()}')
213 os.rmdir_all(tmp) or {}
214 os.mkdir_all(os.join_path(tmp, 'root'))!
215 testdb.write_local(os.join_path(tmp, 'db'), pkgs)!
216 root_path = os.join_path(tmp, 'root') + '/'
217 db_path = os.join_path(tmp, 'db') + '/'
218 return tmp
219}
220
221fn node_of(name string, version string, wanted []string, repo_deps []string) PkgbuildNode {
222 return PkgbuildNode{
223 info: PackagePkgbuild{
224 name: name
225 package_base: name
226 version: version
227 }
228 wanted: wanted
229 repo_deps: repo_deps
230 }
231}
232
233fn test_node_step_is_decided_on_what_is_installed_now() ! {
234 saved_root, saved_db := root_path, db_path
235 tmp := node_db('step', [
236 testdb.Pkg{
237 name: 'lib'
238 version: '1.0-1'
239 },
240 testdb.Pkg{
241 name: 'impl'
242 version: '3.0-1'
243 provides: ['virt=3']
244 },
245 ])!
246 defer {
247 root_path, db_path = saved_root, saved_db
248 os.rmdir_all(tmp) or {}
249 }
250 // a dependency node everything wanted is satisfied for is not built:
251 // by its own name, by a version it meets, by something that provides it
252 for wanted in [['lib'], ['lib>=1.0'], ['lib', 'lib<2'], ['virt'], ['virt>=3', 'virt']] {
253 name := dep_name(wanted[0])
254 step := node_step_now(node_of(name, '9-1', wanted, []), false, false)!
255 assert step.skip, '${wanted} was still to build'
256 assert step.why.contains('installed now'), step.why
257 }
258 // one requirement of several that is not met keeps the build
259 for wanted in [['lib>=2'], ['lib', 'lib>=2'], ['virt=4'], ['gone'], ['lib', 'gone']] {
260 step := node_step_now(node_of(dep_name(wanted[0]), '9-1', wanted, []), false, false)!
261 assert !step.skip, '${wanted} was skipped'
262 }
263 // a package of the name at the node's very version does not skip a
264 // dependency node whose requirement it does not meet, with or without
265 // --needed: the name being there is not the requirement being met
266 for needed in [false, true] {
267 same := node_step_now(node_of('lib', '1.0-1', ['lib>=2'], []), needed, false)!
268 assert !same.skip
269 }
270 // the target is never dropped for being satisfied: it was asked for.
271 // only --needed skips it, and only at the version it would install
272 assert !node_step_now(node_of('lib', '1.0-1', [], []), false, true)!.skip
273 assert !node_step_now(node_of('lib', '1.0-1', ['lib'], []), false, true)!.skip
274 up := node_step_now(node_of('lib', '1.0-1', [], []), true, true)!
275 assert up.skip && up.why == 'lib-1.0-1 is up to date, skipped'
276 assert !node_step_now(node_of('lib', '1.0-2', [], []), true, true)!.skip
277 assert !node_step_now(node_of('lib', '0.9-1', [], []), true, true)!.skip
278 assert !node_step_now(node_of('absent', '1.0-1', [], []), true, true)!.skip
279 // a node nothing wanted and that is no target (never planned so, but
280 // the rule is spelled) is built
281 assert !node_step_now(node_of('lib', '1.0-1', [], []), false, false)!.skip
282 // the repo dependencies left are the ones not installed, in order;
283 // a provider under another name does not count as the package
284 deps := node_step_now(node_of('app', '1-1', [], ['new', 'lib', 'virt', 'other']), false,
285 true)!
286 assert deps.repo_deps == ['new', 'virt', 'other']
287 assert node_step_now(node_of('app', '1-1', [], []), false, true)!.repo_deps.len == 0
288 // a skipped node asks for no repo dependencies
289 assert node_step_now(node_of('lib', '9-1', ['lib'], ['new']), false, false)!.repo_deps.len == 0
290}
291
292fn test_local_now_sees_what_an_older_handle_does_not() ! {
293 saved_root, saved_db := root_path, db_path
294 tmp := node_db('fresh', [testdb.Pkg{
295 name: 'first'
296 }])!
297 defer {
298 root_path, db_path = saved_root, saved_db
299 os.rmdir_all(tmp) or {}
300 }
301 // the run's handle reads the local db once
302 old_handle, old_pkgs := local_now()!
303 defer { old_handle.release() or {} }
304 assert old_pkgs.pkg_find('first') != none
305 // a build's install goes in through another handle: here, written
306 testdb.write_local(os.join_path(tmp, 'db'), [
307 testdb.Pkg{
308 name: 'built'
309 version: '2.0-1'
310 provides: ['virt=2']
311 },
312 ])!
313 // the old list is the past: the staleness the plan must not be made on
314 assert old_pkgs.pkg_find('built') == none
315 assert old_pkgs.find_satisfier('virt') == none
316 // a view opened now has it, and a node waiting for it is dropped
317 view, now := local_now()!
318 assert now.pkg_find('built') != none && now.pkg_find('first') != none
319 assert now.find_satisfier('virt>=2') != none
320 view.release()!
321 assert node_step(old_pkgs, node_of('built', '2.0-1', ['virt'], []), false, false).skip == false
322 assert node_step_now(node_of('built', '2.0-1', ['virt'], []), false, false)!.skip
323 // an empty local db is a view of nothing, not an error
324 os.rmdir_all(os.join_path(tmp, 'db', 'local'))!
325 testdb.write_local(os.join_path(tmp, 'db'), [])!
326 e_view, empty := local_now()!
327 assert empty.pkg_find('first') == none
328 e_view.release()!
329 assert !node_step_now(node_of('x', '1-1', ['x'], ['dep']), false, false)!.skip
330 // a db that is not there is an error, with nothing left open
331 db_path = os.join_path(tmp, 'nowhere') + '/'
332 if _, _ := local_now() {
333 assert false, 'a missing db opened'
334 }
335 // nothing to install asks for no transaction at all
336 install_repo_deps([])!
337}
338
339fn test_upgrade_pkgbuild_config_carries_the_transaction() {
340 up := upgrade_pkgbuild_config(ConfigUpgrade{
341 noconfirm: true
342 no_pkgbuild_confirm: true
343 nodeps: true
344 nodeps_level: 2
345 overwrite: ['usr/*']
346 ignore: ['held']
347 ignoregroup: ['grp']
348 disable_timeout: true
349 })
350 // the questions are review_and_build's own, the gate's word written by
351 // the pass: what the command line said of asking is carried as it was
352 assert up.pkgbuild && up.no_pkgbuild_confirm
353 assert up.noconfirm && up.nodeps && up.nodeps_level == 2
354 assert up.overwrite == ['usr/*'] && up.ignore == ['held'] && up.ignoregroup == ['grp']
355 assert up.disable_timeout
356 // what the outer run settles itself stays behind
357 assert !up.needed && !up.print_only && !up.download_only
358 // without the command line's say, the questions stand
359 plain := upgrade_pkgbuild_config(ConfigUpgrade{})
360 assert plain.pkgbuild && !plain.no_pkgbuild_confirm && !plain.noconfirm
361}
362