Skip to content

Commit

Permalink
Merge pull request #182 from opscode-cookbooks/cwebberOps/migrate_tes…
Browse files Browse the repository at this point in the history
…ting

Migrate testing to serverspec and chefspec
  • Loading branch information
cwebberOps committed Aug 20, 2014
2 parents 950a85c + fcb3ea0 commit bdb4d45
Show file tree
Hide file tree
Showing 39 changed files with 263 additions and 694 deletions.
162 changes: 51 additions & 111 deletions .kitchen.cloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ provisioner:

platforms:
- name: centos-5.8
driver_plugin: digitalocean
driver_plugin: digital_ocean
driver_config:
image_id: 1601
flavor_id: 63
region_id: 4
ssh_key_ids: <%= ENV['DIGITAL_OCEAN_SSH_KEY_IDS'] %>
ssh_key: <%= ENV['DIGITAL_OCEAN_SSH_KEY_PATH'] %>

- name: centos-6.4
driver_plugin: digitalocean
size: 1gb

- name: centos-6.5
driver_plugin: digital_ocean
driver_config:
size: 1gb

- name: centos-7.0
driver_plugin: digital_ocean
driver_config:
image_id: 562354
flavor_id: 63
region_id: 4
ssh_key_ids: <%= ENV['DIGITAL_OCEAN_SSH_KEY_IDS'] %>
ssh_key: <%= ENV['DIGITAL_OCEAN_SSH_KEY_PATH'] %>
size: 1gb

- name: amazon-2013.09
driver_plugin: ec2
Expand All @@ -37,80 +34,73 @@ platforms:
username: ec2-user
ssh_key: <%= ENV['EC2_SSH_KEY_PATH'] %>

- name: fedora-19
driver_plugin: digitalocean
- name: amazon-2014.03
driver_plugin: ec2
driver_config:
image_id: 3102879
flavor_id: 63
region_id: 4
ssh_key_ids: <%= ENV['DIGITAL_OCEAN_SSH_KEY_IDS'] %>
ssh_key: <%= ENV['DIGITAL_OCEAN_SSH_KEY_PATH'] %>
image_id: ami-6b726502
username: ec2-user
ssh_key: <%= ENV['EC2_SSH_KEY_PATH'] %>

- name: suse-11.3
driver_plugin: ec2
driver_config:
image_id: ami-e8084981
username: root
ssh_key: <%= ENV['EC2_SSH_KEY_PATH'] %>

- name: fedora-20
driver_plugin: digitalocean
driver_plugin: digital_ocean
driver_config:
size: 1gb

- name: debian-7.0
driver_plugin: digital_ocean
driver_config:
size: 1gb

- name: ubuntu-10.04
driver_plugin: digital_ocean
driver_config:
image_id: 3243145
flavor_id: 63
region_id: 4
ssh_key_ids: <%= ENV['DIGITAL_OCEAN_SSH_KEY_IDS'] %>
ssh_key: <%= ENV['DIGITAL_OCEAN_SSH_KEY_PATH'] %>
size: 1gb

- name: ubuntu-12.04
driver_plugin: digital_ocean
driver_config:
size: 1gb

- name: ubuntu-14.04
driver_plugin: digital_ocean
driver_config:
size: 1gb

suites:
# This suite tests that the configuration is applied correctly (via attributes)
- name: config
run_list:
- recipe[minitest-handler]
- recipe[chef-client_test::config]
- recipe[chef-client::config]
attributes:
chef_client:
load_gems:
chef-handler-updated-resources:
require_name: "chef/handler/updated_resources"
config:
log_level: ":debug"
ssl_verify_mode: ":verify_peer"
start_handlers: [{class: "SimpleReport::UpdatedResources", arguments: []}]
report_handlers: [{class: "SimpleReport::UpdatedResources", arguments: []}]
exception_handlers: [{class: "SimpleReport::UpdatedResources", arguments: []}]
ohai:
disabled_plugins: ["passwd"]

- name: service_init
run_list:
- recipe[minitest-handler]
- recipe[chef-client::config]
- recipe[chef-client_test::service_init]
- recipe[chef-client::init_service]
attributes: {}

- name: service_runit
run_list:
- recipe[minitest-handler]
- recipe[runit]
- recipe[chef-client_test::service_runit]
- recipe[chef-client::runit_service]
attributes: {}

- name: service_systemd
run_list:
- recipe[minitest-handler]
- recipe[chef-client_test::service_systemd]
- recipe[chef-client::systemd_service]
excludes: ["centos-5.9", "centos-6.4", "ubuntu-10.04", "ubuntu-12.04"]
attributes: {}
#- name: service_systemd
# run_list:
# - recipe[chef-client::systemd_service]
# attributes: {}
# includes: ["centos-7.0", "fedora-20", "debian-7.0"]

- name: service_upstart
run_list:
- recipe[minitest-handler]
- recipe[chef-client_test::service_upstart]
- recipe[chef-client::upstart_service]
excludes: ["centos-5.9"]
includes: ["ubuntu-10.04", "ubuntu-12.04", "ubuntu-14.04"]
attributes: {}

- name: cron
run_list:
- recipe[minitest-handler]
- recipe[chef-client::cron]
attributes: {}

Expand All @@ -119,70 +109,20 @@ suites:
- recipe[chef-client::delete_validation]
attributes: {}

# Probably needs to be moved up to config and tested there
- name: cook-2092
run_list:
- recipe[minitest-handler]
- recipe[chef-client_test::cook-2092]
- recipe[chef-client::config]
attributes:
chef_client:
log_file: "client.log"

# Tests that the root user is set correctly for the cron recipe.
- name: cook-2169-root
run_list:
- recipe[minitest-handler]
- recipe[chef-client_test::cook-2169-root]
- recipe[chef-client::cron]
attributes: {}

# Tests that the "chef server" user is set correctly with the cron recipe.
- name: cook-2169-chef
run_list:
- recipe[minitest-handler]
- recipe[chef-client_test::cook-2169-chef]
- recipe[chef-client::cron]
attributes: {}

# Test that the environment variables are rendered correctly
- name: cook-2086
run_list:
- recipe[minitest-handler]
- recipe[chef-client_test::cook-2086]
- recipe[chef-client::cron]
attributes:
chef_client:
cron:
environment_variables: "FOO=BAR"

# Test that the we can use the cron_d directory
- name: cook-use_cron_d
run_list:
- recipe[minitest-handler]
- recipe[chef-client::cron]
attributes:
chef_client:
cron:
use_cron_d: true

# No longer necessary w/ COOK-2856
# - name: cook-2317
# run_list:
# - recipe[minitest-handler]
# - recipe[chef-client::config]
# - recipe[chef-client_test::cook-2317]
# attributes:
# ohai:
# disabled_plugins: ["passwd"]

# Test that the running process includes command-line "daemon" options
- name: cook-1951
run_list:
- recipe[minitest-handler]
- recipe[chef-client::config]
- recipe[chef-client::service]
- recipe[chef-client_test::cook-1951]
attributes:
chef_client:
init_style: "init"
Expand Down
2 changes: 0 additions & 2 deletions Berksfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ site :opscode
metadata

group :integration do
cookbook "minitest-handler"
cookbook "runit"
cookbook "apt"
cookbook "chef-client_test", :path => "./test/cookbooks/chef-client_test"
end
7 changes: 3 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ group :lint do
end

group :unit do
gem 'berkshelf', '~> 3.0.0.beta6'
gem 'chefspec', '~> 3.1'
gem 'berkshelf', '~> 3.0'
gem 'chefspec', '~> 4.0'
gem 'fauxhai', '~>2.2'
end

group :kitchen_common do
Expand All @@ -28,10 +29,8 @@ group :development do
gem 'ruby_gntp'
gem 'growl'
gem 'rb-fsevent'
gem 'guard', '~> 2.4'
gem 'guard-kitchen'
gem 'guard-foodcritic'
gem 'guard-rspec'
gem 'guard-rubocop'
gem 'rake'
end
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ guard 'rubocop', all_on_start: false do
watch('metadata.rb')
end

guard :rspec, cmd: 'bundle exec rspec', all_on_start: false, notification: false do
guard :rspec, cmd: 'rspec', all_on_start: false, notification: false do
watch(%r{^libraries/(.+)\.rb$})
watch(%r{^spec/(.+)_spec\.rb$})
watch(%r{^(recipes)/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
Expand Down
80 changes: 80 additions & 0 deletions spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'spec_helper'

describe 'chef-client::config' do

let(:chef_run) do
ChefSpec::Runner.new.converge(described_recipe)
end

it 'contains the default chef_server_url setting' do
expect(chef_run).to render_file('/etc/chef/client.rb') \
.with_content(%r{chef_server_url})
end

it 'contains the default validation_client_name setting' do
expect(chef_run).to render_file('/etc/chef/client.rb') \
.with_content(%r{validation_client_name})
end

[
'/var/run',
'/var/chef/cache',
'/var/chef/backup',
'/var/log/chef',
'/etc/chef',
'/etc/chef/client.d'
].each do |dir|
it "contains #{dir} directory" do
expect(chef_run).to create_directory(dir)
end
end

it 'reloads the client config' do
expect(chef_run).to_not run_ruby_block('reload_client_config')
end

context 'Custom Attributes' do

let(:chef_run) do
ChefSpec::Runner.new do |node|
node.set['ohai']['disabled_plugins'] = [:passwd, "dmi"]
node.set['chef_client']['config']['log_level'] = ":debug"
node.set['chef_client']['config']['ssl_verify_mode'] = ":verify_peer"
node.set['chef_client']['config']['exception_handlers'] = [{class: "SimpleReport::UpdatedResources", arguments: []}]
node.set['chef_client']['config']['report_handlers'] = [{class: "SimpleReport::UpdatedResources", arguments: []}]
node.set['chef_client']['config']['start_handlers'] = [{class: "SimpleReport::UpdatedResources", arguments: []}]
node.set['chef_client']['load_gems']['chef-handler-updated-resources']['require_name'] = "chef/handler/updated_resources"
end.converge(described_recipe)
end

it 'disables ohai 6 & 7 plugins' do
expect(chef_run).to render_file('/etc/chef/client.rb') \
.with_content(%r{Ohai::Config\[:disabled_plugins\] =\s+\[:passwd,"dmi"\]})
end

it 'converts log_level to a symbol' do
expect(chef_run).to render_file('/etc/chef/client.rb') \
.with_content(%r{^log_level :debug})
end

it 'converts ssl_verify_mode to a symbol' do
expect(chef_run).to render_file('/etc/chef/client.rb') \
.with_content(%r{^ssl_verify_mode :verify_peer})
end

it 'enables exception_handlers' do
expect(chef_run).to render_file('/etc/chef/client.rb') \
.with_content(%{exception_handlers << SimpleReport::UpdatedResources.new})
end

it 'requires handler libraries' do
expect(chef_run).to install_chef_gem('chef-handler-updated-resources')
expect(chef_run).to render_file('/etc/chef/client.rb') \
.with_content(%{\["chef/handler/updated_resources"\].each do |lib|})
end


end


end
39 changes: 39 additions & 0 deletions spec/unit/cron_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

describe 'chef-client::cron' do

let(:chef_run) do
ChefSpec::Runner.new.converge(described_recipe)
end

[
'/var/run',
'/var/chef/cache',
'/var/chef/backup',
'/var/log/chef',
'/etc/chef'
].each do |dir|
it "creates #{dir} with the correct attributes" do
expect(chef_run).to create_directory(dir).with(
user: 'root',
group: 'root'
)
end
end

context 'Custom Attributes' do

let(:chef_run) do
ChefSpec::Runner.new do |node|
node.set['chef_client']['cron']['environment_variables'] = 'FOO=BAR'
end.converge(described_recipe)
end

it 'sets the FOO=BAR environment variable' do
expect(chef_run).to create_cron('chef-client') \
.with(command: %r{/bin/sleep \d+; FOO=BAR /usr/bin/chef-client > /dev/null 2>&1})
end

end

end
Loading

0 comments on commit bdb4d45

Please sign in to comment.