From d14acb4c335098f52ba229f4c863f076e9fdd7b5 Mon Sep 17 00:00:00 2001 From: Yury Bushmelev Date: Sun, 8 Dec 2024 22:20:31 +0800 Subject: [PATCH] Improve yum::copr idempotency Fixes #340 --- manifests/copr.pp | 7 ++-- spec/acceptance/copr_spec.rb | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 spec/acceptance/copr_spec.rb diff --git a/manifests/copr.pp b/manifests/copr.pp index 4db33af..ce05dc9 100644 --- a/manifests/copr.pp +++ b/manifests/copr.pp @@ -27,25 +27,26 @@ } if $facts['package_provider'] == 'dnf' { + $copr_name = regsubst($copr_repo, '@', 'group_') case $ensure { 'enabled': { exec { "dnf -y copr enable ${copr_repo}": path => '/bin:/usr/bin:/sbin/:/usr/sbin', - unless => "dnf copr list | egrep -q '${copr_repo}\$'", + unless => "dnf copr list | egrep -q '${copr_name}\$'", require => Package[$prereq_plugin], } } 'disabled': { exec { "dnf -y copr disable ${copr_repo}": path => '/bin:/usr/bin:/sbin/:/usr/sbin', - unless => "dnf copr list | egrep -q '${copr_repo} (disabled)\$'", + unless => "dnf copr list | egrep -q '${copr_name} \\(disabled\\)\$'", require => Package[$prereq_plugin], } } 'removed': { exec { "dnf -y copr remove ${copr_repo}": path => '/bin:/usr/bin:/sbin/:/usr/sbin', - onlyif => "dnf copr list | egrep -q '${copr_repo}'", + onlyif => "dnf copr list | egrep -q '${copr_name}'", require => Package[$prereq_plugin], } } diff --git a/spec/acceptance/copr_spec.rb b/spec/acceptance/copr_spec.rb new file mode 100644 index 0000000..fd7512c --- /dev/null +++ b/spec/acceptance/copr_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'spec_helper_acceptance' + +describe 'yum::copr' do + context 'when @caddy/caddy and copart/restic are enabled' do + # Using puppet_apply as a helper + it 'must work idempotently with no errors' do + pp = <<-PUPPET + yum::copr { ['@caddy/caddy', 'copart/restic']: } + PUPPET + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe command('dnf copr list') do + its(:stdout) { is_expected.to match(%r{^copr.fedorainfracloud.org/copart/restic$}) } + its(:stdout) { is_expected.to match(%r{^copr.fedorainfracloud.org/group_caddy/caddy$}) } + end + end + + context 'when copart/restic is disabled' do + # Using puppet_apply as a helper + it 'must work idempotently with no errors' do + pp = <<-PUPPET + yum::copr { ['@caddy/caddy', 'copart/restic']: + ensure => 'disabled', + } + PUPPET + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe command('dnf copr list') do + its(:stdout) { is_expected.to match(%r{^copr.fedorainfracloud.org/copart/restic \(disabled\)$}) } + its(:stdout) { is_expected.to match(%r{^copr.fedorainfracloud.org/group_caddy/caddy \(disabled\)$}) } + end + end + + context 'when copart/restic is removed' do + # Using puppet_apply as a helper + it 'must work idempotently with no errors' do + pp = <<-PUPPET + yum::copr { ['@caddy/caddy', 'copart/restic']: + ensure => 'removed', + } + PUPPET + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe command('dnf copr list') do + its(:stdout) { is_expected.not_to match(%r{^copr.fedorainfracloud.org/copart/restic$}) } + its(:stdout) { is_expected.not_to match(%r{^copr.fedorainfracloud.org/group_caddy/caddy$}) } + end + end +end