| 1 | module installfixtures |
| 2 | |
| 3 | import nspkg.tests.archivefixtures |
| 4 | import crypto.md5 |
| 5 | import os |
| 6 | |
| 7 | pub struct Scenario { |
| 8 | pub: |
| 9 | name string |
| 10 | installed bool = true |
| 11 | version string = '2-1' |
| 12 | local_text string = 'original' |
| 13 | incoming string = 'incoming' |
| 14 | payload string = 'new binary' |
| 15 | scriptlet string = 'pre_install() { :; }\npost_upgrade() { :; }\n' |
| 16 | old_backup bool = true |
| 17 | new_backup bool = true |
| 18 | missing_hash bool |
| 19 | missing_local bool |
| 20 | local_symlink bool |
| 21 | pacnew bool |
| 22 | no_upgrade []string |
| 23 | no_extract []string |
| 24 | db_only bool |
| 25 | no_save bool |
| 26 | all_deps bool |
| 27 | all_explicit bool |
| 28 | partial bool |
| 29 | check_space bool |
| 30 | second bool |
| 31 | interrupt bool |
| 32 | remove_victim bool |
| 33 | mtree bool |
| 34 | extra_entries []archivefixtures.Entry |
| 35 | } |
| 36 | |
| 37 | pub struct Fixture { |
| 38 | pub: |
| 39 | root string |
| 40 | db string |
| 41 | cache string |
| 42 | archives []string |
| 43 | } |
| 44 | |
| 45 | pub fn create(base string, scenario Scenario) !Fixture { |
| 46 | root := os.join_path(base, 'root') |
| 47 | db := os.join_path(base, 'db') |
| 48 | cache := os.join_path(base, 'cache') |
| 49 | os.mkdir_all(cache)! |
| 50 | for directory in ['etc', 'usr/bin'] { os.mkdir_all(os.join_path(root, directory))! } |
| 51 | os.mkdir_all(os.join_path(db, 'local'))! |
| 52 | os.write_file(os.join_path(db, 'local', 'ALPM_DB_VERSION'), '9\n')! |
| 53 | if !scenario.missing_local { |
| 54 | if scenario.local_symlink { |
| 55 | os.write_file(os.join_path(root, 'etc', 'target'), scenario.local_text)! |
| 56 | os.symlink('target', os.join_path(root, 'etc', 'app.conf'))! |
| 57 | } else { |
| 58 | os.write_file(os.join_path(root, 'etc', 'app.conf'), scenario.local_text)! |
| 59 | } |
| 60 | } |
| 61 | if scenario.pacnew { |
| 62 | os.write_file(os.join_path(root, 'etc', 'app.conf.pacnew'), 'previous pacnew')! |
| 63 | } |
| 64 | if scenario.installed { |
| 65 | entry := os.join_path(db, 'local', 'app-1-1') |
| 66 | os.mkdir(entry)! |
| 67 | os.write_file(os.join_path(entry, 'desc'), '%REASON%\n1\n\n%GROUPS%\nold-group\n\n')! |
| 68 | mut backup := '' |
| 69 | if scenario.old_backup { |
| 70 | backup = '%BACKUP%\netc/app.conf' + if scenario.missing_hash { |
| 71 | '\n\n' |
| 72 | } else { |
| 73 | '\t' + md5.sum('original'.bytes()).hex() + '\n\n' |
| 74 | } |
| 75 | } |
| 76 | os.write_file(os.join_path(entry, 'files'), '%FILES%\netc/\netc/app.conf\nold-only\nusr/\nusr/bin/\nusr/bin/app\n\n' + backup)! |
| 77 | os.write_file(os.join_path(root, 'old-only'), 'obsolete payload')! |
| 78 | os.write_file(os.join_path(root, 'usr/bin/app'), 'old binary')! |
| 79 | } |
| 80 | if scenario.remove_victim { |
| 81 | entry := os.join_path(db, 'local', 'victim-1-1') |
| 82 | os.mkdir(entry)! |
| 83 | os.write_file(os.join_path(entry, 'desc'), '%REASON%\n0\n\n')! |
| 84 | os.write_file(os.join_path(entry, 'files'), '%FILES%\nvictim-only\n\n')! |
| 85 | os.write_file(os.join_path(root, 'victim-only'), 'victim')! |
| 86 | } |
| 87 | 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' |
| 88 | if scenario.new_backup { info += 'backup = etc/app.conf\n' } |
| 89 | mut entries := [archivefixtures.Entry{ name: '.PKGINFO', text: info }, |
| 90 | archivefixtures.Entry{ name: '.INSTALL', text: scenario.scriptlet, mode: 0o777 }, |
| 91 | archivefixtures.Entry{ name: '.CHANGELOG', text: 'release\x00notes\n', mode: 0o400 }] |
| 92 | if scenario.mtree { |
| 93 | 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' |
| 94 | entries << archivefixtures.Entry{ name: '.MTREE', text: mtree, mode: 0o400 } |
| 95 | } |
| 96 | entries << [archivefixtures.Entry{ name: 'etc/', directory: true }, |
| 97 | archivefixtures.Entry{ name: 'etc/app.conf', text: scenario.incoming }, |
| 98 | archivefixtures.Entry{ name: 'usr/', directory: true }, |
| 99 | archivefixtures.Entry{ name: 'usr/bin/', directory: true }, |
| 100 | archivefixtures.Entry{ name: 'usr/bin/app', text: scenario.payload, mode: 0o755 }] |
| 101 | if scenario.mtree { entries << archivefixtures.Entry{ name: 'not-in-mtree', text: 'ignored' } } |
| 102 | entries << scenario.extra_entries |
| 103 | archive := os.join_path(base, 'app.pkg.tar') |
| 104 | archivefixtures.write(archive, entries, false)! |
| 105 | mut archives := [archive] |
| 106 | if scenario.second { |
| 107 | second := os.join_path(base, 'second.pkg.tar') |
| 108 | archivefixtures.write(second, [ |
| 109 | archivefixtures.Entry{ name: '.PKGINFO', text: 'pkgname = second\npkgver = 1-1\nsize = 6\n' }, |
| 110 | archivefixtures.Entry{ name: 'usr/bin/second', text: 'second', mode: 0o755 }, |
| 111 | ], false)! |
| 112 | archives << second |
| 113 | } |
| 114 | return Fixture{ root: root, db: db, cache: cache, archives: archives } |
| 115 | } |
| 116 | |
| 117 | pub fn snapshot(root string) !map[string]string { |
| 118 | mut result := map[string]string{} |
| 119 | snapshot_directory(root, '', mut result)! |
| 120 | return result |
| 121 | } |
| 122 | |
| 123 | fn snapshot_directory(root string, path string, mut result map[string]string) ! { |
| 124 | for name in os.ls(os.join_path(root, path))! { |
| 125 | relative := if path == '' { name } else { path + '/' + name } |
| 126 | full := os.join_path(root, relative) |
| 127 | info := os.lstat(full)! |
| 128 | kind := info.mode & 0o170000 |
| 129 | value := match kind { |
| 130 | 0o040000 { 'directory' } |
| 131 | 0o120000 { 'link:' + os.readlink(full)! } |
| 132 | else { os.read_bytes(full)!.hex() } |
| 133 | } |
| 134 | result[relative] = '${info.mode & 0o7777}:${value}' |
| 135 | if kind == 0o040000 { snapshot_directory(root, relative, mut result)! } |
| 136 | } |
| 137 | } |
| 138 | |