module installfixtures import nspkg.tests.archivefixtures import crypto.md5 import os pub struct Scenario { pub: name string installed bool = true version string = '2-1' local_text string = 'original' incoming string = 'incoming' payload string = 'new binary' scriptlet string = 'pre_install() { :; }\npost_upgrade() { :; }\n' old_backup bool = true new_backup bool = true missing_hash bool missing_local bool local_symlink bool pacnew bool no_upgrade []string no_extract []string db_only bool no_save bool all_deps bool all_explicit bool partial bool check_space bool second bool interrupt bool remove_victim bool mtree bool extra_entries []archivefixtures.Entry } pub struct Fixture { pub: root string db string cache string archives []string } pub fn create(base string, scenario Scenario) !Fixture { root := os.join_path(base, 'root') db := os.join_path(base, 'db') cache := os.join_path(base, 'cache') os.mkdir_all(cache)! for directory in ['etc', 'usr/bin'] { os.mkdir_all(os.join_path(root, directory))! } os.mkdir_all(os.join_path(db, 'local'))! os.write_file(os.join_path(db, 'local', 'ALPM_DB_VERSION'), '9\n')! if !scenario.missing_local { if scenario.local_symlink { os.write_file(os.join_path(root, 'etc', 'target'), scenario.local_text)! os.symlink('target', os.join_path(root, 'etc', 'app.conf'))! } else { os.write_file(os.join_path(root, 'etc', 'app.conf'), scenario.local_text)! } } if scenario.pacnew { os.write_file(os.join_path(root, 'etc', 'app.conf.pacnew'), 'previous pacnew')! } if scenario.installed { entry := os.join_path(db, 'local', 'app-1-1') os.mkdir(entry)! os.write_file(os.join_path(entry, 'desc'), '%REASON%\n1\n\n%GROUPS%\nold-group\n\n')! mut backup := '' if scenario.old_backup { backup = '%BACKUP%\netc/app.conf' + if scenario.missing_hash { '\n\n' } else { '\t' + md5.sum('original'.bytes()).hex() + '\n\n' } } os.write_file(os.join_path(entry, 'files'), '%FILES%\netc/\netc/app.conf\nold-only\nusr/\nusr/bin/\nusr/bin/app\n\n' + backup)! os.write_file(os.join_path(root, 'old-only'), 'obsolete payload')! os.write_file(os.join_path(root, 'usr/bin/app'), 'old binary')! } if scenario.remove_victim { entry := os.join_path(db, 'local', 'victim-1-1') os.mkdir(entry)! os.write_file(os.join_path(entry, 'desc'), '%REASON%\n0\n\n')! os.write_file(os.join_path(entry, 'files'), '%FILES%\nvictim-only\n\n')! os.write_file(os.join_path(root, 'victim-only'), 'victim')! } mut info := 'pkgname = app\npkgver = ${scenario.version}\npkgdesc = app description\narch = any\nsize = ${scenario.incoming.len + scenario.payload.len}\nlicense = GPL\ngroup = new-group\ndepend = missing-dep>=1\noptdepend = helper: does things\nxdata = pkgtype=pkg\n' if scenario.new_backup { info += 'backup = etc/app.conf\n' } mut entries := [archivefixtures.Entry{ name: '.PKGINFO', text: info }, archivefixtures.Entry{ name: '.INSTALL', text: scenario.scriptlet, mode: 0o777 }, archivefixtures.Entry{ name: '.CHANGELOG', text: 'release\x00notes\n', mode: 0o400 }] if scenario.mtree { mtree := '#mtree\n./.INSTALL type=file\n./etc type=dir mode=0755\n./etc/app.conf type=file mode=0644 size=${scenario.incoming.len}\n./usr type=dir mode=0755\n./usr/bin type=dir mode=0755\n./usr/bin/app type=file mode=0755 size=${scenario.payload.len}\n' entries << archivefixtures.Entry{ name: '.MTREE', text: mtree, mode: 0o400 } } entries << [archivefixtures.Entry{ name: 'etc/', directory: true }, archivefixtures.Entry{ name: 'etc/app.conf', text: scenario.incoming }, archivefixtures.Entry{ name: 'usr/', directory: true }, archivefixtures.Entry{ name: 'usr/bin/', directory: true }, archivefixtures.Entry{ name: 'usr/bin/app', text: scenario.payload, mode: 0o755 }] if scenario.mtree { entries << archivefixtures.Entry{ name: 'not-in-mtree', text: 'ignored' } } entries << scenario.extra_entries archive := os.join_path(base, 'app.pkg.tar') archivefixtures.write(archive, entries, false)! mut archives := [archive] if scenario.second { second := os.join_path(base, 'second.pkg.tar') archivefixtures.write(second, [ archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = second\npkgver = 1-1\nsize = 6\n' }, archivefixtures.Entry{ name: 'usr/bin/second', text: 'second', mode: 0o755 }, ], false)! archives << second } return Fixture{ root: root, db: db, cache: cache, archives: archives } } pub fn snapshot(root string) !map[string]string { mut result := map[string]string{} snapshot_directory(root, '', mut result)! return result } fn snapshot_directory(root string, path string, mut result map[string]string) ! { for name in os.ls(os.join_path(root, path))! { relative := if path == '' { name } else { path + '/' + name } full := os.join_path(root, relative) info := os.lstat(full)! kind := info.mode & 0o170000 value := match kind { 0o040000 { 'directory' } 0o120000 { 'link:' + os.readlink(full)! } else { os.read_bytes(full)!.hex() } } result[relative] = '${info.mode & 0o7777}:${value}' if kind == 0o040000 { snapshot_directory(root, relative, mut result)! } } }