Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert wmic architecture to a normal standard #151

Merged
merged 1 commit into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/train/extras/os_detect_windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions test/unit/extras/os_detect_windows_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ 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
}

it 'sets the correct family/release for windows' do
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
Expand All @@ -34,14 +35,15 @@ 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
}

it 'sets the correct family/release for windows' do
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
Expand All @@ -51,14 +53,15 @@ 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
}

it 'sets the correct family/release for windows' do
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
Expand All @@ -68,14 +71,15 @@ 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
}

it 'sets the correct family/release for windows' do
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
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion test/windows/local_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/windows/winrm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down