Skip to content

Commit

Permalink
Add cron type
Browse files Browse the repository at this point in the history
  • Loading branch information
mizzy committed May 18, 2013
1 parent 658ce8b commit 46c4897
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 17 deletions.
6 changes: 5 additions & 1 deletion lib/serverspec/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ def check_grouped file, group

def check_cron_entry user, entry
entry_escaped = entry.gsub(/\*/, '\\*')
"crontab -u #{escape(user)} -l | grep -- #{escape(entry_escaped)}"
if user.nil?
"crontab -l | grep -- #{escape(entry_escaped)}"
else
"crontab -u #{escape(user)} -l | grep -- #{escape(entry_escaped)}"
end
end

def check_link link, target
Expand Down
6 changes: 5 additions & 1 deletion lib/serverspec/commands/solaris.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ def check_running service

def check_cron_entry user, entry
entry_escaped = entry.gsub(/\*/, '\\*')
"crontab -l #{escape(user)} | grep -- #{escape(entry_escaped)}"
if user.nil?
"crontab -l | grep -- #{escape(entry_escaped)}"
else
"crontab -l #{escape(user)} | grep -- #{escape(entry_escaped)}"
end
end

def check_zfs zfs, property=nil
Expand Down
6 changes: 4 additions & 2 deletions lib/serverspec/helper/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
require 'serverspec/type/package'
require 'serverspec/type/port'
require 'serverspec/type/file'
require 'serverspec/type/cron'

module Serverspec
module Helper
module Type
%w( service package port file ).each do |type|
define_method type do |name|
%w( service package port file cron ).each do |type|
define_method type do |*args|
name = args.first
self.class.const_get('Serverspec').const_get('Type').const_get(type.capitalize).new(name)
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/serverspec/matchers/have_entry.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
RSpec::Matchers.define :have_entry do |attr|
RSpec::Matchers.define :have_entry do |entry|
match do |subject|
backend.check_routing_table(example, attr)
if subject.class.name == 'Serverspec::Type::Cron'
subject.has_entry?(@user, entry)
else
backend.check_routing_table(example, entry)
end
end
# For cron type
chain :with_user do |user|
@user = user
end
end
2 changes: 1 addition & 1 deletion lib/serverspec/type/base.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Serverspec
module Type
class Base
def initialize name
def initialize(name=nil)
@name = name
end

Expand Down
12 changes: 12 additions & 0 deletions lib/serverspec/type/cron.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Serverspec
module Type
class Cron < Base
def has_entry?(user, entry)
backend.check_cron_entry(nil, user, entry)
end
def to_s
'Cron'
end
end
end
end
11 changes: 9 additions & 2 deletions spec/darwin/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,15 @@
end

describe 'check_cron_entry' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
context 'specify root user' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end

context 'no specified user' do
subject { commands.check_cron_entry(nil, '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end
end

describe 'check_link' do
Expand Down
11 changes: 9 additions & 2 deletions spec/debian/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,15 @@
end

describe 'check_cron_entry' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
context 'specify root user' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end

context 'no specified user' do
subject { commands.check_cron_entry(nil, '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end
end

describe 'check_link' do
Expand Down
11 changes: 9 additions & 2 deletions spec/gentoo/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,15 @@
end

describe 'check_cron_entry' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
context 'specify root user' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end

context 'no specified user' do
subject { commands.check_cron_entry(nil, '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end
end

describe 'check_link' do
Expand Down
11 changes: 9 additions & 2 deletions spec/redhat/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@
end

describe 'check_cron_entry' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
context 'specify root user' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -u root -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end

context 'no specified user' do
subject { commands.check_cron_entry(nil, '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end
end

describe 'check_link' do
Expand Down
11 changes: 9 additions & 2 deletions spec/solaris/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@
end

describe 'check_cron_entry' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -l root | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
context 'specify root user' do
subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -l root | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end

context 'no specified user' do
subject { commands.check_cron_entry(nil, '* * * * * /usr/local/bin/batch.sh') }
it { should eq 'crontab -l | grep -- \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ \\\\\\*\\ /usr/local/bin/batch.sh' }
end
end

describe 'check_link' do
Expand Down

0 comments on commit 46c4897

Please sign in to comment.