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 38
- runManifest line 74
- freshCryptoSmoke line 94
- Mapping line 120
- accept line 120
- forEachMapping line 121
- targetPath line 132
- rootless line 136
- deleteTree line 143
- ok line 150
- fail line 151
- run line 153
- run line 154
- runAllowFail line 164
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(args.length > 1 ? args[1] : "desktop");
else host(args.length > 0 ? args[0] : "desktop");
}
static void host(String hostname) 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 {
run(null, "podman", "run", "--name", container, "--rm", "-d", "--hostname", hostname,
"-e", "PRIMARY_HOSTNAME=" + hostname,
"-v", root + ":/src:ro", "-v", key + ":/dotfiles-aes.key:ro",
"registry.fedoraproject.org/fedora:latest", "sleep", "infinity");
run(null, "podman", "exec", container, "dnf", "-qy", "--setopt=install_weak_deps=False", "install", "git-core", "java-latest-openjdk-devel");
run(null, "podman", "exec", container, "git", "config", "--global", "--add", "safe.directory", "/src");
run(null, "podman", "exec", container, "mkdir", "-p", "/root/work");
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");
run(null, "podman", "exec", "-w", "/root/work/dotfiles", container, "javac", "-d", "/tmp/dotfiles-test-classes", "/src/scripts/dotfiles_integration_test.java");
run(null, "podman", "exec", "-w", "/root/work/dotfiles", container, "java", "-cp", "/tmp/dotfiles-test-classes", "dotfiles_integration_test", "--inside", hostname);
} finally {
runAllowFail(null, "podman", "rm", "-f", container);
}
}
static void inside(String primaryHostname) 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 = 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);
}
System.out.println("primary-manifest=" + primaryHostname + ".symlinks");
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 void runManifest(String host) throws Exception {
var manifest = root.resolve(host + ".symlinks");
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 ok(String what) { System.out.println("ok:" + what); }
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 runAllowFail(Path dir, String... cmd) {
try { run(dir, cmd); } catch (Exception ignored) {}
}
}