94 lines
4.5 KiB
Python
94 lines
4.5 KiB
Python
import json
|
|
|
|
def alice(cmd):
|
|
return machine.succeed(f"su - alice -c '{cmd}'")
|
|
|
|
def roots(query=""):
|
|
out = alice(f"i-am-gcroot list --json -q \"{query}\"")
|
|
return {r["link"]: r for r in json.loads(out)}
|
|
|
|
machine.wait_for_unit("multi-user.target")
|
|
machine.wait_for_unit("nix-daemon.socket")
|
|
|
|
# store paths without building anything
|
|
paths = {}
|
|
for n in ["one", "two", "three", "four", "five", "reg", "g1", "g2"]:
|
|
paths[n] = alice(f"echo {n} > /tmp/{n}.txt && nix-store --add /tmp/{n}.txt").strip()
|
|
|
|
alice(f"nix-store --add-root ~/result1 -r {paths['one']}")
|
|
alice(f"nix-store --add-root ~/result2 -r {paths['two']}")
|
|
alice(f"nix-store --add-root ~/result3 -r {paths['three']} && rm ~/result3")
|
|
alice(f"nix-store --add-root ~/result4 -r {paths['four']}")
|
|
alice(f"nix-store --add-root ~/result5 -r {paths['five']}")
|
|
alice(f"nix-store --add-root ~/result6 -r {paths['five']}")
|
|
alice(f"mkdir -p ~/.cache/nix && nix-store --add-root ~/.cache/nix/flake-registry.json -r {paths['reg']}")
|
|
alice(f"nix-env -p ~/prof --set {paths['g1']}")
|
|
alice(f"nix-env -p ~/prof --set {paths['g2']}")
|
|
|
|
with subtest("list classifies roots"):
|
|
rs = roots("under:/home/alice")
|
|
print(json.dumps(rs, indent=1))
|
|
assert rs["/home/alice/result1"]["kind"] == "indirect"
|
|
assert rs["/home/alice/result1"]["target"] == paths["one"]
|
|
assert rs["/home/alice/result1"]["owner"] == "alice"
|
|
assert rs["/home/alice/result3"]["kind"] == "dangling"
|
|
assert rs["/home/alice/prof-1-link"]["kind"] == "profile"
|
|
assert not rs["/home/alice/prof-1-link"].get("current")
|
|
assert rs["/home/alice/prof-2-link"]["current"]
|
|
assert rs["/home/alice/prof-2-link"]["protected"]
|
|
|
|
with subtest("hidden cache roots"):
|
|
assert "/home/alice/.cache/nix/flake-registry.json" not in roots()
|
|
assert set(roots("is:hidden")) == {"/home/alice/.cache/nix/flake-registry.json"}
|
|
out = alice("i-am-gcroot list --all --json -q flake-registry")
|
|
assert json.loads(out)[0]["hidden"], out
|
|
|
|
with subtest("filters"):
|
|
assert set(roots("result -kind:dangling")) == {"/home/alice/result1", "/home/alice/result2", "/home/alice/result4", "/home/alice/result5", "/home/alice/result6"}
|
|
assert set(roots("owner:me kind:profile")) == {"/home/alice/prof-1-link", "/home/alice/prof-2-link"}
|
|
|
|
with subtest("query delete removes dangling auto entries as root (before gc prunes them)"):
|
|
out = machine.succeed("i-am-gcroot rm -y -q 'kind:dangling under:/home/alice'")
|
|
assert "ok" in out, out
|
|
assert "/home/alice/result3" not in roots()
|
|
|
|
with subtest("delete indirect root and collect"):
|
|
out = alice("i-am-gcroot rm -n ~/result1")
|
|
assert "would rm /home/alice/result1" in out, out
|
|
machine.succeed("test -L /home/alice/result1")
|
|
alice("i-am-gcroot rm -y ~/result1")
|
|
machine.fail("test -e /home/alice/result1")
|
|
machine.succeed("nix-store --gc")
|
|
machine.fail(f"test -e {paths['one']}")
|
|
machine.succeed(f"test -e {paths['two']}")
|
|
|
|
with subtest("delete with targeted collect"):
|
|
out = alice("i-am-gcroot rm -y -c ~/result4")
|
|
print(out)
|
|
assert "collected 1 store paths" in out, out
|
|
machine.fail(f"test -e {paths['four']}")
|
|
# five is still held by result6, so collecting after removing result5 frees nothing
|
|
out = alice("i-am-gcroot rm -y -c ~/result5")
|
|
print(out)
|
|
assert "collected 0 store paths" in out, out
|
|
machine.succeed(f"test -e {paths['five']}")
|
|
machine.succeed(f"test -e {paths['two']}")
|
|
|
|
with subtest("profile generations"):
|
|
machine.fail("su - alice -c 'i-am-gcroot rm -y ~/prof-2-link'")
|
|
machine.succeed("test -L /home/alice/prof-2-link")
|
|
alice("i-am-gcroot rm -y ~/prof-1-link")
|
|
machine.fail("test -e /home/alice/prof-1-link")
|
|
|
|
with subtest("web ui"):
|
|
machine.succeed("su - alice -c 'i-am-gcroot web --addr 127.0.0.1:8377 > /tmp/web.log 2>&1 &'")
|
|
machine.wait_until_succeeds("grep -q serving /tmp/web.log")
|
|
token = machine.succeed("grep -o 'token=[0-9a-f]*' /tmp/web.log").strip().split("=")[1]
|
|
machine.succeed("test $(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8377/api/roots) = 401")
|
|
out = machine.succeed(f"curl -sf -H 'Authorization: Bearer {token}' 'http://127.0.0.1:8377/api/roots?q=result2'")
|
|
assert json.loads(out)[0]["link"] == "/home/alice/result2", out
|
|
machine.succeed(f"curl -sf 'http://127.0.0.1:8377/?token={token}' | grep -q result2")
|
|
out = machine.succeed(f"curl -sf -X POST -H 'X-Token: {token}' -d 'link=/home/alice/result2' http://127.0.0.1:8377/delete")
|
|
assert "Deleted 1 of 1" in out, out
|
|
machine.fail("test -e /home/alice/result2")
|