summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Williams <nathan@teamtreehouse.com>2016-05-06 13:33:21 -0700
committerNathan Williams <nathan@teamtreehouse.com>2016-05-13 10:45:15 -0700
commit1d9a232f90c1001785e8c916b5c5b6cff93e72c1 (patch)
tree93abc5105f35016236ae1a58212cbf1d36e9342d
parent3a982b8becf9b06a8e9ed772bb0e3912a5056254 (diff)
downloadchef-1d9a232f90c1001785e8c916b5c5b6cff93e72c1.tar.gz
reject malformed ini content
-rw-r--r--lib/chef/provider/systemd_unit.rb10
-rw-r--r--spec/unit/provider/systemd_unit_spec.rb27
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)