Skip to content

Commit

Permalink
Merge pull request #5 from Phil-Friderici/cron-arrays
Browse files Browse the repository at this point in the history
Use new data type for cron periodic values
  • Loading branch information
anders-larsson authored Feb 12, 2024
2 parents 8e1a73f + e5f6db9 commit f617912
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 17 deletions.
1 change: 1 addition & 0 deletions .pdkignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/.fixtures.yml
/Gemfile
/.gitattributes
/.github/
/.gitignore
/.pdkignore
/.puppet-lint.rc
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ group :development do
gem "rubocop", '= 1.48.1', require: false
gem "rubocop-performance", '= 1.16.0', require: false
gem "rubocop-rspec", '= 2.19.0', require: false
gem "puppet-strings", '~> 4.0', require: false
gem "rb-readline", '= 0.5.5', require: false, platforms: [:mswin, :mingw, :x64_mingw]
end
group :system_tests do
gem "puppet_litmus", '~> 1.0', require: false, platforms: [:ruby, :x64_mingw]
gem "serverspec", '~> 2.41', require: false
end
group :release_prep do
gem "puppet-strings", '~> 4.0', require: false
gem "puppetlabs_spec_helper", '~> 6.0', require: false
end

puppet_version = ENV['PUPPET_GEM_VERSION']
facter_version = ENV['FACTER_GEM_VERSION']
Expand Down
22 changes: 11 additions & 11 deletions manifests/cron.pp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@
#
define types::cron (
String[1] $command,
Enum['present', 'absent'] $ensure = 'present',
Optional[String[1]] $environment = undef,
Optional[String[1]] $hour = undef,
Optional[String[1]] $minute = undef,
Optional[String[1]] $month = undef,
Optional[String[1]] $monthday = undef,
Optional[String[1]] $provider = undef,
Optional[String[1]] $special = undef,
Optional[String[1]] $target = undef,
Optional[String[1]] $user = undef,
Optional[String[1]] $weekday = undef,
Enum['present', 'absent'] $ensure = 'present',
Optional[String[1]] $environment = undef,
Optional[Types::Cron::Periodic] $hour = undef,
Optional[Types::Cron::Periodic] $minute = undef,
Optional[Types::Cron::Periodic] $month = undef,
Optional[Types::Cron::Periodic] $monthday = undef,
Optional[Types::Cron::Periodic] $weekday = undef,
Optional[Types::Cron::Periodic] $special = undef,
Optional[String[1]] $provider = undef,
Optional[String[1]] $target = undef,
Optional[String[1]] $user = undef,
) {
cron { $name:
ensure => $ensure,
Expand Down
6 changes: 3 additions & 3 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
}
],
"description": "Puppet module to manage default types through hashes in Hiera with the\ncreate_resources() function. This module adds validation and helper functions,\nsuch as ensuring directories. Without specifying any hashes, this module will take no action.",
"pdk-version": "3.0.0",
"template-url": "pdk-default#3.0.0",
"template-ref": "tags/3.0.0-0-g056e50d"
"pdk-version": "3.0.1",
"template-url": "pdk-default#3.0.1",
"template-ref": "tags/3.0.1-0-gd13288a"
}
110 changes: 110 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,116 @@
it { is_expected.to contain_types__cron('cronjob-2') } # only needed for 100% resource coverage
end

context 'with cron periodic attributs specified as valid types' do
let(:params) do
{
crons: {
'array_with_integers' => {
'command' => '/usr/local/bin/array_with_integers.sh',
'hour' => [6, 9],
'minute' => [2, 42],
'month' => [2, 3],
'monthday' => [3, 6],
'weekday' => [0, 3],
'special' => [1, 42],
},
'array_with_strings' => {
'command' => '/usr/local/bin/array_with_strings.sh',
'hour' => ['6, 9'],
'minute' => ['*/2', '*/42'],
'month' => ['*/2'],
'monthday' => ['3', '6'],
'weekday' => ['0'],
'special' => ['1', '42'],
},
'strings' => {
'command' => '/usr/local/bin/strings.sh',
'hour' => '3',
'minute' => '*/42',
'month' => '*/2',
'monthday' => '3',
'weekday' => '0',
'special' => '1',
},
'integers' => {
'command' => '/usr/local/bin/integers.sh',
'hour' => 3,
'minute' => 42,
'month' => 2,
'monthday' => 3,
'weekday' => 0,
'special' => 1,
},
}
}
end

it do
is_expected.to contain_cron('array_with_integers').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/array_with_integers.sh',
'hour' => [6, 9],
'minute' => [2, 42],
'month' => [2, 3],
'monthday' => [3, 6],
'weekday' => [0, 3],
'special' => [1, 42],
},
)
end

it do
is_expected.to contain_cron('array_with_strings').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/array_with_strings.sh',
'hour' => ['6, 9'],
'minute' => ['*/2', '*/42'],
'month' => ['*/2'],
'monthday' => ['3', '6'],
'weekday' => ['0'],
'special' => ['1', '42'],
},
)
end

it do
is_expected.to contain_cron('strings').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/strings.sh',
'hour' => '3',
'minute' => '*/42',
'month' => '*/2',
'monthday' => '3',
'weekday' => '0',
'special' => '1',
},
)
end

it do
is_expected.to contain_cron('integers').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/integers.sh',
'hour' => 3,
'minute' => 42,
'month' => 2,
'monthday' => 3,
'weekday' => 0,
'special' => 1,
},
)
end

it { is_expected.to contain_types__cron('array_with_integers') } # only needed for 100% resource coverage
it { is_expected.to contain_types__cron('array_with_strings') } # only needed for 100% resource coverage
it { is_expected.to contain_types__cron('strings') } # only needed for 100% resource coverage
it { is_expected.to contain_types__cron('integers') } # only needed for 100% resource coverage
end

context 'with cron specified as an invalid type' do
let(:params) { { crons: ['not', 'a', 'hash'] } }

Expand Down
7 changes: 4 additions & 3 deletions spec/default_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#
# Facts specified here will override the values provided by rspec-puppet-facts.
---
ipaddress: "172.16.254.254"
ipaddress6: "FE80:0000:0000:0000:AAAA:AAAA:AAAA"
networking:
ip: "172.16.254.254"
ip6: "FE80:0000:0000:0000:AAAA:AAAA:AAAA"
mac: "AA:AA:AA:AA:AA:AA"
is_pe: false
macaddress: "AA:AA:AA:AA:AA:AA"
120 changes: 120 additions & 0 deletions spec/defines/cron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,126 @@
end
end

context 'with periodic attributs specified as valid array containing integers' do
let(:title) { 'array_with_integers' }
let(:params) do
{
command: '/usr/local/bin/array_with_integers.sh',
hour: [6, 9],
minute: [2, 42],
month: [2, 3],
monthday: [3, 6],
weekday: [0, 3],
special: [1, 42],
}
end

it do
is_expected.to contain_cron('array_with_integers').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/array_with_integers.sh',
'hour' => [6, 9],
'minute' => [2, 42],
'month' => [2, 3],
'monthday' => [3, 6],
'weekday' => [0, 3],
'special' => [1, 42],
},
)
end
end

context 'with periodic attributs specified as valid array containing strings' do
let(:title) { 'array_with_integers' }
let(:params) do
{
command: '/usr/local/bin/array_with_strings.sh',
hour: ['6, 9'],
minute: ['*/2', '*/42'],
month: ['*/2'],
monthday: ['3', '6'],
weekday: ['0'],
special: ['1', '42'],
}
end

it do
is_expected.to contain_cron('array_with_integers').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/array_with_strings.sh',
'hour' => ['6, 9'],
'minute' => ['*/2', '*/42'],
'month' => ['*/2'],
'monthday' => ['3', '6'],
'weekday' => ['0'],
'special' => ['1', '42'],
},
)
end
end

context 'with periodic attributs specified as valid strings' do
let(:title) { 'strings' }
let(:params) do
{
command: '/usr/local/bin/strings.sh',
hour: '3',
minute: '*/42',
month: '*/2',
monthday: '3',
weekday: '0',
special: '1',
}
end

it do
is_expected.to contain_cron('strings').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/strings.sh',
'hour' => '3',
'minute' => '*/42',
'month' => '*/2',
'monthday' => '3',
'weekday' => '0',
'special' => '1',
},
)
end
end

context 'with periodic attributs specified as valid integers' do
let(:title) { 'integers' }
let(:params) do
{
command: '/usr/local/bin/integers.sh',
hour: 3,
minute: 42,
month: 2,
monthday: 3,
weekday: 0,
special: 1,
}
end

it do
is_expected.to contain_cron('integers').with(
{
'ensure' => 'present',
'command' => '/usr/local/bin/integers.sh',
'hour' => 3,
'minute' => 42,
'month' => 2,
'monthday' => 3,
'weekday' => 0,
'special' => 1,
},
)
end
end

context 'cron with invalid ensure' do
let(:title) { 'invalid' }
let(:params) do
Expand Down
6 changes: 6 additions & 0 deletions types/cron/periodic.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Data type for cron periodic values (hour, minute, month, monthday, weekday, or special)
type Types::Cron::Periodic = Variant[
Array,
Integer,
String[1],
]

0 comments on commit f617912

Please sign in to comment.