Skip to content
This repository was archived by the owner on May 28, 2021. It is now read-only.

Commit 8672523

Browse files
author
Harvey Lowndes
committed
Export mysql container logs and delete e2e namespace on test failure
1 parent 0872fec commit 8672523

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

test/e2e/framework/framework.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package framework
1616

1717
import (
1818
"fmt"
19+
"io"
20+
"os"
1921
"os/exec"
2022
"path/filepath"
2123
"strings"
@@ -248,6 +250,8 @@ func (f *Framework) BeforeEach() {
248250
func (f *Framework) AfterEach() {
249251
RemoveCleanupAction(f.cleanupHandle)
250252

253+
printMySQLPodContainerLogs(f)
254+
251255
nsDeletionErrors := map[string]error{}
252256

253257
// Whether to delete namespace is determined by 3 factors: delete-namespace flag, delete-namespace-on-failure flag and the test result
@@ -272,3 +276,64 @@ func (f *Framework) AfterEach() {
272276
}
273277
f.OperatorInstalled = false
274278
}
279+
280+
func printMySQLPodContainerLogs(f *Framework) {
281+
pods, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
282+
if err != nil {
283+
Logf("Error retrieving pod list in namespace %s: %+v", f.Namespace.Name, err)
284+
}
285+
286+
var opPod v1.Pod
287+
var agPods []v1.Pod
288+
for _, pod := range pods.Items {
289+
if strings.Contains(pod.Spec.Containers[0].Image, "mysql-operator") {
290+
opPod = pod
291+
continue
292+
}
293+
if strings.Contains(pod.Spec.Containers[0].Image, "mysql-agent") || strings.Contains(pod.Spec.Containers[0].Image, "mysql-server") {
294+
agPods = append(agPods, pod)
295+
}
296+
}
297+
298+
// Operator Logs
299+
if opPod.Name != "" {
300+
printContainerLogs(f, opPod.GetName(), &v1.PodLogOptions{})
301+
}
302+
303+
for _, agPod := range agPods {
304+
// Server Logs
305+
printContainerLogs(f, agPod.GetName(), &v1.PodLogOptions{Container: "mysql"})
306+
// Agent Logs
307+
printContainerLogs(f, agPod.GetName(), &v1.PodLogOptions{Container: "mysql-agent"})
308+
}
309+
}
310+
311+
func printLogs(read io.ReadCloser, fileName string) error {
312+
defer read.Close()
313+
env := os.Getenv("WERCKER_REPORT_ARTIFACTS_DIR")
314+
dst := os.Stdout
315+
if env != "" {
316+
filepath := env + "/" + fileName + ".log"
317+
file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)
318+
if err != nil {
319+
return fmt.Errorf("Error getting file for logs: %+v", err)
320+
}
321+
dst = file
322+
defer dst.Close()
323+
}
324+
_, err := io.Copy(dst, read)
325+
if err != nil {
326+
return fmt.Errorf("Error exporting logs: %+v", err)
327+
}
328+
return nil
329+
}
330+
331+
func printContainerLogs(f *Framework, podName string, options *v1.PodLogOptions) {
332+
podLogs := f.ClientSet.CoreV1().Pods(f.Namespace.Name).GetLogs(podName, options)
333+
if podLogs != nil {
334+
Logf("Writing %s container logs to file for %s", options.Container, podName)
335+
read, _ := podLogs.Stream()
336+
printLogs(read, podName)
337+
Logf("Finished writing %s container logs for %s", options.Container, podName)
338+
}
339+
}

wercker.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ e2e-test:
155155
--operator-version="$(cat dist/version.txt)" \
156156
--s3-access-key="${S3_ACCESS_KEY}" \
157157
--s3-secret-key="${S3_SECRET_KEY}" \
158-
--delete-namespace-on-failure=false
159158
160159
release:
161160
box:

0 commit comments

Comments
 (0)