mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
Rewrite fltertable functional tests to DRY up and avoid --json-config
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
parent
144c482a24
commit
91c0f1e83b
2 changed files with 107 additions and 167 deletions
|
@ -1,192 +1,126 @@
|
|||
require 'functional/helper'
|
||||
|
||||
describe '2943 inspec exec for filter table profile, method mode for `where' do
|
||||
describe 'filtertable functional tests' do
|
||||
include FunctionalHelper
|
||||
let(:run_opts) { { json: true, ignore_rspec_deprecations: true } }
|
||||
let(:ft_profile_path) { File.join(profile_path, 'filter_table') }
|
||||
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'2943_pass_undeclared_field_in_hash',
|
||||
'2943_pass_irregular_row_key',
|
||||
'2943_pass_raise_error_when_key_not_in_data',
|
||||
'2943_pass_allow_symbols_as_criteria_when_data_is_string_keyed',
|
||||
'2943_pass_allow_strings_as_criteria_when_data_is_symbol_keyed',
|
||||
'2943_pass_no_error_when_no_data',
|
||||
]
|
||||
|
||||
cmd = 'exec ' + File.join(profile_path, 'filter_table')
|
||||
cmd += ' --reporter json --no-create-lockfile'
|
||||
cmd += ' --controls ' + controls.join(' ')
|
||||
cmd = inspec(cmd)
|
||||
|
||||
# RSpec keeps issuing a deprecation count to stdout; I can't seem to disable it.
|
||||
output = cmd.stdout.split("\n").reject {|line| line =~ /deprecation/}.join("\n")
|
||||
data = JSON.parse(output)
|
||||
failed_controls = data['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
control_hash = {}
|
||||
failed_controls.each do |ctl|
|
||||
control_hash[ctl['id']] = ctl['results'][0]['message']
|
||||
end
|
||||
control_hash.must_be_empty
|
||||
cmd.exit_status.must_equal 0
|
||||
def run_result_for_controls(controls)
|
||||
cmd = 'exec ' + ft_profile_path + ' --controls ' + controls.join(' ')
|
||||
run_inspec_process(cmd, run_opts)
|
||||
end
|
||||
|
||||
it 'negative tests should fail but not abort' do
|
||||
controls = [
|
||||
'2943_fail_derail_check',
|
||||
]
|
||||
def failed_control_test_outcomes(run_result)
|
||||
failed_controls = run_result.payload.json['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
|
||||
cmd = inspec('exec ' + File.join(profile_path, 'filter_table') + ' --reporter json --no-create-lockfile' + ' --controls ' + controls.join(' '))
|
||||
|
||||
data = JSON.parse(cmd.stdout)
|
||||
failed_controls = data['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
# Re-package any failed controls into a hash mapping control_id => message
|
||||
# We will later test against this, as it provides more informative test output
|
||||
control_hash = {}
|
||||
failed_controls.each do |ctl|
|
||||
control_hash[ctl['id']] = ctl['results'][0]['message']
|
||||
end
|
||||
control_hash
|
||||
end
|
||||
|
||||
def expect_clean_run(controls)
|
||||
run_result = run_result_for_controls(controls)
|
||||
outcome_hash = failed_control_test_outcomes(run_result)
|
||||
outcome_hash.must_be_empty
|
||||
run_result.exit_status.must_equal 0
|
||||
end
|
||||
|
||||
def expect_all_fail_run(controls)
|
||||
run_result = run_result_for_controls(controls)
|
||||
outcome_hash = failed_control_test_outcomes(run_result)
|
||||
|
||||
controls.each do |expected_control|
|
||||
control_hash.keys.must_include(expected_control)
|
||||
outcome_hash.keys.must_include(expected_control)
|
||||
end
|
||||
|
||||
cmd.stderr_ignore_deprecations.must_equal ''
|
||||
cmd.exit_status.must_equal 100
|
||||
end
|
||||
end
|
||||
|
||||
describe '3103 default methods for filter table' do
|
||||
include FunctionalHelper
|
||||
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'3103_where_defined',
|
||||
'3103_entries_defined',
|
||||
'3103_raw_data_defined',
|
||||
'3103_exists_defined',
|
||||
'3103_count_defined',
|
||||
]
|
||||
|
||||
cmd = 'exec ' + File.join(profile_path, 'filter_table')
|
||||
cmd += ' --reporter json --no-create-lockfile'
|
||||
cmd += ' --controls ' + controls.join(' ')
|
||||
cmd = inspec(cmd)
|
||||
|
||||
# RSpec keeps issuing a deprecation count to stdout
|
||||
# See https://github.com/inspec/inspec/pull/3560
|
||||
output = cmd.stdout.split("\n").reject {|line| line =~ /deprecation/}.join("\n")
|
||||
data = JSON.parse(output)
|
||||
failed_controls = data['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
control_hash = {}
|
||||
failed_controls.each do |ctl|
|
||||
control_hash[ctl['id']] = ctl['results'][0]['message']
|
||||
end
|
||||
control_hash.must_be_empty
|
||||
cmd.exit_status.must_equal 0
|
||||
end
|
||||
end
|
||||
|
||||
describe '2370 lazy_load for filter table' do
|
||||
include FunctionalHelper
|
||||
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'2370_where_block',
|
||||
'2370_where_block_only_referenced',
|
||||
'2370_where_method',
|
||||
'2370_where_method_only_referenced',
|
||||
'2370_populate_once',
|
||||
'2370_no_side_populate',
|
||||
'2370_no_clobber',
|
||||
'2370_list_property',
|
||||
'2370_list_property_filter_method',
|
||||
'2370_list_property_filter_block',
|
||||
'2370_no_rows',
|
||||
]
|
||||
|
||||
cmd = 'exec ' + File.join(profile_path, 'filter_table')
|
||||
cmd += ' --reporter json --no-create-lockfile'
|
||||
cmd += ' --controls ' + controls.join(' ')
|
||||
cmd = inspec(cmd)
|
||||
|
||||
# RSpec keeps issuing a deprecation count to stdout
|
||||
# See https://github.com/inspec/inspec/pull/3560
|
||||
output = cmd.stdout.split("\n").reject {|line| line =~ /deprecation/}.join("\n")
|
||||
data = JSON.parse(output)
|
||||
failed_controls = data['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
control_hash = {}
|
||||
failed_controls.each do |ctl|
|
||||
control_hash[ctl['id']] = ctl['results'][0]['message']
|
||||
end
|
||||
control_hash.must_be_empty
|
||||
cmd.exit_status.must_equal 0
|
||||
run_result.stderr_ignore_deprecations.must_equal '' # TODO: we have a cli_option_json_config triggering somewhere
|
||||
run_result.exit_status.must_equal 100
|
||||
end
|
||||
|
||||
it 'negative tests should fail but not abort' do
|
||||
controls = [
|
||||
'2370_fail_proc_handle_exception',
|
||||
]
|
||||
|
||||
cmd = inspec('exec ' + File.join(profile_path, 'filter_table') + ' --reporter json --no-create-lockfile' + ' --controls ' + controls.join(' '))
|
||||
|
||||
data = JSON.parse(cmd.stdout)
|
||||
failed_controls = data['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
control_hash = {}
|
||||
failed_controls.each do |ctl|
|
||||
control_hash[ctl['id']] = ctl['results'][0]['message']
|
||||
end
|
||||
controls.each do |expected_control|
|
||||
control_hash.keys.must_include(expected_control)
|
||||
describe '2943 inspec exec for filter table profile, method mode for `where' do
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'2943_pass_undeclared_field_in_hash',
|
||||
'2943_pass_irregular_row_key',
|
||||
'2943_pass_raise_error_when_key_not_in_data',
|
||||
'2943_pass_allow_symbols_as_criteria_when_data_is_string_keyed',
|
||||
'2943_pass_allow_strings_as_criteria_when_data_is_symbol_keyed',
|
||||
'2943_pass_no_error_when_no_data',
|
||||
]
|
||||
expect_clean_run(controls)
|
||||
end
|
||||
|
||||
cmd.stderr_ignore_deprecations.must_equal ''
|
||||
cmd.exit_status.must_equal 100
|
||||
it 'negative tests should fail but not abort' do
|
||||
controls = [
|
||||
'2943_fail_derail_check',
|
||||
]
|
||||
expect_all_fail_run(controls)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '2929 exceptions in block-mode where' do
|
||||
include FunctionalHelper
|
||||
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'2929_exception_in_where',
|
||||
]
|
||||
|
||||
cmd = inspec('exec ' + File.join(profile_path, 'filter_table') + ' --reporter json --no-create-lockfile' + ' --controls ' + controls.join(' '))
|
||||
|
||||
data = JSON.parse(cmd.stdout)
|
||||
failed_controls = data['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
control_hash = {}
|
||||
failed_controls.each do |ctl|
|
||||
control_hash[ctl['id']] = ctl['results'][0]['message']
|
||||
describe '3103 default methods for filter table' do
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'3103_where_defined',
|
||||
'3103_entries_defined',
|
||||
'3103_raw_data_defined',
|
||||
'3103_exists_defined',
|
||||
'3103_count_defined',
|
||||
]
|
||||
expect_clean_run(controls)
|
||||
end
|
||||
control_hash.must_be_empty
|
||||
cmd.stderr_ignore_deprecations.must_equal ''
|
||||
cmd.exit_status.must_equal 0
|
||||
end
|
||||
end
|
||||
|
||||
describe '3110 do not expose block-valued properties in raw data' do
|
||||
include FunctionalHelper
|
||||
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'3110_entries_defined',
|
||||
'3110_raw_data_defined',
|
||||
]
|
||||
|
||||
cmd = 'exec ' + File.join(profile_path, 'filter_table')
|
||||
cmd += ' --reporter json --no-create-lockfile'
|
||||
cmd += ' --controls ' + controls.join(' ')
|
||||
cmd = inspec(cmd)
|
||||
|
||||
# RSpec keeps issuing a deprecation count to stdout
|
||||
# See https://github.com/inspec/inspec/pull/3560
|
||||
output = cmd.stdout.split("\n").reject {|line| line =~ /deprecation/}.join("\n")
|
||||
data = JSON.parse(output)
|
||||
failed_controls = data['profiles'][0]['controls'].select { |ctl| ctl['results'][0]['status'] == 'failed' }
|
||||
control_hash = {}
|
||||
failed_controls.each do |ctl|
|
||||
control_hash[ctl['id']] = ctl['results'][0]['message']
|
||||
describe '2370 lazy_load for filter table' do
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'2370_where_block',
|
||||
'2370_where_block_only_referenced',
|
||||
'2370_where_method',
|
||||
'2370_where_method_only_referenced',
|
||||
'2370_populate_once',
|
||||
'2370_no_side_populate',
|
||||
'2370_no_clobber',
|
||||
'2370_list_property',
|
||||
'2370_list_property_filter_method',
|
||||
'2370_list_property_filter_block',
|
||||
'2370_no_rows',
|
||||
]
|
||||
expect_clean_run(controls)
|
||||
end
|
||||
|
||||
it 'negative tests should fail but not abort' do
|
||||
controls = [
|
||||
'2370_fail_proc_handle_exception',
|
||||
]
|
||||
expect_all_fail_run(controls)
|
||||
end
|
||||
control_hash.must_be_empty
|
||||
cmd.exit_status.must_equal 0
|
||||
end
|
||||
end
|
||||
|
||||
describe '2929 exceptions in block-mode where' do
|
||||
include FunctionalHelper
|
||||
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'2929_exception_in_where',
|
||||
]
|
||||
expect_clean_run(controls)
|
||||
end
|
||||
end
|
||||
|
||||
describe '3110 do not expose block-valued properties in raw data' do
|
||||
include FunctionalHelper
|
||||
|
||||
it 'positive tests should pass' do
|
||||
controls = [
|
||||
'3110_entries_defined',
|
||||
'3110_raw_data_defined',
|
||||
]
|
||||
expect_clean_run(controls)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -162,6 +162,12 @@ module FunctionalHelper
|
|||
run_result = inspec(command_line, prefix)
|
||||
end
|
||||
|
||||
if opts[:ignore_rspec_deprecations]
|
||||
# RSpec keeps issuing a deprecation count to stdout when .should is called explicitly
|
||||
# See https://github.com/inspec/inspec/pull/3560
|
||||
run_result.stdout.sub!("\n1 deprecation warning total\n", '')
|
||||
end
|
||||
|
||||
if opts[:json]
|
||||
begin
|
||||
run_result.payload.json = JSON.parse(run_result.stdout)
|
||||
|
|
Loading…
Reference in a new issue