From 37455bdd11a2493718339b14ad32925a3d0177f7 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Wed, 29 Jan 2020 16:15:58 -0500 Subject: [PATCH] Parallelize unit tests Signed-off-by: Clinton Wolfe --- lib/inspec/utils/install_context.rb | 13 ++++- test/unit/utils/install_context_test.rb | 65 ++++++++++++------------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/lib/inspec/utils/install_context.rb b/lib/inspec/utils/install_context.rb index 43e5158f2..4757753f1 100644 --- a/lib/inspec/utils/install_context.rb +++ b/lib/inspec/utils/install_context.rb @@ -31,7 +31,7 @@ module Inspec def docker_install? # Our docker image is based on alpine. This could be easily fooled. - !!(rubygem_install? && File.exist?("/etc/alpine-release")) && File.exist?("/.dockerenv") + !!(rubygem_install? && path_exist?("/etc/alpine-release")) && path_exist?("/.dockerenv") end def habitat_install? @@ -49,7 +49,16 @@ module Inspec def source_install? # These are a couple of examples of dirs removed during packaging %w{habitat test}.all? do |devdir| - Dir.exist?("#{src_root}/#{devdir}") + path_exist?("#{src_root}/#{devdir}") + end + end + + def path_exist?(path) + # This exists for testing + if respond_to? :dummy_paths + dummy_paths.include? path + else + File.exist? path end end diff --git a/test/unit/utils/install_context_test.rb b/test/unit/utils/install_context_test.rb index 3e1158afa..90d5536dc 100644 --- a/test/unit/utils/install_context_test.rb +++ b/test/unit/utils/install_context_test.rb @@ -2,64 +2,65 @@ require "helper" require "inspec/globals" require "inspec/utils/install_context" -def assert_install_contexts(test_expected_to_be_true, also_rubygem) +def assert_install_contexts(test_obj, test_expected_to_be_true, also_rubygem) should_be_false = %w{chef-workstation chefdk docker habitat omnibus rubygem source} should_be_false -= [test_expected_to_be_true] should_be_false = should_be_false.map { |m| "#{m}_install?".tr("-", "_").to_sym } should_be_false -= [:rubygem_install?] if also_rubygem - should_be_false.each { |m| expect(Inspec.send(m)).must_equal false } + should_be_false.each { |m| expect(test_obj.send(m)).must_equal false } should_be_true = ["#{test_expected_to_be_true}_install?".tr("-", "_").to_sym] should_be_true += [:rubygem_install?] if also_rubygem - should_be_true.each { |m| expect(Inspec.send(m)).must_equal true } + should_be_true.each { |m| expect(test_obj.send(m)).must_equal true } - expect(Inspec.guess_install_context).must_equal test_expected_to_be_true + expect(test_obj.guess_install_context).must_equal test_expected_to_be_true +end + +class InstallContextTester + include Inspec::InstallContextHelpers + attr_accessor :src_root, :dummy_paths + def initialize(src_root: "", dummy_paths: []) + @src_root = src_root + @dummy_paths = dummy_paths + end end describe Inspec::InstallContextHelpers do - describe "when it looks like a Docker installation" do - before do - Inspec.expects(:src_root).at_least_once.returns("/somewhere/gems/inspec-4.18.39") - File.expects(:exist?).at_least_once.with("/etc/alpine-release").returns(true) - File.expects(:exist?).at_least_once.with("/.dockerenv").returns(true) - end + parallelize_me! + + describe "when it looks like a Docker installation" do it "should properly detect a Docker install" do - assert_install_contexts("docker", true) + test_obj = InstallContextTester.new( + src_root: "/somewhere/gems/inspec-4.18.39", dummy_paths: [ "/etc/alpine-release", "/.dockerenv" ] + ) + assert_install_contexts(test_obj, "docker", true) end end describe "when it looks like a Habitat installation" do - before do - Inspec.expects(:src_root).at_least_once.returns("/hab/pkgs/chef/inspec/4.18.61/20200121194907/lib/gems/inspec-4.18.61") - end - it "should properly detect a habitat install" do - assert_install_contexts("habitat", true) + test_obj = InstallContextTester.new(src_root: "/hab/pkgs/chef/inspec/4.18.61/20200121194907/lib/gems/inspec-4.18.61") + assert_install_contexts(test_obj, "habitat", true) end end describe "when it looks like a gem installation" do - before do - Inspec.expects(:src_root).at_least_once.returns("/Users/alice/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/inspec-4.18.61") - end - it "should properly detect a rubygem install" do - assert_install_contexts("rubygem", true) + test_obj = InstallContextTester.new(src_root: "/Users/alice/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/inspec-4.18.61") + assert_install_contexts(test_obj, "rubygem", true) end end describe "when it looks like a source installation" do - before do - fake_root = "/Users/alice/src/inspec" - Inspec.expects(:src_root).at_least_once.returns(fake_root) - Dir.expects(:exist?).with("#{fake_root}/habitat").at_least_once.returns(true) - Dir.expects(:exist?).with("#{fake_root}/test").at_least_once.returns(true) - end - it "should properly detect a source install" do - assert_install_contexts("source", false) + fake_root = "/Users/alice/src/inspec" + test_obj = InstallContextTester.new( + src_root: fake_root, + dummy_paths: [ "#{fake_root}/habitat", "#{fake_root}/test" ] + ) + assert_install_contexts(test_obj, "source", false) end end @@ -74,11 +75,9 @@ describe Inspec::InstallContextHelpers do "omnibus" => "inspec", }.each do |inst_mode, inst_subdir| describe "when it looks like a #{inst_mode} installation" do - before do - Inspec.expects(:src_root).at_least_once.returns("#{inst_dir}/#{inst_subdir}/embedded/lib/ruby/gems/2.6.0/gems/inspec-4.18.39") - end it "should properly detect a #{os_name} #{inst_mode} install" do - assert_install_contexts(inst_mode, true) + test_obj = InstallContextTester.new(src_root: "#{inst_dir}/#{inst_subdir}/embedded/lib/ruby/gems/2.6.0/gems/inspec-4.18.39") + assert_install_contexts(test_obj, inst_mode, true) end end end