@@ -3,6 +3,7 @@ package mustgather
33import (
44 "context"
55 "fmt"
6+ "path"
67 "reflect"
78 "testing"
89 "time"
@@ -478,3 +479,152 @@ sync && echo 'Caches written to disk'`,
478479 })
479480 }
480481}
482+
483+ func TestNewPod (t * testing.T ) {
484+ generateMustGatherPod := func (image string , update func (pod * corev1.Pod )) * corev1.Pod {
485+ pod := defaultMustGatherPod (image )
486+ update (pod )
487+ return pod
488+ }
489+
490+ tests := []struct {
491+ name string
492+ options * MustGatherOptions
493+ hasMasters bool
494+ node string
495+ affinity * corev1.Affinity
496+ expectedPod * corev1.Pod
497+ }{
498+ {
499+ name : "node set, affinity provided, affinity not set" ,
500+ options : & MustGatherOptions {
501+ VolumePercentage : defaultVolumePercentage ,
502+ SourceDir : defaultSourceDir ,
503+ },
504+ node : "node1" ,
505+ affinity : buildNodeAffinity ([]string {"node2" }),
506+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
507+ pod .Spec .NodeName = "node1"
508+ }),
509+ },
510+ {
511+ name : "node not set, affinity provided, affinity set" ,
512+ options : & MustGatherOptions {
513+ VolumePercentage : defaultVolumePercentage ,
514+ SourceDir : defaultSourceDir ,
515+ },
516+ affinity : buildNodeAffinity ([]string {"node2" }),
517+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
518+ pod .Spec .Affinity = buildNodeAffinity ([]string {"node2" })
519+ }),
520+ },
521+ {
522+ name : "custom source dir" ,
523+ options : & MustGatherOptions {
524+ VolumePercentage : defaultVolumePercentage ,
525+ SourceDir : "custom-source-dir" ,
526+ },
527+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
528+ volumeUsageChecker := fmt .Sprintf (volumeUsageCheckerScript , "custom-source-dir" , defaultVolumePercentage )
529+ podCmd := buildPodCommand (volumeUsageChecker , defaultMustGatherCommand )
530+ pod .Spec .Containers [0 ].Command = []string {"/bin/bash" , "-c" , podCmd }
531+ pod .Spec .Containers [0 ].VolumeMounts = []corev1.VolumeMount {{
532+ Name : "must-gather-output" ,
533+ MountPath : "custom-source-dir" ,
534+ }}
535+ pod .Spec .Containers [1 ].VolumeMounts = []corev1.VolumeMount {{
536+ Name : "must-gather-output" ,
537+ MountPath : "custom-source-dir" ,
538+ }}
539+ }),
540+ },
541+ {
542+ name : "host network" ,
543+ options : & MustGatherOptions {
544+ VolumePercentage : defaultVolumePercentage ,
545+ SourceDir : defaultSourceDir ,
546+ HostNetwork : true ,
547+ },
548+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
549+ pod .Spec .HostNetwork = true
550+ pod .Spec .Containers [0 ].SecurityContext = & corev1.SecurityContext {
551+ Capabilities : & corev1.Capabilities {
552+ Add : []corev1.Capability {
553+ corev1 .Capability ("CAP_NET_RAW" ),
554+ },
555+ },
556+ }
557+ }),
558+ },
559+ {
560+ name : "with control plane nodes" ,
561+ options : & MustGatherOptions {
562+ VolumePercentage : defaultVolumePercentage ,
563+ SourceDir : defaultSourceDir ,
564+ },
565+ hasMasters : true ,
566+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
567+ pod .Spec .NodeSelector [controlPlaneNodeRoleLabel ] = ""
568+ }),
569+ },
570+ {
571+ name : "with custom command" ,
572+ options : & MustGatherOptions {
573+ VolumePercentage : defaultVolumePercentage ,
574+ SourceDir : defaultSourceDir ,
575+ Command : []string {"custom_command" , "with_params" },
576+ },
577+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
578+ cleanSourceDir := path .Clean (defaultSourceDir )
579+ volumeUsageChecker := fmt .Sprintf (volumeUsageCheckerScript , cleanSourceDir , defaultVolumePercentage )
580+ podCmd := buildPodCommand (volumeUsageChecker , "custom_command with_params" )
581+ pod .Spec .Containers [0 ].Command = []string {"/bin/bash" , "-c" , podCmd }
582+ pod .Spec .Containers [0 ].VolumeMounts = []corev1.VolumeMount {{
583+ Name : "must-gather-output" ,
584+ MountPath : cleanSourceDir ,
585+ }}
586+ pod .Spec .Containers [1 ].VolumeMounts = []corev1.VolumeMount {{
587+ Name : "must-gather-output" ,
588+ MountPath : cleanSourceDir ,
589+ }}
590+ }),
591+ },
592+ {
593+ name : "with since" ,
594+ options : & MustGatherOptions {
595+ VolumePercentage : defaultVolumePercentage ,
596+ SourceDir : defaultSourceDir ,
597+ Since : 2 * time .Minute ,
598+ },
599+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
600+ pod .Spec .Containers [0 ].Env = append (pod .Spec .Containers [0 ].Env , corev1.EnvVar {
601+ Name : "MUST_GATHER_SINCE" ,
602+ Value : "2m0s" ,
603+ })
604+ }),
605+ },
606+ {
607+ name : "with sincetime" ,
608+ options : & MustGatherOptions {
609+ VolumePercentage : defaultVolumePercentage ,
610+ SourceDir : defaultSourceDir ,
611+ SinceTime : "2023-09-24T15:30:00Z" ,
612+ },
613+ expectedPod : generateMustGatherPod ("image" , func (pod * corev1.Pod ) {
614+ pod .Spec .Containers [0 ].Env = append (pod .Spec .Containers [0 ].Env , corev1.EnvVar {
615+ Name : "MUST_GATHER_SINCE_TIME" ,
616+ Value : "2023-09-24T15:30:00Z" ,
617+ })
618+ }),
619+ },
620+ }
621+
622+ for _ , test := range tests {
623+ t .Run (test .name , func (t * testing.T ) {
624+ tPod := test .options .newPod (test .node , "image" , test .hasMasters , test .affinity )
625+ if ! cmp .Equal (test .expectedPod , tPod ) {
626+ t .Errorf ("Unexpected pod command was generated: \n %s\n " , cmp .Diff (test .expectedPod , tPod ))
627+ }
628+ })
629+ }
630+ }
0 commit comments