Skip to content

Commit

Permalink
ChefSpec tests: Create stubs for each cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
zuazo committed Sep 21, 2015
1 parent f5189ae commit 44de096
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 33 deletions.
2 changes: 1 addition & 1 deletion test/unit/recipes/_apache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
let(:chef_runner) { ChefSpec::SoloRunner.new }
let(:chef_run) { chef_runner.converge(described_recipe) }
let(:node) { chef_runner.node }
before { stub_command('/usr/sbin/apache2 -t').and_return(true) }
before { stub_apache2_cookbook }

it 'includes apache2::default recipe' do
expect(chef_run).to include_recipe('apache2::default')
Expand Down
6 changes: 1 addition & 5 deletions test/unit/recipes/_nginx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
let(:node) { chef_runner.node }
before do
node.set['owncloud']['ssl'] = true
stub_command('which nginx').and_return(true)
stub_command(
'test -d /etc/php5/fpm/pool.d || mkdir -p /etc/php5/fpm/pool.d'
).and_return(true)
allow(::File).to receive(:symlink?).and_call_original
stub_nginx_cookbook
allow(::File).to receive(:exist?).and_call_original
allow(::File).to receive(:exist?).with('/etc/init.d/apache2')
.and_return(true)
Expand Down
8 changes: 1 addition & 7 deletions test/unit/recipes/_php_fpm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@
let(:chef_runner) { ChefSpec::SoloRunner.new }
let(:chef_run) { chef_runner.converge(described_recipe) }
let(:node) { chef_runner.node }
before do
stub_command(
'test -d /etc/php5/fpm/pool.d || mkdir -p /etc/php5/fpm/pool.d'
).and_return(true)
stub_command('test -d /etc/php-fpm.d || mkdir -p /etc/php-fpm.d')
.and_return(true)
end
before { stub_php_fpm_cookbook }

it 'include php-fpm recipe' do
expect(chef_run).to include_recipe('php-fpm')
Expand Down
25 changes: 5 additions & 20 deletions test/unit/recipes/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,11 @@
node.set['owncloud']['admin']['pass'] = admin_password
node.set['postgresql']['password']['postgres'] = db_root_password

stub_command('/usr/sbin/apache2 -t').and_return(true)
stub_command('/usr/sbin/httpd -t').and_return(true)
stub_command(
"psql -c 'SELECT lanname FROM pg_catalog.pg_language' #{db_name} "\
"| grep '^ plpgsql$'"
).and_return(false)
stub_command('which nginx').and_return(true)
stub_command(
'test -d /etc/php-fpm.d || mkdir -p /etc/php-fpm.d'
).and_return(true)
stub_command(
'test -d /etc/php5/fpm/pool.d || mkdir -p /etc/php5/fpm/pool.d'
).and_return(true)
stub_command('ls /recovery.conf').and_return(false)
stub_command(
'/usr/bin/test /etc/alternatives/mta -ef /usr/sbin/sendmail.postfix'
).and_return(true)
stub_command('ls /var/lib/postgresql/9.1/main/recovery.conf')
.and_return(true)
stub_command('which php').and_return(true)
stub_apache2_cookbook
stub_postgresql_cookbook
stub_nginx_cookbook
stub_postfix_cookbook

allow(::File).to receive(:exist?).and_call_original
allow(::File)
.to receive(:exist?).with('/var/www/owncloud/config/config.php')
Expand Down
1 change: 1 addition & 0 deletions test/unit/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require 'should_not/rspec'

require_relative 'support/matchers'
require_relative 'support/cookbook_stubs'
require_relative 'support/coverage'

RSpec.configure do |config|
Expand Down
56 changes: 56 additions & 0 deletions test/unit/support/cookbook_stubs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# encoding: UTF-8
#
# Author:: Xabier de Zuazo (<xabier@zuazo.org>)
# Copyright:: Copyright (c) 2015 Xabier de Zuazo
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

if defined?(ChefSpec)
def stub_apache2_cookbook
stub_command('/usr/sbin/apache2 -t').and_return(true)
stub_command('/usr/sbin/httpd -t').and_return(true)
stub_command('which php').and_return(true)
end

def stub_php_fpm_cookbook
stub_command(
'test -d /etc/php5/fpm/pool.d || mkdir -p /etc/php5/fpm/pool.d'
).and_return(true)
stub_command('test -d /etc/php-fpm.d || mkdir -p /etc/php-fpm.d')
.and_return(true)
end

def stub_nginx_cookbook
stub_php_fpm_cookbook
stub_command('which nginx').and_return(true)
allow(::File).to receive(:symlink?).and_call_original
end

def stub_postgresql_cookbook
stub_command(
"psql -c 'SELECT lanname FROM pg_catalog.pg_language' #{db_name} "\
"| grep '^ plpgsql$'"
).and_return(false)
stub_command('ls /recovery.conf').and_return(false)
stub_command('ls /var/lib/postgresql/9.1/main/recovery.conf')
.and_return(true)
end

def stub_postfix_cookbook
stub_command(
'/usr/bin/test /etc/alternatives/mta -ef /usr/sbin/sendmail.postfix'
).and_return(true)
end
end

0 comments on commit 44de096

Please sign in to comment.