From 392a221293f0598c474ca1572f417daf47f90fe2 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 26 Feb 2026 11:25:17 -0800 Subject: [PATCH 1/2] libct/specconv: TestInitSystemdProps: use t.Run Use t.Run for individual tests. Add missing desc fields. Best reviewed with --ignore-all-space. Signed-off-by: Kir Kolyshkin --- libcontainer/specconv/spec_linux_test.go | 60 +++++++++++++----------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/libcontainer/specconv/spec_linux_test.go b/libcontainer/specconv/spec_linux_test.go index b96893c7f..e1a55319f 100644 --- a/libcontainer/specconv/spec_linux_test.go +++ b/libcontainer/specconv/spec_linux_test.go @@ -686,8 +686,9 @@ func TestInitSystemdProps(t *testing.T) { exp expT }{ { - in: inT{"org.systemd.property.TimeoutStopUSec", "uint64 123456789"}, - exp: expT{false, "TimeoutStopUSec", uint64(123456789)}, + desc: "convert USec to Sec (simple case)", + in: inT{"org.systemd.property.TimeoutStopUSec", "uint64 123456789"}, + exp: expT{false, "TimeoutStopUSec", uint64(123456789)}, }, { desc: "convert USec to Sec (default numeric type)", @@ -750,8 +751,9 @@ func TestInitSystemdProps(t *testing.T) { exp: expT{false, "FOOSec", 123}, }, { - in: inT{"org.systemd.property.CollectMode", "'inactive-or-failed'"}, - exp: expT{false, "CollectMode", "inactive-or-failed"}, + desc: "CollectMode", + in: inT{"org.systemd.property.CollectMode", "'inactive-or-failed'"}, + exp: expT{false, "CollectMode", "inactive-or-failed"}, }, { desc: "unrelated property", @@ -778,33 +780,35 @@ func TestInitSystemdProps(t *testing.T) { spec := &specs.Spec{} for _, tc := range testCases { - spec.Annotations = map[string]string{tc.in.name: tc.in.value} + t.Run(tc.desc, func(t *testing.T) { + spec.Annotations = map[string]string{tc.in.name: tc.in.value} - outMap, err := initSystemdProps(spec) - // t.Logf("input %+v, expected %+v, got err:%v out:%+v", tc.in, tc.exp, err, outMap) + outMap, err := initSystemdProps(spec) + // t.Logf("input %+v, expected %+v, got err:%v out:%+v", tc.in, tc.exp, err, outMap) - if tc.exp.isErr != (err != nil) { - t.Errorf("input %+v, expecting error: %v, got %v", tc.in, tc.exp.isErr, err) - } - expLen := 1 // expect a single item - if tc.exp.name == "" { - expLen = 0 // expect nothing - } - if len(outMap) != expLen { - t.Fatalf("input %+v, expected %d, got %d entries: %v", tc.in, expLen, len(outMap), outMap) - } - if expLen == 0 { - continue - } + if tc.exp.isErr != (err != nil) { + t.Errorf("input %+v, expecting error: %v, got %v", tc.in, tc.exp.isErr, err) + } + expLen := 1 // expect a single item + if tc.exp.name == "" { + expLen = 0 // expect nothing + } + if len(outMap) != expLen { + t.Fatalf("input %+v, expected %d, got %d entries: %v", tc.in, expLen, len(outMap), outMap) + } + if expLen == 0 { + return // No more checks. + } - out := outMap[0] - if tc.exp.name != out.Name { - t.Errorf("input %+v, expecting name: %q, got %q", tc.in, tc.exp.name, out.Name) - } - expValue := dbus.MakeVariant(tc.exp.value).String() - if expValue != out.Value.String() { - t.Errorf("input %+v, expecting value: %s, got %s", tc.in, expValue, out.Value) - } + out := outMap[0] + if tc.exp.name != out.Name { + t.Errorf("input %+v, expecting name: %q, got %q", tc.in, tc.exp.name, out.Name) + } + expValue := dbus.MakeVariant(tc.exp.value).String() + if expValue != out.Value.String() { + t.Errorf("input %+v, expecting value: %s, got %s", tc.in, expValue, out.Value) + } + }) } } From a48a7cef962c4f539d87267e139f68e0c3073062 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 26 Feb 2026 11:28:13 -0800 Subject: [PATCH 2/2] libct/specconv: fix panic in initSystemdProps There is a chance of panic here -- eliminate it. Add a test case (which panics before the fix). Reported-by: Luke Hinds Signed-off-by: Kir Kolyshkin --- libcontainer/specconv/spec_linux.go | 2 +- libcontainer/specconv/spec_linux_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 162195d8e..5713460e4 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -758,7 +758,7 @@ func initSystemdProps(spec *specs.Spec) ([]systemdDbus.Property, error) { return nil, fmt.Errorf("annotation %s=%s value parse error: %w", k, v, err) } // Check for Sec suffix. - if trimName := strings.TrimSuffix(name, "Sec"); len(trimName) < len(name) { + if trimName, ok := strings.CutSuffix(name, "Sec"); ok && len(trimName) > 0 { // Check for a lowercase ascii a-z just before Sec. if ch := trimName[len(trimName)-1]; ch >= 'a' && ch <= 'z' { // Convert from Sec to USec. diff --git a/libcontainer/specconv/spec_linux_test.go b/libcontainer/specconv/spec_linux_test.go index e1a55319f..3e532d490 100644 --- a/libcontainer/specconv/spec_linux_test.go +++ b/libcontainer/specconv/spec_linux_test.go @@ -750,6 +750,11 @@ func TestInitSystemdProps(t *testing.T) { in: inT{"org.systemd.property.FOOSec", "123"}, exp: expT{false, "FOOSec", 123}, }, + { + desc: "convert USec to Sec (short name)", + in: inT{"org.systemd.property.Sec", "123"}, + exp: expT{false, "Sec", 123}, + }, { desc: "CollectMode", in: inT{"org.systemd.property.CollectMode", "'inactive-or-failed'"},