Files
worker/internal/sshinstall/detect_test.go
Gleb Tv 4651deb280
Все проверки выполнены успешно
CI / test (push) Successful in 3m33s
Docker / Build and publish worker image (push) Successful in 18m37s
test(installer): add OpenSSH distro harness
2026-08-12 22:00:14 +03:00

121 строка
3.3 KiB
Go

package sshinstall
import (
"strings"
"testing"
)
// prober returns a FileProber backed by a fixed path set.
func prober(exist ...string) FileProber {
want := make(map[string]bool, len(exist))
for _, p := range exist {
want[p] = true
}
return func(paths ...string) map[string]bool {
out := make(map[string]bool, len(paths))
for _, p := range paths {
out[p] = want[p]
}
return out
}
}
func TestParseOSReleaseQuoting(t *testing.T) {
d := parseOSRelease(`NAME="Ubuntu 24.04 LTS"
VERSION_ID="24.04"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 24.04 LTS"
`)
if d.ID != "ubuntu" || d.Name != "Ubuntu 24.04 LTS" || d.VersionID != "24.04" {
t.Fatalf("parseOSRelease = %+v", d)
}
if d.Distro != DistroUbuntu {
t.Fatalf("distro = %q, want ubuntu", d.Distro)
}
}
func TestParseOSReleaseSingleQuotedAndEmpty(t *testing.T) {
d := parseOSRelease("# comment\nID='alpine'\nNAME=Alpine\n\n")
if d.ID != "alpine" || d.Name != "Alpine" {
t.Fatalf("parseOSRelease = %+v", d)
}
if d.Distro != DistroAlpine {
t.Fatalf("distro = %q, want alpine", d.Distro)
}
}
func TestParseOSReleaseGarbage(t *testing.T) {
d := parseOSRelease("not an os-release file\nID = spaced\nFOO=bar\n")
if d.ID != "" {
t.Fatalf("ID = %q, want empty", d.ID)
}
if d.Distro != DistroUnknown {
t.Fatalf("distro = %q, want unknown", d.Distro)
}
}
func TestPackageManagerFor(t *testing.T) {
cases := map[string]PackageManager{
"alpine": PkgApk,
"debian": PkgApt,
"ubuntu": PkgApt,
"arch": PkgPacman,
"centos": PkgDnf,
"fedora": PkgDnf,
"rocky": PkgDnf,
"almalinux": PkgDnf,
"rhel": PkgDnf,
"nonsense": PkgUnknown,
"": PkgUnknown,
}
for id, want := range cases {
if got := packageManagerFor(id); got != want {
t.Fatalf("packageManagerFor(%q) = %q, want %q", id, got, want)
}
}
}
func TestDetectInitMarkers(t *testing.T) {
osRelease := "ID=arch\nNAME=Arch Linux\n"
if got := Detect(osRelease, prober("/usr/lib/systemd/system")); got.InitSystem != InitSystemd {
t.Fatalf("arch systemd markers not detected: %+v", got)
}
if got := Detect(osRelease, nil).InitSystem; got != InitUnknown {
t.Fatalf("nil prober should yield unknown init, got %q", got)
}
alpine := Detect("ID=alpine\n", prober("/sbin/openrc", "/etc/init.d"))
if alpine.InitSystem != InitOpenRC {
t.Fatalf("alpine openrc markers not detected: %+v", alpine)
}
if alpine.PackageManager != PkgApk {
t.Fatalf("alpine pkg = %q, want apk", alpine.PackageManager)
}
ubuntu := Detect("ID=ubuntu\n", prober("/run/systemd/system"))
if ubuntu.InitSystem != InitSystemd || ubuntu.PackageManager != PkgApt {
t.Fatalf("ubuntu detection = %+v", ubuntu)
}
}
func TestDetectSystemdWinsOverOpenRC(t *testing.T) {
// A host that carries both markers must be reported as systemd so a
// systemd distro running a containerized openrc stub is not misplanned.
d := Detect("ID=ubuntu\n", prober("/usr/lib/systemd/system", "/sbin/openrc"))
if d.InitSystem != InitSystemd {
t.Fatalf("init = %q, want systemd", d.InitSystem)
}
}
func TestDetectionSummarize(t *testing.T) {
d := Detect("ID=alpine\nNAME=Alpine Linux\n", prober("/sbin/openrc"))
s := d.Summarize()
for _, want := range []string{"Alpine Linux", "id=alpine", "pkg=apk", "init=openrc"} {
if !strings.Contains(s, want) {
t.Fatalf("Summarize() = %q missing %q", s, want)
}
}
}