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

Export logs as wercker artifacts and delete e2e namespace on test failure #188

Merged
merged 1 commit into from
Aug 2, 2018

Conversation

harveylowndes
Copy link

Closes #177

MySQL Agent, Server and Operator logs will be exported as wercker artifacts on a per pod basis. If tests are run locally they are printed to stdout. E2E test namespace deletion follows regardless of whether the test passes or fails.

@harveylowndes harveylowndes changed the title WIP: Export logs as wercker artifacts and delete e2e namespace on test failure Export logs as wercker artifacts and delete e2e namespace on test failure Jul 19, 2018
@harveylowndes harveylowndes force-pushed the delete-e2e-namespace-on-failure branch 2 times, most recently from 67a194f to 8672523 Compare July 20, 2018 08:55
Copy link

@simonlord simonlord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple improvements/tweaks i've suggested. You could also do with a unit test or two if it's easy

@@ -248,6 +250,8 @@ func (f *Framework) BeforeEach() {
func (f *Framework) AfterEach() {
RemoveCleanupAction(f.cleanupHandle)

printMySQLPodContainerLogs(f)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a more accurate name. It's not printing, mysql pod container logs, it's retrieving, storing, dumping all container logs really...

func printMySQLPodContainerLogs(f *Framework) {
pods, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
if err != nil {
Logf("Error retrieving pod list in namespace %s: %+v", f.Namespace.Name, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we bomb out here, return an error or something instead of logging and continuing

}

// Operator Logs
if opPod.Name != "" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need else block to handle opPod.Name being empty?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonlord Good point. I have just logged a message here as there is no need for a fail.


func printLogs(read io.ReadCloser, fileName string) error {
defer read.Close()
env := os.Getenv("WERCKER_REPORT_ARTIFACTS_DIR")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a better name

env := os.Getenv("WERCKER_REPORT_ARTIFACTS_DIR")
dst := os.Stdout
if env != "" {
filepath := env + "/" + fileName + ".log"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to use os.Separator here instead of "/", maybe use a fmt.Sprintf too

podLogs := f.ClientSet.CoreV1().Pods(f.Namespace.Name).GetLogs(podName, options)
if podLogs != nil {
Logf("Writing %s container logs to file for %s", options.Container, podName)
read, _ := podLogs.Stream()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is _ in this case? If it's an error we can't ignore it...

@@ -272,3 +276,64 @@ func (f *Framework) AfterEach() {
}
f.OperatorInstalled = false
}

func printMySQLPodContainerLogs(f *Framework) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 func (f *Framework) outputLogs() error

func printMySQLPodContainerLogs(f *Framework) {
pods, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
if err != nil {
Logf("Error retrieving pod list in namespace %s: %+v", f.Namespace.Name, err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return errors.Wrap(err, "listing test Pods")

(otherwise pods.Items will panic below if the above fails)

filepath := env + "/" + fileName + ".log"
file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return fmt.Errorf("Error getting file for logs: %+v", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return errors.Wrapf(err, "opening log file %q", filepath)

}
}

func printLogs(read io.ReadCloser, fileName string) error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass in full file path and load os.Getenv("WERCKER_REPORT_ARTIFACTS_DIR") on Framework initialisation

env := os.Getenv("WERCKER_REPORT_ARTIFACTS_DIR")
dst := os.Stdout
if env != "" {
filepath := env + "/" + fileName + ".log"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs moving as per above but:

filePath := path.Join(env, fmt.Sprintf("%s.log", fileName))

}
_, err := io.Copy(dst, read)
if err != nil {
return fmt.Errorf("Error exporting logs: %+v", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return errors.Wrapf(err, "writing logs to %q", filepath)

@@ -248,6 +250,8 @@ func (f *Framework) BeforeEach() {
func (f *Framework) AfterEach() {
RemoveCleanupAction(f.cleanupHandle)

printMySQLPodContainerLogs(f)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err := f.outputLogs(); err != nil {
	Logf("Failed to output container logs: %v", err)
}

@harveylowndes harveylowndes force-pushed the delete-e2e-namespace-on-failure branch 2 times, most recently from aa8b20a to 031fcaf Compare July 20, 2018 13:53
@harveylowndes harveylowndes force-pushed the delete-e2e-namespace-on-failure branch from 031fcaf to 631c5bc Compare July 20, 2018 13:59
Copy link

@prydie prydie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@prydie prydie merged commit 92fa624 into master Aug 2, 2018
@prydie prydie deleted the delete-e2e-namespace-on-failure branch August 2, 2018 13:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants