@@ -6,6 +6,7 @@ package kubernetes
66
77import (
88 "context"
9+ "github.com/sirupsen/logrus"
910 "reflect"
1011 "testing"
1112
@@ -433,3 +434,121 @@ func TestKubernetes_WaitContainer(t *testing.T) {
433434 })
434435 }
435436}
437+
438+ func Test_podTracker_inspectContainerStatuses (t * testing.T ) {
439+ // setup types
440+ logger := logrus .NewEntry (logrus .StandardLogger ())
441+
442+ tests := []struct {
443+ name string
444+ trackedPod string
445+ ctnName string
446+ terminated bool
447+ pod * v1.Pod
448+ }{
449+ {
450+ name : "container is terminated" ,
451+ trackedPod : "test/github-octocat-1" ,
452+ ctnName : "step-github-octocat-1-clone" ,
453+ terminated : true ,
454+ pod : _pod ,
455+ },
456+ {
457+ name : "pod is pending" ,
458+ trackedPod : "test/github-octocat-1" ,
459+ ctnName : "step-github-octocat-1-clone" ,
460+ terminated : false ,
461+ pod : & v1.Pod {
462+ ObjectMeta : _pod .ObjectMeta ,
463+ TypeMeta : _pod .TypeMeta ,
464+ Spec : _pod .Spec ,
465+ Status : v1.PodStatus {
466+ Phase : v1 .PodPending ,
467+ },
468+ },
469+ },
470+ {
471+ name : "container is running" ,
472+ trackedPod : "test/github-octocat-1" ,
473+ ctnName : "step-github-octocat-1-clone" ,
474+ terminated : false ,
475+ pod : & v1.Pod {
476+ ObjectMeta : _pod .ObjectMeta ,
477+ TypeMeta : _pod .TypeMeta ,
478+ Spec : _pod .Spec ,
479+ Status : v1.PodStatus {
480+ Phase : v1 .PodRunning ,
481+ ContainerStatuses : []v1.ContainerStatus {
482+ {
483+ Name : "step-github-octocat-1-clone" ,
484+ State : v1.ContainerState {
485+ Running : & v1.ContainerStateRunning {},
486+ },
487+ },
488+ },
489+ },
490+ },
491+ },
492+ {
493+ name : "pod has an untracked container" ,
494+ trackedPod : "test/github-octocat-1" ,
495+ ctnName : "step-github-octocat-1-clone" ,
496+ terminated : true ,
497+ pod : & v1.Pod {
498+ ObjectMeta : _pod .ObjectMeta ,
499+ TypeMeta : _pod .TypeMeta ,
500+ Spec : _pod .Spec ,
501+ Status : v1.PodStatus {
502+ Phase : v1 .PodRunning ,
503+ ContainerStatuses : []v1.ContainerStatus {
504+ {
505+ Name : "step-github-octocat-1-clone" ,
506+ State : v1.ContainerState {
507+ Terminated : & v1.ContainerStateTerminated {
508+ Reason : "Completed" ,
509+ ExitCode : 0 ,
510+ },
511+ },
512+ },
513+ {
514+ Name : "injected-by-admissions-controller" ,
515+ State : v1.ContainerState {
516+ Running : & v1.ContainerStateRunning {},
517+ },
518+ },
519+ },
520+ },
521+ },
522+ },
523+ }
524+ for _ , tt := range tests {
525+ t .Run (tt .name , func (t * testing.T ) {
526+ ctnTracker := containerTracker {
527+ Name : tt .ctnName ,
528+ Terminated : make (chan struct {}),
529+ }
530+ podTracker := podTracker {
531+ Logger : logger ,
532+ TrackedPod : tt .trackedPod ,
533+ Containers : map [string ]* containerTracker {},
534+ // other fields not used by inspectContainerStatuses
535+ // if they're needed, use newPodTracker
536+ }
537+ podTracker .Containers [tt .ctnName ] = & ctnTracker
538+
539+ podTracker .inspectContainerStatuses (tt .pod )
540+
541+ func () {
542+ // repeat close() should panic
543+ defer func () { recover () }()
544+
545+ close (ctnTracker .Terminated )
546+
547+ // this will only run if close() did not panic
548+ if tt .terminated {
549+ t .Error ("inspectContainerStatuses should have signaled termination" )
550+ }
551+ }()
552+ })
553+ }
554+ }
0 commit comments