Skip to content

Commit 8de2c5c

Browse files
test: group e2e dumps by testcase (#2358)
Signed-off-by: Yaroslav Borbat <yaroslav.borbat@flant.com>
1 parent 514b005 commit 8de2c5c

1 file changed

Lines changed: 45 additions & 38 deletions

File tree

test/e2e/internal/framework/dump.go

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"os"
24+
"path"
2425
"strings"
2526

2627
. "github.com/onsi/ginkgo/v2"
@@ -58,14 +59,21 @@ func truncateTestName(s string, maxLen int) string {
5859
func (f *Framework) saveTestCaseDump() {
5960
ft := GetFormattedTestCaseFullText()
6061
tmpDir := GetTMPDir()
62+
dumpDir := path.Join(tmpDir, ft)
6163

62-
f.saveTestCaseResources(ft, tmpDir)
63-
f.savePodAdditionalInfo(ft, tmpDir)
64-
f.saveIntvirtvmDescriptions(ft, tmpDir)
65-
f.saveIntvirtvmiDescriptions(ft, tmpDir)
66-
f.saveNodeAdditionalInfo(ft, tmpDir)
67-
f.saveEvents(ft, tmpDir)
68-
f.saveClusterNetworkInfo(ft, tmpDir)
64+
err := os.MkdirAll(dumpDir, 0o755)
65+
if err != nil {
66+
GinkgoWriter.Printf("Failed to create dump directory:\nDir: %s\nError: %v\n", dumpDir, err)
67+
return
68+
}
69+
70+
f.saveTestCaseResources(dumpDir)
71+
f.savePodAdditionalInfo(dumpDir)
72+
f.saveIntvirtvmDescriptions(dumpDir)
73+
f.saveIntvirtvmiDescriptions(dumpDir)
74+
f.saveNodeAdditionalInfo(dumpDir)
75+
f.saveEvents(dumpDir)
76+
f.saveClusterNetworkInfo(dumpDir)
6977
}
7078

7179
// GetFormattedTestCaseFullText returns CurrentSpecReport().FullText(), formatted with the following rules:
@@ -104,8 +112,8 @@ func GetTMPDir() string {
104112
return tmpDir
105113
}
106114

107-
func (f *Framework) saveTestCaseResources(testCaseFullText, dumpPath string) {
108-
resFileName := fmt.Sprintf("%s/e2e_failed__%s.yaml", dumpPath, testCaseFullText)
115+
func (f *Framework) saveTestCaseResources(dumpDir string) {
116+
resFileName := path.Join(dumpDir, "resources.yaml")
109117

110118
// TODO: Add CVI and VMC to the request when the environment is isolated.
111119
result := f.Clients.Kubectl().Get("virtualization,intvirt,pod,volumesnapshot,pvc", kubectl.GetOptions{
@@ -125,7 +133,7 @@ func (f *Framework) saveTestCaseResources(testCaseFullText, dumpPath string) {
125133
}
126134
}
127135

128-
func (f *Framework) savePodAdditionalInfo(testCaseFullText, dumpPath string) {
136+
func (f *Framework) savePodAdditionalInfo(dumpDir string) {
129137
pods, err := f.Clients.kubeClient.CoreV1().Pods(f.Namespace().Name).List(context.Background(), metav1.ListOptions{})
130138
if err != nil {
131139
GinkgoWriter.Printf("Failed to get PodList:\n%s\n", err)
@@ -138,39 +146,39 @@ func (f *Framework) savePodAdditionalInfo(testCaseFullText, dumpPath string) {
138146
}
139147

140148
for _, pod := range pods.Items {
141-
f.writePodLogs(pod.Name, pod.Namespace, dumpPath, testCaseFullText)
142-
f.writePodDescription(pod.Name, pod.Namespace, dumpPath, testCaseFullText)
143-
f.writeVirtualMachineGuestInfo(pod, dumpPath, testCaseFullText)
149+
f.writePodLogs(pod.Name, pod.Namespace, dumpDir)
150+
f.writePodDescription(pod.Name, pod.Namespace, dumpDir)
151+
f.writeVirtualMachineGuestInfo(pod, dumpDir)
144152
}
145153
}
146154

147-
func (f *Framework) saveIntvirtvmDescriptions(testCaseFullText, dumpPath string) {
155+
func (f *Framework) saveIntvirtvmDescriptions(dumpDir string) {
148156
describeCmd := f.Clients.Kubectl().RawCommand(fmt.Sprintf("describe intvirtvm --namespace %s", f.Namespace().Name), ShortTimeout)
149157
if describeCmd.Error() != nil {
150158
GinkgoWriter.Printf("Failed to describe InternalVirtualizationVirtualMachine:\nError: %s\n", describeCmd.StdErr())
151159
}
152160

153-
fileName := fmt.Sprintf("%s/e2e_failed__%s__intvirtvm_describe", dumpPath, testCaseFullText)
161+
fileName := path.Join(dumpDir, "intvirtvm_describe.log")
154162
err := os.WriteFile(fileName, describeCmd.StdOutBytes(), 0o644)
155163
if err != nil {
156164
GinkgoWriter.Printf("Failed to save InternalVirtualizationVirtualMachine description:\nError: %s\n", err)
157165
}
158166
}
159167

160-
func (f *Framework) saveIntvirtvmiDescriptions(testCaseFullText, dumpPath string) {
168+
func (f *Framework) saveIntvirtvmiDescriptions(dumpDir string) {
161169
describeCmd := f.Clients.Kubectl().RawCommand(fmt.Sprintf("describe intvirtvmi --namespace %s", f.Namespace().Name), ShortTimeout)
162170
if describeCmd.Error() != nil {
163171
GinkgoWriter.Printf("Failed to describe InternalVirtualizationVirtualMachineInstance:\nError: %s\n", describeCmd.StdErr())
164172
}
165173

166-
fileName := fmt.Sprintf("%s/e2e_failed__%s__intvirtvmi_describe", dumpPath, testCaseFullText)
174+
fileName := path.Join(dumpDir, "intvirtvmi_describe.log")
167175
err := os.WriteFile(fileName, describeCmd.StdOutBytes(), 0o644)
168176
if err != nil {
169177
GinkgoWriter.Printf("Failed to save InternalVirtualizationVirtualMachineInstance description:\nError: %s\n", err)
170178
}
171179
}
172180

173-
func (f *Framework) writePodLogs(name, namespace, filePath, testCaseFullText string) {
181+
func (f *Framework) writePodLogs(name, namespace, dumpDir string) {
174182
pod, err := f.Clients.kubeClient.CoreV1().Pods(namespace).Get(context.Background(), name, metav1.GetOptions{})
175183
if err != nil {
176184
GinkgoWriter.Printf("Failed to get pod:\nPodName: %s\nError: %v\n", name, err)
@@ -182,11 +190,11 @@ func (f *Framework) writePodLogs(name, namespace, filePath, testCaseFullText str
182190
GinkgoWriter.Printf("Skipping container without d8v prefix:\nPodName: %s\nContainer: %s\n", pod.Name, container.Name)
183191
continue
184192
}
185-
f.writePodContainerLogs(pod, container.Name, filePath, testCaseFullText)
193+
f.writePodContainerLogs(pod, container.Name, dumpDir)
186194
}
187195
}
188196

189-
func (f *Framework) writePodContainerLogs(pod *corev1.Pod, containerName, filePath, testCaseFullText string) {
197+
func (f *Framework) writePodContainerLogs(pod *corev1.Pod, containerName, dumpDir string) {
190198
podLogs, err := f.Clients.KubeClient().CoreV1().Pods(pod.Namespace).GetLogs(pod.Name,
191199
&corev1.PodLogOptions{
192200
Container: containerName,
@@ -203,35 +211,35 @@ func (f *Framework) writePodContainerLogs(pod *corev1.Pod, containerName, filePa
203211
return
204212
}
205213

206-
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__%s__logs.json", filePath, testCaseFullText, pod.Name, containerName)
214+
fileName := path.Join(dumpDir, fmt.Sprintf("pod_%s__%s_logs.json", pod.Name, containerName))
207215
err = os.WriteFile(fileName, logs, 0o644)
208216
if err != nil {
209217
GinkgoWriter.Printf("Failed to save logs:\nPodName: %s\nContainer: %s\nError: %v\n", pod.Name, containerName, err)
210218
}
211219
}
212220

213-
func (f *Framework) writePodDescription(name, namespace, filePath, testCaseFullText string) {
221+
func (f *Framework) writePodDescription(name, namespace, dumpDir string) {
214222
describeCmd := f.Clients.Kubectl().RawCommand(fmt.Sprintf("describe pod %s --namespace %s", name, namespace), ShortTimeout)
215223
if describeCmd.Error() != nil {
216224
GinkgoWriter.Printf("Failed to describe pod:\nPodName: %s\nError: %s\n", name, describeCmd.StdErr())
217225
}
218226

219-
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__describe", filePath, testCaseFullText, name)
227+
fileName := path.Join(dumpDir, fmt.Sprintf("pod_%s_describe.log", name))
220228
err := os.WriteFile(fileName, describeCmd.StdOutBytes(), 0o644)
221229
if err != nil {
222230
GinkgoWriter.Printf("Failed to save pod description:\nPodName: %s\nError: %v\n", name, err)
223231
}
224232
}
225233

226-
func (f *Framework) writeVirtualMachineGuestInfo(pod corev1.Pod, filePath, testCaseFullText string) {
234+
func (f *Framework) writeVirtualMachineGuestInfo(pod corev1.Pod, dumpDir string) {
227235
if pod.Labels != nil && pod.Status.Phase == corev1.PodRunning {
228236
if value, ok := pod.Labels["kubevirt.internal.virtualization.deckhouse.io"]; ok && value == "virt-launcher" {
229237
vlctlGuestInfoCmd := f.Clients.Kubectl().RawCommand(fmt.Sprintf("exec %s --namespace %s -- vlctl guest info", pod.Name, pod.Namespace), ShortTimeout)
230238
if vlctlGuestInfoCmd.Error() != nil {
231239
GinkgoWriter.Printf("Failed to get pod guest info:\nPodName: %s\nError: %s\n", pod.Name, vlctlGuestInfoCmd.StdErr())
232240
}
233241

234-
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__vlctl_guest_info", filePath, testCaseFullText, pod.Name)
242+
fileName := path.Join(dumpDir, fmt.Sprintf("pod_%s_vlctl_guest_info.log", pod.Name))
235243
err := os.WriteFile(fileName, vlctlGuestInfoCmd.StdOutBytes(), 0o644)
236244
if err != nil {
237245
GinkgoWriter.Printf("Failed to save pod guest info:\nPodName: %s\nError: %v\n", pod.Name, err)
@@ -240,22 +248,22 @@ func (f *Framework) writeVirtualMachineGuestInfo(pod corev1.Pod, filePath, testC
240248
}
241249
}
242250

243-
func (f *Framework) saveNodeAdditionalInfo(testCaseFullText, dumpPath string) {
251+
func (f *Framework) saveNodeAdditionalInfo(dumpDir string) {
244252
GinkgoHelper()
245253

246-
f.writeNodeDescription(testCaseFullText, dumpPath)
247-
f.writeNodeList(testCaseFullText, dumpPath)
254+
f.writeNodeDescription(dumpDir)
255+
f.writeNodeList(dumpDir)
248256
}
249257

250-
func (f *Framework) writeNodeDescription(testCaseFullText, dumpPath string) {
258+
func (f *Framework) writeNodeDescription(dumpDir string) {
251259
GinkgoHelper()
252260

253261
cmd := f.Clients.Kubectl().RawCommand("describe nodes", ShortTimeout)
254262
if cmd.Error() != nil {
255263
GinkgoWriter.Printf("Failed to run 'kubectl describe nodes':\nCmdError: %v\nStderr: %s\n", cmd.Error(), cmd.StdErr())
256264
}
257265

258-
fileName := fmt.Sprintf("%s/e2e_failed__%s__nodes_describe.log", dumpPath, testCaseFullText)
266+
fileName := path.Join(dumpDir, "nodes_describe.log")
259267
if len(cmd.StdOutBytes()) > 0 {
260268
err := os.WriteFile(fileName, cmd.StdOutBytes(), 0o644)
261269
if err != nil {
@@ -264,15 +272,15 @@ func (f *Framework) writeNodeDescription(testCaseFullText, dumpPath string) {
264272
}
265273
}
266274

267-
func (f *Framework) writeNodeList(testCaseFullText, dumpPath string) {
275+
func (f *Framework) writeNodeList(dumpDir string) {
268276
GinkgoHelper()
269277

270278
cmd := f.Clients.Kubectl().RawCommand("get nodes -o wide", ShortTimeout)
271279
if cmd.Error() != nil {
272280
GinkgoWriter.Printf("Failed to run 'kubectl get nodes -o wide':\nCmdError: %v\nStderr: %s\n", cmd.Error(), cmd.StdErr())
273281
}
274282

275-
fileName := fmt.Sprintf("%s/e2e_failed__%s__nodes_owide.log", dumpPath, testCaseFullText)
283+
fileName := path.Join(dumpDir, "nodes_owide.log")
276284
if len(cmd.StdOutBytes()) > 0 {
277285
err := os.WriteFile(fileName, cmd.StdOutBytes(), 0o644)
278286
if err != nil {
@@ -281,7 +289,7 @@ func (f *Framework) writeNodeList(testCaseFullText, dumpPath string) {
281289
}
282290
}
283291

284-
func (f *Framework) saveEvents(testCaseFullText, dumpPath string) {
292+
func (f *Framework) saveEvents(dumpDir string) {
285293
GinkgoHelper()
286294
namespace := f.Namespace().Name
287295
events, err := f.Clients.kubeClient.CoreV1().Events(namespace).List(context.Background(), metav1.ListOptions{})
@@ -290,7 +298,7 @@ func (f *Framework) saveEvents(testCaseFullText, dumpPath string) {
290298
return
291299
}
292300

293-
fileName := fmt.Sprintf("%s/e2e_failed__%s__%s__events.yaml", dumpPath, testCaseFullText, namespace)
301+
fileName := path.Join(dumpDir, fmt.Sprintf("events_%s.yaml", namespace))
294302
if len(events.Items) > 0 {
295303
data, err := yaml.Marshal(events)
296304
if err != nil {
@@ -305,11 +313,10 @@ func (f *Framework) saveEvents(testCaseFullText, dumpPath string) {
305313
}
306314
}
307315

308-
func (f *Framework) saveClusterNetworkInfo(testCaseFullText, dumpPath string) {
316+
func (f *Framework) saveClusterNetworkInfo(dumpDir string) {
309317
GinkgoHelper()
310318

311319
// Only for tests that use additional networks.
312-
// We use the original full text for checking because testCaseFullText may be truncated.
313320
if !strings.Contains(CurrentSpecReport().FullText(), "VirtualMachineAdditionalNetworkInterfaces") {
314321
return
315322
}
@@ -320,7 +327,7 @@ func (f *Framework) saveClusterNetworkInfo(testCaseFullText, dumpPath string) {
320327
GinkgoWriter.Printf("Failed to get clusternetwork:\nCmdError: %v\nStderr: %s\n", cmd.Error(), cmd.StdErr())
321328
}
322329

323-
fileName := fmt.Sprintf("%s/e2e_failed__%s__clusternetwork.yaml", dumpPath, testCaseFullText)
330+
fileName := path.Join(dumpDir, "clusternetwork.yaml")
324331
if len(cmd.StdOutBytes()) > 0 {
325332
err := os.WriteFile(fileName, cmd.StdOutBytes(), 0o644)
326333
if err != nil {
@@ -334,7 +341,7 @@ func (f *Framework) saveClusterNetworkInfo(testCaseFullText, dumpPath string) {
334341
GinkgoWriter.Printf("Failed to get cep:\nCmdError: %v\nStderr: %s\n", cepCmd.Error(), cepCmd.StdErr())
335342
}
336343

337-
cepFileName := fmt.Sprintf("%s/e2e_failed__%s__cep.yaml", dumpPath, testCaseFullText)
344+
cepFileName := path.Join(dumpDir, "cep.yaml")
338345
if len(cepCmd.StdOutBytes()) > 0 {
339346
err := os.WriteFile(cepFileName, cepCmd.StdOutBytes(), 0o644)
340347
if err != nil {

0 commit comments

Comments
 (0)