package sentinel import ( "os" "path/filepath" "strings" "testing" ) func TestCollectUsesCommandOutputAndPkgSample(t *testing.T) { dir := fakeSentinelCommands(t) t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) snap := Collect() if snap.Hostname != "titan-13" || snap.Kernel != "6.6.63" || snap.OSImage != "Metis OS" { t.Fatalf("unexpected snapshot: %+v", snap) } if snap.K3sVersion != "v1.31.5+k3s1" || snap.Containerd != "1.7.99" { t.Fatalf("unexpected runtime facts: %+v", snap) } if len(snap.PackageSample) != 4 || snap.PackageSample["k3s"] != "v1.31.5+k3s1" { t.Fatalf("unexpected package sample: %+v", snap.PackageSample) } } func TestCommandOutputUsesNsenterWhenRequested(t *testing.T) { dir := fakeSentinelCommands(t) t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) t.Setenv("METIS_SENTINEL_NSENTER", "1") got, err := commandOutput("ignored", "arg") if err != nil { t.Fatalf("commandOutput: %v", err) } if strings.TrimSpace(string(got)) != "nsenter-ok" { t.Fatalf("unexpected nsenter output: %q", string(got)) } } func TestRunAndTrimAndPkgVersionFallbacks(t *testing.T) { dir := t.TempDir() write := func(name, body string) { path := filepath.Join(dir, name) if err := os.WriteFile(path, []byte("#!/usr/bin/env bash\nset -eu\n"+body+"\n"), 0o755); err != nil { t.Fatalf("write %s: %v", name, err) } } write("cat", `printf 'ID=metis\n'`) write("rpm", `exit 1`) t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) if got := runAndTrim("missing-command"); got != "" { t.Fatalf("runAndTrim missing command = %q", got) } if got := osRelease(); got != "" { t.Fatalf("osRelease without PRETTY_NAME = %q", got) } if got := pkgVersion("does-not-exist"); got != "" { t.Fatalf("pkgVersion fallback = %q", got) } } func fakeSentinelCommands(t *testing.T) string { t.Helper() dir := t.TempDir() write := func(name, body string) { path := filepath.Join(dir, name) if err := os.WriteFile(path, []byte("#!/usr/bin/env bash\nset -eu\n"+body+"\n"), 0o755); err != nil { t.Fatalf("write %s: %v", name, err) } } write("hostname", `printf 'titan-13\n'`) write("uname", `printf '6.6.63\n'`) write("k3s", `printf 'v1.31.5+k3s1\n'`) write("containerd", `printf '1.7.99\n'`) write("cat", `printf 'PRETTY_NAME="Metis OS"\n'`) write("dpkg-query", `case "${@: -1}" in containerd) printf '1.7.99\n' ;; k3s) printf 'v1.31.5+k3s1\n' ;; nvidia-container-toolkit) printf '1.16.2\n' ;; linux-image-raspi) printf '6.6.63\n' ;; *) printf '1.0.0\n' ;; esac`) write("rpm", `printf '1.0.0\n'`) write("nsenter", `printf 'nsenter-ok\n'`) return dir }