From 44de0962b42118688651fff5b6fafd2dcd2f11fb Mon Sep 17 00:00:00 2001 From: Xabier de Zuazo Date: Mon, 21 Sep 2015 15:03:16 +0200 Subject: [PATCH] ChefSpec tests: Create stubs for each cookbook --- test/unit/recipes/_apache_spec.rb | 2 +- test/unit/recipes/_nginx_spec.rb | 6 +--- test/unit/recipes/_php_fpm_spec.rb | 8 +---- test/unit/recipes/default_spec.rb | 25 +++---------- test/unit/spec_helper.rb | 1 + test/unit/support/cookbook_stubs.rb | 56 +++++++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 33 deletions(-) create mode 100644 test/unit/support/cookbook_stubs.rb diff --git a/test/unit/recipes/_apache_spec.rb b/test/unit/recipes/_apache_spec.rb index 372f2f2..9cf6aee 100644 --- a/test/unit/recipes/_apache_spec.rb +++ b/test/unit/recipes/_apache_spec.rb @@ -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') diff --git a/test/unit/recipes/_nginx_spec.rb b/test/unit/recipes/_nginx_spec.rb index 0ed2b91..fc08941 100644 --- a/test/unit/recipes/_nginx_spec.rb +++ b/test/unit/recipes/_nginx_spec.rb @@ -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) diff --git a/test/unit/recipes/_php_fpm_spec.rb b/test/unit/recipes/_php_fpm_spec.rb index c4b4874..65c6c9b 100644 --- a/test/unit/recipes/_php_fpm_spec.rb +++ b/test/unit/recipes/_php_fpm_spec.rb @@ -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') diff --git a/test/unit/recipes/default_spec.rb b/test/unit/recipes/default_spec.rb index dfa1dae..485687d 100644 --- a/test/unit/recipes/default_spec.rb +++ b/test/unit/recipes/default_spec.rb @@ -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') diff --git a/test/unit/spec_helper.rb b/test/unit/spec_helper.rb index 9f88c9f..e918823 100644 --- a/test/unit/spec_helper.rb +++ b/test/unit/spec_helper.rb @@ -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| diff --git a/test/unit/support/cookbook_stubs.rb b/test/unit/support/cookbook_stubs.rb new file mode 100644 index 0000000..6b60f9d --- /dev/null +++ b/test/unit/support/cookbook_stubs.rb @@ -0,0 +1,56 @@ +# encoding: UTF-8 +# +# Author:: Xabier de Zuazo () +# 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