Skip to content

Commit 76a7277

Browse files
committed
Added unit tests for handleProcessArgsForIsolatedJobContainer
1 parent 399bba9 commit 76a7277

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

cmd/containerd-shim-runhcs-v1/task_hcs.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -994,14 +994,11 @@ func (ht *hcsTask) requestAddContainerMount(ctx context.Context, resourcePath st
994994
}
995995

996996
func handleProcessArgsForIsolatedJobContainer(specs *specs.Process) {
997-
if specs.CommandLine != "" {
998-
if !strings.HasPrefix(strings.ToLower(specs.CommandLine), "cmd") {
999-
specs.CommandLine = fmt.Sprintf("cmd /c %s", specs.CommandLine)
1000-
}
1001-
} else {
1002-
if strings.ToLower(specs.Args[0]) != "cmd" {
1003-
specs.Args = append([]string{"cmd", "/c"}, specs.Args...)
1004-
}
997+
if specs.CommandLine != "" && !strings.HasPrefix(strings.TrimSpace(strings.ToLower(specs.CommandLine)), "cmd") {
998+
specs.CommandLine = fmt.Sprintf("cmd /c %s", specs.CommandLine)
999+
}
1000+
if len(specs.Args) > 0 && strings.TrimSpace(strings.ToLower(specs.Args[0])) != "cmd" {
1001+
specs.Args = append([]string{"cmd", "/c"}, specs.Args...)
10051002
}
10061003
}
10071004

cmd/containerd-shim-runhcs-v1/task_hcs_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ package main
55
import (
66
"context"
77
"math/rand"
8+
"reflect"
89
"strconv"
910
"testing"
1011
"time"
1112

1213
"github.com/containerd/errdefs"
14+
"github.com/opencontainers/runtime-spec/specs-go"
1315
)
1416

1517
func setupTestHcsTask(t *testing.T) (*hcsTask, *testShimExec, *testShimExec) {
@@ -318,3 +320,98 @@ func Test_hcsTask_DeleteExec_2ndExecID_ExitedState_Success(t *testing.T) {
318320
}
319321
verifyDeleteSuccessValues(t, pid, status, at, second)
320322
}
323+
324+
func Test_handleProcessArgsForIsolatedJobContainer(t *testing.T) {
325+
tests := []struct {
326+
name string
327+
specs *specs.Process
328+
expectedCmdLine string
329+
expectedArgs []string
330+
}{
331+
{
332+
name: "CommandLine starts with 'cmd' (lowercase) – unchanged",
333+
specs: &specs.Process{CommandLine: "cmd /c dir"},
334+
expectedCmdLine: "cmd /c dir",
335+
expectedArgs: nil,
336+
},
337+
{
338+
name: "CommandLine starts with 'CMD' (uppercase) – unchanged",
339+
specs: &specs.Process{CommandLine: "CMD /C whoami"},
340+
expectedCmdLine: "CMD /C whoami",
341+
expectedArgs: nil,
342+
},
343+
{
344+
name: "CommandLine starts with 'cmd.exe' – unchanged",
345+
specs: &specs.Process{CommandLine: "cmd.exe /c ipconfig"},
346+
expectedCmdLine: "cmd.exe /c ipconfig",
347+
expectedArgs: nil,
348+
},
349+
{
350+
name: "CommandLine plain – gets prefixed with 'cmd /c '",
351+
specs: &specs.Process{CommandLine: "echo hello"},
352+
expectedCmdLine: "cmd /c echo hello",
353+
expectedArgs: nil,
354+
},
355+
{
356+
name: "CommandLine mixed case 'CmD' – unchanged",
357+
specs: &specs.Process{CommandLine: "CmD /c ping 127.0.0.1"},
358+
expectedCmdLine: "CmD /c ping 127.0.0.1",
359+
expectedArgs: nil,
360+
},
361+
{
362+
name: "Args plain – gets ['cmd','/c',...] prefix",
363+
specs: &specs.Process{Args: []string{"echo", "hello"}},
364+
expectedCmdLine: "",
365+
expectedArgs: []string{"cmd", "/c", "echo", "hello"},
366+
},
367+
{
368+
name: "Args already start with 'CMD' (uppercase) – unchanged",
369+
specs: &specs.Process{Args: []string{"CMD", "/C", "echo", "hi"}},
370+
expectedCmdLine: "",
371+
expectedArgs: []string{"CMD", "/C", "echo", "hi"},
372+
},
373+
{
374+
name: "Args already start with 'cmd' (lowercase) – unchanged",
375+
specs: &specs.Process{Args: []string{"cmd", "/c", "type", "file.txt"}},
376+
expectedCmdLine: "",
377+
expectedArgs: []string{"cmd", "/c", "type", "file.txt"},
378+
},
379+
{
380+
name: "Empty CommandLine and empty Args – unchanged",
381+
specs: &specs.Process{},
382+
expectedCmdLine: "",
383+
expectedArgs: nil,
384+
},
385+
{
386+
name: "Empty CommandLine and empty slice Args – unchanged (empty slice preserved)",
387+
specs: &specs.Process{Args: []string{}},
388+
expectedCmdLine: "",
389+
expectedArgs: []string{},
390+
},
391+
{
392+
name: "CommandLine has leading spaces before 'cmd' – treated as not starting with cmd - unchanged",
393+
specs: &specs.Process{CommandLine: " cmd /c echo spaced"},
394+
expectedCmdLine: "cmd /c echo spaced",
395+
expectedArgs: nil,
396+
},
397+
{
398+
name: "Args first element mixed case 'Cmd' – unchanged",
399+
specs: &specs.Process{Args: []string{"Cmd", "/c", "echo", "hi"}},
400+
expectedCmdLine: "",
401+
expectedArgs: []string{"Cmd", "/c", "echo", "hi"},
402+
},
403+
}
404+
405+
for _, tt := range tests {
406+
t.Run(tt.name, func(t *testing.T) {
407+
handleProcessArgsForIsolatedJobContainer(tt.specs)
408+
409+
if tt.specs.CommandLine != tt.expectedCmdLine {
410+
t.Errorf("CommandLine mismatch: got: %q want: %q", tt.specs.CommandLine, tt.expectedCmdLine)
411+
}
412+
if !reflect.DeepEqual(tt.specs.Args, tt.expectedArgs) {
413+
t.Errorf("Args mismatch: got: %#v want: %#v", tt.specs.Args, tt.expectedArgs)
414+
}
415+
})
416+
}
417+
}

0 commit comments

Comments
 (0)