Skip to content

Commit

Permalink
Fix String value issues in grub_config
Browse files Browse the repository at this point in the history
* Ensure that Boolean values are converted to Strings
* Ensure the String values have quotes around them if not already
  present

Fixes voxpupuli#44, voxpupuli#45
  • Loading branch information
trevor-vaughan committed Jul 31, 2019
1 parent 402ee21 commit 7aeead4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/puppet/provider/grub_config/grub2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def value
end

def value=(newval)
if newval.is_a?(String)
unless %w[' "].include?(newval[0].chr)
newval = %Q("#{newval}")
end
end

augopen! do |aug|
aug.set("$target/#{resource[:name]}", newval)
end
Expand Down
12 changes: 12 additions & 0 deletions lib/puppet/type/grub_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
desc <<-EOM
Value of the GRUB parameter.
EOM

munge do |value|
value.to_s unless [Hash, Array].include?(value.class)
end

def insync?(is)
if is.is_a?(String) && should.is_a?(String)
is.gsub(/\A("|')|("|')\Z/,'') == should.gsub(/\A("|')|("|')\Z/,'')
else
is == should
end
end
end

autorequire(:file) do
Expand Down
56 changes: 49 additions & 7 deletions spec/acceptance/10_grub_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
context 'set timeout in grub' do
let(:manifest) { %(
grub_config { 'timeout':
value => '1'
value => 1
}
)}

Expand Down Expand Up @@ -40,7 +40,7 @@
context 'set fallback in grub' do
let(:manifest) { %(
grub_config { 'fallback':
value => '0'
value => 0
}
)}

Expand All @@ -62,7 +62,7 @@
context 'set timeout in grub2' do
let(:manifest) { %(
grub_config { 'GRUB_TIMEOUT':
value => '1'
value => 1
}
)}

Expand All @@ -76,15 +76,15 @@
end

it 'should have a timeout of 1' do
on(host, %(grep "GRUB_TIMEOUT=1" /etc/default/grub))
on(host, %(grep "timeout=1" /boot/grub2/grub.cfg))
on(host, %(grep 'GRUB_TIMEOUT="1"' /etc/default/grub))
on(host, %(grep 'timeout=1' /boot/grub2/grub.cfg))
end
end

context 'set arbitrary value in grub2' do
let(:manifest) { %(
grub_config { 'GRUB_FOOBAR':
value => 'BAZ'
value => 'BAZ'
}
)}

Expand All @@ -98,7 +98,7 @@
end

it 'should have a GRUB_FOOBAR of BAZ' do
on(host, %(grep "GRUB_FOOBAR=BAZ" /etc/default/grub))
on(host, %(grep 'GRUB_FOOBAR="BAZ"' /etc/default/grub))
end
end

Expand All @@ -122,5 +122,47 @@
on(host, %(grep "GRUB_FOOBAR" /etc/default/grub), :acceptable_exit_codes => [1])
end
end

context 'set Boolean value in grub2' do
let(:manifest) { %(
grub_config { 'GRUB_BOOLEAN_TEST':
value => true
}
)}

# Using puppet_apply as a helper
it 'should work with no errors' do
apply_manifest_on(host, manifest, :catch_failures => true)
end

it 'should be idempotent' do
apply_manifest_on(host, manifest, {:catch_changes => true})
end

it 'should have a GRUB_BOOLEAN_TEST that is true' do
on(host, %(grep 'GRUB_BOOLEAN_TEST="true"' /etc/default/grub))
end
end

context 'set a value with spaces in grub2' do
let(:manifest) { %(
grub_config { 'GRUB_SPACES_TEST':
value => 'this thing -has --spaces'
}
)}

# Using puppet_apply as a helper
it 'should work with no errors' do
apply_manifest_on(host, manifest, :catch_failures => true)
end

it 'should be idempotent' do
apply_manifest_on(host, manifest, {:catch_changes => true})
end

it 'should have a valid GRUB_SPACES_TEST entry' do
on(host, %(grep 'GRUB_SPACES_TEST="this thing -has --spaces"' /etc/default/grub))
end
end
end
end

0 comments on commit 7aeead4

Please sign in to comment.