2016-08-19 22:58:39 +00:00
# encoding: utf-8
require 'functional/helper'
describe 'inspec shell tests' do
include FunctionalHelper
describe 'cmd' do
def do_shell_c ( code , exit_status , json = false , stderr = '' )
2018-02-08 09:06:58 +00:00
json_suffix = " --reporter 'json' " if json
2016-08-19 22:58:39 +00:00
out = inspec ( " shell -c ' #{ code . tr ( '\'' , '\\\'' ) } ' #{ json_suffix } " )
out . stderr . must_equal stderr
out . exit_status . must_equal exit_status
out
end
2017-12-04 21:40:14 +00:00
2018-01-04 19:39:01 +00:00
it 'loads a dependency' do
res = inspec ( " shell -c 'gordon_config' --depends #{ example_profile } " )
res . stderr . must_equal ''
res . exit_status . must_equal 0
res . stdout . chop . must_equal 'gordon_config'
end
2017-12-04 21:40:14 +00:00
it 'confirm file caching is disabled' do
out = do_shell_c ( 'inspec.backend.cache_enabled?(:file)' , 0 )
out . stdout . chop . must_equal 'false'
end
it 'confirm command caching is disabled' do
out = do_shell_c ( 'inspec.backend.cache_enabled?(:command)' , 0 )
out . stdout . chop . must_equal 'false'
end
2016-08-19 22:58:39 +00:00
it 'can run ruby expressions (json output)' do
x = rand
y = rand
out = do_shell_c ( " #{ x } + #{ y } " , 0 , true )
j = JSON . load ( out . stdout )
j . must_equal x + y
end
it 'can run ruby expressions' do
x = rand
y = rand
out = do_shell_c ( " #{ x } + #{ y } " , 0 )
out . stdout . must_equal " #{ x + y } \n "
end
it 'can run arbitrary ruby (json output)' do
out = do_shell_c ( 'x = [1,2,3].inject(0) {|a,v| a + v*v}; x+10' , 0 , true )
j = JSON . load ( out . stdout )
j . must_equal 24 # 1^2 + 2^2 + 3^2 + 10
end
it 'can run arbitrary ruby' do
out = do_shell_c ( 'x = [1,2,3].inject(0) {|a,v| a + v*v}; x+10' , 0 )
out . stdout . must_equal " 24 \n "
end
it 'retrieves resources (json output)' do
2018-02-14 20:06:39 +00:00
out = do_shell_c ( 'platform.params' , 0 , true )
2016-08-19 22:58:39 +00:00
j = JSON . load ( out . stdout )
j . keys . must_include 'name'
2018-02-14 20:06:39 +00:00
j . keys . must_include 'families'
2016-08-19 22:58:39 +00:00
j . keys . must_include 'arch'
j . keys . must_include 'release'
end
it 'retrieves resources' do
out = do_shell_c ( 'os.params' , 0 )
out . stdout . must_include 'name'
2018-02-14 20:06:39 +00:00
out . stdout . must_include 'families'
2016-08-19 22:58:39 +00:00
out . stdout . must_include 'arch'
out . stdout . must_include 'release'
end
it 'runs anonymous tests that succeed (json output)' do
out = do_shell_c ( " describe file( \" #{ __FILE__ } \" ) do it { should exist } end " , 0 , true )
j = JSON . load ( out . stdout )
j . keys . must_include 'version'
j . keys . must_include 'profiles'
2016-09-14 16:20:32 +00:00
j . keys . must_include 'statistics'
2016-08-19 22:58:39 +00:00
end
it 'runs anonymous tests that succeed' do
out = do_shell_c ( " describe file( \" #{ __FILE__ } \" ) do it { should exist } end " , 0 )
out . stdout . must_include '1 successful'
out . stdout . must_include '0 failures'
end
it 'runs anonymous tests that fail (json output)' do
2018-02-14 16:54:20 +00:00
out = do_shell_c ( " describe file( \" foo/bar/baz \" ) do it { should exist } end " , 100 , true )
2016-08-19 22:58:39 +00:00
j = JSON . load ( out . stdout )
j . keys . must_include 'version'
j . keys . must_include 'profiles'
2016-09-14 16:20:32 +00:00
j . keys . must_include 'statistics'
2016-08-19 22:58:39 +00:00
end
it 'runs anonymous tests that fail' do
2018-02-14 16:54:20 +00:00
out = do_shell_c ( " describe file( \" foo/bar/baz \" ) do it { should exist } end " , 100 )
2016-08-19 22:58:39 +00:00
out . stdout . must_include '0 successful'
2017-08-23 14:29:09 +00:00
out . stdout . must_include '1 failure'
2016-08-19 22:58:39 +00:00
end
it 'runs controls with tests (json output)' do
out = do_shell_c ( " control \" test \" do describe file( \" #{ __FILE__ } \" ) do it { should exist } end end " , 0 , true )
j = JSON . load ( out . stdout )
j . keys . must_include 'version'
j . keys . must_include 'profiles'
2016-09-14 16:20:32 +00:00
j . keys . must_include 'statistics'
2016-08-19 22:58:39 +00:00
end
it 'runs controls with tests' do
out = do_shell_c ( " control \" test \" do describe file( \" #{ __FILE__ } \" ) do it { should exist } end end " , 0 )
out . stdout . must_include '1 successful'
out . stdout . must_include '0 failures'
end
it 'runs controls with multiple tests (json output)' do
2018-02-14 16:54:20 +00:00
out = do_shell_c ( " control \" test \" do describe file( \" #{ __FILE__ } \" ) do it { should exist } end; describe file( \" foo/bar/baz \" ) do it { should exist } end end " , 100 , true )
2016-08-19 22:58:39 +00:00
j = JSON . load ( out . stdout )
j . keys . must_include 'version'
j . keys . must_include 'profiles'
2016-09-14 16:20:32 +00:00
j . keys . must_include 'statistics'
2016-08-19 22:58:39 +00:00
end
it 'runs controls with multiple tests' do
2018-02-14 16:54:20 +00:00
out = do_shell_c ( " control \" test \" do describe file( \" #{ __FILE__ } \" ) do it { should exist } end; describe file( \" foo/bar/baz \" ) do it { should exist } end end " , 100 )
2016-09-14 16:20:32 +00:00
out . stdout . must_include '0 successful'
2017-08-23 14:29:09 +00:00
out . stdout . must_include '1 failure'
2016-08-19 22:58:39 +00:00
end
end
describe 'shell' do
def do_shell ( code , exit_status = 0 , stderr = '' )
cmd = " echo ' #{ code . tr ( '\'' , '\\\'' ) } ' | #{ exec_inspec } shell "
out = CMD . run_command ( cmd )
#out.stderr.must_equal stderr
out . exit_status . must_equal exit_status
out
end
2018-01-04 19:39:01 +00:00
it 'loads a dependency' do
cmd = " echo 'gordon_config' | #{ exec_inspec } shell --depends #{ example_profile } "
res = CMD . run_command ( cmd )
res . exit_status . must_equal 0
res . stdout . must_include " => gordon_config "
end
2017-02-08 21:03:59 +00:00
it 'displays the target device information for the user without requiring the help command' do
out = do_shell ( '1+1' )
out . stdout . must_include 'You are currently running on:'
end
2016-08-19 22:58:39 +00:00
it 'provides a help command' do
out = do_shell ( 'help' )
out . stdout . must_include 'Available commands:'
out . stdout . must_include 'You are currently running on:'
end
2017-04-26 18:32:19 +00:00
it 'provides resource help' do
out = do_shell ( 'help file' )
out . stdout . must_include 'Use the file InSpec audit resource'
end
it 'provides helpful feedback if an invalid resource is provided' do
out = do_shell ( 'help not_a_valid_resource' )
out . stdout . must_include 'The resource not_a_valid_resource does not exist.'
end
it 'provides a list of resources' do
out = do_shell ( 'help resources' )
out . stdout . must_include ' - command'
out . stdout . must_include ' - file'
out . stdout . must_include ' - sshd_config'
end
it 'provides matchers help' do
out = do_shell ( 'help matchers' )
2017-04-27 22:05:51 +00:00
out . stdout . must_include 'For more examples, see: https://www.inspec.io/docs/reference/matchers/'
2017-04-26 18:32:19 +00:00
end
2017-10-06 17:32:16 +00:00
it 'provides empty example help' do
out = do_shell ( 'help file' )
out . stdout . must_include 'Name'
out . stdout . must_include 'Description'
out . stdout . must_include 'Example'
out . stdout . must_include 'Web Reference'
end
2016-08-19 22:58:39 +00:00
it 'exposes all resources' do
out = do_shell ( 'os' )
2016-09-07 11:10:35 +00:00
out . stdout . must_match ( / \ => .*Operating.* .*System.* .*Detection / )
2016-08-19 22:58:39 +00:00
end
it 'can run ruby expressions' do
x = rand
y = rand
out = do_shell ( " #{ x } + #{ y } " )
out . stdout . must_include " #{ x + y } "
end
it 'can run arbitrary ruby' do
out = do_shell ( 'x = [1,2,3].inject(0) {|a,v| a + v*v}; x+10' )
out . stdout . must_include " 24 "
end
it 'runs anonymous tests that succeed' do
out = do_shell ( " describe file( \" #{ __FILE__ } \" ) do it { should exist } end " )
out . stdout . must_include '1 successful'
out . stdout . must_include '0 failures'
end
it 'runs anonymous tests that fail' do
out = do_shell ( " describe file( \" foo/bar/baz \" ) do it { should exist } end " )
out . stdout . must_include '0 successful'
2017-08-23 14:29:09 +00:00
out . stdout . must_include '1 failure'
2016-08-19 22:58:39 +00:00
end
it 'runs controls with tests' do
out = do_shell ( " control \" test \" do describe file( \" #{ __FILE__ } \" ) do it { should exist } end end " )
out . stdout . must_include '1 successful'
out . stdout . must_include '0 failures'
end
it 'runs controls with multiple tests' do
out = do_shell ( " control \" test \" do describe file( \" #{ __FILE__ } \" ) do it { should exist } end; describe file( \" foo/bar/baz \" ) do it { should exist } end end " )
2016-09-14 16:20:32 +00:00
out . stdout . must_include '0 successful'
2017-08-23 14:29:09 +00:00
out . stdout . must_include '1 failure'
2016-08-19 22:58:39 +00:00
end
it 'reruns controls when redefined' do
out = do_shell ( " control \" test \" do describe file( \" #{ __FILE__ } \" ) do it { should exist } end end \n control \" test \" do describe file( \" foo/bar/baz \" ) do it { should exist } end end " )
out . stdout . must_include '1 successful'
2017-08-23 14:29:09 +00:00
out . stdout . must_include '1 failure'
2016-08-19 22:58:39 +00:00
end
end
end