Skip to content

Commit

Permalink
Change facts used by module
Browse files Browse the repository at this point in the history
* Rename kernel_arguments to kdump_kernel_arguments
* Add crashkernel fact
* Checks for crashkernel being set are done using crashkernel fact
  • Loading branch information
treydock committed Aug 19, 2019
1 parent 8a1c608 commit 6f4781d
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 9 deletions.
25 changes: 25 additions & 0 deletions lib/facter/crashkernel.rb
@@ -0,0 +1,25 @@
# Fact: crashkernel
#
# Purpose:
# This fact checks for current crashkernel in kernel boot
#
# Resolution:
# Checks `proc/cmdline`
#
# Caveats:
# Only supports Linux.
#

Facter.add(:crashkernel) do
confine kernel: :linux
setcode do
crashkernel = false
kernel_arguments = Facter.value(:kdump_kernel_arguments)
if kernel_arguments
if kernel_arguments =~ %r{crashkernel=(\S+)}
crashkernel = Regexp.last_match(1)
end
end
crashkernel
end
end
@@ -1,4 +1,4 @@
# Fact: kernel_arguments
# Fact: kdump_kernel_arguments
#
# Purpose:
# This fact provides the arguments used for the currently running kernel
Expand All @@ -10,7 +10,7 @@
# Only supports Linux.
#

Facter.add(:kernel_arguments) do
Facter.add(:kdump_kernel_arguments) do
confine kernel: :linux
setcode do
cmdline_out = Facter::Core::Execution.execute('cat /proc/cmdline 2>/dev/null')
Expand Down
4 changes: 2 additions & 2 deletions manifests/init.pp
Expand Up @@ -132,7 +132,7 @@
notify => Service['kdump'],
}

if $::kernel_arguments !~ /crashkernel/ {
if ! $facts['crashkernel'] {
notify { 'kdump':
message => 'A reboot is required to fully enable the crashkernel'
}
Expand All @@ -151,7 +151,7 @@
}
}

if $::kernel_arguments =~ /crashkernel/ {
if $facts['crashkernel'] {
notify { 'kdump':
message => 'A reboot is required to fully disable the crashkernel'
}
Expand Down
6 changes: 3 additions & 3 deletions spec/classes/kdump_spec.rb
Expand Up @@ -4,7 +4,7 @@
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
facts.merge(kernel_arguments: my_fixture_read('kernelargs-with-crash.txt'))
facts.merge(crashkernel: 'auto')
end

let(:kernel_parameter_provider) do
Expand Down Expand Up @@ -62,7 +62,7 @@

context 'when kernel_arguments does not contain crashkernel' do
let(:facts) do
facts.merge(kernel_arguments: my_fixture_read('kernelargs-without-crash.txt'))
facts.merge(crashkernel: false)
end

it do
Expand Down Expand Up @@ -109,7 +109,7 @@

context 'when kernel_arguments does not contain crashkernel' do
let(:facts) do
facts.merge(kernel_arguments: my_fixture_read('kernelargs-without-crash.txt'))
facts.merge(crashkernel: false)
end

it do
Expand Down
@@ -0,0 +1 @@
ro root=/dev/mapper/vg_sys-lv_root rd_LVM_LV=vg_sys/lv_swap rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD KEYTABLE=us nofb quiet splash=quiet SYSFONT=latarcyrheb-sun16 rd_LVM_LV=vg_sys/lv_root rd_NO_DM rhgb quiet
1 change: 1 addition & 0 deletions spec/fixtures/unit/facter/kdump_kernel_arguments/cmdline1
@@ -0,0 +1 @@
ro root=/dev/mapper/vg_sys-lv_root rd_LVM_LV=vg_sys/lv_swap rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD KEYTABLE=us nofb quiet splash=quiet SYSFONT=latarcyrheb-sun16 rd_LVM_LV=vg_sys/lv_root rd_NO_DM rhgb quiet crashkernel=131M@0M
20 changes: 20 additions & 0 deletions spec/unit/facter/crashkernel_spec.rb
@@ -0,0 +1,20 @@
require 'spec_helper'

describe 'crashkernel fact' do
before :each do
Facter.clear
end

context 'kernel is linux' do
it 'returns crashkernel' do
allow(Facter::Core::Execution).to receive(:exec).with('uname -s').and_return('Linux')
allow(Facter).to receive(:value).with(:kdump_kernel_arguments).and_return(my_fixture_read('kernelargs-with-crash.txt'))
expect(Facter.fact(:crashkernel).value).to eq('131M@0M')
end
it 'returns false' do
allow(Facter::Core::Execution).to receive(:exec).with('uname -s').and_return('Linux')
allow(Facter).to receive(:value).with(:kdump_kernel_arguments).and_return(my_fixture_read('kernelargs-without-crash.txt'))
expect(Facter.fact(:crashkernel).value).to eq(false)
end
end
end
@@ -1,6 +1,6 @@
require 'spec_helper'

describe 'kernel_arguments fact' do
describe 'kdump_kernel_arguments fact' do
before :each do
Facter.clear
end
Expand All @@ -10,7 +10,7 @@
expected_value = my_fixture_read('cmdline1')
allow(Facter::Core::Execution).to receive(:exec).with('uname -s').and_return('Linux')
allow(Facter::Core::Execution).to receive(:execute).with('cat /proc/cmdline 2>/dev/null').and_return(expected_value)
expect(Facter.fact(:kernel_arguments).value).to eq(expected_value)
expect(Facter.fact(:kdump_kernel_arguments).value).to eq(expected_value)
end
end
end

0 comments on commit 6f4781d

Please sign in to comment.