Skip to content

Commit

Permalink
Quite a few changes to incorporate rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed Feb 11, 2014
1 parent 24ec083 commit b140752
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 102 deletions.
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
AllCops:
Includes:
- ./**/*.rb
Excludes:
- vendor/**

Encoding:
Exclude:
- metadata.rb
- Gemfile

WordArray:
MinSize: 3
13 changes: 7 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem "rake"
gem "chef", "~> 11.10"
gem "berkshelf", "~> 2.0"
gem "chefspec", "~> 3.2"
gem "foodcritic", "~> 3.0"
gem 'berkshelf', '~> 2.0'
gem 'chef', '~> 11.10'
gem 'chefspec', '~> 3.2'
gem 'foodcritic', '~> 3.0'
gem 'rake', '~> 10.1'
gem 'rubocop', '~> 0.18'
14 changes: 13 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GEM
akami (1.2.1)
gyoku (>= 0.4.0)
nokogiri
ast (1.1.0)
berkshelf (2.0.14)
activesupport (~> 3.2.0)
addressable (~> 2.3.4)
Expand Down Expand Up @@ -135,14 +136,19 @@ GEM
mixlib-shellout
systemu (~> 2.5.2)
yajl-ruby
parser (2.1.4)
ast (~> 1.1)
slop (~> 3.4, >= 3.4.5)
polyglot (0.3.3)
powerpack (0.0.9)
pry (0.9.12.6)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
puma (1.6.3)
rack (~> 1.2)
rack (1.5.2)
rainbow (2.0.0)
rake (10.1.1)
rbzip2 (0.2.0)
rest-client (1.6.7)
Expand Down Expand Up @@ -176,6 +182,11 @@ GEM
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.5)
rubocop (0.18.1)
json (>= 1.7.7, < 2)
parser (~> 2.1.3)
powerpack (~> 0.0.6)
rainbow (>= 1.99.1, < 3.0)
rubyntlm (0.1.1)
savon (0.9.5)
akami (~> 1.0)
Expand Down Expand Up @@ -218,4 +229,5 @@ DEPENDENCIES
chef (~> 11.10)
chefspec (~> 3.2)
foodcritic (~> 3.0)
rake
rake (~> 10.1)
rubocop (~> 0.18)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Installs/Configures chef-handler
Attributes
==========

* `default['reboot-handler']['reboot_command']` - Reboot command to execute.
* `default['reboot-handler']['command']` - Reboot command to execute.
* `default['reboot-handler']['enabled_role']` - Role to enable the handler on (default: booted).
* `default['reboot-handler']['post_boot_runlist']` - If set will redefine `node['run_list']` to it's value.

Expand Down
13 changes: 8 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require "foodcritic"
require "rspec/core/rake_task"
require 'foodcritic'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

FoodCritic::Rake::LintTask.new

RSpec::Core::RakeTask.new(:unit) do |t|
t.rspec_opts = [].tap do |a|
Expand All @@ -8,7 +11,7 @@ RSpec::Core::RakeTask.new(:unit) do |t|
end.join(' ')
end

FoodCritic::Rake::LintTask.new
Rubocop::RakeTask.new

task :test => [:unit, :foodcritic]
task :default => [:test]
task test: [:unit, :foodcritic, :rubocop]
task default: [:test]
5 changes: 3 additions & 2 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: UTF-8
#
# Cookbook Name:: reboot-handler
# Recipe:: default
Expand All @@ -17,6 +18,6 @@
# limitations under the License.
#

default['reboot-handler']['reboot_command'] = "sync; sync; shutdown -r +1&"
default['reboot-handler']['enabled_role'] = "booted"
default['reboot-handler']['command'] = 'sync; sync; shutdown -r +1&'
default['reboot-handler']['enabled_role'] = 'booted'
default['reboot-handler']['post_boot_runlist'] = []
28 changes: 12 additions & 16 deletions files/default/reboot.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
class Reboot < ::Chef::Handler
# encoding: UTF-8

# A Chef handler to manage reboots.
class Reboot < Chef::Handler
def initialize
end

def report
### If Chef ran successfully.
if run_status.success?
### AND node is in the booted role.
if node.roles.include? node['reboot-handler']['enabled_role']
### AND node has the reboot flag.
if node.run_state['reboot']
### THEN reset run_list if necessary.
unless node['reboot-handler']['post_boot_runlist'].empty?
node.run_list.reset! node['reboot-handler']['post_boot_runlist']
node.save
end

### AND reboot node.
::Mixlib::ShellOut.new(node['reboot-handler']['reboot_command']).run_command
end
return unless run_status.success?
return unless node.roles.include? node['reboot-handler']['enabled_role']
if node.run_state['reboot']
unless node['reboot-handler']['post_boot_runlist'].empty?
node.run_list.reset! node['reboot-handler']['post_boot_runlist']
node.save
end

Mixlib::ShellOut.new(node['reboot-handler']['command']).run_command
end
end
end
16 changes: 8 additions & 8 deletions metadata.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name "reboot-handler"
maintainer "John Dewey"
maintainer_email "[email protected]"
license "Apache 2.0"
description "Installs/Configures reboot-handler"
name 'reboot-handler'
maintainer 'John Dewey'
maintainer_email '[email protected]'
license 'Apache 2.0'
description 'Installs/Configures reboot-handler'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "0.2.2"
version '1.0.0'

recipe "reboot-handler", "Installs/Configures reboot-handler"
recipe 'reboot-handler', 'Installs/Configures reboot-handler'

%w{ debian ubuntu }.each do |os|
supports os
end

depends "chef_handler"
depends 'chef_handler'
18 changes: 10 additions & 8 deletions recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: UTF-8
#
# Cookbook Name:: reboot-handler
# Recipe:: default
Expand All @@ -17,24 +18,25 @@
# limitations under the License.
#

include_recipe "chef_handler"
include_recipe 'chef_handler'

cookbook_file(::File.join(node['chef_handler']['handler_path'], "reboot.rb")).run_action(:create)
handler = File.join(node['chef_handler']['handler_path'], 'reboot.rb')
cookbook_file(handler).run_action(:create)

##
# This was primarily done to prevent others from having to stub
# `include_recipe "reboot_handler"` inside ChefSpec. ChefSpec
# doesn't seem to handle the following well on convergence.

begin
require ::File.join node["chef_handler"]["handler_path"], "reboot"
rescue ::LoadError
log("Unable to require the reboot handler!") { level :error }
require File.join node['chef_handler']['handler_path'], 'reboot'
rescue LoadError
log('Unable to require the reboot handler!') { level :error }
end

chef_handler "Chef::Handler::Reboot" do
source ::File.join node['chef_handler']['handler_path'], "reboot.rb"
supports :report => true
chef_handler 'Chef::Handler::Reboot' do
source File.join node['chef_handler']['handler_path'], 'reboot.rb'
supports report: true

action :nothing
end.run_action(:enable)
46 changes: 24 additions & 22 deletions spec/default_spec.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
require_relative "spec_helper"
# encoding: UTF-8

describe "reboot-handler::default" do
require_relative 'spec_helper'

describe 'reboot-handler::default' do
before do
Chef::Recipe.any_instance.stub(:require).
with("/var/chef/handlers/reboot")
@chef_run = ::ChefSpec::Runner.new
Chef::Recipe.any_instance.stub(:require)
.with('/var/chef/handlers/reboot')
@chef_run = ChefSpec::Runner.new
end

it "includes chef_handler" do
Chef::Recipe.any_instance.should_receive(:include_recipe).
with("chef_handler")
it 'includes chef_handler' do
Chef::Recipe.any_instance.should_receive(:include_recipe)
.with('chef_handler')

@chef_run.converge "reboot-handler::default"
@chef_run.converge 'reboot-handler::default'
end

it "installs the handler" do
@chef_run.converge "reboot-handler::default"
it 'installs the handler' do
@chef_run.converge 'reboot-handler::default'

@chef_run.should create_cookbook_file "/var/chef/handlers/reboot.rb"
@chef_run.should create_cookbook_file '/var/chef/handlers/reboot.rb'
end

it "doesn't log" do
@chef_run.converge "reboot-handler::default"
@chef_run.converge 'reboot-handler::default'

@chef_run.should_not write_log "Unable to require the reboot handler!"
@chef_run.should_not write_log 'Unable to require the reboot handler!'
end

it "logs when handler is missing" do
Chef::Recipe.any_instance.should_receive(:require).
with("/var/chef/handlers/reboot").
and_raise ::LoadError.new
@chef_run.converge "reboot-handler::default"
it 'logs when handler is missing' do
Chef::Recipe.any_instance.should_receive(:require)
.with('/var/chef/handlers/reboot')
.and_raise LoadError.new
@chef_run.converge 'reboot-handler::default'

@chef_run.should write_log "Unable to require the reboot handler!"
@chef_run.should write_log 'Unable to require the reboot handler!'
end

it "chef_handler lwrp" do
pending "No idea how to test this"
it 'chef_handler lwrp' do
pending 'No idea how to test this'
end
end
60 changes: 31 additions & 29 deletions spec/reboot_spec.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
require_relative "spec_helper"
require ::File.join ::File.dirname(__FILE__), "..", "files", "default", "reboot"
# encoding: UTF-8

require_relative 'spec_helper'
require File.join File.dirname(__FILE__), '..', 'files', 'default', 'reboot'

describe Reboot do
::Mixlib::ShellOut.class_eval do
Mixlib::ShellOut.class_eval do
def run_command
true
end
end

before do
@handler = ::Reboot.new
@node = ::ChefSpec::Runner.new.converge("reboot-handler::default").node
@handler = Reboot.new
@node = ChefSpec::Runner.new.converge('reboot-handler::default').node
@node.stub :save
@run_status = ::Chef::RunStatus.new @node, ::Chef::EventDispatch::Dispatcher.new
@status = Chef::RunStatus.new @node, Chef::EventDispatch::Dispatcher.new
end

it "doesn't reboot if the run failed" do
@run_status.exception = ::Exception.new
@status.exception = Exception.new

@handler.run_report_unsafe(@run_status).should_not be_true
@handler.run_report_unsafe(@status).should_not be_true
end

it "doesn't reboot if the node does not have the enabled_role" do
@handler.run_report_unsafe(@run_status).should_not be_true
@handler.run_report_unsafe(@status).should_not be_true
end

it "doesn't reboot if the node has the enabled_role, but missing the reboot flag" do
@node.stub(:roles).and_return ["booted"]
it "doesn't reboot if the node has the enabled_role, but missing the reboot flag" do # rubocop:disable LineLength
@node.stub(:roles).and_return ['booted']

@handler.run_report_unsafe(@run_status).should_not be_true
@handler.run_report_unsafe(@status).should_not be_true
end

describe "with enabled_role and reboot flag" do
describe 'with enabled_role and reboot flag' do
before do
@node.stub(:roles).and_return ["booted"]
@node.stub(:roles).and_return ['booted']
@node.run_state['reboot'] = true
end

it "reboots" do
@handler.run_report_unsafe(@run_status).should be_true
it 'reboots' do
@handler.run_report_unsafe(@status).should be_true
end

it "issues correct reboot_command" do
it 'issues correct command' do
obj = double
obj.stub(:run_command) { true }
::Mixlib::ShellOut.should_receive(:new).
with("sync; sync; shutdown -r +1&").
and_return(obj)
@handler.run_report_unsafe(@run_status)
Mixlib::ShellOut.should_receive(:new)
.with('sync; sync; shutdown -r +1&')
.and_return(obj)
@handler.run_report_unsafe(@status)
end

it "resets run_list if node has a post_boot_runlist attribute" do
node = ::ChefSpec::Runner.new do |n|
n.set['reboot-handler']['post_boot_runlist'] = ["role[foo]"]
end.converge("reboot-handler::default").node
it 'resets run_list if node has a post_boot_runlist attribute' do
node = ChefSpec::Runner.new do |n|
n.set['reboot-handler']['post_boot_runlist'] = ['role[foo]']
end.converge('reboot-handler::default').node
node.stub :save
run_status = ::Chef::RunStatus.new node, ::Chef::EventDispatch::Dispatcher.new
node.stub(:roles).and_return ["booted"]
status = Chef::RunStatus.new node, Chef::EventDispatch::Dispatcher.new
node.stub(:roles).and_return ['booted']
node.run_state['reboot'] = true
@handler.run_report_unsafe(run_status)
@handler.run_report_unsafe(status)

node.run_list.to_s.should eq("role[foo]")
node.run_list.to_s.should eq('role[foo]')
end
end
end
10 changes: 6 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "chef"
require "chefspec"
require "chefspec/berkshelf"
require "chefspec/deprecations"
# encoding: UTF-8

require 'chef'
require 'chefspec'
require 'chefspec/berkshelf'
require 'chefspec/deprecations'

0 comments on commit b140752

Please sign in to comment.