Skip to content

Commit

Permalink
Improve yum::copr idempotency
Browse files Browse the repository at this point in the history
Fixes #340
  • Loading branch information
jay7x committed Dec 8, 2024
1 parent 75df7c0 commit d14acb4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
7 changes: 4 additions & 3 deletions manifests/copr.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}
}
Expand Down
63 changes: 63 additions & 0 deletions spec/acceptance/copr_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d14acb4

Please sign in to comment.