Skip to content

Commit

Permalink
Add ChefSpec unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zuazo committed Sep 15, 2015
1 parent ab3684c commit f3ec28e
Show file tree
Hide file tree
Showing 11 changed files with 1,089 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
source 'https://rubygems.org'

chef_version = ENV.key?('CHEF_VERSION') ? ENV['CHEF_VERSION'] : nil

gem 'berkshelf', '~> 3.0'

group :lint do
gem 'foodcritic', '~> 3.0'
end

group :unit do
gem 'chef', chef_version unless chef_version.nil? # Ruby 1.9.3 support
gem 'should_not', '~> 1.1'
gem 'chefspec', '~> 4.0'
gem 'ohai', '~> 7.4' if RUBY_VERSION < '2'
end

group :kitchen_common do
gem 'test-kitchen', '~> 1.2'
end
Expand Down
4 changes: 3 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
default['owncloud']['web_server'] = 'apache'
default['owncloud']['php-fpm']['pool'] = 'owncloud'
default['owncloud']['max_upload_size'] = '512M'
default['owncloud']['sendfile'] = node['virtualization']['system'].eql?('vbox') ? false : true
default['owncloud']['sendfile'] =
!node['virtualization'].is_a?(Hash) ||
node['virtualization']['system'] != 'vbox'
default['owncloud']['skip_permissions'] = false

default['owncloud']['ssl'] = true
Expand Down
2 changes: 2 additions & 0 deletions recipes/_apache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# limitations under the License.
#

Chef::Recipe.send(:include, OwnCloud::RecipeHelpers)

#==============================================================================
# Set up Apache httpd webserver
#==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions recipes/_nginx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# limitations under the License.
#

Chef::Recipe.send(:include, OwnCloud::RecipeHelpers)

#==============================================================================
# Set up nginx webserver
#==============================================================================
Expand Down
2 changes: 0 additions & 2 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
# limitations under the License.
#

Chef::Recipe.send(:include, OwnCloud::RecipeHelpers)

#==============================================================================
# Calculate dependencies for different distros
#==============================================================================
Expand Down
83 changes: 83 additions & 0 deletions test/unit/recipes/_apache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 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.
#

require_relative '../spec_helper'

describe 'owncloud::_apache' do
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) }

it 'includes apache2::default recipe' do
expect(chef_run).to include_recipe('apache2::default')
end

it 'includes apache2::mod_php5 recipe' do
expect(chef_run).to include_recipe('apache2::mod_php5')
end

context 'apache_site default definition' do
it 'disables default site' do
allow(::File).to receive(:symlink?).and_call_original
allow(::File).to receive(:symlink?)
.with(%r{sites-enabled/default\.conf$}).and_return(true)
expect(chef_run).to run_execute('a2dissite default.conf')
end
end

context 'web_app owncloud definition' do
it 'creates apache2 site' do
expect(chef_run)
.to create_template(%r{/sites-available/owncloud\.conf$})
end
end

context 'with SSL enabled' do
before { node.set['owncloud']['ssl'] = true }

it 'creates SSL certificate' do
expect(chef_run).to create_file('owncloud.key')
expect(chef_run).to create_file('owncloud.pem')
end

context 'web_app owncloud-ssl definition' do
it 'creates apache2 site' do
expect(chef_run)
.to create_template(%r{/sites-available/owncloud-ssl\.conf$})
end
end
end

context 'with SSL disabled' do
before { node.set['owncloud']['ssl'] = false }

it 'does not create SSL certificate' do
expect(chef_run).to_not create_file('owncloud.key')
expect(chef_run).to_not create_file('owncloud.pem')
end

context 'web_app owncloud-ssl definition' do
it 'does not create apache2 site' do
expect(chef_run)
.to_not create_template(%r{/sites-available/owncloud-ssl\.conf$})
end
end
end
end
114 changes: 114 additions & 0 deletions test/unit/recipes/_nginx_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# 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.
#

require_relative '../spec_helper'

describe 'owncloud::_nginx' do
let(:chef_runner) { ChefSpec::SoloRunner.new }
let(:chef_run) { chef_runner.converge(described_recipe) }
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
allow(::File).to receive(:exist?).and_call_original
allow(::File).to receive(:exist?).with('/etc/init.d/apache2')
.and_return(true)
end

it 'includes nginx recipe' do
expect(chef_run).to include_recipe('nginx')
end

it 'includes owncloud::_php_fpm recipe' do
expect(chef_run).to include_recipe('owncloud::_php_fpm')
end

it 'disables nginx default site' do
allow(::File).to receive(:symlink?)
.with('/etc/nginx/sites-enabled/default').and_return(true)
expect(chef_run).to run_execute('nxdissite default')
end

it 'creates owncloud virtualhost' do
expect(chef_run)
.to create_template('/etc/nginx/sites-available/owncloud')
.with_source('nginx_vhost.erb')
.with_mode(00644)
.with_owner('root')
.with_group('root')
end

context 'owncloud virtualhost resource' do
let(:resource) { chef_run.template('/etc/nginx/sites-available/owncloud') }

it 'notifies nginx to reload' do
expect(resource).to notify('service[nginx]').to(:reload).delayed
end
end

it 'enables nginx owncloud site' do
allow(::File).to receive(:symlink?)
.with('/etc/nginx/sites-enabled/owncloud').and_return(false)
expect(chef_run).to run_execute('nxensite owncloud')
end

context 'with SSL enabled' do
before { node.set['owncloud']['ssl'] = true }

it 'does not create SSL certificate' do
expect(chef_run).to create_file('owncloud.key')
expect(chef_run).to create_file('owncloud.pem')
end

it 'creates nginx owncloud-ssl site' do
expect(chef_run)
.to create_template(%r{/sites-available/owncloud-ssl$})
end

it 'enables nginx owncloud-ssl site' do
allow(::File).to receive(:symlink?)
.with('/etc/nginx/sites-enabled/owncloud-ssl').and_return(false)
expect(chef_run).to run_execute('nxensite owncloud-ssl')
end
end

context 'with SSL disabled' do
before { node.set['owncloud']['ssl'] = false }

it 'does not create SSL certificate' do
expect(chef_run).to_not create_file('owncloud.key')
expect(chef_run).to_not create_file('owncloud.pem')
end

it 'does not create nginx owncloud-ssl site' do
expect(chef_run)
.to_not create_template(%r{/sites-available/owncloud-ssl$})
end

it 'does not enable nginx owncloud-ssl site' do
allow(::File).to receive(:symlink?)
.with('/etc/nginx/sites-enabled/owncloud-ssl').and_return(false)
expect(chef_run).to_not run_execute('nxensite owncloud-ssl')
end
end
end
43 changes: 43 additions & 0 deletions test/unit/recipes/_php_fpm_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.
#

require_relative '../spec_helper'

describe 'owncloud::_php_fpm' do
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

it 'include php-fpm recipe' do
expect(chef_run).to include_recipe('php-fpm')
end

it 'creates php FPM pool' do
expect(chef_run).to create_template(
"#{node['php-fpm']['pool_conf_dir']}/owncloud.conf"
)
end
end
Loading

0 comments on commit f3ec28e

Please sign in to comment.