From 46c489794b3f7087d1144b43c4589eae83236945 Mon Sep 17 00:00:00 2001 From: Gosuke Miyashita Date: Sun, 19 May 2013 01:45:16 +0900 Subject: [PATCH 1/3] Add cron type --- lib/serverspec/commands/base.rb | 6 +++++- lib/serverspec/commands/solaris.rb | 6 +++++- lib/serverspec/helper/type.rb | 6 ++++-- lib/serverspec/matchers/have_entry.rb | 12 ++++++++++-- lib/serverspec/type/base.rb | 2 +- lib/serverspec/type/cron.rb | 12 ++++++++++++ spec/darwin/commands_spec.rb | 11 +++++++++-- spec/debian/commands_spec.rb | 11 +++++++++-- spec/gentoo/commands_spec.rb | 11 +++++++++-- spec/redhat/commands_spec.rb | 11 +++++++++-- spec/solaris/commands_spec.rb | 11 +++++++++-- 11 files changed, 82 insertions(+), 17 deletions(-) create mode 100644 lib/serverspec/type/cron.rb diff --git a/lib/serverspec/commands/base.rb b/lib/serverspec/commands/base.rb index 404eeb29..189b60a1 100644 --- a/lib/serverspec/commands/base.rb +++ b/lib/serverspec/commands/base.rb @@ -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 diff --git a/lib/serverspec/commands/solaris.rb b/lib/serverspec/commands/solaris.rb index 653abb20..dba465e7 100644 --- a/lib/serverspec/commands/solaris.rb +++ b/lib/serverspec/commands/solaris.rb @@ -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 diff --git a/lib/serverspec/helper/type.rb b/lib/serverspec/helper/type.rb index 444ad33f..1b5e8fbd 100644 --- a/lib/serverspec/helper/type.rb +++ b/lib/serverspec/helper/type.rb @@ -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 diff --git a/lib/serverspec/matchers/have_entry.rb b/lib/serverspec/matchers/have_entry.rb index 0dab5080..ab3f1179 100644 --- a/lib/serverspec/matchers/have_entry.rb +++ b/lib/serverspec/matchers/have_entry.rb @@ -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 diff --git a/lib/serverspec/type/base.rb b/lib/serverspec/type/base.rb index 00b09efa..9f6ac6c2 100644 --- a/lib/serverspec/type/base.rb +++ b/lib/serverspec/type/base.rb @@ -1,7 +1,7 @@ module Serverspec module Type class Base - def initialize name + def initialize(name=nil) @name = name end diff --git a/lib/serverspec/type/cron.rb b/lib/serverspec/type/cron.rb new file mode 100644 index 00000000..3f841609 --- /dev/null +++ b/lib/serverspec/type/cron.rb @@ -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 diff --git a/spec/darwin/commands_spec.rb b/spec/darwin/commands_spec.rb index 6d2938a7..4f90eba0 100644 --- a/spec/darwin/commands_spec.rb +++ b/spec/darwin/commands_spec.rb @@ -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 diff --git a/spec/debian/commands_spec.rb b/spec/debian/commands_spec.rb index f2b8bb54..b1ceb675 100644 --- a/spec/debian/commands_spec.rb +++ b/spec/debian/commands_spec.rb @@ -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 diff --git a/spec/gentoo/commands_spec.rb b/spec/gentoo/commands_spec.rb index 62273903..afb52221 100644 --- a/spec/gentoo/commands_spec.rb +++ b/spec/gentoo/commands_spec.rb @@ -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 diff --git a/spec/redhat/commands_spec.rb b/spec/redhat/commands_spec.rb index c20a5d84..e967121d 100644 --- a/spec/redhat/commands_spec.rb +++ b/spec/redhat/commands_spec.rb @@ -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 diff --git a/spec/solaris/commands_spec.rb b/spec/solaris/commands_spec.rb index 5855cb6f..c470104f 100644 --- a/spec/solaris/commands_spec.rb +++ b/spec/solaris/commands_spec.rb @@ -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 From cc73ceec64603d44e68ca198b9cd1186d749a3c8 Mon Sep 17 00:00:00 2001 From: Gosuke Miyashita Date: Sun, 19 May 2013 01:53:02 +0900 Subject: [PATCH 2/3] Add specs for cron type --- spec/darwin/cron_spec.rb | 8 ++++++++ spec/debian/cron_spec.rb | 8 ++++++++ spec/gentoo/cron_spec.rb | 8 ++++++++ spec/redhat/cron_spec.rb | 8 ++++++++ spec/redhat/file_spec.rb | 2 +- spec/solaris/cron_spec.rb | 8 ++++++++ spec/support/shared_cron_examples.rb | 23 +++++++++++++++++++++++ 7 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 spec/darwin/cron_spec.rb create mode 100644 spec/debian/cron_spec.rb create mode 100644 spec/gentoo/cron_spec.rb create mode 100644 spec/redhat/cron_spec.rb create mode 100644 spec/solaris/cron_spec.rb create mode 100644 spec/support/shared_cron_examples.rb diff --git a/spec/darwin/cron_spec.rb b/spec/darwin/cron_spec.rb new file mode 100644 index 00000000..66e961bb --- /dev/null +++ b/spec/darwin/cron_spec.rb @@ -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 diff --git a/spec/debian/cron_spec.rb b/spec/debian/cron_spec.rb new file mode 100644 index 00000000..5dc76de4 --- /dev/null +++ b/spec/debian/cron_spec.rb @@ -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 diff --git a/spec/gentoo/cron_spec.rb b/spec/gentoo/cron_spec.rb new file mode 100644 index 00000000..4d4a6e25 --- /dev/null +++ b/spec/gentoo/cron_spec.rb @@ -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 diff --git a/spec/redhat/cron_spec.rb b/spec/redhat/cron_spec.rb new file mode 100644 index 00000000..3d4ae92a --- /dev/null +++ b/spec/redhat/cron_spec.rb @@ -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 diff --git a/spec/redhat/file_spec.rb b/spec/redhat/file_spec.rb index 73429c8b..bd2463d2 100644 --- a/spec/redhat/file_spec.rb +++ b/spec/redhat/file_spec.rb @@ -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' diff --git a/spec/solaris/cron_spec.rb b/spec/solaris/cron_spec.rb new file mode 100644 index 00000000..7bc15601 --- /dev/null +++ b/spec/solaris/cron_spec.rb @@ -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 diff --git a/spec/support/shared_cron_examples.rb b/spec/support/shared_cron_examples.rb new file mode 100644 index 00000000..35fb83f5 --- /dev/null +++ b/spec/support/shared_cron_examples.rb @@ -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 From 0714b63a17f862c69fce3a89a5261071aacf8430 Mon Sep 17 00:00:00 2001 From: Gosuke Miyashita Date: Sun, 19 May 2013 01:56:37 +0900 Subject: [PATCH 3/3] Fix typos --- spec/darwin/file_spec.rb | 2 +- spec/darwin/port_spec.rb | 2 +- spec/debian/file_spec.rb | 2 +- spec/debian/port_spec.rb | 2 +- spec/gentoo/file_spec.rb | 2 +- spec/gentoo/port_spec.rb | 2 +- spec/redhat/port_spec.rb | 2 +- spec/solaris/file_spec.rb | 2 +- spec/solaris/port_spec.rb | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/darwin/file_spec.rb b/spec/darwin/file_spec.rb index 8bde7cfe..1f1ac1b0 100644 --- a/spec/darwin/file_spec.rb +++ b/spec/darwin/file_spec.rb @@ -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' diff --git a/spec/darwin/port_spec.rb b/spec/darwin/port_spec.rb index c81017fe..b10390da 100644 --- a/spec/darwin/port_spec.rb +++ b/spec/darwin/port_spec.rb @@ -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 diff --git a/spec/debian/file_spec.rb b/spec/debian/file_spec.rb index ce8a8886..559c8c5e 100644 --- a/spec/debian/file_spec.rb +++ b/spec/debian/file_spec.rb @@ -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' diff --git a/spec/debian/port_spec.rb b/spec/debian/port_spec.rb index f0a32504..5eabd3b3 100644 --- a/spec/debian/port_spec.rb +++ b/spec/debian/port_spec.rb @@ -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 diff --git a/spec/gentoo/file_spec.rb b/spec/gentoo/file_spec.rb index a71e58ba..e6efdd41 100644 --- a/spec/gentoo/file_spec.rb +++ b/spec/gentoo/file_spec.rb @@ -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' diff --git a/spec/gentoo/port_spec.rb b/spec/gentoo/port_spec.rb index 079d6d97..78528075 100644 --- a/spec/gentoo/port_spec.rb +++ b/spec/gentoo/port_spec.rb @@ -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 diff --git a/spec/redhat/port_spec.rb b/spec/redhat/port_spec.rb index 8e265544..35a7b907 100644 --- a/spec/redhat/port_spec.rb +++ b/spec/redhat/port_spec.rb @@ -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 diff --git a/spec/solaris/file_spec.rb b/spec/solaris/file_spec.rb index 751c0a39..ea019762 100644 --- a/spec/solaris/file_spec.rb +++ b/spec/solaris/file_spec.rb @@ -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' diff --git a/spec/solaris/port_spec.rb b/spec/solaris/port_spec.rb index 8ed28fa3..08d76669 100644 --- a/spec/solaris/port_spec.rb +++ b/spec/solaris/port_spec.rb @@ -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