From 358455fe6ce7f56192a45893d830faade08816b8 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich III Date: Tue, 20 Sep 2016 15:10:46 -0400 Subject: [PATCH] Convert `wmic` architecture to a normal standard This modifies the value of `@platform[:arch]` on Windows to resemble what `uname` would return on Unix/Linux. --- lib/train/extras/os_detect_windows.rb | 31 +++++++++++++++++++++- test/unit/extras/os_detect_windows_test.rb | 15 +++++++---- test/windows/local_test.rb | 2 +- test/windows/winrm_test.rb | 2 +- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/train/extras/os_detect_windows.rb b/lib/train/extras/os_detect_windows.rb index 069c15cc..06d47a67 100644 --- a/lib/train/extras/os_detect_windows.rb +++ b/lib/train/extras/os_detect_windows.rb @@ -50,8 +50,37 @@ def read_wmic @platform[:build] = sys_info[:BuildNumber] @platform[:name] = sys_info[:Caption] @platform[:name] = @platform[:name].gsub('Microsoft', '').strip unless @platform[:name].empty? - @platform[:arch] = sys_info[:OSArchitecture] + @platform[:arch] = read_wmic_cpu end end + + # `OSArchitecture` from `read_wmic` does not match a normal standard + # For example, `x86_64` shows as `64-bit` + def read_wmic_cpu + res = @backend.run_command('wmic cpu get architecture /format:list') + if res.exit_status == 0 + sys_info = {} + res.stdout.lines.each { |line| + m = /^\s*([^=]*?)\s*=\s*(.*?)\s*$/.match(line) + sys_info[m[1].to_sym] = m[2] unless m.nil? || m[1].nil? + } + end + + # This converts `wmic os get architecture` output to a normal standard + # https://msdn.microsoft.com/en-us/library/aa394373(VS.85).aspx + arch_map = { + 0 => 'i386', + 1 => 'mips', + 2 => 'alpha', + 3 => 'powerpc', + 5 => 'arm', + 6 => 'ia64', + 9 => 'x86_64', + } + + # The value of `wmic cpu get architecture` is always a number between 0-9 + arch_number = sys_info[:Architecture].to_i + arch_map[arch_number] + end end end diff --git a/test/unit/extras/os_detect_windows_test.rb b/test/unit/extras/os_detect_windows_test.rb index 0867304b..26a85a83 100644 --- a/test/unit/extras/os_detect_windows_test.rb +++ b/test/unit/extras/os_detect_windows_test.rb @@ -17,6 +17,7 @@ def initialize detector = OsDetectWindowsTester.new detector.backend.mock_command('cmd /c ver', "\r\nMicrosoft Windows [Version 6.3.9600]\r\n", '', 0) detector.backend.mock_command('wmic os get * /format:list',"\r\r\nBuildNumber=9600\r\r\nCaption=Microsoft Windows Server 2012 R2 Standard\r\r\nOSArchitecture=64-bit\r\r\nVersion=6.3.9600\r\r\n" , '', 0) + detector.backend.mock_command('wmic cpu get architecture /format:list',"\r\r\nArchitecture=9\r\r\n" , '', 0) detector } @@ -24,7 +25,7 @@ def initialize detector.detect_windows detector.platform[:family].must_equal('windows') detector.platform[:name].must_equal('Windows Server 2012 R2 Standard') - detector.platform[:arch].must_equal('64-bit') + detector.platform[:arch].must_equal('x86_64') detector.platform[:release].must_equal('6.3.9600') end end @@ -34,6 +35,7 @@ def initialize detector = OsDetectWindowsTester.new detector.backend.mock_command('cmd /c ver', "\r\nMicrosoft Windows [Version 6.1.7601]\r\n", '', 0) detector.backend.mock_command('wmic os get * /format:list',"\r\r\nBuildNumber=7601\r\r\nCaption=Microsoft Windows Server 2008 R2 Standard \r\r\nOSArchitecture=64-bit\r\r\nVersion=6.1.7601\r\r\n" , '', 0) + detector.backend.mock_command('wmic cpu get architecture /format:list',"\r\r\nArchitecture=9\r\r\n" , '', 0) detector } @@ -41,7 +43,7 @@ def initialize detector.detect_windows detector.platform[:family].must_equal('windows') detector.platform[:name].must_equal('Windows Server 2008 R2 Standard') - detector.platform[:arch].must_equal('64-bit') + detector.platform[:arch].must_equal('x86_64') detector.platform[:release].must_equal('6.1.7601') end end @@ -51,6 +53,7 @@ def initialize detector = OsDetectWindowsTester.new detector.backend.mock_command('cmd /c ver', "\r\nMicrosoft Windows [Version 6.1.7601]\r\n", '', 0) detector.backend.mock_command('wmic os get * /format:list',"\r\r\nBuildNumber=7601\r\r\nCaption=Microsoft Windows 7 Enterprise \r\r\nOSArchitecture=32-bit\r\r\nVersion=6.1.7601\r\r\n\r\r\n" , '', 0) + detector.backend.mock_command('wmic cpu get architecture /format:list',"\r\r\nArchitecture=0\r\r\n" , '', 0) detector } @@ -58,7 +61,7 @@ def initialize detector.detect_windows detector.platform[:family].must_equal('windows') detector.platform[:name].must_equal('Windows 7 Enterprise') - detector.platform[:arch].must_equal('32-bit') + detector.platform[:arch].must_equal('i386') detector.platform[:release].must_equal('6.1.7601') end end @@ -68,6 +71,7 @@ def initialize detector = OsDetectWindowsTester.new detector.backend.mock_command('cmd /c ver', "\r\nMicrosoft Windows [Version 10.0.10240]\r\n", '', 0) detector.backend.mock_command('wmic os get * /format:list',"\r\r\nBuildNumber=10240\r\r\nCaption=Microsoft Windows 10 Pro\r\r\nOSArchitecture=64-bit\r\r\nVersion=10.0.10240\r\r\n\r\r\n" , '', 0) + detector.backend.mock_command('wmic cpu get architecture /format:list',"\r\r\nArchitecture=9\r\r\n" , '', 0) detector } @@ -75,7 +79,7 @@ def initialize detector.detect_windows detector.platform[:family].must_equal('windows') detector.platform[:name].must_equal('Windows 10 Pro') - detector.platform[:arch].must_equal('64-bit') + detector.platform[:arch].must_equal('x86_64') detector.platform[:release].must_equal('10.0.10240') end end @@ -84,7 +88,8 @@ def initialize let(:detector) { detector = OsDetectWindowsTester.new detector.backend.mock_command('cmd /c ver', "\r\nMicrosoft Windows [Version 4.10.1998]\r\n", '', 0) - detector.backend.mock_command('wmic os get * /format:list', nil , '', 1) + detector.backend.mock_command('wmic os get * /format:list', nil, '', 1) + detector.backend.mock_command('wmic cpu get architecture /format:list', nil, '', 1) detector } diff --git a/test/windows/local_test.rb b/test/windows/local_test.rb index a97c4599..30c903e4 100644 --- a/test/windows/local_test.rb +++ b/test/windows/local_test.rb @@ -24,7 +24,7 @@ os[:name].must_equal 'Windows Server 2012 R2 Datacenter' os[:family].must_equal "windows" os[:release].must_equal '6.3.9600' - os[:arch].must_equal '64-bit' + os[:arch].must_equal 'x86_64' end it 'run echo test' do diff --git a/test/windows/winrm_test.rb b/test/windows/winrm_test.rb index 7359bec9..37140b37 100644 --- a/test/windows/winrm_test.rb +++ b/test/windows/winrm_test.rb @@ -30,7 +30,7 @@ os[:name].must_equal 'Windows Server 2012 R2 Datacenter' os[:family].must_equal 'windows' os[:release].must_equal '6.3.9600' - os[:arch].must_equal '64-bit' + os[:arch].must_equal 'x86_64' end it 'run echo test' do