Skip to content

Commit

Permalink
Use new data type for cron periodics
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Friderici committed Jan 29, 2024
1 parent 8e1a73f commit 604bc95
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 11 deletions.
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
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
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 604bc95

Please sign in to comment.