diff options
author | Thom May <thom@may.lt> | 2016-05-14 15:55:19 +0100 |
---|---|---|
committer | Thom May <thom@may.lt> | 2016-05-14 15:55:19 +0100 |
commit | f4fa18ae3f763147479c64aa6fc4aa9ed07b5969 (patch) | |
tree | db1d52cbdc23ca0dc4dcaecf0d1df2b93fe47034 | |
parent | 6b61da24846a650981862991d8a63f16ff84e35a (diff) | |
parent | 1d9a232f90c1001785e8c916b5c5b6cff93e72c1 (diff) | |
download | chef-f4fa18ae3f763147479c64aa6fc4aa9ed07b5969.tar.gz |
Merge pull request #4907 from nathwill/systemd-unit-valid-ini
reject malformed ini content in systemd_unit resource
-rw-r--r-- | lib/chef/provider/systemd_unit.rb | 10 | ||||
-rw-r--r-- | spec/unit/provider/systemd_unit_spec.rb | 27 |
2 files changed, 36 insertions, 1 deletions
diff --git a/lib/chef/provider/systemd_unit.rb b/lib/chef/provider/systemd_unit.rb index db71a6c234..a73d0bc0ad 100644 --- a/lib/chef/provider/systemd_unit.rb +++ b/lib/chef/provider/systemd_unit.rb @@ -20,6 +20,7 @@ require "chef/provider" require "chef/mixin/which" require "chef/mixin/shell_out" require "chef/resource/file" +require "iniparse" class Chef class Provider @@ -43,6 +44,15 @@ class Chef current_resource end + def define_resource_requirements + super + + requirements.assert(:create) do |a| + a.assertion { IniParse.parse(new_resource.to_ini) } + a.failure_message "Unit content is not valid INI text" + end + end + def action_create if current_resource.content != new_resource.to_ini converge_by("creating unit: #{new_resource.name}") do diff --git a/spec/unit/provider/systemd_unit_spec.rb b/spec/unit/provider/systemd_unit_spec.rb index e71885f32d..0babc30808 100644 --- a/spec/unit/provider/systemd_unit_spec.rb +++ b/spec/unit/provider/systemd_unit_spec.rb @@ -39,7 +39,8 @@ describe Chef::Provider::SystemdUnit do let(:provider) { Chef::Provider::SystemdUnit.new(new_resource, run_context) } let(:unit_path_system) { "/etc/systemd/system/sysstat-collect.timer" } let(:unit_path_user) { "/etc/systemd/user/sysstat-collect.timer" } - let(:unit_content_string) { "[Unit]\nDescription=Run system activity accounting tool every 10 minutes\n\n[Timer]\nOnCalendar=*:00/10\n\n[Install]\nWantedBy=sysstat.service" } + let(:unit_content_string) { "[Unit]\nDescription = Run system activity accounting tool every 10 minutes\n\n[Timer]\nOnCalendar = *:00/10\n\n[Install]\nWantedBy = sysstat.service\n" } + let(:malformed_content_string) { "derp" } let(:unit_content_hash) do { @@ -86,6 +87,30 @@ describe Chef::Provider::SystemdUnit do .and_return(current_resource) end + describe "define_resource_requirements" do + before(:each) do + provider.action = :create + allow(provider).to receive(:active?).and_return(false) + allow(provider).to receive(:enabled?).and_return(false) + allow(provider).to receive(:masked?).and_return(false) + allow(provider).to receive(:static?).and_return(false) + end + + it "accepts valid resource requirements" do + new_resource.content(unit_content_string) + provider.load_current_resource + provider.define_resource_requirements + expect { provider.process_resource_requirements }.to_not raise_error + end + + it "rejects failed resource requirements" do + new_resource.content(malformed_content_string) + provider.load_current_resource + provider.define_resource_requirements + expect { provider.process_resource_requirements }.to raise_error(IniParse::ParseError) + end + end + describe "load_current_resource" do before(:each) do allow(provider).to receive(:active?).and_return(false) |