Skip to content

Commit

Permalink
add pi::config & pi::config::fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoblitt committed Nov 21, 2023
1 parent 4372992 commit 8259f35
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
---
fixtures:
forge_modules:
stdlib: puppetlabs/stdlib # dep of puppetlabs/concat
augeas: camptocamp/augeas
reboot: puppetlabs/reboot
concat: puppetlabs/concat
1 change: 1 addition & 0 deletions examples/config.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include pi::cmdline
14 changes: 14 additions & 0 deletions examples/config_fragment.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class { 'pi::config':
reboot => false, # reboots are problematic for acceptance testing
}

pi::config::fragment { 'disable_overscan=1': }
pi::config::fragment { 'dtparam=audio=on':
order => 1,
}
pi::config::fragment { 'hdmi_mode=1':
order => 99,
}
pi::config::fragment { 'foo':
content => "hdmi_safe=1\n",
}
20 changes: 20 additions & 0 deletions examples/fragments.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class { 'pi::config':
reboot => false, # reboots are problematic for acceptance testing
fragments => {
'dtoverlay=pps-gpio,gpiopin=4' => {
'order' => 99,
},
'dtparam=i2c_arm=on' => {
'order' => 1,
},
'dtparam=spi=on' => {},
'serial port' => {
# lint:ignore:strict_indent
'content' => @("CONTENT"),
enable_uart=1
init_uart_baud=9600
| CONTENT
# lint:endignore
},
},
}
33 changes: 33 additions & 0 deletions manifests/config.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# @summary
# Manages `/boot/config.txt`
#
# @param fragments
# A hash of profile::pi::config::fragments to be concatenated into
# `/boot/config.txt`.
#
# @param reboot
# Whether or not to force a reboot when `/boot/config.txt` changes.
#
class pi::config (
Hash[String[1], Hash] $fragments = {},
Boolean $reboot = true,
) {
concat { '/boot/config.txt':
ensure => present,
mode => '0755', # this is the default, +x seems odd
}

$fragments.each | String $name, Hash $conf | {
pi::config::fragment { $name:
* => $conf,
}
}

if ($reboot) {
reboot { '/boot/config.txt':
apply => finished,
message => 'Rebooting to apply /boot/config.txt changes',
when => refreshed,
}
}
}
30 changes: 30 additions & 0 deletions manifests/config/fragment.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# @summary
# Create a /boot/config.txt fragment
#
# @param content
# /boot/config.txt configuration fragment
#
# @param order
# Order of the fragment within /boot/config.txt
#
define pi::config::fragment (
Optional[String[1]] $content = undef,
Integer[1] $order = 50,
) {
include pi::config

$_real_content = $content ? {
undef => "${name}\n",
default => $content,
}

concat::fragment { $name:
target => '/boot/config.txt',
content => $_real_content,
order => $order,
}

if Class['pi::config']['reboot'] {
Concat::Fragment[$name] ~> Reboot['/boot/config.txt']
}
}
4 changes: 4 additions & 0 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
{
"name": "puppetlabs/reboot",
"version_requirement": ">= 5.0.0 < 6.0.0"
},
{
"name": "puppetlabs/concat",
"version_requirement": ">= 9.0.0 < 10.0.0"
}
],
"operatingsystem_support": [
Expand Down
22 changes: 22 additions & 0 deletions spec/acceptance/config/fragment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'

describe 'pi::config::fragment define' do
let(:config_txt) do
<<~CONFIG
dtparam=audio=on
disable_overscan=1
hdmi_safe=1
hdmi_mode=1
CONFIG
end

include_context 'config.txt test setup'
include_examples 'the example', 'config_fragment.pp'

describe file('/boot/config.txt') do
it { is_expected.to be_file }
its(:content) { is_expected.to match config_txt }
end
end
29 changes: 29 additions & 0 deletions spec/acceptance/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'

describe 'pi::config class' do
context 'without any parameters' do
include_examples 'the example', 'config.pp'
end

context 'with parameters =>' do
let(:config_txt) do
<<~CONFIG
dtparam=i2c_arm=on
dtparam=spi=on
enable_uart=1
init_uart_baud=9600
dtoverlay=pps-gpio,gpiopin=4
CONFIG
end

include_context 'config.txt test setup'
include_examples 'the example', 'fragments.pp'

describe file('/boot/config.txt') do
it { is_expected.to be_file }
its(:content) { is_expected.to match config_txt }
end
end
end
44 changes: 44 additions & 0 deletions spec/classes/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'pi::config' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }

it { is_expected.to compile.with_all_deps }

it { is_expected.to contain_concat('/boot/config.txt').with_mode('0755') }
it { is_expected.to contain_reboot('/boot/config.txt') }

context 'with fragments param' do
let(:params) do
{
fragments: {
'foo' => {
'content' => 'foo=bar',
},
'baz' => {
'content' => 'baz=quix',
},
},
}
end

it { is_expected.to contain_concat__fragment('foo').with_content('foo=bar') }
it { is_expected.to contain_concat__fragment('baz').with_content('baz=quix') }
end

context 'with reboot => false' do
let(:params) do
{
reboot: false,
}
end

it { is_expected.not_to contain_reboot('/boot/config.txt') }
end
end
end
end
41 changes: 41 additions & 0 deletions spec/defines/config/fragment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'pi::config::fragment' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
let(:title) { 'foo' }
let(:params) { { 'content' => 'foo=bar' } }

it { is_expected.to compile.with_all_deps }

it do
is_expected.to contain_concat__fragment('foo').with(
target: '/boot/config.txt',
content: 'foo=bar',
order: 50,

Check failure on line 18 in spec/defines/config/fragment_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call. (https://rubystyle.guide#no-trailing-params-comma)
).that_notifies('Reboot[/boot/config.txt]')
end

context 'with reboot => false' do
let(:pre_condition) do
<<-PP
class { 'pi::config':
reboot => false,
}
PP
end

it do
is_expected.to contain_concat__fragment('foo').with(
target: '/boot/config.txt',
content: 'foo=bar',
order: 50,

Check failure on line 35 in spec/defines/config/fragment_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call. (https://rubystyle.guide#no-trailing-params-comma)
)
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/support/acceptance/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

shared_context 'config.txt test setup' do
before(:context) do

Check failure on line 4 in spec/support/acceptance/config.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

RSpec/BeforeAfterAll: Beware of using `before(:context)` as it may cause state to leak between tests. If you are using `rspec-rails`, and `use_transactional_fixtures` is enabled, then records created in `before(:context)` are not automatically rolled back. (https://rspec.rubystyle.guide/#avoid-hooks-with-context-scope, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeforeAfterAll)
shell('mkdir -p /boot')
end

after(:context) do

Check failure on line 8 in spec/support/acceptance/config.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

RSpec/BeforeAfterAll: Beware of using `after(:context)` as it may cause state to leak between tests. If you are using `rspec-rails`, and `use_transactional_fixtures` is enabled, then records created in `after(:context)` are not automatically rolled back. (https://rspec.rubystyle.guide/#avoid-hooks-with-context-scope, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeforeAfterAll)
shell('rm -rf /boot/config.txt')
end
end

0 comments on commit 8259f35

Please sign in to comment.