@@ -731,11 +731,11 @@ func IsContainsLabelWithValue(obj client.Object, label, value string) bool {
731
731
return ok && val == value
732
732
}
733
733
734
- // SaveTestResources dump some resources that may help in further diagnostic.
734
+ // SaveTestCaseDump dump some resources, logs and descriptions that may help in further diagnostic.
735
735
//
736
736
// NOTE: This method is called in AfterEach for failed specs only. Avoid to use Expect,
737
737
// as it fails without reporting. Better use GinkgoWriter to report errors at this point.
738
- func SaveTestResources (labels map [string ]string , additional string ) {
738
+ func SaveTestCaseDump (labels map [string ]string , additional , namespace string ) {
739
739
replacer := strings .NewReplacer (
740
740
" " , "_" ,
741
741
":" , "_" ,
@@ -753,10 +753,24 @@ func SaveTestResources(labels map[string]string, additional string) {
753
753
if tmpDir == "" {
754
754
tmpDir = "/tmp"
755
755
}
756
- resFileName := fmt .Sprintf ("%s/e2e_failed__%s__%s.yaml" , tmpDir , labels ["testcase" ], additional )
757
- errorFileName := fmt .Sprintf ("%s/e2e_failed__%s__%s_error.txt" , tmpDir , labels ["testcase" ], additional )
758
756
759
- cmdr := kubectl .Get ("virtualization,intvirt,po,volumesnapshot -A" , kc.GetOptions {Output : "yaml" , Labels : labels })
757
+ SaveTestCaseResources (labels , additional , namespace , tmpDir )
758
+ SavePodLogsAndDescriptions (labels , additional , namespace , tmpDir )
759
+ }
760
+
761
+ func SaveTestCaseResources (labels map [string ]string , additional , namespace , dumpPath string ) {
762
+ resFileName := fmt .Sprintf ("%s/e2e_failed__%s__%s.yaml" , dumpPath , labels ["testcase" ], additional )
763
+ errorFileName := fmt .Sprintf ("%s/e2e_failed__%s__%s_error.txt" , dumpPath , labels ["testcase" ], additional )
764
+
765
+ cmdr := kubectl .Get (
766
+ "virtualization,cvi,vmc,intvirt,pod,volumesnapshot" ,
767
+ kc.GetOptions {
768
+ Labels : labels ,
769
+ Namespace : namespace ,
770
+ Output : "yaml" ,
771
+ ShowManagedFields : true ,
772
+ },
773
+ )
760
774
if cmdr .Error () != nil {
761
775
errReport := fmt .Sprintf ("cmd: %s\n error: %s\n stderr: %s\n " , cmdr .GetCmd (), cmdr .Error (), cmdr .StdErr ())
762
776
GinkgoWriter .Printf ("Get resources error:\n %s\n " , errReport )
@@ -775,6 +789,42 @@ func SaveTestResources(labels map[string]string, additional string) {
775
789
}
776
790
}
777
791
792
+ func SavePodLogsAndDescriptions (labels map [string ]string , additional , namespace , dumpPath string ) {
793
+ pods := & corev1.PodList {}
794
+ err := GetObjects (kc .ResourcePod , pods , kc.GetOptions {Namespace : namespace , Labels : labels })
795
+ if err != nil {
796
+ GinkgoWriter .Printf ("Failed to get PodList:\n %s\n " , err )
797
+ }
798
+
799
+ if len (pods .Items ) == 0 {
800
+ GinkgoWriter .Println ("The list of pods is empty; nothing to dump." )
801
+ }
802
+
803
+ for _ , pod := range pods .Items {
804
+ logCmd := kubectl .RawCommand (fmt .Sprintf ("logs %s --namespace %s" , pod .Name , pod .Namespace ), framework .ShortTimeout )
805
+ if logCmd .Error () != nil {
806
+ GinkgoWriter .Printf ("Failed to get logs:\n PodName: %s\n Error: %s\n " , pod .Name , logCmd .StdErr ())
807
+ }
808
+
809
+ fileName := fmt .Sprintf ("%s/e2e_failed__%s__%s__%s__logs.json" , dumpPath , labels ["testcase" ], additional , pod .Name )
810
+ err := os .WriteFile (fileName , logCmd .StdOutBytes (), 0o644 )
811
+ if err != nil {
812
+ GinkgoWriter .Printf ("Failed to save logs:\n PodName: %s\n Error: %s\n " , pod .Name , err )
813
+ }
814
+
815
+ describeCmd := kubectl .RawCommand (fmt .Sprintf ("describe pod %s --namespace %s" , pod .Name , pod .Namespace ), framework .ShortTimeout )
816
+ if describeCmd .Error () != nil {
817
+ GinkgoWriter .Printf ("Failed to describe pod:\n PodName: %s\n Error: %s\n " , pod .Name , describeCmd .StdErr ())
818
+ }
819
+
820
+ fileName = fmt .Sprintf ("%s/e2e_failed__%s__%s__%s__describe" , dumpPath , labels ["testcase" ], additional , pod .Name )
821
+ err = os .WriteFile (fileName , describeCmd .StdOutBytes (), 0o644 )
822
+ if err != nil {
823
+ GinkgoWriter .Printf ("Failed to save pod description:\n PodName: %s\n Error: %s\n " , pod .Name , err )
824
+ }
825
+ }
826
+ }
827
+
778
828
type Watcher interface {
779
829
Watch (ctx context.Context , opts metav1.ListOptions ) (watch.Interface , error )
780
830
}
0 commit comments