diff options
author | Bryan McLellan <btm@loftninjas.org> | 2019-04-15 17:30:01 -0400 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2019-04-15 20:58:55 -0400 |
commit | 7f62eff6d676365c9e1a984728e9b02bbd74d3ae (patch) | |
tree | 7fe4975031717ea3dfac77f0789b1d57812d02c7 | |
parent | 2fe2f5029df438ff92274b3240df5f5bee10c067 (diff) | |
download | chef-7f62eff6d676365c9e1a984728e9b02bbd74d3ae.tar.gz |
Limit locale resource to Linux
Platforms that don't use /etc/locale.conf aren't supported
Signed-off-by: Bryan McLellan <btm@loftninjas.org>
-rw-r--r-- | lib/chef/resource/locale.rb | 28 | ||||
-rw-r--r-- | spec/functional/resource/locale_spec.rb | 109 |
2 files changed, 80 insertions, 57 deletions
diff --git a/lib/chef/resource/locale.rb b/lib/chef/resource/locale.rb index b16cfe7e3c..52170e8765 100644 --- a/lib/chef/resource/locale.rb +++ b/lib/chef/resource/locale.rb @@ -28,6 +28,7 @@ class Chef LC_VARIABLES = %w{LC_ADDRESS LC_COLLATE LC_CTYPE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME}.freeze LOCALE_CONF = "/etc/locale.conf".freeze LOCALE_REGEX = /\A\S+/.freeze + LOCALE_PLATFORM_FAMILIES = %w{debian}.freeze property :lang, String, description: "Sets the default system language.", @@ -71,28 +72,31 @@ class Chef update_locale end end - rescue - # It might affect debugging - raise "#{node['platform']} platform is not supported by the chef locale resource. " + - "If you believe this is in error please file an issue at https://github.com/chef/chef/issues" end end action_class do + # Avoid running this resource on platforms that don't use /etc/locale.conf + # + def define_resource_requirements + requirements.assert(:all_actions) do |a| + a.assertion { LOCALE_PLATFORM_FAMILIES.include?(node[:platform_family]) } + a.failure_message(Chef::Exceptions::ProviderNotFound, "The locale resource is not supported on platform family: #{node[:platform_family]}") + end + + requirements.assert(:all_actions) do |a| + # RHEL/CentOS type platforms don't have locale-gen + a.assertion { shell_out("locale-gen") } + a.failure_message(Chef::Exceptions::ProviderNotFound, "The locale resource requires the locale-gen tool") + end + end # Generates the localisation files from templates using locale-gen. # @see http://manpages.ubuntu.com/manpages/cosmic/man8/locale-gen.8.html # @raise [Mixlib::ShellOut::ShellCommandFailed] not a supported language or locale # def generate_locales - bash "Generating locales: #{unavailable_locales.join(' ')}" do - code <<~CODE - if type locale-gen >/dev/null 2>&1 - then - locale-gen #{unavailable_locales.join(' ')} - fi - CODE - end + shell_out!("locale-gen #{unavailable_locales.join(' ')}") end # Updates system locale by appropriately writing them in /etc/locale.conf diff --git a/spec/functional/resource/locale_spec.rb b/spec/functional/resource/locale_spec.rb index 61ef4630bc..83dfc2d007 100644 --- a/spec/functional/resource/locale_spec.rb +++ b/spec/functional/resource/locale_spec.rb @@ -18,61 +18,80 @@ require "spec_helper" -describe Chef::Resource::Locale, :requires_root, :not_supported_on_windows do - - let(:node) { Chef::Node.new } +describe Chef::Resource::Locale, :requires_root do + let(:node) do + n = Chef::Node.new + n.consume_external_attrs(OHAI_SYSTEM.data, {}) + n + end let(:events) { Chef::EventDispatch::Dispatcher.new } let(:run_context) { Chef::RunContext.new(node, {}, events) } let(:resource) { Chef::Resource::Locale.new("fakey_fakerton", run_context) } - def sets_system_locale(*locales) - system_locales = File.readlines("/etc/locale.conf") - expect(system_locales.map(&:strip)).to eq(locales) - end + context "on debian/ubuntu", :debian_family_only do + def sets_system_locale(*locales) + system_locales = File.readlines("/etc/locale.conf") + expect(system_locales.map(&:strip)).to eq(locales) + end - def unsets_system_locale(*locales) - system_locales = File.readlines("/etc/locale.conf") - expect(system_locales.map(&:strip)).not_to eq(locales) - end + def unsets_system_locale(*locales) + system_locales = File.readlines("/etc/locale.conf") + expect(system_locales.map(&:strip)).not_to eq(locales) + end - describe "action: update" do - context "Sets system variable" do - it "when LC var is given" do - resource.lc_env({ "LC_MESSAGES" => "en_US" }) - resource.run_action(:update) - sets_system_locale("LC_MESSAGES=en_US") + describe "action: update" do + context "Sets system variable" do + it "when LC var is given" do + resource.lc_env({ "LC_MESSAGES" => "en_US" }) + resource.run_action(:update) + sets_system_locale("LC_MESSAGES=en_US") + end + it "when lang is given" do + resource.lang("en_US") + resource.run_action(:update) + sets_system_locale("LANG=en_US") + end + it "when both lang & LC vars are given" do + resource.lang("en_US") + resource.lc_env({ "LC_TIME" => "en_IN" }) + resource.run_action(:update) + sets_system_locale("LANG=en_US", "LC_TIME=en_IN") + end end - it "when lang is given" do - resource.lang("en_US") - resource.run_action(:update) - sets_system_locale("LANG=en_US") - end - it "when both lang & LC vars are given" do - resource.lang("en_US") - resource.lc_env({ "LC_TIME" => "en_IN" }) - resource.run_action(:update) - sets_system_locale("LANG=en_US", "LC_TIME=en_IN") + + context "Unsets system variable" do + it "when LC var is not given" do + resource.lc_env() + resource.run_action(:update) + unsets_system_locale("LC_MESSAGES=en_US") + end + it "when lang is not given" do + resource.lang() + resource.run_action(:update) + unsets_system_locale("LANG=en_US") + end + it "when both lang & LC vars are not given" do + resource.lang() + resource.lc_env() + resource.run_action(:update) + unsets_system_locale("LANG=en_US", "LC_TIME=en_IN") + sets_system_locale("") + end end end + end - context "Unsets system variable" do - it "when LC var is not given" do - resource.lc_env() - resource.run_action(:update) - unsets_system_locale("LC_MESSAGES=en_US") - end - it "when lang is not given" do - resource.lang() - resource.run_action(:update) - unsets_system_locale("LANG=en_US") - end - it "when both lang & LC vars are not given" do - resource.lang() - resource.lc_env() - resource.run_action(:update) - unsets_system_locale("LANG=en_US", "LC_TIME=en_IN") - sets_system_locale("") - end + context "on rhel", :rhel do + it "raises an exception due lacking the locale-gen tool" do + resource.lang("en_US") + expect { resource.run_action(:update) }.to raise_error(Chef::Exceptions::ProviderNotFound) + end + end + + context "on macos", :macos_only do + it "raises an exception due to being an unsupported platform" do + resource.lang("en_US") + expect { resource.run_action(:update) }.to raise_error(Chef::Exceptions::ProviderNotFound) end end end |