Skip to content

Commit

Permalink
Merge pull request #33 from chef/adamleff/wrl-support
Browse files Browse the repository at this point in the history
Adding support for Wind River Linux in support of Cisco devices
  • Loading branch information
arlimus committed Dec 4, 2015
2 parents d9d5a6f + 30a717e commit 8dd1acf
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
train-*.gem
Gemfile.lock
2 changes: 1 addition & 1 deletion lib/train/extras/os_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def to_hash

OS = {
'redhat' => %w{
redhat oracle centos fedora amazon scientific xenserver
redhat oracle centos fedora amazon scientific xenserver wrlinux
},
'debian' => %w{
debian ubuntu linuxmint raspbian
Expand Down
46 changes: 34 additions & 12 deletions lib/train/extras/os_detect_linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require 'train/extras/linux_lsb'

module Train::Extras
module DetectLinux
module DetectLinux # rubocop:disable Metrics/ModuleLength
include Train::Extras::LinuxLSB

def detect_linux_via_config # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
Expand All @@ -21,7 +21,7 @@ def detect_linux_via_config # rubocop:disable Metrics/AbcSize, Metrics/Cyclomati
elsif !(raw = get_config('/etc/enterprise-release')).nil?
@platform[:family] = 'oracle'
@platform[:release] = redhatish_version(raw)
elsif !(_raw = get_config('/etc/debian_version')).nil?
elsif !(raw = get_config('/etc/debian_version')).nil?
case lsb[:id]
when /ubuntu/i
@platform[:family] = 'ubuntu'
Expand All @@ -30,11 +30,8 @@ def detect_linux_via_config # rubocop:disable Metrics/AbcSize, Metrics/Cyclomati
@platform[:family] = 'linuxmint'
@platform[:release] = lsb[:release]
else
@platform[:family] = 'debian'
@platform[:family] = 'raspbian' if unix_file?('/usr/bin/raspi-config')
unless (rel = get_config('/etc/debian_version')).nil?
@platform[:release] = rel.chomp
end
@platform[:family] = unix_file?('/usr/bin/raspi-config') ? 'raspbian' : 'debian'
@platform[:release] = raw.chomp
end
elsif !(raw = get_config('/etc/parallels-release')).nil?
@platform[:family] = redhatish_platform(raw)
Expand Down Expand Up @@ -82,12 +79,14 @@ def detect_linux_via_config # rubocop:disable Metrics/AbcSize, Metrics/Cyclomati
@platform[:family] = 'coreos'
meta = lsb_config(raw)
@platform[:release] = meta[:release]
else
# in all other cases we didn't detect it
return false
elsif !(os_info = fetch_os_release).nil?
if os_info['ID_LIKE'].match('wrlinux')
@platform[:family] = 'wrlinux'
@platform[:release] = os_info['VERSION']
end
end
# when we get here the detection returned a result
true

!@platform[:family].nil? && !@platform[:release].nil?
end

def uname_s
Expand Down Expand Up @@ -122,5 +121,28 @@ def detect_linux
# in all other cases we failed the detection
@platform[:family] = 'unknown'
end

def fetch_os_release
data = get_config('/etc/os-release')
return if data.nil?

os_info = parse_os_release_info(data)
cisco_info_file = os_info['CISCO_RELEASE_INFO']
if cisco_info_file
os_info.merge!(parse_os_release_info(get_config(cisco_info_file)))
end

os_info
end

def parse_os_release_info(raw)
return {} if raw.nil?

raw.lines.each_with_object({}) do |line, memo|
line.strip!
key, value = line.split('=', 2)
memo[key] = value.gsub(/\A"|"\Z/, '') unless value.empty?
end
end
end
end
9 changes: 9 additions & 0 deletions test/unit/extras/os_common_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ def mock_platform(x)
it { os.unix?.must_equal(true) }
end

describe 'with platform set to wrlinux' do
let(:os) { mock_platform('wrlinux') }
it { os.redhat?.must_equal(true) }
it { os.debian?.must_equal(false) }
it { os.suse?.must_equal(false) }
it { os.linux?.must_equal(true) }
it { os.unix?.must_equal(true) }
end

describe 'with platform set to linux' do
let(:os) { mock_platform('linux') }
it { os.linux?.must_equal(true) }
Expand Down
188 changes: 188 additions & 0 deletions test/unit/extras/os_detect_linux_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
require 'helper'
require 'train/extras'

class OsDetectLinuxTester
attr_reader :platform
include Train::Extras::DetectLinux

def initialize
@platform = {}
end
end

describe 'os_detect_linux' do
let(:detector) { OsDetectLinuxTester.new }

describe '#detect_linux_via_config' do

before do
detector.stubs(:get_config)
detector.stubs(:fetch_os_release)
detector.stubs(:redhatish_version).returns('redhat-version')
end

describe '/etc/enterprise-release' do
it 'sets the correct family/release for oracle' do
detector.stubs(:get_config).with('/etc/enterprise-release').returns('data')

detector.detect_linux_via_config.must_equal(true)
detector.platform[:family].must_equal('oracle')
detector.platform[:release].must_equal('redhat-version')
end
end

describe '/etc/debian_version' do

before { detector.stubs(:get_config).with('/etc/debian_version').returns('deb-version') }

describe 'ubuntu' do
it 'sets the correct family/release for ubuntu' do
detector.stubs(:lsb).returns({ id: 'ubuntu', release: 'ubuntu-release' })

detector.detect_linux_via_config.must_equal(true)
detector.platform[:family].must_equal('ubuntu')
detector.platform[:release].must_equal('ubuntu-release')
end
end

describe 'linuxmint' do
it 'sets the correct family/release for ubuntu' do
detector.stubs(:lsb).returns({ id: 'linuxmint', release: 'mint-release' })

detector.detect_linux_via_config.must_equal(true)
detector.platform[:family].must_equal('linuxmint')
detector.platform[:release].must_equal('mint-release')
end
end

describe 'raspbian' do
it 'sets the correct family/release for raspbian ' do
detector.stubs(:lsb).returns({ id: 'something_else', release: 'some_release' })
detector.expects(:unix_file?).with('/usr/bin/raspi-config').returns(true)

detector.detect_linux_via_config.must_equal(true)
detector.platform[:family].must_equal('raspbian')
detector.platform[:release].must_equal('deb-version')
end
end

describe 'everything else' do
it 'sets the correct family/release for debian ' do
detector.stubs(:lsb).returns({ id: 'something_else', release: 'some_release' })
detector.expects(:unix_file?).with('/usr/bin/raspi-config').returns(false)

detector.detect_linux_via_config.must_equal(true)
detector.platform[:family].must_equal('debian')
detector.platform[:release].must_equal('deb-version')
end
end
end

describe '/etc/os-release' do
describe 'when not on a wrlinux build' do
it 'does not set a platform family/release' do
detector.stubs(:fetch_os_release).returns({ 'ID_LIKE' => 'something_else' })

detector.detect_linux_via_config.must_equal(false)
detector.platform[:family].must_equal(nil)
detector.platform[:release].must_equal(nil)
end
end

describe 'when on a wrlinux build' do
let(:data) do
{
'ID_LIKE' => 'cisco-wrlinux',
'VERSION' => 'cisco123'
}
end

it 'sets the correct family/release for wrlinux' do
detector.stubs(:fetch_os_release).returns(data)

detector.detect_linux_via_config.must_equal(true)
detector.platform[:family].must_equal('wrlinux')
detector.platform[:release].must_equal('cisco123')
end
end
end
end

describe '#fetch_os_release' do
describe 'when no os-release data is available' do
it 'returns nil' do
detector.expects(:get_config).with('/etc/os-release').returns(nil)
detector.fetch_os_release.must_equal(nil)
end
end

describe 'when os-release data exists with no CISCO_RELEASE_INFO' do
let(:os_release) { { 'KEY1' => 'VALUE1' } }

it 'returns a correct hash' do
detector.expects(:get_config).with('/etc/os-release').returns('os-release data')
detector.expects(:parse_os_release_info).with('os-release data').returns(os_release)
detector.fetch_os_release['KEY1'].must_equal('VALUE1')
end
end

describe 'when os-release data exists with CISCO_RELEASE_INFO' do
let(:os_release) { { 'KEY1' => 'VALUE1', 'CISCO_RELEASE_INFO' => 'cisco_file' } }
let(:cisco_release) { { 'KEY1' => 'NEWVALUE1', 'KEY2' => 'VALUE2' } }

it 'returns a correct hash' do
detector.expects(:get_config).with('/etc/os-release').returns('os-release data')
detector.expects(:get_config).with('cisco_file').returns('cisco data')
detector.expects(:parse_os_release_info).with('os-release data').returns(os_release)
detector.expects(:parse_os_release_info).with('cisco data').returns(cisco_release)

os_info = detector.fetch_os_release
os_info['KEY1'].must_equal('NEWVALUE1')
os_info['KEY2'].must_equal('VALUE2')
end
end
end

describe '#parse_os_release_info' do
describe 'when nil is supplied' do
it 'returns an empty hash' do
detector.parse_os_release_info(nil).must_equal({})
end
end

describe 'when unexpectedly-formatted data is supplied' do
let(:data) do
<<-EOL
blah blah
no good data here
EOL
end

it 'returns an empty hash' do
detector.parse_os_release_info(nil).must_equal({})
end
end

describe 'when properly-formatted data is supplied' do
let(:data) do
<<-EOL
KEY1=value1
KEY2=
KEY3=value3
KEY4="value4 with spaces"
KEY5="value5 with a = sign"
EOL
end

it 'parses the data correctly' do
parsed_data = detector.parse_os_release_info(data)

parsed_data['KEY1'].must_equal('value1')
parsed_data.key?('KEY2').must_equal(false)
parsed_data['KEY3'].must_equal('value3')
parsed_data['KEY4'].must_equal('value4 with spaces')
parsed_data['KEY5'].must_equal('value5 with a = sign')
end
end
end
end
1 change: 1 addition & 0 deletions test/unit/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

require 'minitest/autorun'
require 'minitest/spec'
require 'mocha/setup'

require 'train'
24 changes: 12 additions & 12 deletions test/unit/transports/local_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,41 @@
end

describe 'when running a local command' do
let(:mock) { Minitest::Mock.new }
let(:cmd_runner) { Minitest::Mock.new }

def mock_run_cmd(cmd, &block)
mock.expect :run_command, nil
Mixlib::ShellOut.stub :new, mock do |*args|
cmd_runner.expect :run_command, nil
Mixlib::ShellOut.stub :new, cmd_runner do |*args|
block.call()
end
end

it 'gets stdout' do
mock_run_cmd(rand) do
x = rand
mock.expect :stdout, x
mock.expect :stderr, nil
mock.expect :exitstatus, nil
cmd_runner.expect :stdout, x
cmd_runner.expect :stderr, nil
cmd_runner.expect :exitstatus, nil
connection.run_command(rand).stdout.must_equal x
end
end

it 'gets stderr' do
mock_run_cmd(rand) do
x = rand
mock.expect :stdout, nil
mock.expect :stderr, x
mock.expect :exitstatus, nil
cmd_runner.expect :stdout, nil
cmd_runner.expect :stderr, x
cmd_runner.expect :exitstatus, nil
connection.run_command(rand).stderr.must_equal x
end
end

it 'gets exit_status' do
mock_run_cmd(rand) do
x = rand
mock.expect :stdout, nil
mock.expect :stderr, nil
mock.expect :exitstatus, x
cmd_runner.expect :stdout, nil
cmd_runner.expect :stderr, nil
cmd_runner.expect :exitstatus, x
connection.run_command(rand).exit_status.must_equal x
end
end
Expand Down
5 changes: 3 additions & 2 deletions train.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Gem::Specification.new do |spec|
spec.add_dependency 'winrm-transport', '~> 1.0'
spec.add_dependency 'docker-api', '~> 1.22'

spec.add_development_dependency 'rake', '~> 10.4'
spec.add_development_dependency 'rake', '~> 10.4'
spec.add_development_dependency 'minitest', '~> 5.8'
spec.add_development_dependency 'rubocop', '~> 0.34'
spec.add_development_dependency 'mocha', '~> 1.1'
spec.add_development_dependency 'rubocop', '~> 0.34'
end

0 comments on commit 8dd1acf

Please sign in to comment.