@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "io/ioutil"
2222 "testing"
23+ "time"
2324
2425 "github.com/containerd/containerd/oci"
2526 "github.com/containerd/containerd/plugin"
@@ -34,6 +35,7 @@ import (
3435
3536 criconfig "github.com/containerd/containerd/pkg/cri/config"
3637 "github.com/containerd/containerd/pkg/cri/store"
38+ containerstore "github.com/containerd/containerd/pkg/cri/store/container"
3739 imagestore "github.com/containerd/containerd/pkg/cri/store/image"
3840)
3941
@@ -501,3 +503,94 @@ func TestEnsureRemoveAllWithFile(t *testing.T) {
501503 t .Fatal (err )
502504 }
503505}
506+
507+ // Helper function for setting up an environment to test PID namespace targeting.
508+ func addContainer (c * criService , containerID , sandboxID string , PID uint32 , createdAt , startedAt , finishedAt int64 ) error {
509+ meta := containerstore.Metadata {
510+ ID : containerID ,
511+ SandboxID : sandboxID ,
512+ }
513+ status := containerstore.Status {
514+ Pid : PID ,
515+ CreatedAt : createdAt ,
516+ StartedAt : startedAt ,
517+ FinishedAt : finishedAt ,
518+ }
519+ container , err := containerstore .NewContainer (meta ,
520+ containerstore .WithFakeStatus (status ),
521+ )
522+ if err != nil {
523+ return err
524+ }
525+ return c .containerStore .Add (container )
526+ }
527+
528+ func TestValidateTargetContainer (t * testing.T ) {
529+ testSandboxID := "test-sandbox-uid"
530+
531+ // The existing container that will be targeted.
532+ testTargetContainerID := "test-target-container"
533+ testTargetContainerPID := uint32 (4567 )
534+
535+ // A container that has finished running and cannot be targeted.
536+ testStoppedContainerID := "stopped-target-container"
537+ testStoppedContainerPID := uint32 (6789 )
538+
539+ // A container from another pod.
540+ testOtherContainerSandboxID := "other-sandbox-uid"
541+ testOtherContainerID := "other-target-container"
542+ testOtherContainerPID := uint32 (7890 )
543+
544+ // Container create/start/stop times.
545+ createdAt := time .Now ().Add (- 15 * time .Second ).UnixNano ()
546+ startedAt := time .Now ().Add (- 10 * time .Second ).UnixNano ()
547+ finishedAt := time .Now ().Add (- 5 * time .Second ).UnixNano ()
548+
549+ c := newTestCRIService ()
550+
551+ // Create a target container.
552+ err := addContainer (c , testTargetContainerID , testSandboxID , testTargetContainerPID , createdAt , startedAt , 0 )
553+ require .NoError (t , err , "error creating test target container" )
554+
555+ // Create a stopped container.
556+ err = addContainer (c , testStoppedContainerID , testSandboxID , testStoppedContainerPID , createdAt , startedAt , finishedAt )
557+ require .NoError (t , err , "error creating test stopped container" )
558+
559+ // Create a container in another pod.
560+ err = addContainer (c , testOtherContainerID , testOtherContainerSandboxID , testOtherContainerPID , createdAt , startedAt , 0 )
561+ require .NoError (t , err , "error creating test container in other pod" )
562+
563+ for desc , test := range map [string ]struct {
564+ targetContainerID string
565+ expectError bool
566+ }{
567+ "target container in pod" : {
568+ targetContainerID : testTargetContainerID ,
569+ expectError : false ,
570+ },
571+ "target stopped container in pod" : {
572+ targetContainerID : testStoppedContainerID ,
573+ expectError : true ,
574+ },
575+ "target container does not exist" : {
576+ targetContainerID : "no-container-with-this-id" ,
577+ expectError : true ,
578+ },
579+ "target container in other pod" : {
580+ targetContainerID : testOtherContainerID ,
581+ expectError : true ,
582+ },
583+ } {
584+ t .Run (desc , func (t * testing.T ) {
585+ targetContainer , err := c .validateTargetContainer (testSandboxID , test .targetContainerID )
586+ if test .expectError {
587+ require .Error (t , err , "target should have been invalid but no error" )
588+ return
589+ }
590+ require .NoErrorf (t , err , "target should have been valid but got error" )
591+
592+ assert .Equal (t , test .targetContainerID , targetContainer .ID , "returned target container does not have expected ID" )
593+ })
594+ }
595+
596+ }
0 commit comments