Skip to content

Commit a8a5369

Browse files
Merge pull request #2381 from Nordix/lentzi90/e2e-logging
🌱 E2E: Unify logging and cleanup
2 parents 9ab76ac + 36c0476 commit a8a5369

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

test/e2e/common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ func GenerateIPPoolPreallocations(ctx context.Context, ippool ipamv1.IPPool, poo
561561
Expect(c.List(ctx, &m3MachineList, &client.ListOptions{})).To(Succeed())
562562
newAllocations := make(map[string]ipamv1.IPAddressStr)
563563
for m3dataPoolName, ipaddress := range allocations {
564-
fmt.Println("datapoolName:", m3dataPoolName, "=>", "ipaddress:", ipaddress)
564+
Logf("datapoolName:", m3dataPoolName, "=>", "ipaddress:", ipaddress)
565565
BMHName := strings.Split(m3dataPoolName, "-"+poolName)[0]
566566
Logf("poolName: %s", poolName)
567567
Logf("BMHName: %s", BMHName)
@@ -950,10 +950,10 @@ func KubectlDelete(ctx context.Context, kubeconfigPath string, resources []byte,
950950
testexec.WithStdin(rbytes),
951951
)
952952

953-
fmt.Printf("Running kubectl %s\n", strings.Join(aargs, " "))
953+
Logf("Running kubectl %s\n", strings.Join(aargs, " "))
954954
stdout, stderr, err := deleteCmd.Run(ctx)
955-
fmt.Printf("stderr:\n%s\n", string(stderr))
956-
fmt.Printf("stdout:\n%s\n", string(stdout))
955+
Logf("stderr:\n%s", string(stderr))
956+
Logf("stdout:\n%s", string(stdout))
957957
return err
958958
}
959959

test/e2e/logcollector.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func FetchManifests(clusterProxy framework.ClusterProxy, outputPath string) erro
184184
}
185185
}
186186
}
187-
fmt.Printf("Successfully collected manifests for cluster %s.", clusterProxy.GetName())
187+
Logf("Successfully collected manifests for cluster %s.", clusterProxy.GetName())
188188
return nil
189189
}
190190

@@ -244,7 +244,7 @@ func FetchClusterLogs(clusterProxy framework.ClusterProxy, outputPath string) er
244244
// Get all pods in the namespace
245245
pods, err := clientset.CoreV1().Pods(namespace.Name).List(ctx, metav1.ListOptions{})
246246
if err != nil {
247-
fmt.Printf("couldn't list pods in namespace %s: %v", namespace.Name, err)
247+
Logf("couldn't list pods in namespace %s: %v", namespace.Name, err)
248248
continue
249249
}
250250
for _, pod := range pods.Items {
@@ -259,13 +259,16 @@ func FetchClusterLogs(clusterProxy framework.ClusterProxy, outputPath string) er
259259
}
260260
podDescription, err := podDescriber.Describe(namespace.Name, pod.Name, describerSettings)
261261
if err != nil {
262-
fmt.Printf("couldn't describe pod %s in namespace %s: %v", pod.Name, namespace.Name, err)
262+
Logf("couldn't describe pod %s in namespace %s: %v", pod.Name, namespace.Name, err)
263263
continue
264264
}
265265

266266
machineName := pod.Spec.NodeName
267267
podDir := filepath.Join(baseDir, "machines", machineName, namespace.Name, pod.Name)
268-
writeToFile([]byte(podDescription), "stdout_describe.log", podDir)
268+
err = writeToFile([]byte(podDescription), "stdout_describe.log", podDir)
269+
if err != nil {
270+
return fmt.Errorf("couldn't write to file: %v", err)
271+
}
269272

270273
// Get containers of the Pod
271274
for _, container := range pod.Spec.Containers {
@@ -274,13 +277,13 @@ func FetchClusterLogs(clusterProxy framework.ClusterProxy, outputPath string) er
274277

275278
err := CollectContainerLogs(ctx, namespace.Name, pod.Name, container.Name, clientset, containerDir)
276279
if err != nil {
277-
fmt.Printf("Error %v.", err)
280+
Logf("Error %v.", err)
278281
continue
279282
}
280283
}
281284
}
282285
}
283-
fmt.Printf("Successfully collected logs for cluster %s.", clusterProxy.GetName())
286+
Logf("Successfully collected logs for cluster %s.", clusterProxy.GetName())
284287
return nil
285288
}
286289

@@ -307,25 +310,29 @@ func CollectContainerLogs(ctx context.Context, namespace string, podName string,
307310
}
308311
podStr := buf.String()
309312

310-
writeToFile([]byte(podStr), "stdout.log", outputPath)
313+
err = writeToFile([]byte(podStr), "stdout.log", outputPath)
314+
if err != nil {
315+
return fmt.Errorf("couldn't write to file: %v", err)
316+
}
311317

312318
return nil
313319
}
314320

315321
// writeToFile writes content to a file,
316322
// creating any missing directories in the path.
317-
func writeToFile(content []byte, fileName string, filePath string) {
323+
func writeToFile(content []byte, fileName string, filePath string) error {
318324
// Create any missing directories in the path
319325
err := os.MkdirAll(filePath, 0775)
320326
if err != nil {
321-
fmt.Printf("couldn't create directory: %v", err)
327+
return fmt.Errorf("couldn't create directory: %v", err)
322328
}
323329
// Write content to file
324330
file := filepath.Join(filePath, fileName)
325331
err = os.WriteFile(file, content, 0600)
326332
if err != nil {
327-
fmt.Printf("couldn't write to file: %v", err)
333+
return fmt.Errorf("couldn't write to file: %v", err)
328334
}
335+
return nil
329336
}
330337

331338
// crdIsInList checks if a CustomResourceDefinition is in the provided list of
@@ -370,7 +377,10 @@ func DumpGVR(ctx context.Context, dynamicClient *dynamic.DynamicClient, gvr sche
370377
if err != nil {
371378
return fmt.Errorf("could not marshal content: %v", err)
372379
}
373-
writeToFile(content, fileName, filePath)
380+
err = writeToFile(content, fileName, filePath)
381+
if err != nil {
382+
return fmt.Errorf("couldn't write to file: %v", err)
383+
}
374384
}
375385
return nil
376386
}

test/e2e/pivoting.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func pivoting(ctx context.Context, inputGetter func() PivotingInput) {
9292
By("Fetch manifest for bootstrap cluster before pivot")
9393
err = FetchManifests(input.BootstrapClusterProxy, "/tmp/manifests/")
9494
if err != nil {
95-
fmt.Printf("Error fetching manifests for bootstrap cluster before pivot: %v\n", err)
95+
Logf("Error fetching manifests for bootstrap cluster before pivot: %v", err)
9696
}
9797
By("Fetch target cluster kubeconfig for target cluster log collection")
9898
kconfigPathWorkload := input.TargetCluster.GetKubeconfigPath()
@@ -377,7 +377,7 @@ func rePivoting(ctx context.Context, inputGetter func() RePivotingInput) {
377377
workloadClusterProxy := framework.NewClusterProxy("workload-cluster-after-pivot", os.Getenv("KUBECONFIG"), runtime.NewScheme())
378378
err = FetchManifests(workloadClusterProxy, "/tmp/manifests/")
379379
if err != nil {
380-
fmt.Printf("Error fetching manifests for workload cluster after pivot: %v\n", err)
380+
Logf("Error fetching manifests for workload cluster after pivot: %v", err)
381381
}
382382
os.Unsetenv("KUBECONFIG_WORKLOAD")
383383

@@ -401,7 +401,7 @@ func rePivoting(ctx context.Context, inputGetter func() RePivotingInput) {
401401
//#nosec G204:gosec
402402
cmd := exec.Command("sh", "-c", "export CONTAINER_RUNTIME=docker; "+ironicCommand)
403403
stdoutStderr, err := cmd.CombinedOutput()
404-
fmt.Printf("%s\n", stdoutStderr)
404+
Logf("Output: %s", stdoutStderr)
405405
Expect(err).ToNot(HaveOccurred(), "Cannot run local ironic")
406406
} else {
407407
By("Install Ironic in the bootstrap cluster")
@@ -498,7 +498,7 @@ func rePivoting(ctx context.Context, inputGetter func() RePivotingInput) {
498498
By("Fetch manifest for bootstrap cluster after re-pivot")
499499
err = FetchManifests(input.BootstrapClusterProxy, "/tmp/manifests/")
500500
if err != nil {
501-
fmt.Printf("Error fetching manifests for bootstrap cluster before pivot: %v\n", err)
501+
Logf("Error fetching manifests for bootstrap cluster before pivot: %v", err)
502502
}
503503
os.Unsetenv("KUBECONFIG_BOOTSTRAP")
504504

test/e2e/upgrade_clusterctl_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func preInitFunc(clusterProxy framework.ClusterProxy, bmoRelease string, ironicR
210210
By("Fetch manifest for bootstrap cluster")
211211
err := FetchManifests(clusterProxy, "/tmp/manifests/")
212212
if err != nil {
213-
fmt.Printf("Error fetching manifests for bootstrap cluster: %v\n", err)
213+
Logf("Error fetching manifests for bootstrap cluster: %v", err)
214214
}
215215

216216
By("Fetch target cluster kubeconfig for target cluster log collection")
@@ -220,8 +220,7 @@ func preInitFunc(clusterProxy framework.ClusterProxy, bmoRelease string, ironicR
220220
kubeconfigPathTemp := "/tmp/kubeconfig-test1.yaml"
221221
cmd := exec.Command("cp", kconfigPathWorkload, kubeconfigPathTemp) // #nosec G204:gosec
222222
stdoutStderr, er := cmd.CombinedOutput()
223-
Logf("%s\n", stdoutStderr)
224-
Expect(er).ToNot(HaveOccurred(), "Cannot fetch target cluster kubeconfig")
223+
Expect(er).ToNot(HaveOccurred(), "Cannot fetch target cluster kubeconfig: %s", stdoutStderr)
225224
// install certmanager
226225
installCertManager(clusterProxy)
227226
// Remove ironic
@@ -364,7 +363,7 @@ func preCleanupManagementCluster(clusterProxy framework.ClusterProxy, ironicRele
364363
//#nosec G204 -- We take the BMOPATH from a variable.
365364
cmd := exec.Command("sh", "-c", "export CONTAINER_RUNTIME=docker; "+ironicCommand)
366365
stdoutStderr, err := cmd.CombinedOutput()
367-
fmt.Printf("%s\n", stdoutStderr)
366+
Logf("Output: %s", stdoutStderr)
368367
Expect(err).ToNot(HaveOccurred(), "Cannot run local ironic")
369368
} else {
370369
By("Install Ironic in the source cluster as deployments")

0 commit comments

Comments
 (0)