From e05a8cfe3d778e639acd6280f793927af977d134 Mon Sep 17 00:00:00 2001 From: Steve Traylen Date: Sat, 3 Jun 2023 22:03:40 +0200 Subject: [PATCH] New yum::groups parameter to manage groups A hash of Yum groups can now be managed easily from hiera: ```yaml --- yum::groups: 'Puppet Dev': ensure: installed 'Rpm Dev': ensure: absent ``` the existing `yum::group` defined type is used. --- REFERENCE.md | 20 ++++++++++ manifests/init.pp | 18 +++++++++ spec/acceptance/define_group_spec.rb | 60 ++++++++++++++++++++++++++++ spec/classes/init_spec.rb | 19 +++++++++ 4 files changed, 117 insertions(+) create mode 100644 spec/acceptance/define_group_spec.rb diff --git a/REFERENCE.md b/REFERENCE.md index bfbefeb..8cd25c9 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -101,6 +101,17 @@ yum::repos: mirrorlist: '--' ``` +##### Install a couple of `yum::group`s. + +```puppet +--- +yum::groups: + 'Development Tools': + ensure: present + 'System Tools': + ensure: present +``` + #### Parameters The following parameters are available in the `yum` class: @@ -115,6 +126,7 @@ The following parameters are available in the `yum` class: * [`repo_exclusions`](#-yum--repo_exclusions) * [`gpgkeys`](#-yum--gpgkeys) * [`utils_package_name`](#-yum--utils_package_name) +* [`groups`](#-yum--groups) ##### `clean_old_kernels` @@ -220,6 +232,14 @@ Name of the utils package, e.g. 'yum-utils', or 'dnf-utils'. Default value: `'yum-utils'` +##### `groups` + +Data type: `Stdlib::CreateResources` + +A hash of yum::group instances to manage. + +Default value: `{}` + ### `yum::clean` A $(yum clean all) Exec to be notified if desired. diff --git a/manifests/init.pp b/manifests/init.pp index 60b7c21..625450a 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -55,6 +55,9 @@ # @param utils_package_name # Name of the utils package, e.g. 'yum-utils', or 'dnf-utils'. # +# @param groups +# A hash of yum::group instances to manage. +# # @example Enable management of the default repos for a supported OS: # --- # yum::manage_os_default_repos: true @@ -94,6 +97,14 @@ # baseurl: 'https://repos.example.com/CentOS/base/' # mirrorlist: '--' # +# @example Install a couple of `yum::group`s. +# --- +# yum::groups: +# 'Development Tools': +# ensure: present +# 'System Tools': +# ensure: present +# class yum ( Boolean $clean_old_kernels = true, Boolean $keep_kernel_devel = false, @@ -105,6 +116,7 @@ Array[String] $repo_exclusions = [], Hash[String, Hash[String, String]] $gpgkeys = {}, String $utils_package_name = 'yum-utils', + Stdlib::CreateResources $groups = {} ) { $module_metadata = load_module_metadata($module_name) $supported_operatingsystems = $module_metadata['operatingsystem_support'] @@ -222,4 +234,10 @@ require => Package[$utils_package_name], subscribe => $_clean_old_kernels_subscribe, } + + $groups.each |$_group, $_group_attrs| { + yum::group { $_group: + * => $_group_attrs, + } + } } diff --git a/spec/acceptance/define_group_spec.rb b/spec/acceptance/define_group_spec.rb new file mode 100644 index 0000000..e22996d --- /dev/null +++ b/spec/acceptance/define_group_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'spec_helper_acceptance' + +describe 'managing a yum::group' do + context 'installing group RPM dev tools' do + # Using puppet_apply as a helper + it 'must work idempotently with no errors' do + pp = <<-EOS + yum::group { 'Development Tools': + ensure => 'installed', + } + EOS + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + # On stupid 7 test package has to be leaf package + # to be removed with a "groupremove". Can't find + # a common package that works. + case fact('os.release.major') + when '7' + describe package('libtool') do + it { is_expected.to be_installed } + end + else + describe package('make') do + it { is_expected.to be_installed } + end + end + end + + context 'removing group RPM dev tools' do + # Using puppet_apply as a helper + it 'must work idempotently with no errors' do + pp = <<-EOS + yum::group { 'Development Tools': + ensure => 'absent', + } + EOS + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + case fact('os.release.major') + when '7' + describe package('libtool') do + it { is_expected.not_to be_installed } + end + else + describe package('make') do + it { is_expected.not_to be_installed } + end + end + end +end diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index a78e783..23199b2 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -43,6 +43,7 @@ it_behaves_like 'a Yum class' it { is_expected.to have_yumrepo_resource_count(0) } + it { is_expected.to have_yum__group_resource_count(0) } end context 'when `manage_os_default_repos` is enabled' do @@ -797,6 +798,24 @@ it { is_expected.to contain_yum__gpgkey('/etc/pki/rpm-gpg/RPM-GPG-KEY-example') } it { is_expected.to contain_yum__gpgkey('/etc/pki/rpm-gpg/RPM-GPG-KEY-example2') } end + + context 'when groups parameter is set' do + let(:params) do + { + groups: { + 'Dev Tools': { + ensure: 'installed', + }, + 'Puppet Tools': { + ensure: 'absent', + }, + }, + } + end + + it { is_expected.to contain_yum__group('Puppet Tools').with_ensure('absent') } + it { is_expected.to contain_yum__group('Dev Tools').with_ensure('installed') } + end end end