Skip to content

Commit 7f0014c

Browse files
hardcoretimeIsteb4k
authored andcommitted
test: add pods and descriptions dump (#1519)
This is required to get more information about the state of test case resources when a test fails. Signed-off-by: Roman Sysoev <[email protected]> (cherry picked from commit 0ed00cf)
1 parent 0a94bba commit 7f0014c

21 files changed

+88
-29
lines changed

tests/e2e/affinity_toleration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var _ = Describe("VirtualMachineAffinityAndToleration", framework.CommonE2ETestD
7171

7272
AfterEach(func() {
7373
if CurrentSpecReport().Failed() {
74-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
74+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
7575
}
7676
})
7777

tests/e2e/complex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), fun
4444

4545
AfterEach(func() {
4646
if CurrentSpecReport().Failed() {
47-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
47+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
4848
}
4949
})
5050

tests/e2e/image_hotplug_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var _ = Describe("ImageHotplug", framework.CommonE2ETestDecorators(), func() {
6969

7070
AfterEach(func() {
7171
if CurrentSpecReport().Failed() {
72-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
72+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
7373
}
7474
})
7575

tests/e2e/images_creation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var _ = Describe("VirtualImageCreation", framework.CommonE2ETestDecorators(), fu
6969

7070
AfterEach(func() {
7171
if CurrentSpecReport().Failed() {
72-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
72+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
7373
}
7474
})
7575

tests/e2e/importer_network_policy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var _ = Describe("ImporterNetworkPolicy", framework.CommonE2ETestDecorators(), f
5353

5454
AfterEach(func() {
5555
if CurrentSpecReport().Failed() {
56-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
56+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
5757
}
5858
})
5959

tests/e2e/kubectl/kubectl.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ type DeleteOptions struct {
9393
}
9494

9595
type GetOptions struct {
96-
ExcludedLabels []string
97-
IgnoreNotFound bool
98-
Labels map[string]string
99-
Namespace string
100-
Output string
96+
ExcludedLabels []string
97+
IgnoreNotFound bool
98+
Labels map[string]string
99+
Namespace string
100+
Output string
101+
ShowManagedFields bool
101102
}
102103

103104
type LogOptions struct {
@@ -368,6 +369,13 @@ func (k KubectlCMD) addFollow(cmd string, follow bool) string {
368369
return cmd
369370
}
370371

372+
func (k KubectlCMD) addShowManagedFields(cmd string, showManagedFields bool) string {
373+
if showManagedFields {
374+
return fmt.Sprintf("%s --show-managed-fields=true", cmd)
375+
}
376+
return cmd
377+
}
378+
371379
func (k KubectlCMD) applyOptions(cmd string, opts ApplyOptions) string {
372380
var resourceEmptyValue Resource = ""
373381
cmd = k.addFilenameOptions(cmd, resourceEmptyValue, opts.FilenameOption, opts.Recursive, opts.Filename...)
@@ -387,6 +395,7 @@ func (k KubectlCMD) getOptions(cmd string, opts GetOptions) string {
387395
cmd = k.addOutput(cmd, opts.Output)
388396
cmd = k.addIgnoreNotFound(cmd, opts.IgnoreNotFound)
389397
cmd = k.addLabels(cmd, opts.Labels, opts.ExcludedLabels)
398+
cmd = k.addShowManagedFields(cmd, opts.ShowManagedFields)
390399
return cmd
391400
}
392401

tests/e2e/sizing_policy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var _ = Describe("SizingPolicy", framework.CommonE2ETestDecorators(), func() {
7272

7373
AfterEach(func() {
7474
if CurrentSpecReport().Failed() {
75-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
75+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
7676
}
7777
})
7878

tests/e2e/util_test.go

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,11 @@ func IsContainsLabelWithValue(obj client.Object, label, value string) bool {
731731
return ok && val == value
732732
}
733733

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.
735735
//
736736
// NOTE: This method is called in AfterEach for failed specs only. Avoid to use Expect,
737737
// 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) {
739739
replacer := strings.NewReplacer(
740740
" ", "_",
741741
":", "_",
@@ -753,10 +753,24 @@ func SaveTestResources(labels map[string]string, additional string) {
753753
if tmpDir == "" {
754754
tmpDir = "/tmp"
755755
}
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)
758756

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+
)
760774
if cmdr.Error() != nil {
761775
errReport := fmt.Sprintf("cmd: %s\nerror: %s\nstderr: %s\n", cmdr.GetCmd(), cmdr.Error(), cmdr.StdErr())
762776
GinkgoWriter.Printf("Get resources error:\n%s\n", errReport)
@@ -775,6 +789,42 @@ func SaveTestResources(labels map[string]string, additional string) {
775789
}
776790
}
777791

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:\nPodName: %s\nError: %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:\nPodName: %s\nError: %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:\nPodName: %s\nError: %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:\nPodName: %s\nError: %s\n", pod.Name, err)
824+
}
825+
}
826+
}
827+
778828
type Watcher interface {
779829
Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
780830
}

tests/e2e/vd_snapshots_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var _ = Describe("VirtualDiskSnapshots", framework.CommonE2ETestDecorators(), fu
7777

7878
AfterEach(func() {
7979
if CurrentSpecReport().Failed() {
80-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
80+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
8181
}
8282
})
8383

tests/e2e/vm_configuration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var _ = Describe(fmt.Sprintf("VirtualMachineConfiguration %d", GinkgoParallelPro
5757

5858
AfterEach(func() {
5959
if CurrentSpecReport().Failed() {
60-
SaveTestResources(testCaseLabel, CurrentSpecReport().LeafNodeText)
60+
SaveTestCaseDump(testCaseLabel, CurrentSpecReport().LeafNodeText, ns)
6161
}
6262
})
6363

0 commit comments

Comments
 (0)