@@ -5,11 +5,13 @@ package main
55import (
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
1517func 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