Files
nadir-agent/internal/modules/packages/packages_test.go
T
2026-06-22 16:06:57 +02:00

81 lines
2.3 KiB
Go

package packages
import (
"reflect"
"testing"
)
func TestParseTabbed(t *testing.T) {
out := "zeromq\t4.3.5-22.fc43\nspice-server\t0.16.0-2.fc43\n\nbad-no-tab\n"
got := parseTabbed(out)
want := []Package{{"zeromq", "4.3.5-22.fc43"}, {"spice-server", "0.16.0-2.fc43"}}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}
func TestParseSpaced(t *testing.T) {
got := parseSpaced("linux 6.9.1\nhtop 3.3.0\n")
want := []Package{{"linux", "6.9.1"}, {"htop", "3.3.0"}}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}
func TestParseDnf(t *testing.T) {
// dnf5 emits a section header ("Upgrades") that must be skipped.
out := "Upgrades\n" +
"code.x86_64 1.125.1-1781859648.el8 code\n" +
"containerd.io.x86_64 2.2.5-1.fc44 docker-ce-stable\n" +
"\nObsoleting Packages\n"
got := parseDnf(out)
want := []Package{{"code", "1.125.1-1781859648.el8"}, {"containerd.io", "2.2.5-1.fc44"}}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}
func TestParseApt(t *testing.T) {
out := "Listing...\n" +
"vim/jammy-updates 2:8.2.3995-1ubuntu2.15 amd64 [upgradable from: 2:8.2.3995-1ubuntu2.1]\n"
got := parseApt(out)
want := []Package{{"vim", "2:8.2.3995-1ubuntu2.15"}}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}
func TestParsePacmanUpdates(t *testing.T) {
got := parsePacmanUpdates("linux 6.9.1-1 -> 6.9.2-1\nfoo 1.0 1.0\n")
want := []Package{{"linux", "6.9.2-1"}}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}
func TestStripArch(t *testing.T) {
cases := map[string]string{
"code.x86_64": "code",
"python3.11.noarch": "python3.11", // arch is only the final segment
"noarchhere": "noarchhere",
}
for in, want := range cases {
if got := stripArch(in); got != want {
t.Errorf("stripArch(%q) = %q, want %q", in, got, want)
}
}
}
func TestValidateName(t *testing.T) {
for _, n := range []string{"htop", "openssh-server", "lib32-glibc", "g++", "python3.11"} {
if err := validateName(n); err != nil {
t.Errorf("validateName(%q) = %v, want nil", n, err)
}
}
for _, n := range []string{"", "-rf", "foo;rm", "foo bar", "pkg=1.0", "a/b"} {
if err := validateName(n); err == nil {
t.Errorf("validateName(%q) = nil, want error", n)
}
}
}