diff options
author | Collin McNeese <cmcneese@chef.io> | 2021-02-22 10:45:07 -0600 |
---|---|---|
committer | Collin McNeese <cmcneese@chef.io> | 2021-02-22 10:45:36 -0600 |
commit | c8ca5b505053c7afcfd6a2ced7e0270119f9d1aa (patch) | |
tree | fee118c433d38fc5744ff4d5a96627bae5e8d033 | |
parent | 6453c81de7c2fcd2a8815120d2893293988988e0 (diff) | |
download | ohai-c8ca5b505053c7afcfd6a2ced7e0270119f9d1aa.tar.gz |
Adds chef_effortless collection to chef plugin
Signed-off-by: Collin McNeese <cmcneese@chef.io>
-rw-r--r-- | lib/ohai/plugins/chef.rb | 11 | ||||
-rw-r--r-- | spec/unit/plugins/chef_spec.rb | 19 |
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/ohai/plugins/chef.rb b/lib/ohai/plugins/chef.rb index 8a01e68c..268663d6 100644 --- a/lib/ohai/plugins/chef.rb +++ b/lib/ohai/plugins/chef.rb @@ -20,9 +20,19 @@ Ohai.plugin(:Chef) do provides "chef_packages/chef" + def chef_effortless? + # Determine if client is being run as a Habitat package. + if Chef::CHEF_ROOT.include?("hab/pkgs/chef/chef") + # Determine if client is running in zero mode which would show it is using the Effortless pattern. + # Explicitly set response to true or nil, not false + ChefConfig::Config["chef_server_url"].include?("chefzero://") || nil + end + end + collect_data(:default, :target) do begin require "chef/version" + require "chef-config/config" unless defined?(ChefConfig::Config) rescue Gem::LoadError logger.trace("Plugin Chef: Unable to load the chef gem to determine the version") # this catches when you've done a major version bump of ohai, but @@ -35,5 +45,6 @@ Ohai.plugin(:Chef) do chef_packages[:chef] = Mash.new chef_packages[:chef][:version] = Chef::VERSION chef_packages[:chef][:chef_root] = Chef::CHEF_ROOT + chef_packages[:chef][:chef_effortless] = chef_effortless? end end diff --git a/spec/unit/plugins/chef_spec.rb b/spec/unit/plugins/chef_spec.rb index 2584a278..0db506fc 100644 --- a/spec/unit/plugins/chef_spec.rb +++ b/spec/unit/plugins/chef_spec.rb @@ -20,6 +20,8 @@ begin require "spec_helper" + require "chef/version" + require "chef-config/config" unless defined?(ChefConfig::Config) describe Ohai::System, "plugin chef" do before do @@ -31,6 +33,23 @@ begin expect(@plugin[:chef_packages][:chef][:version]).to eq(Chef::VERSION) end + it "sets [:chef_packages][:chef][:chef_root] to the current chef root directory", if: defined?(Chef) do + @plugin.run + expect(@plugin[:chef_packages][:chef][:chef_root]).to eq(Chef::CHEF_ROOT) + end + + it "does not create [:chef_packages][:chef][:chef_effortless] by default", if: defined?(Chef) do + @plugin.run + expect(@plugin[:chef_packages][:chef][:chef_effortless]).to eq(nil) + end + + it "sets [:chef_packages][:chef][:chef_effortless] to TRUE if executed from Habitat via CHEF_ROOT using Chef zero", if: defined?(Chef) do + stub_const("Chef::CHEF_ROOT", "/hab/pkgs/chef/chef-infra-client/X.X.X/XXXX/vendor/gems/chef-X.X.X/lib") + stub_const("ChefConfig::Config", { "chef_server_url" => "chefzero://localhost:1" }) + @plugin.run + expect(@plugin[:chef_packages][:chef][:chef_effortless]).to eq(true) + end + pending "would set [:chef_packages][:chef][:version] if chef was available", unless: defined?(Chef) end rescue LoadError |