From 3e1d08cafe39bd5aa420ff707901bdd8e3f68b77 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Tue, 16 Oct 2018 21:23:27 -0700 Subject: [PATCH 01/18] Add support for relative paths to the Git fetcher This adds support for specifying a path to a profile in a Git repo. For example, you could have a Git repo containing multiple profiles and you want to specify a single one like below: ```yaml depends: - name: profile-1 git: https://github.com/myorg/all-my-profiles.git profile_path: profiles/my-specific-profile-1 - name: profile-2 git: https://github.com/myorg/all-my-profiles.git profile_path: profiles/my-specific-profile-2 ``` Signed-off-by: Jerry Aldrich --- docs/profiles.md | 5 +- lib/fetchers/git.rb | 26 +++++-- test/functional/gitfetcher_test.rb | 109 ++++++++++++++++++++++++----- 3 files changed, 112 insertions(+), 28 deletions(-) diff --git a/docs/profiles.md b/docs/profiles.md index 92a698965..ba3c30471 100644 --- a/docs/profiles.md +++ b/docs/profiles.md @@ -212,9 +212,7 @@ depends: ### git -A `git` setting specifies a profile that is located in a git repository, with optional settings for branch, tag, commit, and version. The source location is translated into a URL upon resolution. This type of dependency supports version constraints via semantic versioning as git tags. - -For example: +A `git` setting specifies a profile that is located in a git repository, with optional settings for branch, tag, commit, version, and profile_path. The source location is translated into a URL upon resolution. This type of dependency supports version constraints via semantic versioning as git tags. ```YAML depends: @@ -224,6 +222,7 @@ depends: tag: desired_version commit: pinned_commit version: semver_via_tags + profile_path: optional/path/to/profile ``` ### supermarket diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 61af0f29e..72dc74efd 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -37,10 +37,12 @@ module Fetchers def initialize(remote_url, opts = {}) @branch = opts[:branch] + @profile_directory = nil @tag = opts[:tag] @ref = opts[:ref] @remote_url = remote_url @repo_directory = nil + @target_profile_path = opts[:profile_path] end def fetch(dir) @@ -52,23 +54,35 @@ module Fetchers else Dir.mktmpdir do |tmpdir| checkout(tmpdir) - Inspec::Log.debug("Checkout of #{resolved_ref} successful. Moving checkout to #{dir}") - FileUtils.cp_r(tmpdir + "/.", @repo_directory) + if @target_profile_path + @profile_directory = dir + Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ + "Moving #{@target_profile_path} to #{dir}") + target_profile = File.join(tmpdir, @target_profile_path) + FileUtils.cp_r(target_profile, dir) + else + Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ + "Moving checkout to #{dir}") + FileUtils.cp_r(tmpdir + '/.', dir) + end end end - @repo_directory + @profile_directory || @repo_directory end def cache_key - resolved_ref + return resolved_ref unless @target_profile_path + OpenSSL::Digest::SHA256.hexdigest(resolved_ref + @target_profile_path) end def archive_path - @repo_directory + @profile_directory || @repo_directory end def resolved_source - { git: @remote_url, ref: resolved_ref } + source = { git: @remote_url, ref: resolved_ref } + source[:profile_path] = @target_profile_path if @target_profile_path + source end private diff --git a/test/functional/gitfetcher_test.rb b/test/functional/gitfetcher_test.rb index 1da7f9ea4..fbf9f8747 100644 --- a/test/functional/gitfetcher_test.rb +++ b/test/functional/gitfetcher_test.rb @@ -7,39 +7,100 @@ describe "profiles with git-based dependencies" do include FunctionalHelper before(:all) do @tmpdir = Dir.mktmpdir - @profile_dir = File.join(@tmpdir, "test-profile") - @git_dep_dir = File.join(@tmpdir, "git-dep") + @profile_dir = File.join(@tmpdir, 'test-profile') + @git_dep_dir = File.join(@tmpdir, 'git-dep') + @deep_git_dep_dir = File.join(@tmpdir, 'deep-git-dep') Dir.chdir(@tmpdir) do - inspec("init profile git-dep") - inspec("init profile test-profile") + inspec('init profile git-dep') + inspec('init profile test-profile') + + # InSpec will search directories and execute successfully if there is just + # one profile. We create two here to ensure we execute both as intended. + deep_profiles = File.join(@deep_git_dep_dir, 'profiles') + FileUtils.mkdir_p(deep_profiles) + inspec('init profile ' + File.join(deep_profiles, 'deep-profile')) + inspec('init profile ' + File.join(deep_profiles, 'another-deep-profile')) end - Dir.chdir(@git_dep_dir) do - CMD.run_command("git init") - CMD.run_command("git add .") - CMD.run_command("git config user.name \"test\"") - CMD.run_command("git config user.email \"test@yahoo.com\"") - CMD.run_command("git commit -m \"initial commit\" --no-gpg-sign") - CMD.run_command("git commit -m \"another commit\" --allow-empty --no-gpg-sign") - CMD.run_command("git tag antag") - end - - inspec_yml = YAML.load(File.read(File.join(@profile_dir, "inspec.yml"))) + inspec_yml = YAML.load(File.read(File.join(@profile_dir, 'inspec.yml'))) inspec_yml["depends"] = [ { - "name" => "git-dep", - "git" => @git_dep_dir, - "tag" => "antag", + 'name' => 'git-dep', + 'git' => @git_dep_dir, + 'tag' => 'v1.0' }, + { + 'name' => 'deep-git-dep', + 'git' => @deep_git_dep_dir, + 'profile_path' => 'profiles/deep-profile' + }, + { + 'name' => 'another-deep-git-dep', + 'git' => @deep_git_dep_dir, + 'profile_path' => 'profiles/another-deep-profile' + } ] - File.write(File.join(@profile_dir, "inspec.yml"), YAML.dump(inspec_yml)) + File.write(File.join(@profile_dir, 'inspec.yml'), YAML.dump(inspec_yml)) + + Dir.chdir(@git_dep_dir) do + CMD.run_command('git init') + CMD.run_command('git config user.name "testuser"') + CMD.run_command('git config user.email "testuser@example.com"') + CMD.run_command('git add .') + CMD.run_command('git commit -m "Initial commit" --no-gpg-sign') + CMD.run_command('git tag v1.0') + + # Need to overwrite default test because `/tmp` does not exist on + # Windows and the default generated test checks for `/tmp`. + File.open(File.join('controls', 'example.rb'), 'w') do |f| + f.write <<~EOF + control 'example-1.0' do + describe 'value' do + it { should cmp 'value' } + end + end + EOF + end + end + + Dir.chdir(@deep_git_dep_dir) do + profile_1 = File.join('profiles', 'deep-profile') + profile_2 = File.join('profiles', 'another-deep-profile') + [profile_1, profile_2].each do |profile| + # Need to overwrite default test because `/tmp` does not exist on + # Windows and the default generated test checks for `/tmp`. + File.open(File.join(profile, 'controls', 'example.rb'), 'w') do |f| + f.write <<~EOF + control '#{File.basename(profile)}-control' do + describe 'value' do + it { should cmp 'value' } + end + end + EOF + end + end + CMD.run_command('git init') + CMD.run_command('git config user.name "testuser"') + CMD.run_command('git config user.email "testuser@example.com"') + CMD.run_command('git add .') + CMD.run_command('git commit -m "Initial commit" --no-gpg-sign') + end + + File.open(File.join(@profile_dir, 'controls', 'example.rb'), 'w') do |f| + f.write <<~EOF + include_controls 'git-dep' + include_controls 'deep-git-dep' + include_controls 'another-deep-git-dep' + EOF + end end after(:all) do FileUtils.rm_rf(@tmpdir) end +<<<<<<< HEAD it "executes a profile with a git based dependency" do out = inspec("exec #{@profile_dir} --no-create-lockfile") @@ -47,5 +108,15 @@ describe "profiles with git-based dependencies" do out.stderr.must_equal "" assert_exit_code 0, out +======= + it 'executes a profile with a git based dependencies' do + Dir.mktmpdir do |tmpdir| + out = inspec("exec #{@profile_dir} " \ + "--no-create-lockfile " \ + "--vendor-cache #{tmpdir}") + out.stderr.must_equal '' + out.exit_status.must_equal 0 + end +>>>>>>> Add support for relative paths to the Git fetcher end end From 9c1fa967fd223dea313c5934bb2b1bc86da7b8a0 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Sun, 31 Mar 2019 07:31:06 -0400 Subject: [PATCH 02/18] Rename profile_path to path_within_repo Signed-off-by: Clinton Wolfe --- docs/profiles.md | 2 +- lib/fetchers/git.rb | 14 +++++++------- test/functional/gitfetcher_test.rb | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/profiles.md b/docs/profiles.md index ba3c30471..30ad35900 100644 --- a/docs/profiles.md +++ b/docs/profiles.md @@ -222,7 +222,7 @@ depends: tag: desired_version commit: pinned_commit version: semver_via_tags - profile_path: optional/path/to/profile + path_within_repo: relative/optional/path/to/profile ``` ### supermarket diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 72dc74efd..e9c0d9b51 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -42,7 +42,7 @@ module Fetchers @ref = opts[:ref] @remote_url = remote_url @repo_directory = nil - @target_profile_path = opts[:profile_path] + @path_within_repo = opts[:path_within_repo] end def fetch(dir) @@ -54,11 +54,11 @@ module Fetchers else Dir.mktmpdir do |tmpdir| checkout(tmpdir) - if @target_profile_path + if @path_within_repo @profile_directory = dir Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ - "Moving #{@target_profile_path} to #{dir}") - target_profile = File.join(tmpdir, @target_profile_path) + "Moving #{@path_within_repo} to #{dir}") + target_profile = File.join(tmpdir, @path_within_repo) FileUtils.cp_r(target_profile, dir) else Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ @@ -71,8 +71,8 @@ module Fetchers end def cache_key - return resolved_ref unless @target_profile_path - OpenSSL::Digest::SHA256.hexdigest(resolved_ref + @target_profile_path) + return resolved_ref unless @path_within_repo + OpenSSL::Digest::SHA256.hexdigest(resolved_ref + @path_within_repo) end def archive_path @@ -81,7 +81,7 @@ module Fetchers def resolved_source source = { git: @remote_url, ref: resolved_ref } - source[:profile_path] = @target_profile_path if @target_profile_path + source[:path_within_repo] = @path_within_repo if @path_within_repo source end diff --git a/test/functional/gitfetcher_test.rb b/test/functional/gitfetcher_test.rb index fbf9f8747..0106ed40a 100644 --- a/test/functional/gitfetcher_test.rb +++ b/test/functional/gitfetcher_test.rb @@ -33,12 +33,12 @@ describe "profiles with git-based dependencies" do { 'name' => 'deep-git-dep', 'git' => @deep_git_dep_dir, - 'profile_path' => 'profiles/deep-profile' + 'path_within_repo' => 'profiles/deep-profile' }, { 'name' => 'another-deep-git-dep', 'git' => @deep_git_dep_dir, - 'profile_path' => 'profiles/another-deep-profile' + 'path_within_repo' => 'profiles/another-deep-profile' } ] File.write(File.join(@profile_dir, 'inspec.yml'), YAML.dump(inspec_yml)) From f1a892c857345ad1ba14e8cea44e6ff03a95a02a Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Sun, 31 Mar 2019 08:01:59 -0400 Subject: [PATCH 03/18] Move existing git fixture profile to a directory for git-fetchers Signed-off-by: Clinton Wolfe --- test/functional/inspec_vendor_test.rb | 4 ++-- .../profiles/{git-depends => git-fetcher/basic}/inspec.yml | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename test/unit/mock/profiles/{git-depends => git-fetcher/basic}/inspec.yml (100%) diff --git a/test/functional/inspec_vendor_test.rb b/test/functional/inspec_vendor_test.rb index fb3eef4e2..c29400128 100644 --- a/test/functional/inspec_vendor_test.rb +++ b/test/functional/inspec_vendor_test.rb @@ -68,8 +68,8 @@ describe "example inheritance profile" do end end - it "can vendor profile dependencies from git" do - git_depends_path = File.join(profile_path, "git-depends") + it 'can vendor profile dependencies from git' do + git_depends_path = File.join(profile_path, 'git-fetcher', 'basic') Dir.mktmpdir do |tmpdir| FileUtils.cp_r(git_depends_path + "/.", tmpdir) diff --git a/test/unit/mock/profiles/git-depends/inspec.yml b/test/unit/mock/profiles/git-fetcher/basic/inspec.yml similarity index 100% rename from test/unit/mock/profiles/git-depends/inspec.yml rename to test/unit/mock/profiles/git-fetcher/basic/inspec.yml From 0e32d2429f3fbcb95ad0b3b8396e886e0c855177 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Thu, 13 Jun 2019 15:57:36 -0400 Subject: [PATCH 04/18] Rename path_within_repo option to relative_path Signed-off-by: Clinton Wolfe --- docs/profiles.md | 4 ++-- lib/fetchers/git.rb | 17 +++++++++-------- test/functional/gitfetcher_test.rb | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/docs/profiles.md b/docs/profiles.md index 30ad35900..e03d0fadb 100644 --- a/docs/profiles.md +++ b/docs/profiles.md @@ -212,7 +212,7 @@ depends: ### git -A `git` setting specifies a profile that is located in a git repository, with optional settings for branch, tag, commit, version, and profile_path. The source location is translated into a URL upon resolution. This type of dependency supports version constraints via semantic versioning as git tags. +A `git` setting specifies a profile that is located in a git repository, with optional settings for branch, tag, commit, version, and relative_path. The source location is translated into a URL upon resolution. This type of dependency supports version constraints via semantic versioning as git tags. ```YAML depends: @@ -222,7 +222,7 @@ depends: tag: desired_version commit: pinned_commit version: semver_via_tags - path_within_repo: relative/optional/path/to/profile + relative_path: relative/optional/path/to/profile ``` ### supermarket diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index e9c0d9b51..4499ef74b 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -37,12 +37,12 @@ module Fetchers def initialize(remote_url, opts = {}) @branch = opts[:branch] - @profile_directory = nil @tag = opts[:tag] @ref = opts[:ref] @remote_url = remote_url @repo_directory = nil - @path_within_repo = opts[:path_within_repo] + @profile_directory = nil # TODO remove this if possible, distinction without a difference + @relative_path = opts[:relative_path] end def fetch(dir) @@ -51,14 +51,15 @@ module Fetchers if cloned? checkout + # TODO - verify this still works with relative path else Dir.mktmpdir do |tmpdir| checkout(tmpdir) - if @path_within_repo + if @relative_path @profile_directory = dir Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ - "Moving #{@path_within_repo} to #{dir}") - target_profile = File.join(tmpdir, @path_within_repo) + "Moving #{@relative_path} to #{dir}") + target_profile = File.join(tmpdir, @relative_path) FileUtils.cp_r(target_profile, dir) else Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ @@ -71,8 +72,8 @@ module Fetchers end def cache_key - return resolved_ref unless @path_within_repo - OpenSSL::Digest::SHA256.hexdigest(resolved_ref + @path_within_repo) + return resolved_ref unless @relative_path + OpenSSL::Digest::SHA256.hexdigest(resolved_ref + @relative_path) end def archive_path @@ -81,7 +82,7 @@ module Fetchers def resolved_source source = { git: @remote_url, ref: resolved_ref } - source[:path_within_repo] = @path_within_repo if @path_within_repo + source[:relative_path] = @relative_path if @relative_path source end diff --git a/test/functional/gitfetcher_test.rb b/test/functional/gitfetcher_test.rb index 0106ed40a..d9bb3fba0 100644 --- a/test/functional/gitfetcher_test.rb +++ b/test/functional/gitfetcher_test.rb @@ -33,12 +33,12 @@ describe "profiles with git-based dependencies" do { 'name' => 'deep-git-dep', 'git' => @deep_git_dep_dir, - 'path_within_repo' => 'profiles/deep-profile' + 'relative_path' => 'profiles/deep-profile' }, { 'name' => 'another-deep-git-dep', 'git' => @deep_git_dep_dir, - 'path_within_repo' => 'profiles/another-deep-profile' + 'relative_path' => 'profiles/another-deep-profile' } ] File.write(File.join(@profile_dir, 'inspec.yml'), YAML.dump(inspec_yml)) From 12fcb24932ca369970f13fc4cee7d886e8d20498 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Fri, 14 Jun 2019 09:53:14 -0400 Subject: [PATCH 05/18] Experiment with git subrepo storage Signed-off-by: Clinton Wolfe --- .../profiles/git-fetcher/basic-local/README.md | 1 + .../basic-local/controls/basic-local.rb | 5 +++++ .../basic-local/git-fixture/COMMIT_EDITMSG | 1 + .../git-fetcher/basic-local/git-fixture/HEAD | 1 + .../git-fetcher/basic-local/git-fixture/config | 10 ++++++++++ .../basic-local/git-fixture/description | 1 + .../git-fetcher/basic-local/git-fixture/index | Bin 0 -> 266 bytes .../basic-local/git-fixture/info/exclude | 6 ++++++ .../git-fetcher/basic-local/git-fixture/logs/HEAD | 4 ++++ .../git-fixture/logs/refs/heads/master | 1 + .../git-fixture/logs/refs/heads/test-branch | 2 ++ .../02/145c02bed66651584950fd3c56a0f8e08495ad | Bin 0 -> 59 bytes .../0e/7d2b9c2c5a1372341e36febceab86558439149 | Bin 0 -> 124 bytes .../26/dec4a490f980527c06528510ed85512fd55e10 | 4 ++++ .../38/b63f30119439953105a30b6b54201ccdf161ad | 2 ++ .../53/7a2ff34c95a18643627a95ae39b1af3f645346 | Bin 0 -> 59 bytes .../54/d0671d3e2c4a28865a0ecc98863859bd4d7475 | 4 ++++ .../b7/b45291b2ed5c7885e355f12dd8da6bcd6c2437 | Bin 0 -> 89 bytes .../d4/7c4fc2bd4c7e35dc5e1f40885335cc63bcd241 | Bin 0 -> 130 bytes .../ea/56cd2cd0923474011fb248a3cd3c2a18585b5f | Bin 0 -> 89 bytes .../basic-local/git-fixture/refs/heads/master | 1 + .../git-fixture/refs/heads/test-branch | 1 + .../basic-local/git-fixture/refs/tags/v1.0 | 1 + .../profiles/git-fetcher/basic-local/inspec.yml | 8 ++++++++ .../profiles/git-fetcher/git-repo-01/README.md | 1 + .../git-repo-01/child-01/controls/child-01.rb | 5 +++++ .../git-fetcher/git-repo-01/child-01/inspec.yml | 8 ++++++++ .../in/repo/child-02/controls/child-02.rb | 5 +++++ .../deeper-path/in/repo/child-02/inspec.yml | 8 ++++++++ .../git-repo-01/git-fixture/COMMIT_EDITMSG | 1 + .../git-fetcher/git-repo-01/git-fixture/HEAD | 1 + .../git-fetcher/git-repo-01/git-fixture/config | 10 ++++++++++ .../git-repo-01/git-fixture/description | 1 + .../git-fetcher/git-repo-01/git-fixture/index | Bin 0 -> 689 bytes .../git-repo-01/git-fixture/info/exclude | 6 ++++++ .../git-fetcher/git-repo-01/git-fixture/logs/HEAD | 2 ++ .../git-fixture/logs/refs/heads/master | 2 ++ .../0c/0abc0b6dc094bf6756b369db878c69f11839c5 | Bin 0 -> 51 bytes .../16/d0355317bef35f2dc7e71607b5e6df7c812713 | Bin 0 -> 90 bytes .../36/f4c4ecd2b870c0d0869c14a3e41dd506e5a887 | 3 +++ .../3d/06a6c531116e47438e0009be37aad4c19eeec1 | Bin 0 -> 50 bytes .../4e/45530f8d46d447cba28d4a41e55cd5b06c0f72 | Bin 0 -> 44 bytes .../65/dcdc3491c0c138538379f404f2517d968282a7 | Bin 0 -> 140 bytes .../74/198b01ae86430df0149b4389f112f6a94ba47d | Bin 0 -> 46 bytes .../87/da758286290d2ec7de5d0a748b4db7a5c4eff5 | Bin 0 -> 85 bytes .../a6/a86bbacc7daf98fb1155a7ebfca37164f8873f | Bin 0 -> 90 bytes .../b7/923d86b8b8951df0eee505671907a59410a978 | Bin 0 -> 56 bytes .../df/e8db2e9636f83a29b42d790c4061f08da60ccd | Bin 0 -> 89 bytes .../e0/a79951d85e314d4f091201bceaeab5001ea8e9 | Bin 0 -> 56 bytes .../e6/d4da70d17f77ab9276a448633b69de7d253f39 | Bin 0 -> 90 bytes .../e9/dd6fd217cced2639b2ad64319bfd73b28a413a | 3 +++ .../ff/13d726419b64726a7ef787377367d598cbb509 | 1 + .../git-repo-01/git-fixture/refs/heads/master | 1 + .../top-level-relative/controls/top-level.rb | 8 ++++++++ .../git-fetcher/top-level-relative/inspec.yml | 14 ++++++++++++++ 55 files changed, 133 insertions(+) create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/README.md create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/controls/basic-local.rb create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/COMMIT_EDITMSG create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/HEAD create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/config create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/description create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/index create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/info/exclude create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/HEAD create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/master create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/test-branch create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/02/145c02bed66651584950fd3c56a0f8e08495ad create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/0e/7d2b9c2c5a1372341e36febceab86558439149 create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/26/dec4a490f980527c06528510ed85512fd55e10 create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/38/b63f30119439953105a30b6b54201ccdf161ad create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/53/7a2ff34c95a18643627a95ae39b1af3f645346 create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/54/d0671d3e2c4a28865a0ecc98863859bd4d7475 create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/b7/b45291b2ed5c7885e355f12dd8da6bcd6c2437 create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/d4/7c4fc2bd4c7e35dc5e1f40885335cc63bcd241 create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/ea/56cd2cd0923474011fb248a3cd3c2a18585b5f create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/master create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/test-branch create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/tags/v1.0 create mode 100644 test/unit/mock/profiles/git-fetcher/basic-local/inspec.yml create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/README.md create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/controls/child-01.rb create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/inspec.yml create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/controls/child-02.rb create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/inspec.yml create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/COMMIT_EDITMSG create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/HEAD create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/config create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/description create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/index create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/info/exclude create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/HEAD create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/refs/heads/master create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/0c/0abc0b6dc094bf6756b369db878c69f11839c5 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/16/d0355317bef35f2dc7e71607b5e6df7c812713 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/36/f4c4ecd2b870c0d0869c14a3e41dd506e5a887 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/3d/06a6c531116e47438e0009be37aad4c19eeec1 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/4e/45530f8d46d447cba28d4a41e55cd5b06c0f72 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/65/dcdc3491c0c138538379f404f2517d968282a7 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/74/198b01ae86430df0149b4389f112f6a94ba47d create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/87/da758286290d2ec7de5d0a748b4db7a5c4eff5 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/a6/a86bbacc7daf98fb1155a7ebfca37164f8873f create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/b7/923d86b8b8951df0eee505671907a59410a978 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/df/e8db2e9636f83a29b42d790c4061f08da60ccd create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e0/a79951d85e314d4f091201bceaeab5001ea8e9 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e6/d4da70d17f77ab9276a448633b69de7d253f39 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e9/dd6fd217cced2639b2ad64319bfd73b28a413a create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/ff/13d726419b64726a7ef787377367d598cbb509 create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/refs/heads/master create mode 100644 test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb create mode 100644 test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/README.md b/test/unit/mock/profiles/git-fetcher/basic-local/README.md new file mode 100644 index 000000000..1a577de31 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/README.md @@ -0,0 +1 @@ +This is a git repo used as a test fixture. Because we cannot directly store the .git/ directory, it is stored as git-fixture/, and a symlink is created at test runtime. diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/controls/basic-local.rb b/test/unit/mock/profiles/git-fetcher/basic-local/controls/basic-local.rb new file mode 100644 index 000000000..38b63f301 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/controls/basic-local.rb @@ -0,0 +1,5 @@ +control 'basic-local-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/COMMIT_EDITMSG b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/COMMIT_EDITMSG new file mode 100644 index 000000000..718d0a25f --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/COMMIT_EDITMSG @@ -0,0 +1 @@ +Add change only on one branch diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/HEAD b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/config b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/config new file mode 100644 index 000000000..271d6fa51 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/config @@ -0,0 +1,10 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true +[user] + name = test user + email = test@test.org diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/description b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/index b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/index new file mode 100644 index 0000000000000000000000000000000000000000..3aa985884432c3cdeb2a850a8e559f32ddcd093c GIT binary patch literal 266 zcmZ?q402{*U|<5_SY~-6o{PJyVKgHH0}I37d^QG##w834j9-CjM1VNOVw=5z;1tWL zhOCRZvqKbQ&VEc>%OIYdpI1_ppHr-#lvtdZtecacoS36mlmyfe%Pf7&aH7L37!5Hu z=LMR%bFS3*AKL3vXL=`2-k~Gd^i1-eOO6a&nR&$psmXekxj8_iz#t^Z)fLFFVlYxL z 1560485536 -0400 commit (initial): Initial commit +0e7d2b9c2c5a1372341e36febceab86558439149 0e7d2b9c2c5a1372341e36febceab86558439149 test user 1560485563 -0400 checkout: moving from master to test-branch +0e7d2b9c2c5a1372341e36febceab86558439149 54d0671d3e2c4a28865a0ecc98863859bd4d7475 test user 1560485674 -0400 commit: Add change only on one branch +54d0671d3e2c4a28865a0ecc98863859bd4d7475 0e7d2b9c2c5a1372341e36febceab86558439149 test user 1560485682 -0400 checkout: moving from test-branch to master diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/master b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/master new file mode 100644 index 000000000..3f84a39cb --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 0e7d2b9c2c5a1372341e36febceab86558439149 test user 1560485536 -0400 commit (initial): Initial commit diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/test-branch b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/test-branch new file mode 100644 index 000000000..d5f96b612 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/logs/refs/heads/test-branch @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 0e7d2b9c2c5a1372341e36febceab86558439149 test user 1560485563 -0400 branch: Created from HEAD +0e7d2b9c2c5a1372341e36febceab86558439149 54d0671d3e2c4a28865a0ecc98863859bd4d7475 test user 1560485674 -0400 commit: Add change only on one branch diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/02/145c02bed66651584950fd3c56a0f8e08495ad b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/02/145c02bed66651584950fd3c56a0f8e08495ad new file mode 100644 index 0000000000000000000000000000000000000000..b1f7bd8791d612fec8dae54a0acdcdd9bdb6d76b GIT binary patch literal 59 zcmb)oD2-}C4|%gXQCA> literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/0e/7d2b9c2c5a1372341e36febceab86558439149 b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/0e/7d2b9c2c5a1372341e36febceab86558439149 new file mode 100644 index 0000000000000000000000000000000000000000..165073fb28e0c207478115af7d76ba1531f3b45a GIT binary patch literal 124 zcmV-?0E7Q{0hNtG4#FT1MO|}>xj@YTGl+>XZhH(mP$jXF(CPiPdIC2;Z}DEfv}LIZ zHt<1vDNqsz$xbq!ou?C_we#dCCNJ5GOEz-kiD#2GeY6f*R)uXX9WFcG_BpnGzk=m} e6q$X10SVE__ewwNACVbfRcm^}uQqRr5jC0yp*E)g literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/26/dec4a490f980527c06528510ed85512fd55e10 b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/26/dec4a490f980527c06528510ed85512fd55e10 new file mode 100644 index 000000000..283de660e --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/26/dec4a490f980527c06528510ed85512fd55e10 @@ -0,0 +1,4 @@ +x +0 D;+n$h{$j+o +] +;-'y6ܐ)+ (5t4lwރVl@@,~^;e&E2W kI \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/38/b63f30119439953105a30b6b54201ccdf161ad b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/38/b63f30119439953105a30b6b54201ccdf161ad new file mode 100644 index 000000000..32b3d0d2f --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/38/b63f30119439953105a30b6b54201ccdf161ad @@ -0,0 +1,2 @@ +xU +0 oT HS.7]McXTY $!YqtYs@a- {# \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/53/7a2ff34c95a18643627a95ae39b1af3f645346 b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/53/7a2ff34c95a18643627a95ae39b1af3f645346 new file mode 100644 index 0000000000000000000000000000000000000000..14270ee3d040a695b708f84cd07d844d26dc54f4 GIT binary patch literal 59 zcmV-B0L1@z0V^p=O;s>4VlXr?Ff%bxNJ=cuOxDfGPfpCyD@tOp*k*4aIK^_RA?sr9 R><|T+vmX=J0swd>5Iul|7JvW% literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/54/d0671d3e2c4a28865a0ecc98863859bd4d7475 b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/54/d0671d3e2c4a28865a0ecc98863859bd4d7475 new file mode 100644 index 000000000..671488c97 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/54/d0671d3e2c4a28865a0ecc98863859bd4d7475 @@ -0,0 +1,4 @@ +xQ +0D)J&)QͶl+im0AW-u`T0)^!Kypp2)oMHwhkr<|Z*U|zbJOlpPge~~-pk?Y> vLjwad6BC8ZyyAk?WWCDV9EK}3{)hJZ)S2FilXvI{Ha(NP=aM4;l9?Y>+MOxs literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/d4/7c4fc2bd4c7e35dc5e1f40885335cc63bcd241 b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/objects/d4/7c4fc2bd4c7e35dc5e1f40885335cc63bcd241 new file mode 100644 index 0000000000000000000000000000000000000000..87dfa22738e73c7efc1d7cb266fbebae811047ff GIT binary patch literal 130 zcmV-|0Db>>0bR^74#F@DMq%ch;=e$v772!I9K{JPL^yR6y9jPi)sgj+-bAW-u`T0)^!Kypp2)oMMLHD*ewsQx~>5Csj>dXSs2`eM+#K vp@D&!iHSmHUU5NcvR-9w4#Sli|3iCy>P+v%$vboeo1RJDbIB0^n3^93*BU4& literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/master b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/master new file mode 100644 index 000000000..79075aec4 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/master @@ -0,0 +1 @@ +0e7d2b9c2c5a1372341e36febceab86558439149 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/test-branch b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/test-branch new file mode 100644 index 000000000..5ebfd1b19 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/heads/test-branch @@ -0,0 +1 @@ +54d0671d3e2c4a28865a0ecc98863859bd4d7475 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/tags/v1.0 b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/tags/v1.0 new file mode 100644 index 000000000..79075aec4 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/git-fixture/refs/tags/v1.0 @@ -0,0 +1 @@ +0e7d2b9c2c5a1372341e36febceab86558439149 diff --git a/test/unit/mock/profiles/git-fetcher/basic-local/inspec.yml b/test/unit/mock/profiles/git-fetcher/basic-local/inspec.yml new file mode 100644 index 000000000..d47c4fc2b --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/basic-local/inspec.yml @@ -0,0 +1,8 @@ +name: basic-local +title: basic-local +license: Apache-2.0 +summary: A profile to be executed as a local git checkout +version: 0.1.0 +supports: + platform: os + diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/README.md b/test/unit/mock/profiles/git-fetcher/git-repo-01/README.md new file mode 100644 index 000000000..1a577de31 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/README.md @@ -0,0 +1 @@ +This is a git repo used as a test fixture. Because we cannot directly store the .git/ directory, it is stored as git-fixture/, and a symlink is created at test runtime. diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/controls/child-01.rb b/test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/controls/child-01.rb new file mode 100644 index 000000000..e6d4da70d --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/controls/child-01.rb @@ -0,0 +1,5 @@ +control 'child-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/inspec.yml b/test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/inspec.yml new file mode 100644 index 000000000..ff13d7264 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/child-01/inspec.yml @@ -0,0 +1,8 @@ +name: child-01 +title: child-01 +license: Apache-2.0 +summary: A profile to be included via git and a relative path +version: 0.1.0 +supports: + platform: os + diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/controls/child-02.rb b/test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/controls/child-02.rb new file mode 100644 index 000000000..16d035531 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/controls/child-02.rb @@ -0,0 +1,5 @@ +control 'child-02' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/inspec.yml b/test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/inspec.yml new file mode 100644 index 000000000..e9dd6fd21 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/deeper-path/in/repo/child-02/inspec.yml @@ -0,0 +1,8 @@ +name: child-02 +title: child-02 +license: Apache-2.0 +summary: A profile to be included via git and a deep relative path +version: 0.1.0 +supports: + platform: os + diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/COMMIT_EDITMSG b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/COMMIT_EDITMSG new file mode 100644 index 000000000..07ae63da3 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/COMMIT_EDITMSG @@ -0,0 +1 @@ +Add child-02 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/HEAD b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/config b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/config new file mode 100644 index 000000000..ed0b78f63 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/config @@ -0,0 +1,10 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true +[user] + name = InSpec Test Fixture + email = inspec@chef.io diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/description b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/index b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/index new file mode 100644 index 0000000000000000000000000000000000000000..b139775260ed7aafbbebc09a47a100e245dd7642 GIT binary patch literal 689 zcmZ?q402{*U|<4bmY4;b0$CZH7rheV^lXk3Sb<-}>x+O{2OngJDW)YC&p|Zb4#62GD5z zqSS(Xn9GeY{9*+13(y{A4uj`cVKmgfGBo?A`tNm7KQ=2BQ-tl{}TqWJU+m{}B{}mP_3`PouT=!qx)|+Pb z!%B0DZY7UH;)mX4JZE81!Cv16+2 mhj&j|(H_Z|S2I{h2~ literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/info/exclude b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/HEAD b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/HEAD new file mode 100644 index 000000000..2626ce40e --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/HEAD @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 65dcdc3491c0c138538379f404f2517d968282a7 InSpec Test Fixture 1554036073 -0400 commit (initial): Add child-01 profile +65dcdc3491c0c138538379f404f2517d968282a7 36f4c4ecd2b870c0d0869c14a3e41dd506e5a887 InSpec Test Fixture 1554036109 -0400 commit: Add child-02 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/refs/heads/master b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/refs/heads/master new file mode 100644 index 000000000..2626ce40e --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/logs/refs/heads/master @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 65dcdc3491c0c138538379f404f2517d968282a7 InSpec Test Fixture 1554036073 -0400 commit (initial): Add child-01 profile +65dcdc3491c0c138538379f404f2517d968282a7 36f4c4ecd2b870c0d0869c14a3e41dd506e5a887 InSpec Test Fixture 1554036109 -0400 commit: Add child-02 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/0c/0abc0b6dc094bf6756b369db878c69f11839c5 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/0c/0abc0b6dc094bf6756b369db878c69f11839c5 new file mode 100644 index 0000000000000000000000000000000000000000..69749181e53fda6f7ba98ba945fa3d75f19562aa GIT binary patch literal 51 zcmb3Y4G__9A4+u0eM HLxng1-aix0 literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/16/d0355317bef35f2dc7e71607b5e6df7c812713 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/16/d0355317bef35f2dc7e71607b5e6df7c812713 new file mode 100644 index 0000000000000000000000000000000000000000..0b4a5004af9fd360335a755a6eee9d39ca1a9530 GIT binary patch literal 90 zcmV-g0HyzU0ZYosPg1ZnVMxx;D=Es)QBY6L$jnL6H84_FNXh3?P)JEFPAxx# w%qdT-EY>YZEG~x1DJW!?C{!yHXXKaWq$nik7N9Ds1!_;tOW^`y0EU(zw)2@K{{R30 literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/36/f4c4ecd2b870c0d0869c14a3e41dd506e5a887 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/36/f4c4ecd2b870c0d0869c14a3e41dd506e5a887 new file mode 100644 index 000000000..26486e354 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/36/f4c4ecd2b870c0d0869c14a3e41dd506e5a887 @@ -0,0 +1,3 @@ +xK +1 @]~2mAD7k@MR32VGp={_ER)4┣( 55⠵yE -#['s #%ߖ+єWN }v8w- +6=pϣMw`b5Dÿˮ <љ-H8 \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/3d/06a6c531116e47438e0009be37aad4c19eeec1 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/3d/06a6c531116e47438e0009be37aad4c19eeec1 new file mode 100644 index 0000000000000000000000000000000000000000..22fa52dcaff97be8dc8635f3d84d32b4ede02d0a GIT binary patch literal 50 zcmV-20L}k+0V^p=O;s>9WiT-S0)^y^%$yWm14D-UFK+8iGy7qsxka~<$06}U?=qgV I05HlA+1K3_Jpcdz literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/4e/45530f8d46d447cba28d4a41e55cd5b06c0f72 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/4e/45530f8d46d447cba28d4a41e55cd5b06c0f72 new file mode 100644 index 0000000000000000000000000000000000000000..55e7ac97d79c3337ffb92f4d439b4428be74fd4f GIT binary patch literal 44 zcmb)5VqnO?)OyCIP3UCavC`agd*ofxtjp^}er>x{?WxOf={&D30As5W ATmS$7 literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/65/dcdc3491c0c138538379f404f2517d968282a7 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/65/dcdc3491c0c138538379f404f2517d968282a7 new file mode 100644 index 0000000000000000000000000000000000000000..a529d9ee70862a3799b733cfb3c416ba62af890b GIT binary patch literal 140 zcmV;70CWF%0iDf33c@fDMq$@E#ay5=L(1@i&&4n71B&O^!o literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/74/198b01ae86430df0149b4389f112f6a94ba47d b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/74/198b01ae86430df0149b4389f112f6a94ba47d new file mode 100644 index 0000000000000000000000000000000000000000..b0a8988ac63e0ccd74c8225986ecb09d63664122 GIT binary patch literal 46 zcmV+}0MY+=0V^p=O;s>9WH2!R0)?W~f_w%Zu07nj2d3;#58Ir1yS*p#qlD#A0Q2<@ ElZM|D;{X5v literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/87/da758286290d2ec7de5d0a748b4db7a5c4eff5 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/87/da758286290d2ec7de5d0a748b4db7a5c4eff5 new file mode 100644 index 0000000000000000000000000000000000000000..9eb0fd8cc41633a53ffcb2344512979391a0ba1f GIT binary patch literal 85 zcmV-b0IL6Z0V^p=O;s>AW-u`T0)^y^%$yWm14D-UFK+8iGy7qsxka~<$06}U?=qgV r5G5(8sRgM;x&?_P84P}|!Th~$SKLo8>h*Gb8gq3+4u25AW-u`T0)^!Kypp2)oMMLUlWf~|?3gP1;oVc#bV>H5Qv_C4 w7#bLunV2YK<`oyDChJw^<}kdxn}13C%v&|fO>0vOXa6nU)a7Ue0Mv6J=X822m;e9( literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/b7/923d86b8b8951df0eee505671907a59410a978 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/b7/923d86b8b8951df0eee505671907a59410a978 new file mode 100644 index 0000000000000000000000000000000000000000..512aea4a76be3ccc2dba81d0438b17aa3b683ce9 GIT binary patch literal 56 zcmV-80LTA$0V^p=O;s?qWH2-^Ff%bxNY2R2NzpYh(kn`05W8R+EWYn^yzcSmV(eR= O-LGj>7X|=Tc@SS`92ZRh literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/df/e8db2e9636f83a29b42d790c4061f08da60ccd b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/df/e8db2e9636f83a29b42d790c4061f08da60ccd new file mode 100644 index 0000000000000000000000000000000000000000..9bdf8409c1b7ca8a4f9459a06d07bedfba227269 GIT binary patch literal 89 zcmV-f0H*(V0V^p=O;s>AW-u`T0)^!Kypp2)oMMIt%V!4Oh%@x{=M-Yx^Xk=B2Due4 v4Gj#;OiUCq^NI^nll3Zda~S>$UsrRSol=xl_r2Y`IQ{C3(_1+Krz#)D(J?66 literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e0/a79951d85e314d4f091201bceaeab5001ea8e9 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e0/a79951d85e314d4f091201bceaeab5001ea8e9 new file mode 100644 index 0000000000000000000000000000000000000000..ef9e9a4c2aa4e180a0726a90f233d65a17b6acf4 GIT binary patch literal 56 zcmV-80LTA$0V^p=O;s?qWH2-^Ff%bxNY2R2NzpYh)GJD2cy{Gh!NvOW)sxDWcqChA O-m6u$w*&xmxDknlH5n!V literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e6/d4da70d17f77ab9276a448633b69de7d253f39 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e6/d4da70d17f77ab9276a448633b69de7d253f39 new file mode 100644 index 0000000000000000000000000000000000000000..3cb4847fc1fcb9e5038cd45029288d5eb25e4179 GIT binary patch literal 90 zcmV-g0HyzU0ZYosPg1ZnVMxx;D=Es)QBY6L$jnL6H850HNXh3?P)JEFPAxx# w%qdT-EY>YZEG~x1DJW!?C{!yHXXKaWq$nik7N9Ds1!_;tOW^`y0ES2)weu_`vH$=8 literal 0 HcmV?d00001 diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e9/dd6fd217cced2639b2ad64319bfd73b28a413a b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e9/dd6fd217cced2639b2ad64319bfd73b28a413a new file mode 100644 index 000000000..154842f2c --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/e9/dd6fd217cced2639b2ad64319bfd73b28a413a @@ -0,0 +1,3 @@ +xMA +0E)hv=ʘu I}.֤+Q(G%aZ\9In6 +;8vL&+CJHGSĂʉ k-8fZ{۴fm}7+ \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/ff/13d726419b64726a7ef787377367d598cbb509 b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/ff/13d726419b64726a7ef787377367d598cbb509 new file mode 100644 index 000000000..8fe1a3a2b --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/objects/ff/13d726419b64726a7ef787377367d598cbb509 @@ -0,0 +1 @@ +xM1 S-;J< ppΖXRh4[ qT#$qZVgb2F!t7R.u0Ɛ9[ T#3F}Ve^ִhWk/:5f \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/refs/heads/master b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/refs/heads/master new file mode 100644 index 000000000..5798ee814 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/git-fixture/refs/heads/master @@ -0,0 +1 @@ +36f4c4ecd2b870c0d0869c14a3e41dd506e5a887 diff --git a/test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb b/test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb new file mode 100644 index 000000000..d0c3298ac --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb @@ -0,0 +1,8 @@ +# include_controls 'child-01' +# include_controls 'child-02' + +control 'top-level-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml b/test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml new file mode 100644 index 000000000..bd94c6bca --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml @@ -0,0 +1,14 @@ +name: top-level-relative +title: top-level-relative +license: Apache-2.0 +summary: A profile that depends on profiles that are stored in a repo in subdirs +version: 0.1.0 +supports: + platform: os +depends: +- name: child-01 + git: test/unit/mock/profiles/git-fetcher/git-repo-01 + relative_path: child-01 +# - name: child-02 +# git: ./git-repo-01 +# relative_path: deeper-path/in/repo/child-01 From aa8101201392b9742f66272ca0d20289502474d0 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Fri, 14 Jun 2019 10:55:32 -0400 Subject: [PATCH 06/18] Rework tests to be fixture-based, not generated per-run Signed-off-by: Clinton Wolfe --- test/functional/git_fetcher_test.rb | 68 ++++++++++++++++ test/functional/gitfetcher_test.rb | 122 ---------------------------- 2 files changed, 68 insertions(+), 122 deletions(-) create mode 100644 test/functional/git_fetcher_test.rb delete mode 100644 test/functional/gitfetcher_test.rb diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb new file mode 100644 index 000000000..5f1bf2ebc --- /dev/null +++ b/test/functional/git_fetcher_test.rb @@ -0,0 +1,68 @@ +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 + #======================================================================# + fixture_repos = ["basic-local", "git-repo-01"] + + before(:all) do + skip_windows! # Right now, this is due to symlinking + + # 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 + + # TODO: move private SSH+git test from inspec_exec_test to here + + #======================================================================# + # 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 + run_result.must_have_all_controls_passing + end + end + # describe "running a profile with a basic remote dependency" + + #======================================================================# + # Revision Selection + #======================================================================# + # TODO: test branch, rev, and tag capabilities + + #======================================================================# + # Relative Path Support + #======================================================================# + + #------------ Happy Cases for Relative Path Support -------------------# + # describe "running a profile with a shallow relative path dependency" + # describe "running a profile with a deep relative path dependency" + # describe "running a profile with a combination of relative path dependencies" + + #------------ Edge Cases for Relative Path Support -------------------# + # describe "running a profile with an '' relative path dependency" + # describe "running a profile with an ./ relative path dependency" + # describe "running a profile with a relative path dependency that does not exist" + +end diff --git a/test/functional/gitfetcher_test.rb b/test/functional/gitfetcher_test.rb deleted file mode 100644 index d9bb3fba0..000000000 --- a/test/functional/gitfetcher_test.rb +++ /dev/null @@ -1,122 +0,0 @@ -require "functional/helper" -require "fileutils" -require "tmpdir" -require "yaml" - -describe "profiles with git-based dependencies" do - include FunctionalHelper - before(:all) do - @tmpdir = Dir.mktmpdir - @profile_dir = File.join(@tmpdir, 'test-profile') - @git_dep_dir = File.join(@tmpdir, 'git-dep') - @deep_git_dep_dir = File.join(@tmpdir, 'deep-git-dep') - - Dir.chdir(@tmpdir) do - inspec('init profile git-dep') - inspec('init profile test-profile') - - # InSpec will search directories and execute successfully if there is just - # one profile. We create two here to ensure we execute both as intended. - deep_profiles = File.join(@deep_git_dep_dir, 'profiles') - FileUtils.mkdir_p(deep_profiles) - inspec('init profile ' + File.join(deep_profiles, 'deep-profile')) - inspec('init profile ' + File.join(deep_profiles, 'another-deep-profile')) - end - - inspec_yml = YAML.load(File.read(File.join(@profile_dir, 'inspec.yml'))) - inspec_yml["depends"] = [ - { - 'name' => 'git-dep', - 'git' => @git_dep_dir, - 'tag' => 'v1.0' - }, - { - 'name' => 'deep-git-dep', - 'git' => @deep_git_dep_dir, - 'relative_path' => 'profiles/deep-profile' - }, - { - 'name' => 'another-deep-git-dep', - 'git' => @deep_git_dep_dir, - 'relative_path' => 'profiles/another-deep-profile' - } - ] - File.write(File.join(@profile_dir, 'inspec.yml'), YAML.dump(inspec_yml)) - - Dir.chdir(@git_dep_dir) do - CMD.run_command('git init') - CMD.run_command('git config user.name "testuser"') - CMD.run_command('git config user.email "testuser@example.com"') - CMD.run_command('git add .') - CMD.run_command('git commit -m "Initial commit" --no-gpg-sign') - CMD.run_command('git tag v1.0') - - # Need to overwrite default test because `/tmp` does not exist on - # Windows and the default generated test checks for `/tmp`. - File.open(File.join('controls', 'example.rb'), 'w') do |f| - f.write <<~EOF - control 'example-1.0' do - describe 'value' do - it { should cmp 'value' } - end - end - EOF - end - end - - Dir.chdir(@deep_git_dep_dir) do - profile_1 = File.join('profiles', 'deep-profile') - profile_2 = File.join('profiles', 'another-deep-profile') - [profile_1, profile_2].each do |profile| - # Need to overwrite default test because `/tmp` does not exist on - # Windows and the default generated test checks for `/tmp`. - File.open(File.join(profile, 'controls', 'example.rb'), 'w') do |f| - f.write <<~EOF - control '#{File.basename(profile)}-control' do - describe 'value' do - it { should cmp 'value' } - end - end - EOF - end - end - CMD.run_command('git init') - CMD.run_command('git config user.name "testuser"') - CMD.run_command('git config user.email "testuser@example.com"') - CMD.run_command('git add .') - CMD.run_command('git commit -m "Initial commit" --no-gpg-sign') - end - - File.open(File.join(@profile_dir, 'controls', 'example.rb'), 'w') do |f| - f.write <<~EOF - include_controls 'git-dep' - include_controls 'deep-git-dep' - include_controls 'another-deep-git-dep' - EOF - end - end - - after(:all) do - FileUtils.rm_rf(@tmpdir) - end - -<<<<<<< HEAD - it "executes a profile with a git based dependency" do - out = inspec("exec #{@profile_dir} --no-create-lockfile") - - skip_windows! - out.stderr.must_equal "" - - assert_exit_code 0, out -======= - it 'executes a profile with a git based dependencies' do - Dir.mktmpdir do |tmpdir| - out = inspec("exec #{@profile_dir} " \ - "--no-create-lockfile " \ - "--vendor-cache #{tmpdir}") - out.stderr.must_equal '' - out.exit_status.must_equal 0 - end ->>>>>>> Add support for relative paths to the Git fetcher - end -end From c257763de61f5449fa1e84125298cd37caac1467 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Fri, 14 Jun 2019 11:03:06 -0400 Subject: [PATCH 07/18] Linting Signed-off-by: Clinton Wolfe --- lib/fetchers/git.rb | 2 +- test/functional/inspec_vendor_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 4499ef74b..12274f9f5 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -64,7 +64,7 @@ module Fetchers else Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ "Moving checkout to #{dir}") - FileUtils.cp_r(tmpdir + '/.', dir) + FileUtils.cp_r(tmpdir + "/.", dir) end end end diff --git a/test/functional/inspec_vendor_test.rb b/test/functional/inspec_vendor_test.rb index c29400128..aff0dc2ce 100644 --- a/test/functional/inspec_vendor_test.rb +++ b/test/functional/inspec_vendor_test.rb @@ -68,8 +68,8 @@ describe "example inheritance profile" do end end - it 'can vendor profile dependencies from git' do - git_depends_path = File.join(profile_path, 'git-fetcher', 'basic') + it "can vendor profile dependencies from git" do + git_depends_path = File.join(profile_path, "git-fetcher", "basic") Dir.mktmpdir do |tmpdir| FileUtils.cp_r(git_depends_path + "/.", tmpdir) From 80241ec9719c0db6e039cbceecb45b70ebbbad30 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Mon, 17 Jun 2019 13:25:19 -0400 Subject: [PATCH 08/18] Passing basic test harness Signed-off-by: Clinton Wolfe --- lib/fetchers/git.rb | 13 ++++++++++++- test/functional/git_fetcher_test.rb | 18 +++++++++++++++++- .../git-repo-01/controls/red-dye.rb | 11 +++++++++++ .../git-fetcher/git-repo-01/inspec.yml | 2 ++ .../relative-shallow/controls/top-level.rb | 7 +++++++ .../git-fetcher/relative-shallow/inspec.yml | 11 +++++++++++ 6 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/controls/red-dye.rb create mode 100644 test/unit/mock/profiles/git-fetcher/git-repo-01/inspec.yml create mode 100644 test/unit/mock/profiles/git-fetcher/relative-shallow/controls/top-level.rb create mode 100644 test/unit/mock/profiles/git-fetcher/relative-shallow/inspec.yml diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 12274f9f5..8a3c6eb09 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -39,12 +39,23 @@ module Fetchers @branch = opts[:branch] @tag = opts[:tag] @ref = opts[:ref] - @remote_url = remote_url + @remote_url = expand_local_path(remote_url) @repo_directory = nil @profile_directory = nil # TODO remove this if possible, distinction without a difference @relative_path = opts[:relative_path] end + def expand_local_path(url_or_file_path) + # We could try to do something clever with URI + # processing, but then again, if you passed a relative path + # to an on-disk repo, you probably expect it to exist. + return url_or_file_path unless File.exist?(url_or_file_path) + # It's important to expand this path, because it may be specified + # locally in the metadata files, and when we clone, we will be + # in a temp dir. + File.expand_path(url_or_file_path) + end + def fetch(dir) @repo_directory = dir FileUtils.mkdir_p(dir) unless Dir.exist?(dir) diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index 5f1bf2ebc..a42012cef 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -56,7 +56,23 @@ describe "running profiles with git-based dependencies" do #======================================================================# #------------ Happy Cases for Relative Path Support -------------------# - # describe "running a profile with a shallow relative path dependency" + describe "running a profile with a shallow relative path dependency" do + it "should find the relative path profile and execute exactly those controls" do + run_result = run_inspec_process("exec #{git_profiles}/relative-shallow", json: true) + assert_empty run_result.stderr + run_result.must_have_all_controls_passing + + # Should know about the top-level profile and the child profile + assert_equal ["relative-shallow", "child-01"], (run_result.payload.json["profiles"].map { |p| p["name"] }) + + controls = run_result.payload.json["profiles"].map { |p| p["controls"] }.flatten.map { |c| c["id"] }.uniq + # Should have controls from the top-level and and included child profile + assert_includes controls, "top-level-01" + assert_includes controls, "child-01" + # should not have controls from the profile defined at the top of the repo of the child profile + refute_includes controls, "red-dye" + end + end # describe "running a profile with a deep relative path dependency" # describe "running a profile with a combination of relative path dependencies" diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/controls/red-dye.rb b/test/unit/mock/profiles/git-fetcher/git-repo-01/controls/red-dye.rb new file mode 100644 index 000000000..af4ea93f3 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/controls/red-dye.rb @@ -0,0 +1,11 @@ +# This control would only appear if it were accidentally included +control 'red-dye' do + # In rural areas where diesel fuel is sold for + # agricultrual purposes and is exempt from taxation (as part + # of farm subsidies), tractor diesel fuel is dyed red. Highway + # patrol officers check for red dye in the fuel when they + # stop grain hauling trucks, as it is a form of tax fraud. + describe 'truck fuel color' do + it { should cmp 'clear' } + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/git-repo-01/inspec.yml b/test/unit/mock/profiles/git-fetcher/git-repo-01/inspec.yml new file mode 100644 index 000000000..5527619f7 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/git-repo-01/inspec.yml @@ -0,0 +1,2 @@ +name: red-dye +description: A profile designed to detect if controls are accidentally included \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/relative-shallow/controls/top-level.rb b/test/unit/mock/profiles/git-fetcher/relative-shallow/controls/top-level.rb new file mode 100644 index 000000000..a37f5f89f --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-shallow/controls/top-level.rb @@ -0,0 +1,7 @@ +include_controls 'child-01' + +control 'top-level-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/relative-shallow/inspec.yml b/test/unit/mock/profiles/git-fetcher/relative-shallow/inspec.yml new file mode 100644 index 000000000..bee308f11 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-shallow/inspec.yml @@ -0,0 +1,11 @@ +name: relative-shallow +title: relative-shallow +license: Apache-2.0 +summary: A profile that depends on profiles that are stored in a repo in subdirs +version: 0.1.0 +supports: + platform: os +depends: +- name: child-01 + git: test/unit/mock/profiles/git-fetcher/git-repo-01 + relative_path: child-01 From e9ef8e25a328fa33ce882b4083a9d8082d3f3331 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 18 Jun 2019 12:28:26 -0400 Subject: [PATCH 09/18] Deep dependency works Signed-off-by: Clinton Wolfe --- test/functional/git_fetcher_test.rb | 19 ++++++++++++++++++- .../relative-deep/controls/relative-deep.rb | 7 +++++++ .../git-fetcher/relative-deep/inspec.yml | 11 +++++++++++ .../top-level-relative/controls/top-level.rb | 8 -------- .../git-fetcher/top-level-relative/inspec.yml | 14 -------------- 5 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 test/unit/mock/profiles/git-fetcher/relative-deep/controls/relative-deep.rb create mode 100644 test/unit/mock/profiles/git-fetcher/relative-deep/inspec.yml delete mode 100644 test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb delete mode 100644 test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index a42012cef..605e888e2 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -73,7 +73,24 @@ describe "running profiles with git-based dependencies" do refute_includes controls, "red-dye" end end - # describe "running a profile with a deep relative path dependency" + + describe "running a profile with a deep relative path dependency" do + it "should find the relative path profile and execute exactly those controls" do + run_result = run_inspec_process("exec #{git_profiles}/relative-deep", json: true) + assert_empty run_result.stderr + run_result.must_have_all_controls_passing + + # Should know about the top-level profile and the child profile + assert_equal ["relative-deep", "child-02"], (run_result.payload.json["profiles"].map { |p| p["name"] }) + + controls = run_result.payload.json["profiles"].map { |p| p["controls"] }.flatten.map { |c| c["id"] }.uniq + # Should have controls from the top-level and and included child profile + assert_includes controls, "relative-deep-01" + assert_includes controls, "child-02" + # should not have controls from the profile defined at the top of the repo of the child profile + refute_includes controls, "red-dye" + end + end # describe "running a profile with a combination of relative path dependencies" #------------ Edge Cases for Relative Path Support -------------------# diff --git a/test/unit/mock/profiles/git-fetcher/relative-deep/controls/relative-deep.rb b/test/unit/mock/profiles/git-fetcher/relative-deep/controls/relative-deep.rb new file mode 100644 index 000000000..5bf65811b --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-deep/controls/relative-deep.rb @@ -0,0 +1,7 @@ +include_controls 'child-02' + +control 'relative-deep-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/relative-deep/inspec.yml b/test/unit/mock/profiles/git-fetcher/relative-deep/inspec.yml new file mode 100644 index 000000000..17e0bca05 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-deep/inspec.yml @@ -0,0 +1,11 @@ +name: relative-deep +title: releative-deep +license: Apache-2.0 +summary: A profile that uses git to depend on profiles in deep subdirs +version: 0.1.0 +supports: + platform: os +depends: +- name: child-02 + git: test/unit/mock/profiles/git-fetcher/git-repo-01 + relative_path: deeper-path/in/repo/child-02 diff --git a/test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb b/test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb deleted file mode 100644 index d0c3298ac..000000000 --- a/test/unit/mock/profiles/git-fetcher/top-level-relative/controls/top-level.rb +++ /dev/null @@ -1,8 +0,0 @@ -# include_controls 'child-01' -# include_controls 'child-02' - -control 'top-level-01' do - describe 'always-pass' do - it { should cmp 'always-pass'} - end -end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml b/test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml deleted file mode 100644 index bd94c6bca..000000000 --- a/test/unit/mock/profiles/git-fetcher/top-level-relative/inspec.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: top-level-relative -title: top-level-relative -license: Apache-2.0 -summary: A profile that depends on profiles that are stored in a repo in subdirs -version: 0.1.0 -supports: - platform: os -depends: -- name: child-01 - git: test/unit/mock/profiles/git-fetcher/git-repo-01 - relative_path: child-01 -# - name: child-02 -# git: ./git-repo-01 -# relative_path: deeper-path/in/repo/child-01 From fab58b590e2b2b5f10d0637738223dbdce43e57d Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 18 Jun 2019 12:52:25 -0400 Subject: [PATCH 10/18] Combo relative test in place Signed-off-by: Clinton Wolfe --- test/functional/git_fetcher_test.rb | 60 ++++++++++--------- .../relative-combo/controls/relative-combo.rb | 8 +++ .../git-fetcher/relative-combo/inspec.yml | 14 +++++ 3 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 test/unit/mock/profiles/git-fetcher/relative-combo/controls/relative-combo.rb create mode 100644 test/unit/mock/profiles/git-fetcher/relative-combo/inspec.yml diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index 605e888e2..23616e6d6 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -32,7 +32,24 @@ describe "running profiles with git-based dependencies" do end end - # TODO: move private SSH+git test from inspec_exec_test to here + #======================================================================# + # 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 + run_result.must_have_all_controls_passing + + # Should know about the top-level profile and the child profile + assert_equal expected_profiles, (run_result.payload.json["profiles"].map { |p| p["name"] }) + + controls = run_result.payload.json["profiles"].map { |p| p["controls"] }.flatten.map { |c| c["id"] }.uniq + # 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 #======================================================================# # Basic Git Fetching @@ -46,6 +63,8 @@ describe "running profiles with git-based dependencies" do end # describe "running a profile with a basic remote dependency" + # TODO: move private SSH+git test from inspec_exec_test to here + #======================================================================# # Revision Selection #======================================================================# @@ -58,40 +77,25 @@ describe "running profiles with git-based dependencies" do #------------ Happy Cases for Relative Path Support -------------------# describe "running a profile with a shallow relative path dependency" do it "should find the relative path profile and execute exactly those controls" do - run_result = run_inspec_process("exec #{git_profiles}/relative-shallow", json: true) - assert_empty run_result.stderr - run_result.must_have_all_controls_passing - - # Should know about the top-level profile and the child profile - assert_equal ["relative-shallow", "child-01"], (run_result.payload.json["profiles"].map { |p| p["name"] }) - - controls = run_result.payload.json["profiles"].map { |p| p["controls"] }.flatten.map { |c| c["id"] }.uniq - # Should have controls from the top-level and and included child profile - assert_includes controls, "top-level-01" - assert_includes controls, "child-01" - # should not have controls from the profile defined at the top of the repo of the child profile - refute_includes controls, "red-dye" + assert_relative_fetch_works("relative-shallow", ["relative-shallow", "child-01"], ["top-level-01", "child-01"]) end end describe "running a profile with a deep relative path dependency" do it "should find the relative path profile and execute exactly those controls" do - run_result = run_inspec_process("exec #{git_profiles}/relative-deep", json: true) - assert_empty run_result.stderr - run_result.must_have_all_controls_passing - - # Should know about the top-level profile and the child profile - assert_equal ["relative-deep", "child-02"], (run_result.payload.json["profiles"].map { |p| p["name"] }) - - controls = run_result.payload.json["profiles"].map { |p| p["controls"] }.flatten.map { |c| c["id"] }.uniq - # Should have controls from the top-level and and included child profile - assert_includes controls, "relative-deep-01" - assert_includes controls, "child-02" - # should not have controls from the profile defined at the top of the repo of the child profile - refute_includes controls, "red-dye" + assert_relative_fetch_works("relative-deep", ["relative-deep", "child-02"], ["relative-deep-01", "child-02"]) + end + end + + 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", + ["relative-combo", "child-01", "child-02"], + ["relative-combo-01", "child-01", "child-02"] + ) end end - # describe "running a profile with a combination of relative path dependencies" #------------ Edge Cases for Relative Path Support -------------------# # describe "running a profile with an '' relative path dependency" diff --git a/test/unit/mock/profiles/git-fetcher/relative-combo/controls/relative-combo.rb b/test/unit/mock/profiles/git-fetcher/relative-combo/controls/relative-combo.rb new file mode 100644 index 000000000..ed217b6e6 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-combo/controls/relative-combo.rb @@ -0,0 +1,8 @@ +include_controls 'child-01' +include_controls 'child-02' + +control 'relative-combo-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/relative-combo/inspec.yml b/test/unit/mock/profiles/git-fetcher/relative-combo/inspec.yml new file mode 100644 index 000000000..30fe852bd --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-combo/inspec.yml @@ -0,0 +1,14 @@ +name: relative-combo +title: relative-combo +license: Apache-2.0 +summary: A profile that uses git to depend on profiles in deep subdirs +version: 0.1.0 +supports: + platform: os +depends: +- name: child-01 + git: test/unit/mock/profiles/git-fetcher/git-repo-01 + relative_path: child-01 +- name: child-02 + git: test/unit/mock/profiles/git-fetcher/git-repo-01 + relative_path: deeper-path/in/repo/child-02 From 31bedf7b3730eb5961118556c021915bbd89e791 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 18 Jun 2019 13:13:05 -0400 Subject: [PATCH 11/18] Empty-string edge case Signed-off-by: Clinton Wolfe --- test/functional/git_fetcher_test.rb | 7 ++++++- .../relative-empty/controls/relative-empty.rb | 7 +++++++ .../profiles/git-fetcher/relative-empty/inspec.yml | 11 +++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/unit/mock/profiles/git-fetcher/relative-empty/controls/relative-empty.rb create mode 100644 test/unit/mock/profiles/git-fetcher/relative-empty/inspec.yml diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index 23616e6d6..0bec45bee 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -98,7 +98,12 @@ describe "running profiles with git-based dependencies" do end #------------ Edge Cases for Relative Path Support -------------------# - # describe "running a profile with an '' relative path dependency" + 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 + assert_relative_fetch_works("relative-empty", ["relative-empty", "basic-local"], ["relative-empty-01", "basic-local-01"]) + end + end + # describe "running a profile with an ./ relative path dependency" # describe "running a profile with a relative path dependency that does not exist" diff --git a/test/unit/mock/profiles/git-fetcher/relative-empty/controls/relative-empty.rb b/test/unit/mock/profiles/git-fetcher/relative-empty/controls/relative-empty.rb new file mode 100644 index 000000000..60c093c7f --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-empty/controls/relative-empty.rb @@ -0,0 +1,7 @@ +include_controls 'basic-local' + +control 'relative-empty-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/relative-empty/inspec.yml b/test/unit/mock/profiles/git-fetcher/relative-empty/inspec.yml new file mode 100644 index 000000000..418bcdeff --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-empty/inspec.yml @@ -0,0 +1,11 @@ +name: relative-empty +title: relative-empty +license: Apache-2.0 +summary: A profile that depends on profiles using an empty-string path +version: 0.1.0 +supports: + platform: os +depends: +- name: basic-local + git: test/unit/mock/profiles/git-fetcher/basic-local + relative_path: '' From 860d21a37036da7b00cf19062b01c4f19bef2191 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 18 Jun 2019 15:22:14 -0400 Subject: [PATCH 12/18] Test for ./ Signed-off-by: Clinton Wolfe --- test/functional/git_fetcher_test.rb | 13 +++++++++++-- .../controls/relative-dot-slash.rb | 7 +++++++ .../git-fetcher/relative-dot-slash/inspec.yml | 11 +++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/unit/mock/profiles/git-fetcher/relative-dot-slash/controls/relative-dot-slash.rb create mode 100644 test/unit/mock/profiles/git-fetcher/relative-dot-slash/inspec.yml diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index 0bec45bee..044c8eebf 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -98,13 +98,22 @@ describe "running profiles with git-based dependencies" do end #------------ Edge Cases for Relative Path Support -------------------# + + # TODO: These next two tests trigger a bug, in which the profile appears + # to have a key collision in the cache. + 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 assert_relative_fetch_works("relative-empty", ["relative-empty", "basic-local"], ["relative-empty-01", "basic-local-01"]) end end - # describe "running a profile with an ./ relative path dependency" - # describe "running a profile with a relative path dependency that does not exist" + 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 + assert_relative_fetch_works("relative-dot-slash", ["relative-dot-slash", "basic-local"], ["relative-dot-slash-01", "basic-local-01"]) + end + end + + # describe "running a profile with a relative path dependency that does not exist" do end diff --git a/test/unit/mock/profiles/git-fetcher/relative-dot-slash/controls/relative-dot-slash.rb b/test/unit/mock/profiles/git-fetcher/relative-dot-slash/controls/relative-dot-slash.rb new file mode 100644 index 000000000..a893c2bf4 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-dot-slash/controls/relative-dot-slash.rb @@ -0,0 +1,7 @@ +include_controls 'basic-local' + +control 'relative-dot-slash-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/relative-dot-slash/inspec.yml b/test/unit/mock/profiles/git-fetcher/relative-dot-slash/inspec.yml new file mode 100644 index 000000000..1dc08f6c0 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-dot-slash/inspec.yml @@ -0,0 +1,11 @@ +name: relative-dot-slash +title: relative-dot-slash +license: Apache-2.0 +summary: A profile that depends on profiles using an ./ path +version: 0.1.0 +supports: + platform: os +depends: +- name: basic-local + git: test/unit/mock/profiles/git-fetcher/basic-local + relative_path: ./ From 4f6d296a22e3f95fab72ecb8f68727eda01a6faf Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 18 Jun 2019 18:06:51 -0400 Subject: [PATCH 13/18] The error case in which the path does not exist Signed-off-by: Clinton Wolfe --- lib/fetchers/git.rb | 8 +++++++- test/functional/git_fetcher_test.rb | 17 +++++++++++++++-- .../controls/relative-nonesuch.rb | 7 +++++++ .../git-fetcher/relative-nonesuch/inspec.yml | 11 +++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 test/unit/mock/profiles/git-fetcher/relative-nonesuch/controls/relative-nonesuch.rb create mode 100644 test/unit/mock/profiles/git-fetcher/relative-nonesuch/inspec.yml diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 8a3c6eb09..05113415b 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -46,6 +46,9 @@ module Fetchers end def expand_local_path(url_or_file_path) + # This paths to local on-disk repos, not relative paths within repos. + # This is especially needed with testing. + # We could try to do something clever with URI # processing, but then again, if you passed a relative path # to an on-disk repo, you probably expect it to exist. @@ -62,7 +65,6 @@ module Fetchers if cloned? checkout - # TODO - verify this still works with relative path else Dir.mktmpdir do |tmpdir| checkout(tmpdir) @@ -70,7 +72,11 @@ module Fetchers @profile_directory = dir Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ "Moving #{@relative_path} to #{dir}") + unless File.exist?("#{tmpdir}/#{@relative_path}") + raise ArgumentError.new("Cannot find relative path '#{@relative_path}' within profile in git repo specified by '#{@remote_url}'") + end target_profile = File.join(tmpdir, @relative_path) + FileUtils.cp_r(target_profile, dir) else Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index 044c8eebf..bb8b18cd4 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -114,6 +114,19 @@ describe "running profiles with git-based dependencies" do end end - # describe "running a profile with a relative path dependency that does not exist" do - + 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_exit_code(1, run_result) # General user error + 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) + assert_includes run_result.stderr, "test/unit/mock/profiles/git-fetcher/git-repo-01" + end + end end diff --git a/test/unit/mock/profiles/git-fetcher/relative-nonesuch/controls/relative-nonesuch.rb b/test/unit/mock/profiles/git-fetcher/relative-nonesuch/controls/relative-nonesuch.rb new file mode 100644 index 000000000..748c26a12 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-nonesuch/controls/relative-nonesuch.rb @@ -0,0 +1,7 @@ +include_controls 'none-such-01' + +control 'relative-nonesuch-01' do + describe 'always-pass' do + it { should cmp 'always-pass'} + end +end \ No newline at end of file diff --git a/test/unit/mock/profiles/git-fetcher/relative-nonesuch/inspec.yml b/test/unit/mock/profiles/git-fetcher/relative-nonesuch/inspec.yml new file mode 100644 index 000000000..fda4ee7e7 --- /dev/null +++ b/test/unit/mock/profiles/git-fetcher/relative-nonesuch/inspec.yml @@ -0,0 +1,11 @@ +name: relative-nonesuch +title: relative-nonesuch +license: Apache-2.0 +summary: A profile that depends on a profile with a bad relative path +version: 0.1.0 +supports: + platform: os +depends: +- name: none-such-01 + git: test/unit/mock/profiles/git-fetcher/git-repo-01 + relative_path: no/such/path From 1a47026419ac868ca29cf9dd95f786f24374f7e5 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Wed, 19 Jun 2019 23:44:13 -0400 Subject: [PATCH 14/18] Rename some vars, eliminate @profile_directory Signed-off-by: Clinton Wolfe --- lib/fetchers/git.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 05113415b..8153575ef 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -41,7 +41,6 @@ module Fetchers @ref = opts[:ref] @remote_url = expand_local_path(remote_url) @repo_directory = nil - @profile_directory = nil # TODO remove this if possible, distinction without a difference @relative_path = opts[:relative_path] end @@ -59,33 +58,30 @@ module Fetchers File.expand_path(url_or_file_path) end - def fetch(dir) - @repo_directory = dir - FileUtils.mkdir_p(dir) unless Dir.exist?(dir) + def fetch(destination_path) + @repo_directory = destination_path + FileUtils.mkdir_p(destination_path) unless Dir.exist?(destination_path) if cloned? checkout else - Dir.mktmpdir do |tmpdir| - checkout(tmpdir) + Dir.mktmpdir do |working_dir| + checkout(working_dir) if @relative_path - @profile_directory = dir Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ - "Moving #{@relative_path} to #{dir}") - unless File.exist?("#{tmpdir}/#{@relative_path}") + "Moving #{@relative_path} to #{destination_path}") + unless File.exist?("#{working_dir}/#{@relative_path}") raise ArgumentError.new("Cannot find relative path '#{@relative_path}' within profile in git repo specified by '#{@remote_url}'") end - target_profile = File.join(tmpdir, @relative_path) - - FileUtils.cp_r(target_profile, dir) + FileUtils.cp_r("#{working_dir}/#{@relative_path}", destination_path) else Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ - "Moving checkout to #{dir}") - FileUtils.cp_r(tmpdir + "/.", dir) + "Moving checkout to #{destination_path}") + FileUtils.cp_r(working_dir + "/.", destination_path) end end end - @profile_directory || @repo_directory + @repo_directory end def cache_key @@ -94,7 +90,7 @@ module Fetchers end def archive_path - @profile_directory || @repo_directory + @repo_directory end def resolved_source From abc5e84b87504fba05b44e6561020c98ee324d79 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Thu, 20 Jun 2019 00:57:58 -0400 Subject: [PATCH 15/18] Fix two tiresome cache issues with simple fixes Signed-off-by: Clinton Wolfe --- lib/fetchers/git.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 8153575ef..01a14633c 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -41,7 +41,7 @@ module Fetchers @ref = opts[:ref] @remote_url = expand_local_path(remote_url) @repo_directory = nil - @relative_path = opts[:relative_path] + @relative_path = opts[:relative_path] if opts[:relative_path] && !opts[:relative_path].empty? end def expand_local_path(url_or_file_path) @@ -59,7 +59,7 @@ module Fetchers end def fetch(destination_path) - @repo_directory = destination_path + @repo_directory = destination_path # Might be the cache, or vendoring, or something else FileUtils.mkdir_p(destination_path) unless Dir.exist?(destination_path) if cloned? @@ -71,7 +71,14 @@ module Fetchers Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ "Moving #{@relative_path} to #{destination_path}") unless File.exist?("#{working_dir}/#{@relative_path}") - raise ArgumentError.new("Cannot find relative path '#{@relative_path}' within profile in git repo specified by '#{@remote_url}'") + # Cleanup the destination path - otherwise we'll have an empty dir + # in the cache, which is enough to confuse the cache reader + # This is acourtesy, assuming we're writing to the cache; if we're + # vendoring to somthing more complex, don't bother. + FileUtils.rmdir(destination_path) if Dir.empty?(destination_path) + + raise ArgumentError.new("Cannot find relative path '#{@relative_path}' " \ + "within profile in git repo specified by '#{@remote_url}'") end FileUtils.cp_r("#{working_dir}/#{@relative_path}", destination_path) else From c03862fe7bf7cf5d566b10054a00ef1b21f68274 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Thu, 20 Jun 2019 14:56:57 -0400 Subject: [PATCH 16/18] Code climate updates Signed-off-by: Clinton Wolfe --- .codeclimate.yml | 3 +++ lib/fetchers/git.rb | 30 ++++++++++++++++------------- test/functional/git_fetcher_test.rb | 5 +---- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/.codeclimate.yml b/.codeclimate.yml index 6d90eae38..bfa974e69 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -5,6 +5,9 @@ checks: identical-code: config: threshold: 40 # Unfortunately, we have a lot of duplicate code in places like lib/inspec/control_eval_context.rb + method-complexity: # 'Cognitive Complexity' in the UI + config: + threshold: 10 # 5 Default 5 is really tight plugins: fixme: enabled: true diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index 01a14633c..a78b55f9f 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -68,19 +68,7 @@ module Fetchers Dir.mktmpdir do |working_dir| checkout(working_dir) if @relative_path - Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ - "Moving #{@relative_path} to #{destination_path}") - unless File.exist?("#{working_dir}/#{@relative_path}") - # Cleanup the destination path - otherwise we'll have an empty dir - # in the cache, which is enough to confuse the cache reader - # This is acourtesy, assuming we're writing to the cache; if we're - # vendoring to somthing more complex, don't bother. - FileUtils.rmdir(destination_path) if Dir.empty?(destination_path) - - raise ArgumentError.new("Cannot find relative path '#{@relative_path}' " \ - "within profile in git repo specified by '#{@remote_url}'") - end - FileUtils.cp_r("#{working_dir}/#{@relative_path}", destination_path) + perform_relative_path_fetch(destination_path, working_dir) else Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ "Moving checkout to #{destination_path}") @@ -91,6 +79,22 @@ module Fetchers @repo_directory end + def perform_relative_path_fetch(destination_path, working_dir) + Inspec::Log.debug("Checkout of #{resolved_ref} successful. " \ + "Moving #{@relative_path} to #{destination_path}") + unless File.exist?("#{working_dir}/#{@relative_path}") + # Cleanup the destination path - otherwise we'll have an empty dir + # in the cache, which is enough to confuse the cache reader + # This is acourtesy, assuming we're writing to the cache; if we're + # vendoring to somthing more complex, don't bother. + FileUtils.rmdir(destination_path) if Dir.empty?(destination_path) + + raise ArgumentError.new("Cannot find relative path '#{@relative_path}' " \ + "within profile in git repo specified by '#{@remote_url}'") + end + FileUtils.cp_r("#{working_dir}/#{@relative_path}", destination_path) + end + def cache_key return resolved_ref unless @relative_path OpenSSL::Digest::SHA256.hexdigest(resolved_ref + @relative_path) diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index bb8b18cd4..a93bddd09 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -68,7 +68,7 @@ describe "running profiles with git-based dependencies" do #======================================================================# # Revision Selection #======================================================================# - # TODO: test branch, rev, and tag capabilities + # NOTE: test branch, rev, and tag capabilities are tested in unit tests #======================================================================# # Relative Path Support @@ -99,9 +99,6 @@ describe "running profiles with git-based dependencies" do #------------ Edge Cases for Relative Path Support -------------------# - # TODO: These next two tests trigger a bug, in which the profile appears - # to have a key collision in the cache. - 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 assert_relative_fetch_works("relative-empty", ["relative-empty", "basic-local"], ["relative-empty-01", "basic-local-01"]) From f00e74199973422d61c32f53e907f5beddea0f35 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Thu, 20 Jun 2019 22:12:16 -0400 Subject: [PATCH 17/18] PR feedback Signed-off-by: Clinton Wolfe --- lib/fetchers/git.rb | 8 ++++---- test/functional/git_fetcher_test.rb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/fetchers/git.rb b/lib/fetchers/git.rb index a78b55f9f..49203a701 100644 --- a/lib/fetchers/git.rb +++ b/lib/fetchers/git.rb @@ -85,12 +85,12 @@ module Fetchers unless File.exist?("#{working_dir}/#{@relative_path}") # Cleanup the destination path - otherwise we'll have an empty dir # in the cache, which is enough to confuse the cache reader - # This is acourtesy, assuming we're writing to the cache; if we're - # vendoring to somthing more complex, don't bother. + # This is a courtesy, assuming we're writing to the cache; if we're + # vendoring to something more complex, don't bother. FileUtils.rmdir(destination_path) if Dir.empty?(destination_path) - raise ArgumentError.new("Cannot find relative path '#{@relative_path}' " \ - "within profile in git repo specified by '#{@remote_url}'") + raise ArgumentError, "Cannot find relative path '#{@relative_path}' " \ + "within profile in git repo specified by '#{@remote_url}'" end FileUtils.cp_r("#{working_dir}/#{@relative_path}", destination_path) end diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index a93bddd09..4ff08f778 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -68,7 +68,7 @@ describe "running profiles with git-based dependencies" do #======================================================================# # Revision Selection #======================================================================# - # NOTE: test branch, rev, and tag capabilities are tested in unit tests + # NOTE: test branch, rev, and tag capabilities are (lighty) tested in unit tests #======================================================================# # Relative Path Support @@ -114,7 +114,6 @@ describe "running profiles with git-based dependencies" do 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_exit_code(1, run_result) # General user error 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 @@ -124,6 +123,7 @@ describe "running profiles with git-based dependencies" do assert_includes run_result.stderr, "profile in git repo" # The containing git repo (the only identifier the user will have) assert_includes run_result.stderr, "test/unit/mock/profiles/git-fetcher/git-repo-01" + assert_exit_code(1, run_result) # General user error end end end From 88898ce7a938473485a16f50f64483f497463a31 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 30 Jul 2019 13:42:24 -0400 Subject: [PATCH 18/18] Linting Signed-off-by: Clinton Wolfe --- test/functional/git_fetcher_test.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/functional/git_fetcher_test.rb b/test/functional/git_fetcher_test.rb index 4ff08f778..d5940db6e 100644 --- a/test/functional/git_fetcher_test.rb +++ b/test/functional/git_fetcher_test.rb @@ -9,7 +9,7 @@ describe "running profiles with git-based dependencies" do #======================================================================# # Git Repo Setup #======================================================================# - fixture_repos = ["basic-local", "git-repo-01"] + fixture_repos = %w{basic-local git-repo-01} before(:all) do skip_windows! # Right now, this is due to symlinking @@ -77,13 +77,13 @@ describe "running profiles with git-based dependencies" do #------------ Happy Cases for Relative Path Support -------------------# describe "running a profile with a shallow relative path dependency" do it "should find the relative path profile and execute exactly those controls" do - assert_relative_fetch_works("relative-shallow", ["relative-shallow", "child-01"], ["top-level-01", "child-01"]) + assert_relative_fetch_works("relative-shallow", %w{relative-shallow child-01}, %w{top-level-01 child-01}) end end describe "running a profile with a deep relative path dependency" do it "should find the relative path profile and execute exactly those controls" do - assert_relative_fetch_works("relative-deep", ["relative-deep", "child-02"], ["relative-deep-01", "child-02"]) + assert_relative_fetch_works("relative-deep", %w{relative-deep child-02}, %w{relative-deep-01 child-02}) end end @@ -91,8 +91,8 @@ describe "running profiles with git-based dependencies" do it "should find the relative path profiles and execute exactly those controls" do assert_relative_fetch_works( "relative-combo", - ["relative-combo", "child-01", "child-02"], - ["relative-combo-01", "child-01", "child-02"] + %w{relative-combo child-01 child-02}, + %w{relative-combo-01 child-01 child-02} ) end end @@ -101,13 +101,13 @@ describe "running profiles with git-based dependencies" do 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 - assert_relative_fetch_works("relative-empty", ["relative-empty", "basic-local"], ["relative-empty-01", "basic-local-01"]) + assert_relative_fetch_works("relative-empty", %w{relative-empty basic-local}, %w{relative-empty-01 basic-local-01}) end end 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 - assert_relative_fetch_works("relative-dot-slash", ["relative-dot-slash", "basic-local"], ["relative-dot-slash-01", "basic-local-01"]) + assert_relative_fetch_works("relative-dot-slash", %w{relative-dot-slash basic-local}, %w{relative-dot-slash-01 basic-local-01}) end end