Skip to content

Commit

Permalink
Merge pull request #8 from evertrue/add-test-goodies
Browse files Browse the repository at this point in the history
Add unit & integration testing, tied in to Travis
  • Loading branch information
chr4 committed Dec 4, 2014
2 parents aed1c7a + b283436 commit 68d1b70
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 5 deletions.
17 changes: 17 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
driver:
name: vagrant

provisioner:
name: chef_zero

platforms:
- name: ubuntu-12.04
- name: ubuntu-14.04
- name: centos-5.10
- name: centos-6.5

suites:
- name: default
run_list:
- recipe[motd::default]
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ rvm:
- 2.0
- 2.1
script:
- bundle exec rubocop
- bundle exec foodcritic -f any .
- bundle exec rake travis
3 changes: 3 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://supermarket.getchef.com'

metadata
15 changes: 12 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
source 'https://rubygems.org'

group :lint do
gem 'foodcritic'
gem 'rubocop'
group :test, :development do
gem 'rake'
end

group :test do
gem 'berkshelf', '~> 3.1'
gem 'chefspec', '~> 4.0'
gem 'foodcritic', '~> 3.0'
gem 'rubocop', '~> 0.24'

gem 'test-kitchen', '~> 1.2'
gem 'kitchen-vagrant', '~> 0.14'
end
36 changes: 36 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'bundler/setup'
require 'rubocop/rake_task'
require 'foodcritic'
require 'rspec/core/rake_task'
require 'kitchen'

namespace :style do
desc 'Run Ruby style checks'
RuboCop::RakeTask.new(:ruby)

desc 'Run Chef style checks'
FoodCritic::Rake::LintTask.new(:chef) do |t|
t.options = { fail_tags: ['any'] }
end
end

desc 'Run all style checks'
task style: ['style:chef', 'style:ruby']

desc 'Run ChefSpec unit tests'
RSpec::Core::RakeTask.new(:unit) do |t|
t.rspec_opts = '--color --format progress'
end

desc 'Run Test Kitchen integration tests'
task :integration do
Kitchen.logger = Kitchen.default_file_logger
Kitchen::Config.new.instances.each do |instance|
instance.test(:always)
end
end

# The default rake task should just run it all
task default: %w(style integration)

task travis: %w(style)
9 changes: 9 additions & 0 deletions spec/recipes/cow_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

describe 'motd::cow' do
let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }

it 'adds a cow motd' do
expect(chef_run).to create_motd('50-cow')
end
end
11 changes: 11 additions & 0 deletions spec/recipes/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe 'motd::default' do
let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }

%w(cow knife_status).each do |recipe|
it "includes the `#{recipe}` recipe" do
expect(chef_run).to include_recipe "motd::#{recipe}"
end
end
end
31 changes: 31 additions & 0 deletions spec/recipes/knife-status_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

describe 'motd::knife_status' do
let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe) }

before do
File.stub(:directory?).and_call_original
File.stub(:directory?).with('/etc/update-motd.d').and_return(true)
end

it 'adds a knife status motd' do
expect(chef_run).to create_motd('98-knife-status')
end

it "creates #{Chef::Config[:file_cache_path]}/handlers directory" do
expect(chef_run).to create_directory("#{Chef::Config[:file_cache_path]}/handlers").with(
mode: 00755,
)
end

it 'adds a Chef handler' do
expect(chef_run).to create_template("#{Chef::Config[:file_cache_path]}/handlers/knife_status.rb").with(
mode: 00644,
source: 'knife_status_handler.rb',
)
end

it 'enables Motd::KnifeStatus as a Chef handler' do
expect(chef_run).to enable_chef_handler('Motd::KnifeStatus')
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'chefspec'
require 'chefspec/berkshelf'
48 changes: 48 additions & 0 deletions test/integration/default/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

describe 'MOTD' do
if os[:family] == 'Ubuntu'
describe file '/etc/update-motd.d/50-cow' do
it { should be_file }
it { should be_mode '755' }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its(:content) do
should match(/This is (\e\[0;34;49m)?default-ubuntu-(.*?)(\e\[0m)?, a vagrantup\.com _default server/)
end
end

describe file '/etc/update-motd.d/98-knife-status' do
it { should be_file }
it { should be_mode '755' }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its(:content) { should include 'Last chef run:' }
end

describe file '/tmp/kitchen/cache/handlers' do
it { should be_directory }
it { should be_mode '755' }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
end

describe file '/tmp/kitchen/cache/handlers/knife_status.rb' do
it { should be_file }
it { should be_mode '644' }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its(:content) { should include 'KnifeStatus < Chef::Handler' }
end
else
describe file '/etc/motd' do
it { should be_file }
it { should be_mode '644' }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its(:content) do
should match(/This is (\e\[0;34;49m)?default-centos-(.*?)(\e\[0m)?, a vagrantup\.com _default server/)
end
end
end
end
11 changes: 11 additions & 0 deletions test/integration/default/serverspec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'serverspec'
require 'pathname'

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
c.before :all do
c.path = '/sbin:/usr/sbin'
end
end

0 comments on commit 68d1b70

Please sign in to comment.