mirror of
https://github.com/inspec/inspec
synced 2025-02-16 22:18:38 +00:00
Prevent stack trace from being set to the user
This also verifies Habitat setup during create as well Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
This commit is contained in:
parent
1ac435bceb
commit
7168c41c09
2 changed files with 30 additions and 16 deletions
|
@ -18,8 +18,12 @@ module InspecPlugins
|
||||||
|
|
||||||
def create
|
def create
|
||||||
logger.info("Creating a Habitat artifact for '#{@path}'...")
|
logger.info("Creating a Habitat artifact for '#{@path}'...")
|
||||||
|
|
||||||
|
# Need to create working directory first so `ensure` doesn't error
|
||||||
working_dir = create_working_dir
|
working_dir = create_working_dir
|
||||||
|
|
||||||
habitat_config = read_habitat_config
|
habitat_config = read_habitat_config
|
||||||
|
verify_habitat_setup(habitat_config)
|
||||||
|
|
||||||
output_dir = @options[:output_dir] || Dir.pwd
|
output_dir = @options[:output_dir] || Dir.pwd
|
||||||
unless File.directory?(output_dir)
|
unless File.directory?(output_dir)
|
||||||
|
@ -27,8 +31,6 @@ module InspecPlugins
|
||||||
'or does not exist.')
|
'or does not exist.')
|
||||||
end
|
end
|
||||||
|
|
||||||
verify_habitat_setup(habitat_config)
|
|
||||||
|
|
||||||
duplicated_profile = duplicate_profile(@path, working_dir)
|
duplicated_profile = duplicate_profile(@path, working_dir)
|
||||||
prepare_profile!(duplicated_profile)
|
prepare_profile!(duplicated_profile)
|
||||||
|
|
||||||
|
@ -44,8 +46,10 @@ module InspecPlugins
|
||||||
logger.debug(e.backtrace.join("\n"))
|
logger.debug(e.backtrace.join("\n"))
|
||||||
exit_with_error('Unable to create Habitat artifact.')
|
exit_with_error('Unable to create Habitat artifact.')
|
||||||
ensure
|
ensure
|
||||||
logger.debug("Deleting working directory #{working_dir}")
|
if Dir.exist?(working_dir)
|
||||||
FileUtils.rm_rf(working_dir)
|
logger.debug("Deleting working directory #{working_dir}")
|
||||||
|
FileUtils.rm_rf(working_dir)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup(profile = profile_from_path(@path))
|
def setup(profile = profile_from_path(@path))
|
||||||
|
@ -265,7 +269,7 @@ module InspecPlugins
|
||||||
logger.error(error_msg)
|
logger.error(error_msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
raise
|
exit 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,6 +29,11 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
{ output_dir: @output_dir },
|
{ output_dir: @output_dir },
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@mock_hab_config = {
|
||||||
|
'auth_token' => 'FAKETOKEN',
|
||||||
|
'origin' => 'fake_origin',
|
||||||
|
}
|
||||||
|
|
||||||
Inspec::Log.level(:fatal)
|
Inspec::Log.level(:fatal)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -45,16 +50,18 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert_raises { profile.create }
|
assert_raises(SystemExit) { profile.create }
|
||||||
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_create
|
def test_create
|
||||||
file_count = Dir.glob(File.join(@test_profile_path, '**/*')).count
|
file_count = Dir.glob(File.join(@test_profile_path, '**/*')).count
|
||||||
|
|
||||||
@hab_profile.stub :verify_habitat_setup, nil do
|
@hab_profile.stub :read_habitat_config, @mock_hab_config do
|
||||||
@hab_profile.stub :build_hart, @fake_hart_file do
|
@hab_profile.stub :verify_habitat_setup, nil do
|
||||||
@hab_profile.create
|
@hab_profile.stub :build_hart, @fake_hart_file do
|
||||||
|
@hab_profile.create
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -74,7 +81,7 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
cmd.expect(:run_command, nil)
|
cmd.expect(:run_command, nil)
|
||||||
|
|
||||||
Mixlib::ShellOut.stub :new, cmd, 'hab --version' do
|
Mixlib::ShellOut.stub :new, cmd, 'hab --version' do
|
||||||
assert_raises { @hab_profile.create }
|
assert_raises(SystemExit) { @hab_profile.create }
|
||||||
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -82,7 +89,7 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_upload
|
def test_upload
|
||||||
@hab_profile.stub :read_habitat_config, { 'auth_token' => 'FAKETOKEN' } do
|
@hab_profile.stub :read_habitat_config, @mock_hab_config do
|
||||||
@hab_profile.stub :create, @fake_hart_file do
|
@hab_profile.stub :create, @fake_hart_file do
|
||||||
@hab_profile.stub :upload_hart, nil do
|
@hab_profile.stub :upload_hart, nil do
|
||||||
@hab_profile.upload
|
@hab_profile.upload
|
||||||
|
@ -94,7 +101,7 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
|
|
||||||
def test_upload_raises_if_no_habitat_auth_token_is_found
|
def test_upload_raises_if_no_habitat_auth_token_is_found
|
||||||
@hab_profile.stub :read_habitat_config, {} do
|
@hab_profile.stub :read_habitat_config, {} do
|
||||||
assert_raises { @hab_profile.upload(@fake_hart_file) }
|
assert_raises(SystemExit) { @hab_profile.upload }
|
||||||
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -151,7 +158,7 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
bad_profile_path,
|
bad_profile_path,
|
||||||
backend: Inspec::Backend.create(Inspec::Config.mock),
|
backend: Inspec::Backend.create(Inspec::Config.mock),
|
||||||
)
|
)
|
||||||
assert_raises { @hab_profile.send(:verify_profile, bad_profile) }
|
assert_raises(SystemExit) { @hab_profile.send(:verify_profile, bad_profile) }
|
||||||
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -193,9 +200,10 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
mock = MiniTest::Mock.new
|
mock = MiniTest::Mock.new
|
||||||
mock.expect(:run_command, nil)
|
mock.expect(:run_command, nil)
|
||||||
mock.expect(:error?, true)
|
mock.expect(:error?, true)
|
||||||
|
mock.expect(:stderr, 'This would be an error message')
|
||||||
|
|
||||||
Mixlib::ShellOut.stub(:new, mock) do
|
Mixlib::ShellOut.stub(:new, mock) do
|
||||||
assert_raises { @hab_profile.send(:verify_habitat_setup, {}) }
|
assert_raises(SystemExit) { @hab_profile.send(:verify_habitat_setup, {}) }
|
||||||
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
||||||
end
|
end
|
||||||
mock.verify
|
mock.verify
|
||||||
|
@ -207,7 +215,7 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
mock.expect(:error?, false)
|
mock.expect(:error?, false)
|
||||||
|
|
||||||
Mixlib::ShellOut.stub(:new, mock) do
|
Mixlib::ShellOut.stub(:new, mock) do
|
||||||
assert_raises { @hab_profile.send(:verify_habitat_setup, {}) }
|
assert_raises(SystemExit) { @hab_profile.send(:verify_habitat_setup, {}) }
|
||||||
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
||||||
end
|
end
|
||||||
mock.verify
|
mock.verify
|
||||||
|
@ -221,9 +229,11 @@ class InspecPlugins::Habitat::ProfileTest < MiniTest::Unit::TestCase
|
||||||
mock = MiniTest::Mock.new
|
mock = MiniTest::Mock.new
|
||||||
mock.expect(:run_command, nil)
|
mock.expect(:run_command, nil)
|
||||||
mock.expect(:error?, true)
|
mock.expect(:error?, true)
|
||||||
|
mock.expect(:stdout, 'This would contain output from `hab`')
|
||||||
|
mock.expect(:stderr, 'This would be an error message')
|
||||||
|
|
||||||
Mixlib::ShellOut.stub(:new, mock) do
|
Mixlib::ShellOut.stub(:new, mock) do
|
||||||
assert_raises { @hab_profile.send(:upload_hart, @fake_hart_file, {}) }
|
assert_raises(SystemExit) { @hab_profile.send(:upload_hart, @fake_hart_file, {}) }
|
||||||
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue