Skip to content

Commit

Permalink
Merge pull request #110 from mizzy/explicit-cron-subject-type
Browse files Browse the repository at this point in the history
Explicit cron subject type
  • Loading branch information
mizzy committed May 19, 2013
2 parents 658ce8b + 0714b63 commit 5208186
Show file tree
Hide file tree
Showing 27 changed files with 155 additions and 27 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
8 changes: 8 additions & 0 deletions spec/darwin/cron_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

include Serverspec::Helper::Darwin

describe 'Serverspec cron matchers of Darwin family' do
it_behaves_like 'support cron have_entry matcher', '* * * * * /usr/local/bin/batch.sh'
it_behaves_like 'support cron have_entry with user matcher', '* * * * * /usr/local/bin/batch.sh', 'root'
end
2 changes: 1 addition & 1 deletion spec/darwin/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

include Serverspec::Helper::Darwin

describe 'Serverspec service matchers of Darwin family' do
describe 'Serverspec file matchers of Darwin family' do
it_behaves_like 'support file be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support file be_directory matcher', '/etc/ssh'
it_behaves_like 'support file contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
Expand Down
2 changes: 1 addition & 1 deletion spec/darwin/port_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

include Serverspec::Helper::Darwin

describe 'Serverspec package matchers of Darwin family' do
describe 'Serverspec port matchers of Darwin family' do
it_behaves_like 'support port listening matcher', 80
end
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
8 changes: 8 additions & 0 deletions spec/debian/cron_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

include Serverspec::Helper::Debian

describe 'Serverspec cron matchers of Debian family' do
it_behaves_like 'support cron have_entry matcher', '* * * * * /usr/local/bin/batch.sh'
it_behaves_like 'support cron have_entry with user matcher', '* * * * * /usr/local/bin/batch.sh', 'root'
end
2 changes: 1 addition & 1 deletion spec/debian/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

include Serverspec::Helper::Debian

describe 'Serverspec service matchers of Debian family' do
describe 'Serverspec file matchers of Debian family' do
it_behaves_like 'support file be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support file be_directory matcher', '/etc/ssh'
it_behaves_like 'support file contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
Expand Down
2 changes: 1 addition & 1 deletion spec/debian/port_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

include Serverspec::Helper::Debian

describe 'Serverspec package matchers of Debian family' do
describe 'Serverspec port matchers of Debian family' do
it_behaves_like 'support port listening matcher', 80
end
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
8 changes: 8 additions & 0 deletions spec/gentoo/cron_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

include Serverspec::Helper::Gentoo

describe 'Serverspec cron matchers of Gentoo family' do
it_behaves_like 'support cron have_entry matcher', '* * * * * /usr/local/bin/batch.sh'
it_behaves_like 'support cron have_entry with user matcher', '* * * * * /usr/local/bin/batch.sh', 'root'
end
2 changes: 1 addition & 1 deletion spec/gentoo/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

include Serverspec::Helper::Gentoo

describe 'Serverspec service matchers of Gentoo family' do
describe 'Serverspec file matchers of Gentoo family' do
it_behaves_like 'support file be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support file be_directory matcher', '/etc/ssh'
it_behaves_like 'support file contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
Expand Down
2 changes: 1 addition & 1 deletion spec/gentoo/port_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

include Serverspec::Helper::Gentoo

describe 'Serverspec package matchers of Gentoo family' do
describe 'Serverspec port matchers of Gentoo family' do
it_behaves_like 'support port listening matcher', 80
end
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
8 changes: 8 additions & 0 deletions spec/redhat/cron_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

include Serverspec::Helper::RedHat

describe 'Serverspec cron matchers of Red Hat family' do
it_behaves_like 'support cron have_entry matcher', '* * * * * /usr/local/bin/batch.sh'
it_behaves_like 'support cron have_entry with user matcher', '* * * * * /usr/local/bin/batch.sh', 'root'
end
2 changes: 1 addition & 1 deletion spec/redhat/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

include Serverspec::Helper::RedHat

describe 'Serverspec service matchers of Red Hat family' do
describe 'Serverspec file matchers of Red Hat family' do
it_behaves_like 'support file be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support file be_directory matcher', '/etc/ssh'
it_behaves_like 'support file contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
Expand Down
2 changes: 1 addition & 1 deletion spec/redhat/port_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

include Serverspec::Helper::RedHat

describe 'Serverspec package matchers of Red Hat family' do
describe 'Serverspec port matchers of Red Hat family' do
it_behaves_like 'support port listening matcher', 80
end
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
8 changes: 8 additions & 0 deletions spec/solaris/cron_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

include Serverspec::Helper::Solaris

describe 'Serverspec cron matchers of Solaris family' do
it_behaves_like 'support cron have_entry matcher', '* * * * * /usr/local/bin/batch.sh'
it_behaves_like 'support cron have_entry with user matcher', '* * * * * /usr/local/bin/batch.sh', 'root'
end
2 changes: 1 addition & 1 deletion spec/solaris/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

include Serverspec::Helper::Solaris

describe 'Serverspec service matchers of Solaris family' do
describe 'Serverspec file matchers of Solaris family' do
it_behaves_like 'support file be_file matcher', '/etc/ssh/sshd_config'
it_behaves_like 'support file be_directory matcher', '/etc/ssh'
it_behaves_like 'support file contain matcher', '/etc/ssh/sshd_config', 'This is the sshd server system-wide configuration file'
Expand Down
2 changes: 1 addition & 1 deletion spec/solaris/port_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

include Serverspec::Helper::Solaris

describe 'Serverspec package matchers of Solaris family' do
describe 'Serverspec port matchers of Solaris family' do
it_behaves_like 'support port listening matcher', 80
end
23 changes: 23 additions & 0 deletions spec/support/shared_cron_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
shared_examples_for 'support cron have_entry matcher' do |entry|
describe 'have_cron_entry' do
describe cron do
it { should have_cron_entry entry }
end

describe cron do
it { should_not have_cron_entry 'invalid entry' }
end
end
end

shared_examples_for 'support cron have_entry with user matcher' do |entry, user|
describe 'have_cron_entry' do
describe cron do
it { should have_cron_entry(entry).with_user(user) }
end

describe cron do
it { should_not have_cron_entry('invalid entry').with_user(user) }
end
end
end

0 comments on commit 5208186

Please sign in to comment.