From af09ebbe4e1b0ac3838bc69d598d8f106094c468 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 6 Jan 2016 11:08:14 +0100 Subject: [PATCH 1/2] This code is only useful to crash reports Signed-off-by: David Gageot --- libmachine/crashreport/crash_report.go | 3 +-- libmachine/{mcnutils => crashreport}/os_darwin.go | 4 ++-- libmachine/{mcnutils => crashreport}/os_linux.go | 4 ++-- libmachine/{mcnutils => crashreport}/os_windows.go | 4 ++-- libmachine/{mcnutils => crashreport}/os_windows_test.go | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) rename libmachine/{mcnutils => crashreport}/os_darwin.go (79%) rename libmachine/{mcnutils => crashreport}/os_linux.go (80%) rename libmachine/{mcnutils => crashreport}/os_windows.go (92%) rename libmachine/{mcnutils => crashreport}/os_windows_test.go (97%) diff --git a/libmachine/crashreport/crash_report.go b/libmachine/crashreport/crash_report.go index e816ef3b51..de49e40f1a 100644 --- a/libmachine/crashreport/crash_report.go +++ b/libmachine/crashreport/crash_report.go @@ -17,7 +17,6 @@ import ( "github.com/bugsnag/bugsnag-go" "github.com/docker/machine/libmachine/log" - "github.com/docker/machine/libmachine/mcnutils" "github.com/docker/machine/version" ) @@ -146,5 +145,5 @@ func detectUname(metaData *bugsnag.MetaData) { } func detectOSVersion(metaData *bugsnag.MetaData) { - metaData.Add("device", "os version", mcnutils.LocalOSVersion()) + metaData.Add("device", "os version", localOSVersion()) } diff --git a/libmachine/mcnutils/os_darwin.go b/libmachine/crashreport/os_darwin.go similarity index 79% rename from libmachine/mcnutils/os_darwin.go rename to libmachine/crashreport/os_darwin.go index 8de0bdc3d2..2e887e4d10 100644 --- a/libmachine/mcnutils/os_darwin.go +++ b/libmachine/crashreport/os_darwin.go @@ -1,8 +1,8 @@ -package mcnutils +package crashreport import "os/exec" -func LocalOSVersion() string { +func localOSVersion() string { command := exec.Command("bash", "-c", `sw_vers | grep ProductVersion | cut -d$'\t' -f2`) output, err := command.Output() if err != nil { diff --git a/libmachine/mcnutils/os_linux.go b/libmachine/crashreport/os_linux.go similarity index 80% rename from libmachine/mcnutils/os_linux.go rename to libmachine/crashreport/os_linux.go index 0c869f0c5e..31b72a89b0 100644 --- a/libmachine/mcnutils/os_linux.go +++ b/libmachine/crashreport/os_linux.go @@ -1,8 +1,8 @@ -package mcnutils +package crashreport import "os/exec" -func LocalOSVersion() string { +func localOSVersion() string { command := exec.Command("bash", "-c", `cat /etc/os-release | grep 'VERSION=' | cut -d'=' -f2`) output, err := command.Output() if err != nil { diff --git a/libmachine/mcnutils/os_windows.go b/libmachine/crashreport/os_windows.go similarity index 92% rename from libmachine/mcnutils/os_windows.go rename to libmachine/crashreport/os_windows.go index 03cf004ef1..da60e9c969 100644 --- a/libmachine/mcnutils/os_windows.go +++ b/libmachine/crashreport/os_windows.go @@ -1,11 +1,11 @@ -package mcnutils +package crashreport import ( "os/exec" "strings" ) -func LocalOSVersion() string { +func localOSVersion() string { command := exec.Command("ver") output, err := command.Output() if err == nil { diff --git a/libmachine/mcnutils/os_windows_test.go b/libmachine/crashreport/os_windows_test.go similarity index 97% rename from libmachine/mcnutils/os_windows_test.go rename to libmachine/crashreport/os_windows_test.go index a794d5f803..e20b2c7174 100644 --- a/libmachine/mcnutils/os_windows_test.go +++ b/libmachine/crashreport/os_windows_test.go @@ -1,4 +1,4 @@ -package mcnutils +package crashreport import ( "testing" From f466aa94997a6876edb800827a16602bb5dd43aa Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 6 Jan 2016 11:24:14 +0100 Subject: [PATCH 2/2] FIX #2745 better Windows version detection Signed-off-by: David Gageot --- libmachine/crashreport/os_windows.go | 13 ++++++++++++- libmachine/crashreport/os_windows_test.go | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/libmachine/crashreport/os_windows.go b/libmachine/crashreport/os_windows.go index da60e9c969..6e3a33cc8d 100644 --- a/libmachine/crashreport/os_windows.go +++ b/libmachine/crashreport/os_windows.go @@ -24,10 +24,21 @@ func localOSVersion() string { func parseSystemInfoOutput(output string) string { lines := strings.Split(string(output), "\n") for _, line := range lines { - if strings.HasPrefix(line, "OS Version") { + if strings.HasPrefix(line, "OS Version:") { return strings.TrimSpace(line[len("OS Version:"):]) } } + + // If we couldn't find the version, maybe the output is not in english + // Let's parse the fourth line since it seems to be the one always used + // for the version. + if len(lines) >= 4 { + parts := strings.Split(lines[3], ":") + if len(parts) == 2 { + return strings.TrimSpace(parts[1]) + } + } + return "" } diff --git a/libmachine/crashreport/os_windows_test.go b/libmachine/crashreport/os_windows_test.go index e20b2c7174..b9cdcdc983 100644 --- a/libmachine/crashreport/os_windows_test.go +++ b/libmachine/crashreport/os_windows_test.go @@ -27,5 +27,21 @@ OS Build Type: Multiprocessor Free Registered Owner: Windows User ` - assert.Equal(t, "10.0.10240 N/A Build 10240", parseOutput(output)) + assert.Equal(t, "10.0.10240 N/A Build 10240", parseSystemInfoOutput(output)) +} + +func TestParseNonEnglishSystemInfoOutput(t *testing.T) { + output := ` +Ignored: ... +Ignored: ... +Version du Système: 10.0.10350 +` + + assert.Equal(t, "10.0.10350", parseSystemInfoOutput(output)) +} + +func TestParseInvalidSystemInfoOutput(t *testing.T) { + output := "Invalid" + + assert.Empty(t, parseSystemInfoOutput(output)) }