From 4cf1cd276ad29ae0a3a418d6834250b119649382 Mon Sep 17 00:00:00 2001 From: Philipp Hossner Date: Fri, 2 Aug 2024 11:04:16 +0000 Subject: [PATCH] Add validate_cmd for keepalived.conf Fixes #345 --- REFERENCE.md | 10 ++++++++++ manifests/config.pp | 7 ++++--- manifests/init.pp | 10 +++++++--- spec/acceptance/keepalived_spec.rb | 15 +++++++++++++++ spec/classes/keepalived_spec.rb | 7 ++++--- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index 5432f3359..a1cfa026c 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -54,6 +54,7 @@ The following parameters are available in the `keepalived` class: * [`config_dir`](#-keepalived--config_dir) * [`config_dir_mode`](#-keepalived--config_dir_mode) * [`config_file_mode`](#-keepalived--config_file_mode) +* [`config_validate_cmd`](#-keepalived--config_validate_cmd) * [`config_group`](#-keepalived--config_group) * [`config_owner`](#-keepalived--config_owner) * [`daemon_group`](#-keepalived--daemon_group) @@ -113,6 +114,15 @@ Data type: `Stdlib::Filemode` Default value: `'0644'` +##### `config_validate_cmd` + +Data type: `Optional[String]` + +Input for the `validate_cmd` param of the keepalived.conf concat fragment. +Default is `/usr/sbin/keepalived -l -t -f %`. + +Default value: `'/usr/sbin/keepalived -l -t -f %'` + ##### `config_group` Data type: `String[1]` diff --git a/manifests/config.pp b/manifests/config.pp index 71abb4ee5..f323af194 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -31,9 +31,10 @@ } concat { "${keepalived::config_dir}/keepalived.conf": - owner => $keepalived::config_owner, - group => $keepalived::config_group, - mode => $keepalived::config_file_mode, + owner => $keepalived::config_owner, + group => $keepalived::config_group, + mode => $keepalived::config_file_mode, + validate_cmd => $keepalived::config_validate_cmd, } concat::fragment { 'keepalived.conf_header': diff --git a/manifests/init.pp b/manifests/init.pp index 9e2dd236c..343269d47 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -11,6 +11,9 @@ # # @param config_file_mode # +# @param config_validate_cmd Input for the `validate_cmd` param of the keepalived.conf concat fragment. +# Default is `/usr/sbin/keepalived -l -t -f %`. +# # @param config_group # # @param config_owner @@ -61,9 +64,10 @@ Optional[Boolean] $service_hasrestart = undef, Optional[Boolean] $service_hasstatus = undef, - Stdlib::Absolutepath $config_dir = '/etc/keepalived', - Stdlib::Filemode $config_dir_mode = '0755', - Stdlib::Filemode $config_file_mode = '0644', + Stdlib::Absolutepath $config_dir = '/etc/keepalived', + Stdlib::Filemode $config_dir_mode = '0755', + Stdlib::Filemode $config_file_mode = '0644', + Optional[String] $config_validate_cmd = '/usr/sbin/keepalived -l -t -f %', Array[Stdlib::Absolutepath] $include_external_conf_files = [], diff --git a/spec/acceptance/keepalived_spec.rb b/spec/acceptance/keepalived_spec.rb index f00c39e62..4ee35adc5 100644 --- a/spec/acceptance/keepalived_spec.rb +++ b/spec/acceptance/keepalived_spec.rb @@ -130,4 +130,19 @@ class { 'keepalived': expect(service_fact.output).to match %r{.*Keepalived version was: (\d.\d.\d).*} end end + + context 'with broken config' do + pp = <<-EOS + class { 'keepalived': + } + + class { 'keepalived::global_defs': + notification_email_from => '', # this will generate an invalid config + } + EOS + it 'fails validate command' do + apply_result = apply_manifest(pp, catch_failures: true) + expect(apply_result.output).to match %r{.*emailfrom missing.*} + end + end end diff --git a/spec/classes/keepalived_spec.rb b/spec/classes/keepalived_spec.rb index 4860a6859..eb6a3b0b1 100644 --- a/spec/classes/keepalived_spec.rb +++ b/spec/classes/keepalived_spec.rb @@ -23,9 +23,10 @@ it { is_expected.to contain_concat('/etc/keepalived/keepalived.conf').with( - 'group' => 'root', - 'mode' => '0644', - 'owner' => 'root' + 'group' => 'root', + 'mode' => '0644', + 'owner' => 'root', + 'validate_cmd' => '/usr/sbin/keepalived -l -t -f %', ) }