2019-06-11 22:24:35 +00:00
|
|
|
require "mixlib/log"
|
|
|
|
require "fileutils"
|
|
|
|
require "minitest/autorun"
|
2019-06-10 22:04:14 +00:00
|
|
|
require "inspec/backend"
|
2019-06-11 22:24:35 +00:00
|
|
|
require_relative "../../lib/inspec-habitat/profile.rb"
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-04-24 00:21:31 +00:00
|
|
|
class InspecPlugins::Habitat::ProfileTest < Minitest::Test
|
2019-02-07 06:43:50 +00:00
|
|
|
def setup
|
|
|
|
@tmpdir = Dir.mktmpdir
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
@output_dir = File.join(@tmpdir, "output")
|
2019-02-07 06:43:50 +00:00
|
|
|
FileUtils.mkdir(@output_dir)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
@fake_hart_file = FileUtils.touch(File.join(@tmpdir, "fake-hart.hart"))[0]
|
2019-02-07 06:43:50 +00:00
|
|
|
|
|
|
|
# Path from `__FILE__` needed to support running tests in `inspec/inspec`
|
|
|
|
@test_profile_path = File.join(
|
|
|
|
File.expand_path(File.dirname(__FILE__)),
|
2019-06-11 22:24:35 +00:00
|
|
|
"../",
|
|
|
|
"support",
|
|
|
|
"example_profile"
|
2019-02-07 06:43:50 +00:00
|
|
|
)
|
|
|
|
@test_profile = Inspec::Profile.for_target(
|
|
|
|
@test_profile_path,
|
2019-06-11 22:24:35 +00:00
|
|
|
backend: Inspec::Backend.create(Inspec::Config.mock)
|
2017-02-22 13:36:42 +00:00
|
|
|
)
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
@hab_profile = InspecPlugins::Habitat::Profile.new(
|
|
|
|
@test_profile_path,
|
2019-06-11 22:24:35 +00:00
|
|
|
{ output_dir: @output_dir }
|
2019-02-07 06:43:50 +00:00
|
|
|
)
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-28 19:33:21 +00:00
|
|
|
@mock_hab_config = {
|
2019-06-11 22:24:35 +00:00
|
|
|
"auth_token" => "FAKETOKEN",
|
|
|
|
"origin" => "fake_origin",
|
2019-02-28 19:33:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 17:10:07 +00:00
|
|
|
Inspec::Log.level(:fatal)
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def after_run
|
|
|
|
FileUtils.remove_entry_secure(@tmpdir)
|
2017-03-24 20:28:00 +00:00
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_create_raises_if_output_dir_does_not_exist
|
|
|
|
profile = InspecPlugins::Habitat::Profile.new(
|
|
|
|
@test_profile_path,
|
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
output_dir: "/not/a/real/path",
|
|
|
|
log_level: "fatal",
|
|
|
|
}
|
2019-02-07 06:43:50 +00:00
|
|
|
)
|
2017-03-24 20:28:00 +00:00
|
|
|
|
2019-02-28 19:33:21 +00:00
|
|
|
assert_raises(SystemExit) { profile.create }
|
2019-02-07 06:43:50 +00:00
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create
|
2019-06-11 22:24:35 +00:00
|
|
|
file_count = Dir.glob(File.join(@test_profile_path, "**/*")).count
|
2017-03-24 20:28:00 +00:00
|
|
|
|
2019-02-28 19:33:21 +00:00
|
|
|
@hab_profile.stub :read_habitat_config, @mock_hab_config do
|
|
|
|
@hab_profile.stub :verify_habitat_setup, nil do
|
|
|
|
@hab_profile.stub :build_hart, @fake_hart_file do
|
|
|
|
@hab_profile.create
|
|
|
|
end
|
2017-03-24 20:28:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
# It should not modify target profile
|
2019-06-11 22:24:35 +00:00
|
|
|
new_file_count = Dir.glob(File.join(@test_profile_path, "**/*")).count
|
2019-02-07 06:43:50 +00:00
|
|
|
assert_equal new_file_count, file_count
|
2017-03-24 20:28:00 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
# It should create 1 Habitat artifact
|
2019-06-11 22:24:35 +00:00
|
|
|
output_files = Dir.glob(File.join(@output_dir, "**/*"))
|
2019-02-07 06:43:50 +00:00
|
|
|
assert_equal 1, output_files.count
|
2019-06-11 22:24:35 +00:00
|
|
|
assert_equal "fake-hart.hart", File.basename(output_files.first)
|
2019-02-07 06:43:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_rasies_if_habitat_is_not_installed
|
2019-04-24 00:21:31 +00:00
|
|
|
cmd = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
cmd.expect(:error?, true)
|
|
|
|
cmd.expect(:run_command, nil)
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
Mixlib::ShellOut.stub :new, cmd, "hab --version" do
|
2019-02-28 19:33:21 +00:00
|
|
|
assert_raises(SystemExit) { @hab_profile.create }
|
2019-02-07 06:43:50 +00:00
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
2017-03-24 20:28:00 +00:00
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
cmd.verify
|
|
|
|
end
|
2017-03-24 20:28:00 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_upload
|
2019-02-28 19:33:21 +00:00
|
|
|
@hab_profile.stub :read_habitat_config, @mock_hab_config do
|
2019-02-07 06:43:50 +00:00
|
|
|
@hab_profile.stub :create, @fake_hart_file do
|
|
|
|
@hab_profile.stub :upload_hart, nil do
|
|
|
|
@hab_profile.upload
|
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
|
|
|
end
|
2017-03-24 20:28:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_upload_raises_if_no_habitat_auth_token_is_found
|
|
|
|
@hab_profile.stub :read_habitat_config, {} do
|
2019-02-28 19:33:21 +00:00
|
|
|
assert_raises(SystemExit) { @hab_profile.upload }
|
2019-02-07 06:43:50 +00:00
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_create_working_dir
|
2019-06-11 22:24:35 +00:00
|
|
|
Dir.stub :mktmpdir, "/tmp/fakedir" do
|
|
|
|
assert_equal "/tmp/fakedir", @hab_profile.send(:create_working_dir)
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
2019-02-07 06:43:50 +00:00
|
|
|
end
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_duplicate_profile
|
|
|
|
current_profile = @test_profile
|
|
|
|
duplicated_profile = @hab_profile.send(:duplicate_profile,
|
2019-07-09 00:20:30 +00:00
|
|
|
@test_profile_path,
|
|
|
|
@tmpdir)
|
2019-02-07 06:43:50 +00:00
|
|
|
assert duplicated_profile.is_a?(Inspec::Profile)
|
|
|
|
assert duplicated_profile.sha256 == current_profile.sha256.to_s
|
|
|
|
refute_same duplicated_profile.root_path, current_profile.root_path
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_profile_from_path
|
|
|
|
profile = @hab_profile.send(:profile_from_path, @test_profile_path)
|
|
|
|
assert profile.is_a?(Inspec::Profile)
|
|
|
|
end
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_copy_profile_to_working_dir
|
|
|
|
duplicated_profile = @hab_profile.send(:duplicate_profile,
|
2019-07-09 00:20:30 +00:00
|
|
|
@test_profile_path,
|
|
|
|
@tmpdir)
|
2019-02-07 06:43:50 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
dst = File.join(@tmpdir, "working_dir")
|
2019-02-07 06:43:50 +00:00
|
|
|
FileUtils.mkdir_p(dst)
|
|
|
|
@hab_profile.send(:copy_profile_to_working_dir, duplicated_profile, dst)
|
|
|
|
|
|
|
|
expected_files = %w{
|
|
|
|
README.md
|
|
|
|
inspec.yml
|
|
|
|
example.rb
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
actual_files = Dir.glob(File.join(dst, "**/*")).map do |path|
|
2019-02-07 06:43:50 +00:00
|
|
|
next unless File.file?(path)
|
2019-07-09 00:20:30 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
File.basename(path)
|
|
|
|
end.compact
|
|
|
|
|
|
|
|
assert(actual_files.sort == expected_files.sort)
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_verify_profile_raises_if_profile_is_not_valid
|
2019-06-11 22:24:35 +00:00
|
|
|
bad_profile_path = File.join(@tmpdir, "bad_profile")
|
2019-02-07 06:43:50 +00:00
|
|
|
FileUtils.mkdir_p(File.join(bad_profile_path))
|
2019-06-11 22:24:35 +00:00
|
|
|
FileUtils.touch(File.join(bad_profile_path, "inspec.yml"))
|
2019-02-07 06:43:50 +00:00
|
|
|
bad_profile = Inspec::Profile.for_target(
|
|
|
|
bad_profile_path,
|
2019-06-11 22:24:35 +00:00
|
|
|
backend: Inspec::Backend.create(Inspec::Config.mock)
|
2019-02-07 06:43:50 +00:00
|
|
|
)
|
2019-02-28 19:33:21 +00:00
|
|
|
assert_raises(SystemExit) { @hab_profile.send(:verify_profile, bad_profile) }
|
2019-02-07 06:43:50 +00:00
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
|
|
|
end
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_vendor_profile_dependencies_does_not_vendor_if_already_vendored
|
2019-04-24 00:21:31 +00:00
|
|
|
mock_lock_file = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock_lock_file.expect(:exist?, true)
|
2019-04-24 00:21:31 +00:00
|
|
|
mock_cache_path = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock_cache_path.expect(:exist?, true)
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-04-24 00:21:31 +00:00
|
|
|
mock = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.expect(:lockfile, mock_lock_file)
|
|
|
|
mock.expect(:cache_path, mock_cache_path)
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
Inspec::ProfileVendor.stub :new, mock do
|
|
|
|
new_profile = @hab_profile.send(:vendor_profile_dependencies!,
|
2019-07-09 00:20:30 +00:00
|
|
|
@test_profile)
|
2019-02-07 06:43:50 +00:00
|
|
|
assert new_profile.is_a?(Inspec::Profile)
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
2019-02-07 06:43:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_vendor_profile_dependencies
|
2019-04-24 00:21:31 +00:00
|
|
|
mock_lock_file = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock_lock_file.expect(:exist?, false)
|
|
|
|
|
2019-04-24 00:21:31 +00:00
|
|
|
mock = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.expect(:lockfile, mock_lock_file)
|
|
|
|
mock.expect(:vendor!, nil)
|
|
|
|
mock.expect(:make_readable, nil)
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
Inspec::ProfileVendor.stub :new, mock do
|
|
|
|
new_profile = @hab_profile.send(:vendor_profile_dependencies!,
|
2019-07-09 00:20:30 +00:00
|
|
|
@test_profile)
|
2019-02-07 06:43:50 +00:00
|
|
|
assert new_profile.is_a?(Inspec::Profile)
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.verify
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_verify_habitat_setup_raises_if_hab_version_errors
|
2019-04-24 00:21:31 +00:00
|
|
|
mock = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.expect(:run_command, nil)
|
|
|
|
mock.expect(:error?, true)
|
2019-06-11 22:24:35 +00:00
|
|
|
mock.expect(:stderr, "This would be an error message")
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
Mixlib::ShellOut.stub(:new, mock) do
|
2019-02-28 19:33:21 +00:00
|
|
|
assert_raises(SystemExit) { @hab_profile.send(:verify_habitat_setup, {}) }
|
2019-02-07 06:43:50 +00:00
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.verify
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
def test_verify_habitat_setup_raises_if_not_habitat_origin
|
2019-04-24 00:21:31 +00:00
|
|
|
mock = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.expect(:run_command, nil)
|
|
|
|
mock.expect(:error?, false)
|
|
|
|
|
|
|
|
Mixlib::ShellOut.stub(:new, mock) do
|
2019-02-28 19:33:21 +00:00
|
|
|
assert_raises(SystemExit) { @hab_profile.send(:verify_habitat_setup, {}) }
|
2019-02-07 06:43:50 +00:00
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Figure out how to stub system()
|
|
|
|
# def test_build_hart
|
|
|
|
# end
|
|
|
|
|
|
|
|
def test_upload_hart_raises_if_hab_pkg_upload_fails
|
2019-04-24 00:21:31 +00:00
|
|
|
mock = Minitest::Mock.new
|
2019-02-07 06:43:50 +00:00
|
|
|
mock.expect(:run_command, nil)
|
|
|
|
mock.expect(:error?, true)
|
2019-06-11 22:24:35 +00:00
|
|
|
mock.expect(:stdout, "This would contain output from `hab`")
|
|
|
|
mock.expect(:stderr, "This would be an error message")
|
2017-02-22 13:36:42 +00:00
|
|
|
|
2019-02-07 06:43:50 +00:00
|
|
|
Mixlib::ShellOut.stub(:new, mock) do
|
2019-02-28 19:33:21 +00:00
|
|
|
assert_raises(SystemExit) { @hab_profile.send(:upload_hart, @fake_hart_file, {}) }
|
2019-02-07 06:43:50 +00:00
|
|
|
# TODO: Figure out how to capture and validate `Inspec::Log.error`
|
2017-02-22 13:36:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|