init
This commit is contained in:
24 files changed
+3822
No files matched your search
@@ -0,0 +1,209 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeTree struct {
|
||||
cfg Config
|
||||
home string
|
||||
}
|
||||
|
||||
func symlink(t *testing.T, target, link string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(target, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func newFakeTree(t *testing.T) *fakeTree {
|
||||
dir := t.TempDir()
|
||||
ft := &fakeTree{
|
||||
cfg: Config{
|
||||
StoreDir: filepath.Join(dir, "store"),
|
||||
StateDir: filepath.Join(dir, "state"),
|
||||
NixStoreBin: "false",
|
||||
NixBin: "false",
|
||||
NixEnvBin: "true",
|
||||
},
|
||||
home: filepath.Join(dir, "home"),
|
||||
}
|
||||
store := func(name string) string {
|
||||
p := filepath.Join(ft.cfg.StoreDir, name)
|
||||
os.MkdirAll(p, 0o755)
|
||||
return p
|
||||
}
|
||||
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
|
||||
prof := filepath.Join(ft.cfg.StateDir, "profiles")
|
||||
|
||||
// indirect root: auto/h1 -> home/result -> store
|
||||
symlink(t, store("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")+"/bin/hello", filepath.Join(ft.home, "result"))
|
||||
symlink(t, filepath.Join(ft.home, "result"), filepath.Join(gc, "auto", "h1"))
|
||||
// dangling: auto/h2 -> home/gone
|
||||
symlink(t, filepath.Join(ft.home, "gone"), filepath.Join(gc, "auto", "h2"))
|
||||
// auto entry pointing to a regular file is not a root
|
||||
os.WriteFile(filepath.Join(ft.home, "file"), nil, 0o644)
|
||||
symlink(t, filepath.Join(ft.home, "file"), filepath.Join(gc, "auto", "h3"))
|
||||
// direct root
|
||||
symlink(t, store("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-direct"), filepath.Join(gc, "manual", "keep"))
|
||||
// gcroots/profiles -> profiles dir must not produce duplicates
|
||||
symlink(t, prof, filepath.Join(gc, "profiles"))
|
||||
// profile with two generations, 2 is current
|
||||
symlink(t, store("cccccccccccccccccccccccccccccccc-sys1"), filepath.Join(prof, "system-1-link"))
|
||||
symlink(t, store("dddddddddddddddddddddddddddddddd-sys2"), filepath.Join(prof, "system-2-link"))
|
||||
symlink(t, "system-2-link", filepath.Join(prof, "system"))
|
||||
// a user profile registered through auto/
|
||||
symlink(t, store("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-hm"), filepath.Join(ft.home, "profiles", "hm-3-link"))
|
||||
symlink(t, "hm-3-link", filepath.Join(ft.home, "profiles", "hm"))
|
||||
symlink(t, filepath.Join(ft.home, "profiles", "hm-3-link"), filepath.Join(gc, "auto", "h4"))
|
||||
// nix's own cache root, hidden by default
|
||||
symlink(t, store("ffffffffffffffffffffffffffffffff-flake-registry.json"), filepath.Join(ft.home, ".cache", "nix", "flake-registry.json"))
|
||||
symlink(t, filepath.Join(ft.home, ".cache", "nix", "flake-registry.json"), filepath.Join(gc, "auto", "h5"))
|
||||
return ft
|
||||
}
|
||||
|
||||
func byLink(rs []*Root) map[string]*Root {
|
||||
m := map[string]*Root{}
|
||||
for _, r := range rs {
|
||||
m[r.Link] = r
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func TestScan(t *testing.T) {
|
||||
ft := newFakeTree(t)
|
||||
rs, err := Scan(ft.cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := byLink(rs)
|
||||
if len(rs) != 7 {
|
||||
for _, r := range rs {
|
||||
t.Logf("%s %s -> %s", r.Kind, r.Link, r.Target)
|
||||
}
|
||||
t.Fatalf("want 7 roots, got %d", len(rs))
|
||||
}
|
||||
if r := m[filepath.Join(ft.home, ".cache", "nix", "flake-registry.json")]; r == nil || !r.Hidden {
|
||||
t.Errorf("cache root should be hidden: %+v", r)
|
||||
}
|
||||
if m[filepath.Join(ft.home, "result")].Hidden {
|
||||
t.Error("result should not be hidden")
|
||||
}
|
||||
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
|
||||
prof := filepath.Join(ft.cfg.StateDir, "profiles")
|
||||
|
||||
r := m[filepath.Join(ft.home, "result")]
|
||||
if r == nil || r.Kind != KindIndirect || r.Via != filepath.Join(gc, "auto", "h1") ||
|
||||
r.Target != filepath.Join(ft.cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello") {
|
||||
t.Errorf("indirect root wrong: %+v", r)
|
||||
}
|
||||
if r.TargetName() != "hello" {
|
||||
t.Errorf("TargetName = %q", r.TargetName())
|
||||
}
|
||||
if r := m[filepath.Join(ft.home, "gone")]; r == nil || r.Kind != KindDangling {
|
||||
t.Errorf("dangling root wrong: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(gc, "manual", "keep")]; r == nil || r.Kind != KindDirect {
|
||||
t.Errorf("direct root wrong: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(prof, "system-1-link")]; r == nil || r.Kind != KindProfile || r.Generation != 1 || r.Current {
|
||||
t.Errorf("profile gen 1 wrong: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(prof, "system-2-link")]; r == nil || !r.Current || !r.Protected {
|
||||
t.Errorf("profile gen 2 should be current and protected: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(ft.home, "profiles", "hm-3-link")]; r == nil || r.Kind != KindProfile || !r.Current || r.Via == "" {
|
||||
t.Errorf("indirect profile wrong: %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ft := newFakeTree(t)
|
||||
rs, err := Scan(ft.cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := byLink(rs)
|
||||
ctx := context.Background()
|
||||
prof := filepath.Join(ft.cfg.StateDir, "profiles")
|
||||
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
|
||||
|
||||
result := filepath.Join(ft.home, "result")
|
||||
all := []Root{*m[result], *m[filepath.Join(ft.home, "gone")], *m[filepath.Join(prof, "system-1-link")], *m[filepath.Join(prof, "system-2-link")]}
|
||||
|
||||
// dry run touches nothing
|
||||
res := Delete(ctx, ft.cfg, all, DeleteOpts{DryRun: true})
|
||||
if _, err := os.Lstat(result); err != nil {
|
||||
t.Fatal("dry run removed link")
|
||||
}
|
||||
if !res[0].OK() || res[0].Action != "rm "+result {
|
||||
t.Errorf("dry run result: %+v", res[0])
|
||||
}
|
||||
if res[3].OK() {
|
||||
t.Errorf("current generation must be refused: %+v", res[3])
|
||||
}
|
||||
|
||||
res = Delete(ctx, ft.cfg, all, DeleteOpts{})
|
||||
for i, r := range res[:3] {
|
||||
if !r.OK() {
|
||||
t.Errorf("delete %d failed: %+v", i, r)
|
||||
}
|
||||
}
|
||||
for _, p := range []string{result, filepath.Join(gc, "auto", "h1"), filepath.Join(gc, "auto", "h2")} {
|
||||
if _, err := os.Lstat(p); err == nil {
|
||||
t.Errorf("%s still exists", p)
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(ft.cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")); err != nil {
|
||||
t.Error("store path must not be touched")
|
||||
}
|
||||
if res[2].Action != "nix-env --profile "+filepath.Join(prof, "system")+" --delete-generations 1" {
|
||||
t.Errorf("profile action: %q", res[2].Action)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveSymlinkRefusesNonLinks(t *testing.T) {
|
||||
ft := newFakeTree(t)
|
||||
if err := removeSymlink(ft.cfg, filepath.Join(ft.home, "file"), false); err == nil {
|
||||
t.Error("removed a regular file")
|
||||
}
|
||||
if err := removeSymlink(ft.cfg, filepath.Join(ft.cfg.StoreDir, "x"), false); err == nil {
|
||||
t.Error("removed something in the store")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRuntime(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
out := []byte(`/proc/1/maps -> /nix/store/a-x
|
||||
/proc/1/environ -> /nix/store/b-y
|
||||
/proc/1/maps -> /nix/store/a-x
|
||||
/proc/22/exe -> /nix/store/c-z
|
||||
{censored} -> /nix/store/d-w
|
||||
/home/u/result -> /nix/store/e-v
|
||||
`)
|
||||
rs := parseRuntime(cfg, out, map[string]bool{})
|
||||
if len(rs) != 3 {
|
||||
t.Fatalf("want 3 runtime groups, got %d", len(rs))
|
||||
}
|
||||
m := byLink(rs)
|
||||
if m["/proc/1"].Count != 2 || m["/proc/22"].Target != "/nix/store/c-z" || m["{censored}"] == nil {
|
||||
t.Errorf("bad grouping: %+v %+v", m["/proc/1"], m["/proc/22"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePathInfo(t *testing.T) {
|
||||
obj := parsePathInfo([]byte(`{"/nix/store/a-x":{"closureSize":10,"narSize":5},"/nix/store/b-y":null}`))
|
||||
if obj["/nix/store/a-x"].ClosureSize != 10 || len(obj) != 1 {
|
||||
t.Errorf("object format: %+v", obj)
|
||||
}
|
||||
arr := parsePathInfo([]byte(`[{"path":"/nix/store/a-x","closureSize":7,"narSize":3},{"path":"/nix/store/b-y","valid":false}]`))
|
||||
if arr["/nix/store/a-x"].ClosureSize != 7 || len(arr) != 1 {
|
||||
t.Errorf("array format: %+v", arr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user