repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
scripts/dotfiles_integration_test.java
RawFile outline
- dotfiles_integration_test line 7
- root line 8
- main line 10
- host line 15
- inside line 42
- allHosts line 72
- runManifest line 82
- freshCryptoSmoke line 103
- Mapping line 129
- accept line 129
- forEachMapping line 130
- targetPath line 141
- rootless line 145
- deleteTree line 152
- step line 159
- ok line 160
- log line 161
- fail line 162
- run line 164
- run line 165
- runInherit line 175
- runAllowFail line 182
import java.io.*;
import java.nio.file.*;
import java.time.*;
import java.util.*;
import java.util.stream.*;
public class dotfiles_integration_test {
static Path root;
public static void main(String[] args) throws Exception {
if (args.length > 0 && args[0].equals("--inside")) inside();
else host();
}
static void host() throws Exception {
root = Path.of(run(null, "git", "rev-parse", "--show-toplevel").trim());
var key = root.resolve(".git/dotfiles-aes.key");
if (!Files.isReadable(key)) fail("missing " + key);
var container = "dotfiles-integration-" + LocalDateTime.now() + "-" + ProcessHandle.current().pid();
container = container.replaceAll("[^A-Za-z0-9_.-]", "-");
try {
step("start container");
run(null, "podman", "run", "--name", container, "--rm", "-d", "--hostname", "desktop",
"-v", root + ":/src:ro", "-v", key + ":/dotfiles-aes.key:ro",
"registry.fedoraproject.org/fedora:latest", "sleep", "infinity");
ok("container-started");
step("install packages");
run(null, "podman", "exec", container, "dnf", "-qy", "--setopt=install_weak_deps=False", "install", "git-core", "java-latest-openjdk-devel");
ok("packages-installed");
run(null, "podman", "exec", container, "git", "config", "--global", "--add", "safe.directory", "/src");
run(null, "podman", "exec", container, "mkdir", "-p", "/root/work");
step("clone repo");
run(null, "podman", "exec", container, "git", "clone", "--quiet", "--no-local", "/src", "/root/work/dotfiles");
run(null, "podman", "exec", container, "install", "-m", "600", "/dotfiles-aes.key", "/root/work/dotfiles/.git/dotfiles-aes.key");
step("run checks");
runInherit(null, "podman", "exec", "-w", "/root/work/dotfiles", container, "java", "/src/scripts/dotfiles_integration_test.java", "--inside");
} finally {
runAllowFail(null, "podman", "rm", "-f", container);
}
}
static void inside() throws Exception {
root = Path.of(".").toAbsolutePath().normalize();
System.out.println("rootless=" + rootless());
ok("deps");
System.out.println(run(root, "git", "--version").trim());
System.out.println(run(root, "java", "-version").lines().findFirst().orElse("java"));
run(root, "java", "bootstripper.java", "install-hooks");
if (!Files.isExecutable(root.resolve(".git/hooks/pre-commit"))) fail("hook-executable");
if (!Files.readString(root.resolve(".git/hooks/pre-commit")).contains("exec java bootstripper.java pre-commit")) fail("hook-content");
ok("install-hooks");
if (!run(root, "git", "show", "HEAD:nushell/secrets.nu").startsWith("DFAES1")) fail("committed-encrypted");
ok("committed-encrypted");
var hosts = allHosts();
System.out.println("manifests=" + String.join(" ", hosts));
for (var host : hosts) runManifest(host);
ok("symlink-manifests:" + hosts.size());
if (Files.readString(root.resolve("nushell/secrets.nu")).startsWith("DFAES1")) fail("worktree-still-encrypted");
ok("worktree-decrypted");
run(root, "java", "bootstripper.java", "pre-commit");
ok("pre-commit");
freshCryptoSmoke();
System.out.println("RESULT:ok");
}
static List<String> allHosts() throws IOException {
var hosts = new ArrayList<String>();
try (var s = Files.list(root)) {
s.filter(p -> p.getFileName().toString().endsWith(".symlinks"))
.map(p -> p.getFileName().toString().replaceFirst("\\.symlinks$", ""))
.sorted().forEach(hosts::add);
}
return hosts;
}
static void runManifest(String host) throws Exception {
var manifest = root.resolve(host + ".symlinks");
if (!Files.exists(manifest)) fail("missing-manifest:" + manifest.getFileName());
var home = Path.of("/tmp/home-" + host);
System.out.println("manifest=" + manifest.getFileName());
deleteTree(home);
Files.createDirectories(home);
var env = Map.of("HOSTNAME", host);
run(root, env, "java", "-Duser.home=" + home, "bootstripper.java", "bootstrap");
ok("bootstrap:" + host);
run(root, env, "java", "-Duser.home=" + home, "bootstripper.java", "check");
ok("check:" + host);
forEachMapping(manifest, (source, target) -> {
var path = targetPath(home, target);
if (!Files.isSymbolicLink(path)) throw new RuntimeException("FAIL:link:" + host + ":" + target);
});
ok("links:" + host);
run(root, env, "java", "-Duser.home=" + home, "bootstripper.java", "clean");
ok("clean:" + host);
}
static void freshCryptoSmoke() throws Exception {
var repo = Path.of("/tmp/fresh-crypto");
var home = Path.of("/tmp/fresh-crypto-home");
deleteTree(repo); deleteTree(home);
Files.createDirectories(repo.resolve("tools"));
Files.createDirectories(home);
Files.copy(root.resolve("bootstripper.java"), repo.resolve("bootstripper.java"));
Files.copy(root.resolve("tools/aes.java"), repo.resolve("tools/aes.java"));
Files.writeString(repo.resolve(".secrets"), "secret.txt\n");
Files.writeString(repo.resolve("mini.symlinks"), "dummy.txt\n~/dummy.txt\n");
Files.writeString(repo.resolve("secret.txt"), "not a real secret\n");
Files.writeString(repo.resolve("dummy.txt"), "dummy\n");
run(repo, "git", "init", "--quiet");
var env = Map.of("DOTFILES_AES_PASSPHRASE", "test");
run(repo, env, "java", "bootstripper.java", "init-crypto");
run(repo, env, "java", "bootstripper.java", "check");
run(repo, env, "git", "add", ".dotfiles-aes-salt", ".secrets", "bootstripper.java", "tools/aes.java", "mini.symlinks", "dummy.txt", "secret.txt");
run(repo, env, "java", "bootstripper.java", "check");
if (!run(repo, "git", "show", ":secret.txt").startsWith("DFAES1")) fail("fresh-staged-encrypted");
var env2 = new HashMap<>(env); env2.put("HOSTNAME", "mini");
run(repo, env2, "java", "-Duser.home=" + home, "bootstripper.java", "bootstrap");
if (!Files.isSymbolicLink(home.resolve("dummy.txt"))) fail("fresh-link");
run(repo, env2, "java", "-Duser.home=" + home, "bootstripper.java", "clean");
ok("fresh-crypto");
}
interface Mapping { void accept(String source, String target) throws Exception; }
static void forEachMapping(Path manifest, Mapping mapping) throws Exception {
String source = null;
for (var raw : Files.readAllLines(manifest)) {
var line = raw.strip();
if (line.isEmpty()) continue;
if (source == null) source = line;
else { mapping.accept(source, line); source = null; }
}
if (source != null) fail("missing target after " + source);
}
static Path targetPath(Path home, String target) {
return target.startsWith("~/") ? home.resolve(target.substring(2)) : Path.of(target);
}
static String rootless() throws IOException {
var p = Path.of("/proc/self/uid_map");
if (!Files.exists(p)) return "unknown";
var parts = Files.readString(p).trim().split("\\s+");
return parts.length >= 2 && parts[0].equals("0") && !parts[1].equals("0") ? "yes" : "unknown";
}
static void deleteTree(Path p) throws IOException {
if (!Files.exists(p)) return;
try (var s = Files.walk(p)) {
for (var x : s.sorted(Comparator.reverseOrder()).toList()) Files.deleteIfExists(x);
}
}
static void step(String what) { log("step:" + what); }
static void ok(String what) { log("ok:" + what); }
static void log(String message) { System.out.println(message); System.out.flush(); }
static void fail(String what) { throw new RuntimeException("FAIL:" + what); }
static String run(Path dir, String... cmd) throws Exception { return run(dir, Map.of(), cmd); }
static String run(Path dir, Map<String, String> env, String... cmd) throws Exception {
var pb = new ProcessBuilder(cmd);
if (dir != null) pb.directory(dir.toFile());
pb.environment().putAll(env);
var p = pb.redirectErrorStream(true).start();
var out = new String(p.getInputStream().readAllBytes());
var code = p.waitFor();
if (code != 0) throw new RuntimeException(String.join(" ", cmd) + "\n" + out);
return out;
}
static void runInherit(Path dir, String... cmd) throws Exception {
var pb = new ProcessBuilder(cmd);
if (dir != null) pb.directory(dir.toFile());
var code = pb.inheritIO().start().waitFor();
if (code != 0) throw new RuntimeException(String.join(" ", cmd) + " exited " + code);
}
static void runAllowFail(Path dir, String... cmd) {
try { run(dir, cmd); } catch (Exception ignored) {}
}
}