From ccc0a28e16f932b3bc724aae396ef058f6db8f90 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Tue, 14 Sep 2021 17:42:02 -0400 Subject: [PATCH 1/4] fingerprint: don't load powershell profile on windows --- client/fingerprint/network_windows.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/fingerprint/network_windows.go b/client/fingerprint/network_windows.go index 0361e1c1ce8..db52a355d11 100644 --- a/client/fingerprint/network_windows.go +++ b/client/fingerprint/network_windows.go @@ -11,7 +11,8 @@ import ( func (f *NetworkFingerprint) linkSpeed(device string) int { command := fmt.Sprintf("Get-NetAdapter -IncludeHidden | Where name -eq '%s' | Select -ExpandProperty LinkSpeed", device) path := "powershell.exe" - outBytes, err := exec.Command(path, command).Output() + powershellParams := "-NoProfile" + outBytes, err := exec.Command(path, powershellParams, command).Output() if err != nil { f.logger.Warn("failed to detect link speed", "device", device, "path", path, "command", command, "error", err) From 183d18f5c8d8690c9d9165127f80413e9e9f6aa3 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Tue, 14 Sep 2021 17:53:00 -0400 Subject: [PATCH 2/4] fingerprint: parse decimal values for link speed on windows --- client/fingerprint/network_windows.go | 3 ++- client/fingerprint/network_windows_test.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/client/fingerprint/network_windows.go b/client/fingerprint/network_windows.go index db52a355d11..5ca1dff56f1 100644 --- a/client/fingerprint/network_windows.go +++ b/client/fingerprint/network_windows.go @@ -32,11 +32,12 @@ func (f *NetworkFingerprint) parseLinkSpeed(device, commandOutput string) int { } unit := strings.Replace(args[1], "\r\n", "", -1) - value, err := strconv.Atoi(args[0]) + valueFloat, err := strconv.ParseFloat(args[0], 64) if err != nil { f.logger.Warn("unable to parse LinkSpeed value", "device", device, "value", commandOutput, "error", err) return 0 } + value := int(valueFloat) switch unit { case "Mbps": diff --git a/client/fingerprint/network_windows_test.go b/client/fingerprint/network_windows_test.go index 96482bc74d3..004749cd1e6 100644 --- a/client/fingerprint/network_windows_test.go +++ b/client/fingerprint/network_windows_test.go @@ -23,6 +23,7 @@ func TestNetworkFingerPrint_linkspeed_parse(t *testing.T) { {"2 2 Mbps", 0}, {"a Mbps", 0}, {"1 Tbps", 0}, + {"866.7 Mbps", 866}, } for _, ot := range outputTests { From 5d5fa41df9327ad24bc7e979aad0da0944a3c130 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Tue, 14 Sep 2021 18:07:32 -0400 Subject: [PATCH 3/4] changelog: add entry for #11183 --- .changelog/11183.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/11183.txt diff --git a/.changelog/11183.txt b/.changelog/11183.txt new file mode 100644 index 00000000000..4a277501715 --- /dev/null +++ b/.changelog/11183.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Fixed a bug where network speed fingerprint could fail on Windows +``` From 8f3843c28ebf85bf23d879f0aa5092907bc44368 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Thu, 16 Sep 2021 15:27:05 -0400 Subject: [PATCH 4/4] fingerprint: simplify windows network speed fingerprinting --- client/fingerprint/network_windows.go | 34 ++++----------------- client/fingerprint/network_windows_test.go | 35 ---------------------- 2 files changed, 5 insertions(+), 64 deletions(-) delete mode 100644 client/fingerprint/network_windows_test.go diff --git a/client/fingerprint/network_windows.go b/client/fingerprint/network_windows.go index 5ca1dff56f1..c843da32ef7 100644 --- a/client/fingerprint/network_windows.go +++ b/client/fingerprint/network_windows.go @@ -9,46 +9,22 @@ import ( // linkSpeed returns link speed in Mb/s, or 0 when unable to determine it. func (f *NetworkFingerprint) linkSpeed(device string) int { - command := fmt.Sprintf("Get-NetAdapter -IncludeHidden | Where name -eq '%s' | Select -ExpandProperty LinkSpeed", device) + command := fmt.Sprintf("Get-NetAdapter -Name '%s' -ErrorAction Ignore | Select-Object -ExpandProperty 'Speed'", device) path := "powershell.exe" powershellParams := "-NoProfile" - outBytes, err := exec.Command(path, powershellParams, command).Output() + outBytes, err := exec.Command(path, powershellParams, command).Output() if err != nil { f.logger.Warn("failed to detect link speed", "device", device, "path", path, "command", command, "error", err) return 0 } - output := strings.TrimSpace(string(outBytes)) - return f.parseLinkSpeed(device, output) -} - -func (f *NetworkFingerprint) parseLinkSpeed(device, commandOutput string) int { - args := strings.Split(commandOutput, " ") - if len(args) != 2 { - f.logger.Warn("couldn't split LinkSpeed output", "device", device, "output", commandOutput) - return 0 - } - - unit := strings.Replace(args[1], "\r\n", "", -1) - valueFloat, err := strconv.ParseFloat(args[0], 64) + value, err := strconv.Atoi(output) if err != nil { - f.logger.Warn("unable to parse LinkSpeed value", "device", device, "value", commandOutput, "error", err) + f.logger.Warn("unable to parse Speed value", "device", device, "value", output, "error", err) return 0 } - value := int(valueFloat) - - switch unit { - case "Mbps": - return value - case "Kbps": - return value / 1000 - case "Gbps": - return value * 1000 - case "bps": - return value / 1000000 - } - return 0 + return value / 1000000 } diff --git a/client/fingerprint/network_windows_test.go b/client/fingerprint/network_windows_test.go deleted file mode 100644 index 004749cd1e6..00000000000 --- a/client/fingerprint/network_windows_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package fingerprint - -import ( - "testing" - - "github.com/hashicorp/nomad/helper/testlog" -) - -func TestNetworkFingerPrint_linkspeed_parse(t *testing.T) { - f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &DefaultNetworkInterfaceDetector{}} - - var outputTests = []struct { - in string - out int - }{ - {"10 Mbps", 10}, - {"2 bps", 0}, - {"1 Gbps", 1000}, - {"2Mbps", 0}, - {"1000 Kbps", 1}, - {"1 Kbps", 0}, - {"0 Mbps", 0}, - {"2 2 Mbps", 0}, - {"a Mbps", 0}, - {"1 Tbps", 0}, - {"866.7 Mbps", 866}, - } - - for _, ot := range outputTests { - out := f.parseLinkSpeed(ot.in) - if out != ot.out { - t.Errorf("parseLinkSpeed(%s) => %d, should be %d", ot.in, out, ot.out) - } - } -}