Skip to content

Commit

Permalink
Merge pull request #90 from chorankates/os_type-population
Browse files Browse the repository at this point in the history
don't assume systems will have standard flag files
  • Loading branch information
chorankates-sfdc authored Aug 25, 2016
2 parents 2dcbf2e + e028945 commit dfbafa6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/rouster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,17 +518,17 @@ def os_type
:osx => '/System/Library/CoreServices/SystemVersion.plist',
}

res = nil
res = :invalid

files.each do |os,file|
files.each do |os, file|
if self.is_file?(file)
@logger.debug(sprintf('determined OS to be[%s] via[%s]', os, file))
res = os
break
end
end

@logger.error(sprintf('unable to determine OS, looking for[%s]', files)) if res.nil?
@logger.error(sprintf('unable to determine OS, looking for[%s]', files)) if res.eql?(:invalid)

@ostype = res
res
Expand Down
11 changes: 8 additions & 3 deletions lib/rouster/deltas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ def get_services(cache=true, humanize=true, type=:all, seed=nil)
:systemv => '/sbin/service --status-all',
:upstart => 'initctl list',
},

:invalid => {
:invalid => 'invalid',
},
}

if type.eql?(:all)
Expand All @@ -486,7 +490,7 @@ def get_services(cache=true, humanize=true, type=:all, seed=nil)

type.each do |provider|

raise InternalError.new(sprintf('unable to get service information from VM operating system[%s]', os)) unless commands.has_key?(os)
raise InternalError.new(sprintf('unable to get service information from VM operating system[%s]', os)) if provider.eql?(:invalid)
raise ArgumentError.new(sprintf('unable to find command provider[%s] for [%s]', provider, os)) if commands[os][provider].nil?

@logger.info(sprintf('get_services using provider [%s] on [%s]', provider, os))
Expand Down Expand Up @@ -665,10 +669,11 @@ def get_services(cache=true, humanize=true, type=:all, seed=nil)
end

end

# end of os casing
else
raise InternalError.new(sprintf('unable to get service information from VM operating system[%s]', os))
end


# end of provider processing
end

Expand Down
68 changes: 68 additions & 0 deletions test/functional/deltas/test_get_os.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require sprintf('%s/../../../path_helper', File.dirname(File.expand_path(__FILE__)))

require 'rouster'
require 'rouster/puppet'
require 'rouster/testing'
require 'test/unit'

class TestValidateFileFunctional < Test::Unit::TestCase

# TODO this should probably be further abstracted into the implementation, but this is fine for now
FLAG_FILES = {
:ubuntu => '/etc/os-release', # debian too
:solaris => '/etc/release',
:redhat => '/etc/redhat-release', # centos too
:osx => '/System/Library/CoreServices/SystemVersion.plist',
}

def setup
# expose private methods
Rouster.send(:public, *Rouster.private_instance_methods)
Rouster.send(:public, *Rouster.protected_instance_methods)

@app = Rouster.new(:name => 'app')
end

def teardown
# put the flag file back in place
FLAG_FILES.each_pair do |_os, ff|
bkup = sprintf('%s.bkup', ff)
if @app.is_file?(bkup)
@app.run(sprintf('mv %s %s', bkup, ff))
end
end
end

def test_happy_path

type = @app.os_type
assert_not_nil(type, sprintf('unable to determine vm[%s] OS', @app))
assert_not_equal(:invalid, type)

assert_nothing_raised do
@app.get_services
end

end

def test_unhappy_path
# move the flag file out of the way
FLAG_FILES.each_pair do |_os, ff|
if @app.is_file?(ff)
@app.run(sprintf('mv %s %s.bkup', ff, ff))
end
end

type = @app.os_type

assert_equal(:invalid, type, sprintf('got wrong value for unmarked OS[%s]', type))

e = assert_raise do
@app.get_services
end

assert_equal(Rouster::InternalError, e.class, sprintf('wrong exception raised[%s] [%s]', e.class, e.message))

end

end

0 comments on commit dfbafa6

Please sign in to comment.