Skip to content

Commit 28cbdc5

Browse files
bjmcnicandrewkroh
andauthored
Fix major and minor version parsing on older Windows (#175)
On Windows versions earlier than 10/2016, the `Major` version was getting overwritten with what would have been the `Minor` version, and the `Minor` version was not being set. Windows 7: Before fix: ``` "os": { "type": "windows", "family": "windows", "platform": "windows", "name": "Windows 7 Enterprise", "version": "6.1", "major": 1, "minor": 0, "patch": 0, "build": "7601.0" }, ``` With fix: ``` "os": { "type": "windows", "family": "windows", "platform": "windows", "name": "Windows 7 Enterprise", "version": "6.1", "major": 6, "minor": 1, "patch": 0, "build": "7601.0" }, ``` Windows 8: Before fix: ``` "os": { "type": "windows", "family": "windows", "platform": "windows", "name": "Windows 8 Enterprise", "version": "6.2", "major": 2, "minor": 0, "patch": 0, "build": "9200.0" }, ``` With fix: ``` "os": { "type": "windows", "family": "windows", "platform": "windows", "name": "Windows 8 Enterprise", "version": "6.2", "major": 6, "minor": 2, "patch": 0, "build": "9200.0" }, ``` Win 10: Separate code path, unimpacted, same before and after: ``` "os": { "type": "windows", "family": "windows", "platform": "windows", "name": "Windows 11 Pro", "version": "10.0", "major": 10, "minor": 0, "patch": 0, "build": "22621.1702" }, ``` --------- Co-authored-by: Andrew Kroh <andrew.kroh@elastic.co>
1 parent a5eea30 commit 28cbdc5

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

.changelog/175.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```release-note:bug
2+
windows: On versions earlier than 10/2016, the `os.major` version was getting overwritten with
3+
what would have been the minor version, and the `os.minor` version was not being set.
4+
```

providers/windows/os_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func OperatingSystem() (*types.OSInfo, error) {
6868
case 0:
6969
osInfo.Major, _ = strconv.Atoi(p)
7070
case 1:
71-
osInfo.Major, _ = strconv.Atoi(p)
71+
osInfo.Minor, _ = strconv.Atoi(p)
7272
}
7373
}
7474
}

providers/windows/os_windows_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
package windows
1919

2020
import (
21+
"os/exec"
22+
"strconv"
23+
"strings"
2124
"testing"
2225

2326
"github.com/stretchr/testify/assert"
@@ -91,3 +94,27 @@ func TestFixWindows11Naming(t *testing.T) {
9194
assert.Equal(t, tc.expectedName, tc.osInfo.Name)
9295
}
9396
}
97+
98+
func TestOperatingSystemMajorMinor(t *testing.T) {
99+
// User PowerShell to get the expected OS version.
100+
var major, minor int
101+
if stdout, err := exec.Command("powershell.exe", "-c", "[System.Environment]::OSVersion.Version.Major").Output(); err != nil {
102+
t.Fatal(err)
103+
} else if major, err = strconv.Atoi(strings.TrimSpace(string(stdout))); err != nil {
104+
t.Fatal(err)
105+
}
106+
if stdout, err := exec.Command("powershell.exe", "-c", "[System.Environment]::OSVersion.Version.Minor").Output(); err != nil {
107+
t.Fatal(err)
108+
} else if minor, err = strconv.Atoi(strings.TrimSpace(string(stdout))); err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
// Verify expected output.
113+
osInfo, err := OperatingSystem()
114+
if err != nil {
115+
t.Fatal(err)
116+
}
117+
118+
assert.Equal(t, major, osInfo.Major)
119+
assert.Equal(t, minor, osInfo.Minor)
120+
}

0 commit comments

Comments
 (0)