mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
Install-path based install context detection
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
parent
2089bf80c7
commit
76a9c3e590
3 changed files with 148 additions and 1 deletions
|
@ -1,9 +1,15 @@
|
|||
require_relative "utils/install_context"
|
||||
|
||||
module Inspec
|
||||
|
||||
extend Inspec::InstallContextHelpers
|
||||
|
||||
def self.config_dir
|
||||
ENV["INSPEC_CONFIG_DIR"] ? ENV["INSPEC_CONFIG_DIR"] : File.join(Dir.home, ".inspec")
|
||||
end
|
||||
|
||||
def self.src_root
|
||||
File.expand_path(File.join(__FILE__, "..", "..", ".."))
|
||||
@src_root ||= File.expand_path(File.join(__FILE__, "..", "..", ".."))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
55
lib/inspec/utils/install_context.rb
Normal file
55
lib/inspec/utils/install_context.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
module Inspec
|
||||
|
||||
# Hueristics to determine how InSpec was installed.
|
||||
module InstallContextHelpers
|
||||
|
||||
def guess_install_context
|
||||
# These all work by simple path recognition
|
||||
return "chef-workstation" if chef_workstation_install?
|
||||
return "omnibus" if omnibus_install?
|
||||
return "chefdk" if chefdk_install?
|
||||
return "habitat" if habitat_install?
|
||||
|
||||
# Order matters here - gem mode is easily mistaken for one of the above
|
||||
return "docker" if docker_install?
|
||||
return "rubygem" if rubygem_install?
|
||||
|
||||
return "source" if source_install?
|
||||
|
||||
"unknown"
|
||||
end
|
||||
|
||||
def chef_workstation_install?
|
||||
!!(src_root.start_with?("/opt/chef-workstation") || src_root.start_with?("C:/opscode/chef-workstation"))
|
||||
end
|
||||
|
||||
def chefdk_install?
|
||||
!!(src_root.start_with?("/opt/chef-dk") || src_root.start_with?("C:/opscode/chef-dk"))
|
||||
end
|
||||
|
||||
def docker_install?
|
||||
# Our docker image is based on alpine. This could be easily fooled.
|
||||
!!(rubygem_install? && File.exist?("/etc/alpine-release"))
|
||||
end
|
||||
|
||||
def habitat_install?
|
||||
!!src_root.match(%r{hab/pkgs/chef/inspec/\d+\.\d+\.\d+/\d{14}})
|
||||
end
|
||||
|
||||
def omnibus_install?
|
||||
!!(src_root.start_with?("/opt/inspec") || src_root.start_with?("C:/opscode/inspec"))
|
||||
end
|
||||
|
||||
def rubygem_install?
|
||||
!!src_root.match(%r{gems/inspec-\d+\.\d+\.\d+})
|
||||
end
|
||||
|
||||
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}")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
86
test/unit/utils/install_context_test.rb
Normal file
86
test/unit/utils/install_context_test.rb
Normal file
|
@ -0,0 +1,86 @@
|
|||
require "helper"
|
||||
require "inspec/globals"
|
||||
require "inspec/utils/install_context"
|
||||
|
||||
def check_install_contexts(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.method(m).call).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.method(m).call).must_equal true }
|
||||
|
||||
expect(Inspec.guess_install_context).must_equal test_expected_to_be_true
|
||||
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)
|
||||
end
|
||||
|
||||
it "should properly detect a Docker install" do
|
||||
check_install_contexts("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
|
||||
check_install_contexts("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
|
||||
check_install_contexts("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
|
||||
check_install_contexts("source", false)
|
||||
end
|
||||
end
|
||||
|
||||
{
|
||||
"Windows" => "C:/opscode",
|
||||
"Unix-like" => "/opt",
|
||||
}.each do |os_name, inst_dir|
|
||||
describe "when on #{os_name} machines" do
|
||||
{
|
||||
"chef-workstation" => "chef-workstation",
|
||||
"chefdk" => "chef-dk",
|
||||
"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
|
||||
check_install_contexts(inst_mode, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue