Repository navigation
Expand file tree
/
Copy pathgentoo-install.test.js
More file actions
172 lines (163 loc) · 11.1 KB
/
Copy pathgentoo-install.test.js
File metadata and controls
172 lines (163 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const cp = require("node:child_process");
const library = path.join(__dirname, "lib/gentoo-install.sh");
const marker = "# Generated by codex-desktop-linux install-gentoo.sh";
function setup(t) {
const root = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), "gentoo-install-test-"));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const source = path.join(root, "incoming/repository");
const pkg = path.join(source, "app-misc/codex-desktop");
const repository = path.join(root, "system/repos/codex-desktop-local");
const config = path.join(root, "system/config/repos.conf/community");
const keywords = path.join(root, "system/config/package.accept_keywords/community");
const license = path.join(root, "system/config/package.license/community");
const distdir = path.join(root, "system/distfiles");
const command = path.join(root, "emerge");
const payload = `codex-desktop-26.930.41038-amd64-payload-${"a".repeat(64)}.tar.xz`;
fs.mkdirSync(pkg, { recursive: true });
fs.writeFileSync(path.join(source, ".codex-desktop-generated"), "generated\n");
fs.writeFileSync(path.join(pkg, "Manifest"), `DIST ${payload} 11 BLAKE2B a SHA512 b\n`);
fs.writeFileSync(path.join(pkg, "codex-desktop-26.930.41038.ebuild"), "EAPI=8\n");
fs.writeFileSync(path.join(root, "incoming", payload), "new payload");
fs.mkdirSync(repository, { recursive: true });
fs.writeFileSync(path.join(repository, ".codex-desktop-generated"), "old generation\n");
fs.writeFileSync(path.join(repository, "keep"), "previous repository\n", { mode: 0o600 });
for (const file of [config, keywords, license]) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, `${marker}\nold ${path.basename(path.dirname(file))}\n`, { mode: 0o640 });
}
fs.mkdirSync(distdir);
fs.writeFileSync(command, "#!/bin/bash\nprintf '%s\\n' \"$*\" > \"$GENTOO_TEST_INSTALL_LOG\"\nexit \"${GENTOO_TEST_INSTALL_STATUS:-0}\"\n", { mode: 0o755 });
return { root, source, repository, config, keywords, license, distdir, command, payload };
}
function snapshot(root) {
const result = {};
function walk(file) {
const s = fs.lstatSync(file), rel = path.relative(root, file);
result[rel] = { mode: s.mode & 0o7777, type: s.isDirectory() ? "dir" : s.isSymbolicLink() ? "link" : "file" };
if (s.isSymbolicLink()) result[rel].target = fs.readlinkSync(file);
else if (s.isDirectory()) for (const name of fs.readdirSync(file).sort()) walk(path.join(file, name));
else result[rel].bytes = fs.readFileSync(file).toString("base64");
}
walk(root);
return result;
}
function run(f, env = {}, args = []) {
return cp.spawnSync("bash", ["-c", '. "$1"; shift; gentoo_install_transaction "$@"', "--", library,
f.source, f.repository, f.config, f.keywords, f.license, f.distdir, "amd64", f.command, ...args], {
encoding: "utf8", env: { ...process.env, GENTOO_TEST_INSTALL_LOG: path.join(f.root, "install.log"), ...env },
});
}
for (const policy of ["keywords", "license"]) {
test(`unmanaged ${policy} refusal preserves repository and every configuration`, t => {
const f = setup(t);
fs.writeFileSync(f[policy], "user-authored policy\n");
const before = snapshot(path.join(f.root, "system"));
const result = run(f);
assert.notEqual(result.status, 0);
assert.match(result.stderr, /Refusing unmanaged configuration/);
assert.deepEqual(snapshot(path.join(f.root, "system")), before);
assert.equal(fs.existsSync(path.join(f.root, "install.log")), false);
});
}
test("protected symlink and conflicting distfile are refused without mutation", t => {
const f = setup(t), external = path.join(f.root, "user-policy");
fs.writeFileSync(external, marker + "\nuser data\n");
fs.unlinkSync(f.license); fs.symlinkSync(external, f.license);
const before = snapshot(path.join(f.root, "system"));
assert.notEqual(run(f).status, 0);
assert.deepEqual(snapshot(path.join(f.root, "system")), before);
assert.equal(fs.readFileSync(external, "utf8"), marker + "\nuser data\n");
fs.unlinkSync(f.license); fs.writeFileSync(f.license, marker + "\nold license\n");
fs.writeFileSync(path.join(f.distdir, f.payload), "another payload");
const conflict = snapshot(path.join(f.root, "system"));
assert.notEqual(run(f).status, 0);
assert.deepEqual(snapshot(path.join(f.root, "system")), conflict);
});
test("failure after repository promotion restores original repository/configs/permissions", t => {
const f = setup(t), bin = path.join(f.root, "bin");
fs.mkdirSync(bin);
// Fail the first new policy rename, after the repository has been replaced.
// Rollback renames use original backups and pass through unchanged.
const mv = cp.execFileSync("which", ["mv"], { encoding: "utf8" }).trim();
fs.writeFileSync(path.join(bin, "mv"), `#!/bin/bash\nif [[ "$3" == *codex-desktop-stage* && "$4" == "$GENTOO_TEST_FAIL_TARGET" ]]; then exit 77; fi\nexec "$GENTOO_TEST_MV" "$@"\n`, { mode: 0o755 });
const before = snapshot(path.join(f.root, "system"));
const result = run(f, { PATH: bin + path.delimiter + process.env.PATH, GENTOO_TEST_MV: mv, GENTOO_TEST_FAIL_TARGET: f.config });
assert.notEqual(result.status, 0);
assert.deepEqual(snapshot(path.join(f.root, "system")), before, result.stderr);
assert.equal(fs.existsSync(path.join(f.root, "install.log")), false);
});
test("Portage failure restores old state and removes newly introduced policies/distfile", t => {
const f = setup(t);
fs.unlinkSync(f.license);
const before = snapshot(path.join(f.root, "system"));
const result = run(f, { GENTOO_TEST_INSTALL_STATUS: "31" });
assert.equal(result.status, 31, result.stderr);
assert.deepEqual(snapshot(path.join(f.root, "system")), before);
assert.equal(fs.existsSync(f.license), false);
});
test("successful Portage merge publishes the complete generation and cleans recovery copies", t => {
const f = setup(t), result = run(f);
assert.equal(result.status, 0, result.stderr);
assert.equal(fs.existsSync(path.join(f.repository, "keep")), false);
assert.equal(fs.readFileSync(f.keywords, "utf8"), `${marker}\napp-misc/codex-desktop::codex-desktop-local ~amd64\n`);
assert.match(fs.readFileSync(f.license, "utf8"), /all-rights-reserved/);
assert.equal(fs.readFileSync(path.join(f.distdir, f.payload), "utf8"), "new payload");
assert.equal(fs.readFileSync(path.join(f.root, "install.log"), "utf8").trim(), "=app-misc/codex-desktop-26.930.41038::codex-desktop-local");
assert.equal(Object.keys(snapshot(path.join(f.root, "system"))).some(p => /codex-desktop-(stage|backup)/.test(p)), false);
});
test("application merge ignores inherited modes without presence-sensitive binary-only options", () => {
const source = fs.readFileSync(path.join(__dirname, 'install-gentoo.sh'), 'utf8');
const invocation = source.slice(source.lastIndexOf('gentoo_install_transaction '));
const result = cp.spawnSync('bash', ['-c', 'source_repository=/fixture; portageq(){ printf "fixture\\n"; }; gentoo_install_transaction(){ printf "%s\\n" "$@"; }; eval "$1"', '--', invocation], {
encoding: 'utf8', env: { ...process.env, EMERGE_DEFAULT_OPTS: '--oneshot --select=n --usepkgonly --getbinpkgonly' },
});
assert.equal(result.status, 0, result.stderr);
assert.ok(result.stdout.split('\n').includes('--select=y'));
assert.ok(result.stdout.split('\n').includes('--selective=n'));
assert.ok(result.stdout.split('\n').includes('--ignore-default-opts'));
assert.ok(result.stdout.split('\n').includes('--usepkg=n'));
assert.ok(result.stdout.split('\n').includes('--getbinpkg=n'));
assert.equal(result.stdout.split('\n').some(arg => /^--(?:usepkgonly|getbinpkgonly)(?:=|$)/.test(arg)), false);
});
test("source merge survives Portage's presence-only usepkgonly filtering", t => {
const f = setup(t);
const source = fs.readFileSync(path.join(__dirname, 'install-gentoo.sh'), 'utf8');
const invocation = source.slice(source.lastIndexOf('gentoo_install_transaction '));
const flags = cp.execFileSync('bash', ['-c', 'source_repository=/fixture; portageq(){ printf "fixture\\n"; }; gentoo_install_transaction(){ printf "%s\\n" "$@"; }; eval "$1"', '--', invocation], { encoding: 'utf8' }).trim().split('\n').slice(8);
// Model the resolver's membership check: even --usepkgonly=n excludes
// ebuilds. Inherited defaults must also be ignored for this invocation.
fs.writeFileSync(f.command, '#!/bin/bash\nignore=0\nfor arg in "$@"; do\n case "$arg" in\n --ignore-default-opts) ignore=1 ;;\n --usepkgonly|--usepkgonly=*) echo "no ebuilds to satisfy atom" >&2; exit 42 ;;\n esac\ndone\nif [[ "$EMERGE_DEFAULT_OPTS" == *--usepkgonly* || "$EMERGE_DEFAULT_OPTS" == *--getbinpkgonly* ]] && [ "$ignore" = 0 ]; then exit 43; fi\n', { mode: 0o755 });
const env = { EMERGE_DEFAULT_OPTS: '--usepkgonly --getbinpkgonly --oneshot --noreplace' };
const before = snapshot(path.join(f.root, 'system'));
assert.equal(run(f, env, ['--usepkg=n', '--getbinpkg=n', '--usepkgonly=n', '--getbinpkgonly=n']).status, 42);
assert.deepEqual(snapshot(path.join(f.root, 'system')), before);
assert.equal(run(f, env, ['--usepkg=n', '--getbinpkg=n']).status, 43);
assert.deepEqual(snapshot(path.join(f.root, 'system')), before);
const result = run(f, env, flags);
assert.equal(result.status, 0, result.stderr);
assert.equal(fs.existsSync(path.join(f.repository, 'keep')), false);
});
test("same-version changed payload is merged despite inherited update/noreplace", t => {
const f = setup(t), installed = path.join(f.root, 'installed-payload');
const source = fs.readFileSync(path.join(__dirname, 'install-gentoo.sh'), 'utf8');
const invocation = source.slice(source.lastIndexOf('gentoo_install_transaction '));
const flags = cp.execFileSync('bash', ['-c', 'source_repository=/fixture; portageq(){ printf "fixture\\n"; }; gentoo_install_transaction(){ printf "%s\\n" "$@"; }; eval "$1"', '--', invocation], { encoding: 'utf8' }).trim().split('\n').slice(8);
fs.writeFileSync(f.command, '#!/bin/bash\nforce=0\nfor arg in "$@"; do [ "$arg" != --selective=n ] || force=1; done\nif [[ "$EMERGE_DEFAULT_OPTS" == *--update* || "$EMERGE_DEFAULT_OPTS" == *--noreplace* ]] && [ "$force" = 0 ] && [ -f "$GENTOO_TEST_INSTALLED" ]; then exit 0; fi\ncp "$GENTOO_TEST_ACTIVE_PAYLOAD" "$GENTOO_TEST_INSTALLED"\n', { mode: 0o755 });
const env = { EMERGE_DEFAULT_OPTS: '--update --noreplace', GENTOO_TEST_INSTALLED: installed,
GENTOO_TEST_ACTIVE_PAYLOAD: path.join(f.root, 'incoming', f.payload) };
assert.equal(run(f, env, flags).status, 0);
assert.equal(fs.readFileSync(installed, 'utf8'), 'new payload');
const next = f.payload.replace('a'.repeat(64), 'b'.repeat(64));
fs.writeFileSync(path.join(f.root, 'incoming', next), 'regenerated payload');
const manifest = path.join(f.source, 'app-misc/codex-desktop/Manifest');
fs.writeFileSync(manifest, fs.readFileSync(manifest, 'utf8').replace(f.payload, next));
const result = run(f, { ...env, GENTOO_TEST_ACTIVE_PAYLOAD: path.join(f.root, 'incoming', next) }, flags);
assert.equal(result.status, 0, result.stderr);
assert.equal(fs.readFileSync(installed, 'utf8'), 'regenerated payload');
});