Merge pull request #237 from chef/osenv

api: don't force root on os_env
This commit is contained in:
jcreedcmu 2015-11-13 10:16:57 -05:00
commit 6b2d4e2306
4 changed files with 26 additions and 7 deletions

View file

@ -11,15 +11,16 @@
# its(:split) { should_not include('.') }
# end
require 'utils/simpleconfig'
class OsEnv < Inspec.resource(1)
name 'os_env'
attr_reader :content
def initialize(env)
def initialize(env = nil)
@osenv = env
cmd = inspec.command("su - root -c 'echo $#{env}'")
@content = cmd.stdout.chomp
@content = nil if cmd.exit_status != 0
@content = nil
@content = params[env] unless env.nil?
end
def split
@ -29,6 +30,24 @@ class OsEnv < Inspec.resource(1)
end
def to_s
"Environment Variable #{@osenv}"
if @osenv.nil?
'Environment variables'
else
"Environment variable #{@osenv}"
end
end
private
def params
return @params if defined? @params
out = inspec.command('env')
out = inspec.command('printenv') unless out.exit_status == 0
unless out.exit_status == 0
skip_resource "Can't read environment variables on #{os[:family]}. "\
"Tried `env` and `printenv` which returned #{out.exit_status}"
end
@params = SimpleConfig.new(out.stdout).params
end
end

View file

@ -108,7 +108,7 @@ class MockLoader
'type win_secpol.cfg' => cmd.call('secedit-export'),
'secedit /export /cfg win_secpol.cfg' => cmd.call('success'),
'del win_secpol.cfg' => cmd.call('success'),
'su - root -c \'echo $PATH\'' => cmd.call('PATH'),
'env' => cmd.call('env'),
'(Get-Item \'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule\').GetValue(\'Start\')' => cmd.call('reg_schedule'),
'Auditpol /get /subcategory:\'User Account Management\' /r' => cmd.call('auditpol'),
'/sbin/auditctl -l' => cmd.call('auditctl'),

View file

@ -1 +0,0 @@
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

1
test/unit/mock/cmd/env Normal file
View file

@ -0,0 +1 @@
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin