Disable PowerShell profile and simplify fingerprinting link speed on Windows (#11183)

This commit is contained in:
Luiz Aoqui 2021-09-22 11:17:47 -04:00 committed by GitHub
parent 0745fdbcf6
commit a7698dedba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 62 deletions

3
.changelog/11183.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
client: Fixed a bug where network speed fingerprint could fail on Windows
```

View File

@ -9,44 +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"
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)
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)
value, err := strconv.Atoi(args[0])
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
}
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
}

View File

@ -1,34 +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},
}
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)
}
}
}