From fd25762c059e72b74ae57c0270d9ac8c8f76cefb Mon Sep 17 00:00:00 2001 From: Jean-Laurent de Morlhon Date: Thu, 17 Dec 2015 13:42:09 +0100 Subject: [PATCH] Add vbox.log to crashreport Signed-off-by: Jean-Laurent de Morlhon --- libmachine/crashreport/crash_report.go | 25 +++++++++++-- libmachine/crashreport/crash_report_test.go | 39 +++++++++++++++++++++ libmachine/libmachine.go | 11 +++++- 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 libmachine/crashreport/crash_report_test.go diff --git a/libmachine/crashreport/crash_report.go b/libmachine/crashreport/crash_report.go index 0485258fc4..283d581fd9 100644 --- a/libmachine/crashreport/crash_report.go +++ b/libmachine/crashreport/crash_report.go @@ -13,6 +13,8 @@ import ( "errors" + "io/ioutil" + "github.com/bugsnag/bugsnag-go" "github.com/docker/machine/commands/mcndirs" "github.com/docker/machine/libmachine/log" @@ -35,8 +37,7 @@ func Configure(key string) { } } -// Send through http the crash report to bugsnag need a call to Configure(apiKey) before -func Send(err error, context string, driverName string, command string) error { +func SendWithFile(err error, context string, driverName string, command string, path string) error { if noReportFileExist() || apiKey == noreportAPIKey { log.Debug("Opting out of crash reporting.") return nil @@ -67,6 +68,7 @@ func Send(err error, context string, driverName string, command string) error { detectRunningShell(&metaData) detectUname(&metaData) detectOSVersion(&metaData) + addFile(path, &metaData) var buffer bytes.Buffer for _, message := range log.History() { @@ -76,6 +78,25 @@ func Send(err error, context string, driverName string, command string) error { return bugsnag.Notify(err, metaData, bugsnag.SeverityError, bugsnag.Context{String: context}, bugsnag.ErrorClass{Name: fmt.Sprintf("%s/%s", driverName, command)}) } +// Send through http the crash report to bugsnag need a call to Configure(apiKey) before +func Send(err error, context string, driverName string, command string) error { + return SendWithFile(err, context, driverName, command, "") +} + +func addFile(path string, metaData *bugsnag.MetaData) { + file, err := os.Open(path) + if err != nil { + log.Debug(err) + return + } + data, err := ioutil.ReadAll(file) + if err != nil { + log.Debug(err) + return + } + metaData.Add("logfile", filepath.Base(path), string(data)) +} + func noReportFileExist() bool { optOutFilePath := filepath.Join(mcndirs.GetBaseDir(), "no-error-report") if _, err := os.Stat(optOutFilePath); os.IsNotExist(err) { diff --git a/libmachine/crashreport/crash_report_test.go b/libmachine/crashreport/crash_report_test.go new file mode 100644 index 0000000000..b71e4367bc --- /dev/null +++ b/libmachine/crashreport/crash_report_test.go @@ -0,0 +1,39 @@ +package crashreport + +import ( + "testing" + + "io/ioutil" + + "os" + "path/filepath" + + "github.com/bugsnag/bugsnag-go" + "github.com/stretchr/testify/assert" +) + +func TestFileIsNotReadWhenNotExisting(t *testing.T) { + metaData := bugsnag.MetaData{} + addFile("not existing", &metaData) + assert.Empty(t, metaData) +} + +func TestRead(t *testing.T) { + metaData := bugsnag.MetaData{} + content := "foo\nbar\nqix\n" + fileName := createTempFile(t, content) + defer os.Remove(fileName) + addFile(fileName, &metaData) + assert.Equal(t, "foo\nbar\nqix\n", metaData["logfile"][filepath.Base(fileName)]) +} + +func createTempFile(t *testing.T, content string) string { + file, err := ioutil.TempFile("", "") + if err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(file.Name(), []byte(content), 0644); err != nil { + t.Fatal(err) + } + return file.Name() +} diff --git a/libmachine/libmachine.go b/libmachine/libmachine.go index 566bedbe63..24bde9448b 100644 --- a/libmachine/libmachine.go +++ b/libmachine/libmachine.go @@ -98,7 +98,7 @@ func (api *Client) Create(h *host.Host) error { log.Info("Creating machine...") if err := api.performCreate(h); err != nil { - crashreport.Send(err, "api.performCreate", h.DriverName, "Create") + sendCrashReport(err, api, h) return err } @@ -152,3 +152,12 @@ func (api *Client) performCreate(h *host.Host) error { return nil } + +func sendCrashReport(err error, api *Client, host *host.Host) { + if host.DriverName == "virtualbox" { + vboxlogPath := filepath.Join(api.GetMachinesDir(), host.Name, host.Name, "Logs", "VBox.log") + crashreport.SendWithFile(err, "api.performCreate", host.DriverName, "Create", vboxlogPath) + } else { + crashreport.Send(err, "api.performCreate", host.DriverName, "Create") + } +}