Skip to content

Commit b6160b7

Browse files
test: fix pod logs dumping (#1954)
Description Fix error logging in e2e failure dumps by replacing %w with %v in GinkgoWriter.Printf, so runtime logs render correctly instead of %!w(...). Improve container logging --------- Signed-off-by: Nikita Korolev <nikita.korolev@flant.com>
1 parent cd5e02a commit b6160b7

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

test/e2e/controller/err_checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (l *LogChecker) Start() error {
8585
// It should not cause tests to fail.
8686
var goAwayError *http2.GoAwayError
8787
if errors.As(err, &goAwayError) {
88-
ginkgo.GinkgoWriter.Printf("Warning! %w\n", err)
88+
ginkgo.GinkgoWriter.Printf("Warning! %v\n", err)
8989
} else {
9090
l.resultErr = errors.Join(l.resultErr, err)
9191
}

test/e2e/internal/framework/dump.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"github.com/deckhouse/virtualization/test/e2e/internal/kubectl"
3131
)
3232

33+
const d8vContainerPrefix = "d8v"
34+
3335
// SaveTestCaseDump dump some resources, logs and descriptions that may help in further diagnostic.
3436
//
3537
// NOTE: This method is called in AfterEach for failed specs only. Avoid to use Expect,
@@ -89,7 +91,7 @@ func (f *Framework) saveTestCaseResources(testCaseFullText, dumpPath string) {
8991
ShowManagedFields: true,
9092
})
9193
if result.Error() != nil {
92-
GinkgoWriter.Printf("Get resources error:\n%s\n%w\n%s\n", result.GetCmd(), result.Error(), result.StdErr())
94+
GinkgoWriter.Printf("Get resources error:\n%s\n%v\n%s\n", result.GetCmd(), result.Error(), result.StdErr())
9395
}
9496

9597
// Stdout may present even if error is occurred.
@@ -147,23 +149,42 @@ func (f *Framework) saveIntvirtvmiDescriptions(testCaseFullText, dumpPath string
147149
}
148150

149151
func (f *Framework) writePodLogs(name, namespace, filePath, testCaseFullText string) {
150-
podLogs, err := f.Clients.KubeClient().CoreV1().Pods(namespace).GetLogs(name, &corev1.PodLogOptions{}).Stream(context.Background())
152+
pod, err := f.Clients.kubeClient.CoreV1().Pods(namespace).Get(context.Background(), name, metav1.GetOptions{})
153+
if err != nil {
154+
GinkgoWriter.Printf("Failed to get pod:\nPodName: %s\nError: %v\n", name, err)
155+
return
156+
}
157+
158+
for _, container := range pod.Spec.Containers {
159+
if !strings.HasPrefix(container.Name, d8vContainerPrefix) {
160+
GinkgoWriter.Printf("Skipping container without d8v prefix:\nPodName: %s\nContainer: %s\n", pod.Name, container.Name)
161+
continue
162+
}
163+
f.writePodContainerLogs(pod, container.Name, filePath, testCaseFullText)
164+
}
165+
}
166+
167+
func (f *Framework) writePodContainerLogs(pod *corev1.Pod, containerName, filePath, testCaseFullText string) {
168+
podLogs, err := f.Clients.KubeClient().CoreV1().Pods(pod.Namespace).GetLogs(pod.Name,
169+
&corev1.PodLogOptions{
170+
Container: containerName,
171+
}).Stream(context.Background())
151172
if err != nil {
152-
GinkgoWriter.Printf("Failed to get logs:\nPodName: %s\nError: %w\n", name, err)
173+
GinkgoWriter.Printf("Failed to get logs:\nPodName: %s\nContainer: %s\nError: %v\n", pod.Name, containerName, err)
153174
return
154175
}
155176
defer podLogs.Close()
156177

157178
logs, err := io.ReadAll(podLogs)
158179
if err != nil {
159-
GinkgoWriter.Printf("Failed to read logs:\nPodName: %s\nError: %w\n", name, err)
180+
GinkgoWriter.Printf("Failed to read logs:\nPodName: %s\nContainer: %s\nError: %v\n", pod.Name, containerName, err)
160181
return
161182
}
162183

163-
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__logs.json", filePath, testCaseFullText, name)
184+
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__%s__logs.json", filePath, testCaseFullText, pod.Name, containerName)
164185
err = os.WriteFile(fileName, logs, 0o644)
165186
if err != nil {
166-
GinkgoWriter.Printf("Failed to save logs:\nPodName: %s\nError: %w\n", name, err)
187+
GinkgoWriter.Printf("Failed to save logs:\nPodName: %s\nContainer: %s\nError: %v\n", pod.Name, containerName, err)
167188
}
168189
}
169190

@@ -176,7 +197,7 @@ func (f *Framework) writePodDescription(name, namespace, filePath, testCaseFullT
176197
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__describe", filePath, testCaseFullText, name)
177198
err := os.WriteFile(fileName, describeCmd.StdOutBytes(), 0o644)
178199
if err != nil {
179-
GinkgoWriter.Printf("Failed to save pod description:\nPodName: %s\nError: %w\n", name, err)
200+
GinkgoWriter.Printf("Failed to save pod description:\nPodName: %s\nError: %v\n", name, err)
180201
}
181202
}
182203

@@ -191,7 +212,7 @@ func (f *Framework) writeVirtualMachineGuestInfo(pod corev1.Pod, filePath, testC
191212
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__vlctl_guest_info", filePath, testCaseFullText, pod.Name)
192213
err := os.WriteFile(fileName, vlctlGuestInfoCmd.StdOutBytes(), 0o644)
193214
if err != nil {
194-
GinkgoWriter.Printf("Failed to save pod guest info:\nPodName: %s\nError: %w\n", pod.Name, err)
215+
GinkgoWriter.Printf("Failed to save pod guest info:\nPodName: %s\nError: %v\n", pod.Name, err)
195216
}
196217
}
197218
}

test/e2e/legacy/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ func SaveTestCaseResources(labels map[string]string, additional, namespace, dump
674674
ShowManagedFields: true,
675675
})
676676
if clusterResourceResult.Error() != nil {
677-
GinkgoWriter.Printf("Get resources error:\n%s\n%w\n%s\n", clusterResourceResult.GetCmd(), clusterResourceResult.Error(), clusterResourceResult.StdErr())
677+
GinkgoWriter.Printf("Get resources error:\n%s\n%v\n%s\n", clusterResourceResult.GetCmd(), clusterResourceResult.Error(), clusterResourceResult.StdErr())
678678
}
679679

680680
namespacedResourceResult := kubectl.Get("virtualization,intvirt,pod,volumesnapshot,pvc", kc.GetOptions{
@@ -683,7 +683,7 @@ func SaveTestCaseResources(labels map[string]string, additional, namespace, dump
683683
ShowManagedFields: true,
684684
})
685685
if namespacedResourceResult.Error() != nil {
686-
GinkgoWriter.Printf("Get resources error:\n%s\n%w\n%s\n", namespacedResourceResult.GetCmd(), namespacedResourceResult.Error(), namespacedResourceResult.StdErr())
686+
GinkgoWriter.Printf("Get resources error:\n%s\n%v\n%s\n", namespacedResourceResult.GetCmd(), namespacedResourceResult.Error(), namespacedResourceResult.StdErr())
687687
}
688688

689689
// Stdout may present even if error is occurred.

0 commit comments

Comments
 (0)