2019-06-14 14:55:32 +00:00
require " functional/helper "
require " fileutils "
require " tmpdir "
describe " running profiles with git-based dependencies " do
include FunctionalHelper
let ( :git_profiles ) { " #{ profile_path } /git-fetcher " }
#======================================================================#
# Git Repo Setup
#======================================================================#
2019-07-30 17:42:24 +00:00
fixture_repos = %w{ basic-local git-repo-01 }
2019-06-14 14:55:32 +00:00
before ( :all ) do
2020-06-03 08:38:10 +00:00
skip_windows! # Right now, this is due to symlinking, break executes on L24 <nickchecked>
2020-08-17 12:27:58 +00:00
# Stacktrace on windows machine:
# Error:
# running profiles with git-based dependencies::running a profile with an ./ relative path dependency#test_0001_should find the top-level profile in the git-referenced child profile and execute that:
# Errno::ENOENT: No such file or directory @ apply2files - C:/Users/some/path/inspec/inspec/test/fixtures/profiles/git-fetcher/basic-local/.git
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:1437:in `unlink'
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:1437:in `block in remove_file'
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:1445:in `platform_support'
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:1436:in `remove_file'
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:775:in `remove_file'
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:562:in `block in rm'
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:561:in `each'
# C:/Ruby/lib/ruby/2.6.0/fileutils.rb:561:in `rm'
# C:/Users/some/path/inspec/inspec/test/functional/git_fetcher_test.rb:31:in `block (3 levels) in <top (required)>'
# C:/Users/some/path/inspec/inspec/test/functional/git_fetcher_test.rb:29:in `each'
# C:/Users/some/path/inspec/inspec/test/functional/git_fetcher_test.rb:29:in `block (2 levels) in <top (required)>'"
2019-06-14 14:55:32 +00:00
# We need a git repo for some of the profile test fixtures,
# but we can't store those directly in git.
# Here, one approach is to store the .git/ directory under a
# different name and then symlink to its proper name.
fixture_repos . each do | profile_name |
link_src = " #{ git_profiles } / #{ profile_name } /git-fixture "
link_dst = " #{ git_profiles } / #{ profile_name } /.git "
FileUtils . ln_sf ( link_src , link_dst ) # -f to tolerate existing links created during manual testing
end
end
after ( :all ) do
fixture_repos . each do | profile_name |
link = " #{ git_profiles } / #{ profile_name } /.git "
FileUtils . rm ( link )
end
end
2019-06-18 16:52:25 +00:00
#======================================================================#
# Custom Local Assertions
#======================================================================#
def assert_relative_fetch_works ( profile_name , expected_profiles , expected_controls )
run_result = run_inspec_process ( " exec #{ git_profiles } / #{ profile_name } " , json : true )
assert_empty run_result . stderr
2019-10-21 23:13:03 +00:00
assert_json_controls_passing ( run_result )
2019-06-18 16:52:25 +00:00
# Should know about the top-level profile and the child profile
2019-10-21 23:13:03 +00:00
assert_equal expected_profiles , ( @json [ " profiles " ] . map { | p | p [ " name " ] } )
2019-06-18 16:52:25 +00:00
2019-10-21 23:13:03 +00:00
controls = @json [ " profiles " ] . map { | p | p [ " controls " ] } . flatten . map { | c | c [ " id " ] } . uniq
2019-06-18 16:52:25 +00:00
# Should have controls from the top-level and included child profile
expected_controls . each { | control | assert_includes controls , control }
# should not have controls from the profile defined at the top of the repo of the child profile
refute_includes controls , " red-dye "
end
2019-06-14 14:55:32 +00:00
#======================================================================#
# Basic Git Fetching
#======================================================================#
describe " running a profile with a basic local dependency " do
it " should work on a local checkout " do
run_result = run_inspec_process ( " exec #{ git_profiles } /basic-local " , json : true )
assert_empty run_result . stderr
2019-10-21 23:13:03 +00:00
assert_json_controls_passing ( run_result )
2019-06-14 14:55:32 +00:00
end
end
# describe "running a profile with a basic remote dependency"
2019-06-18 16:52:25 +00:00
# TODO: move private SSH+git test from inspec_exec_test to here
2019-06-14 14:55:32 +00:00
#======================================================================#
# Revision Selection
#======================================================================#
2019-06-21 02:12:16 +00:00
# NOTE: test branch, rev, and tag capabilities are (lighty) tested in unit tests
2019-06-14 14:55:32 +00:00
#======================================================================#
# Relative Path Support
#======================================================================#
#------------ Happy Cases for Relative Path Support -------------------#
2019-06-17 17:25:19 +00:00
describe " running a profile with a shallow relative path dependency " do
it " should find the relative path profile and execute exactly those controls " do
2019-07-30 17:42:24 +00:00
assert_relative_fetch_works ( " relative-shallow " , %w{ relative-shallow child-01 } , %w{ top-level-01 child-01 } )
2019-06-17 17:25:19 +00:00
end
end
2019-06-18 16:28:26 +00:00
describe " running a profile with a deep relative path dependency " do
it " should find the relative path profile and execute exactly those controls " do
2019-07-30 17:42:24 +00:00
assert_relative_fetch_works ( " relative-deep " , %w{ relative-deep child-02 } , %w{ relative-deep-01 child-02 } )
2019-06-18 16:52:25 +00:00
end
end
2019-06-18 16:28:26 +00:00
2019-06-18 16:52:25 +00:00
describe " running a profile with a combination of relative path dependencies " do
it " should find the relative path profiles and execute exactly those controls " do
assert_relative_fetch_works (
" relative-combo " ,
2019-07-30 17:42:24 +00:00
%w{ relative-combo child-01 child-02 } ,
%w{ relative-combo-01 child-01 child-02 }
2019-06-18 16:52:25 +00:00
)
2019-06-18 16:28:26 +00:00
end
end
2019-06-14 14:55:32 +00:00
#------------ Edge Cases for Relative Path Support -------------------#
2019-06-18 19:22:14 +00:00
2019-06-18 17:13:05 +00:00
describe " running a profile with an '' relative path dependency " do
it " should find the top-level profile in the git-referenced child profile and execute that " do
2019-07-30 17:42:24 +00:00
assert_relative_fetch_works ( " relative-empty " , %w{ relative-empty basic-local } , %w{ relative-empty-01 basic-local-01 } )
2019-06-18 17:13:05 +00:00
end
end
2019-06-18 19:22:14 +00:00
describe " running a profile with an ./ relative path dependency " do
it " should find the top-level profile in the git-referenced child profile and execute that " do
2019-07-30 17:42:24 +00:00
assert_relative_fetch_works ( " relative-dot-slash " , %w{ relative-dot-slash basic-local } , %w{ relative-dot-slash-01 basic-local-01 } )
2019-06-18 19:22:14 +00:00
end
end
2019-06-18 22:06:51 +00:00
describe " running a profile with a relative path dependency that does not exist " do
it " should fail gracefully " do
run_result = run_inspec_process ( " exec #{ git_profiles } /relative-nonesuch " )
assert_empty run_result . stdout
refute_includes run_result . stderr , " Errno::ENOENT " # No ugly file missing error
assert_equal 1 , run_result . stderr . lines . count # Not a giant stacktrace
# Spot check important parts of the message
assert_includes run_result . stderr , " Cannot find relative path "
assert_includes run_result . stderr , " no/such/path " # the actual missing path
assert_includes run_result . stderr , " profile in git repo "
# The containing git repo (the only identifier the user will have)
2019-11-09 03:08:20 +00:00
assert_includes run_result . stderr , " test/fixtures/profiles/git-fetcher/git-repo-01 "
2019-06-21 02:12:16 +00:00
assert_exit_code ( 1 , run_result ) # General user error
2019-06-18 22:06:51 +00:00
end
end
2019-06-14 14:55:32 +00:00
end