diff --git a/manifests/init.pp b/manifests/init.pp index 60b7c21..1c51723 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -205,19 +205,22 @@ default => '3', } - $_pc_cmd = [ - '/usr/bin/package-cleanup', - '--oldkernels', - "--count=${_real_installonly_limit}", - '-y', - $keep_kernel_devel ? { - true => '--keepdevel', - default => undef, + $_keep_kernel_devel = $keep_kernel_devel ? { + true => $facts['package_provider'] ? { + 'yum' => '--keepdevel ', + 'dnf' => '--exclude kernel-release', + default => fail("Fact package_provider is not set to \'yum\' or \'dnf\' - giving up"), }, - ].filter |$val| { $val =~ NotUndef } + default => '', + } + + $_pc_cmd = $facts['package_provider'] ? { + 'yum' => "/usr/bin/package-cleanup --oldkernels --count=${_real_installonly_limit} -y${$_keep_kernel_devel}", + default => "/usr/bin/dnf -y remove $(/usr/bin/dnf repoquery --installonly --latest-limit=-${_real_installonly_limit}${_keep_kernel_devel} | /usr/bin/grep -v ${facts['kernelrelease']})" + } exec { 'package-cleanup_oldkernels': - command => shellquote($_pc_cmd), + command => $_pc_cmd, refreshonly => true, require => Package[$utils_package_name], subscribe => $_clean_old_kernels_subscribe, diff --git a/spec/acceptance/yum_config_install_limit_2_spec.rb b/spec/acceptance/yum_config_install_limit_2_spec.rb new file mode 100644 index 0000000..b9ff051 --- /dev/null +++ b/spec/acceptance/yum_config_install_limit_2_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'spec_helper_acceptance' + +describe 'yum::config installonly_limit 1' do + context 'simple parameters' do + # Using puppet_apply as a helper + it 'must work idempotently with no errors' do + pp = <<-EOS + yum::config{'installonly_limit': + ensure => 2, # yum (and not dnf) does not like the value 1 + } + include yum + EOS + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + end +end diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index a78e783..69cafda 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -10,7 +10,7 @@ it 'contains Exec[package-cleanup_oldkernels' do is_expected.to contain_exec('package-cleanup_oldkernels').with( - command: "/usr/bin/package-cleanup --oldkernels --count=#{value} -y", + command: %r{/usr/bin/dnf -y remove \$\(/usr/bin/dnf repoquery --installonly --latest-limit=-\$\{value\} | /usr/bin/grep -v \S+\)}, refreshonly: true ).that_subscribes_to('Yum::Config[installonly_limit]') end @@ -45,6 +45,20 @@ it { is_expected.to have_yumrepo_resource_count(0) } end + context 'with package_provider yum' do + let(:facts) do + facts.merge({ package_provider: 'yum' }) + end + + it { is_expected.to compile.with_all_deps } + + it { + is_expected.to contain_exec('package-cleanup_oldkernels').with( + command: '/usr/bin/package-cleanup --oldkernels --count=3 -y' + ) + } + end + context 'when `manage_os_default_repos` is enabled' do let(:params) { { 'manage_os_default_repos' => true } }