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

Commit aa8b20a

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

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

test/e2e/framework/framework.go

Lines changed: 82 additions & 2 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"
@@ -59,6 +61,8 @@ type Framework struct {
5961
// we install a Cleanup action before each test and clear it after. If we
6062
// should abort, the AfterSuite hook should run all Cleanup actions.
6163
cleanupHandle CleanupActionHandle
64+
65+
werckerReportArtifactsDir string
6266
}
6367

6468
// NewDefaultFramework constructs a new e2e test Framework with default options.
@@ -70,8 +74,9 @@ func NewDefaultFramework(baseName string) *Framework {
7074
// NewFramework constructs a new e2e test Framework.
7175
func NewFramework(baseName string, client clientset.Interface) *Framework {
7276
f := &Framework{
73-
BaseName: baseName,
74-
ClientSet: client,
77+
BaseName: baseName,
78+
ClientSet: client,
79+
werckerReportArtifactsDir: os.Getenv("WERCKER_REPORT_ARTIFACTS_DIR"),
7580
}
7681

7782
BeforeEach(f.BeforeEach)
@@ -248,6 +253,10 @@ func (f *Framework) BeforeEach() {
248253
func (f *Framework) AfterEach() {
249254
RemoveCleanupAction(f.cleanupHandle)
250255

256+
if err := f.outputLogs(); err != nil {
257+
Logf("Failed to output container logs: %v", err)
258+
}
259+
251260
nsDeletionErrors := map[string]error{}
252261

253262
// Whether to delete namespace is determined by 3 factors: delete-namespace flag, delete-namespace-on-failure flag and the test result
@@ -272,3 +281,74 @@ func (f *Framework) AfterEach() {
272281
}
273282
f.OperatorInstalled = false
274283
}
284+
285+
func (f *Framework) outputLogs() error {
286+
pods, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
287+
if err != nil {
288+
return errors.Wrap(err, "listing test Pods")
289+
}
290+
291+
var opPod v1.Pod
292+
var agPods []v1.Pod
293+
for _, pod := range pods.Items {
294+
if strings.Contains(pod.Spec.Containers[0].Image, "mysql-operator") {
295+
opPod = pod
296+
continue
297+
}
298+
if strings.Contains(pod.Spec.Containers[0].Image, "mysql-agent") || strings.Contains(pod.Spec.Containers[0].Image, "mysql-server") {
299+
agPods = append(agPods, pod)
300+
}
301+
}
302+
303+
// Operator Logs
304+
if opPod.Name != "" {
305+
f.printContainerLogs(opPod.GetName(), &v1.PodLogOptions{})
306+
}
307+
308+
for _, agPod := range agPods {
309+
// Server Logs
310+
f.printContainerLogs(agPod.GetName(), &v1.PodLogOptions{Container: "mysql"})
311+
// Agent Logs
312+
f.printContainerLogs(agPod.GetName(), &v1.PodLogOptions{Container: "mysql-agent"})
313+
}
314+
315+
return nil
316+
}
317+
318+
func printLogs(read io.ReadCloser, filepath string) error {
319+
defer read.Close()
320+
dst := os.Stdout
321+
if filepath != "" {
322+
file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)
323+
if err != nil {
324+
return errors.Wrapf(err, "opening log file %q", filepath)
325+
}
326+
dst = file
327+
defer dst.Close()
328+
}
329+
_, err := io.Copy(dst, read)
330+
if err != nil {
331+
var s string
332+
if filepath != "" {
333+
s = filepath
334+
} else {
335+
s = "stdout"
336+
}
337+
return errors.Wrapf(err, "writing logs to %q", s)
338+
}
339+
return nil
340+
}
341+
342+
func (f *Framework) printContainerLogs(podName string, options *v1.PodLogOptions) error {
343+
podLogs := f.ClientSet.CoreV1().Pods(f.Namespace.Name).GetLogs(podName, options)
344+
if podLogs != nil {
345+
Logf("Writing %s container logs to file for %s", options.Container, podName)
346+
read, err := podLogs.Stream()
347+
if err != nil {
348+
return errors.Wrapf(err, "streaming request response for %s", podName)
349+
}
350+
printLogs(read, podName)
351+
Logf("Finished writing %s container logs for %s", options.Container, podName)
352+
}
353+
return nil
354+
}

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)