From 2e3df98de8231af5cac38737f66702add49c0105 Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Sat, 22 Mar 2014 14:26:41 +0000 Subject: [PATCH] move Mixlib::ShellOut stubbing code to separate module It was not specific to Pacemaker in any way, yet was previously in Chef::RSpec::Pacemaker::CIBObject, in a file alongside other Pacemaker-specific code. --- spec/helpers/cib_object.rb | 70 +------------------------------ spec/helpers/provider.rb | 7 +++- spec/helpers/runnable_resource.rb | 5 ++- spec/helpers/shellout.rb | 68 ++++++++++++++++++++++++++++++ spec/providers/clone_spec.rb | 2 - spec/providers/colocation_spec.rb | 2 - spec/providers/group_spec.rb | 2 - spec/providers/location_spec.rb | 2 - spec/providers/ms_spec.rb | 2 - spec/providers/primitive_spec.rb | 3 +- 10 files changed, 80 insertions(+), 83 deletions(-) create mode 100644 spec/helpers/shellout.rb diff --git a/spec/helpers/cib_object.rb b/spec/helpers/cib_object.rb index 17e9388..aa02a5e 100644 --- a/spec/helpers/cib_object.rb +++ b/spec/helpers/cib_object.rb @@ -4,76 +4,10 @@ require 'mixlib/shellout' this_dir = File.dirname(__FILE__) require File.expand_path('../../libraries/pacemaker/cib_object', this_dir) - -module Chef::RSpec - module Pacemaker - module CIBObject - # Return a Mixlib::ShellOut double which mimics successful - # execution of a command, returning the given string on STDOUT. - def succeeding_shellout_double(string) - shellout = double(Mixlib::ShellOut) - shellout.stub(:environment).and_return({}) - shellout.stub(:run_command) - shellout.stub(:error!) - expect(shellout).to receive(:stdout).and_return(string) - shellout - end - - # Return a Mixlib::ShellOut double which mimics failed - # execution of a command, raising an exception when #error! is - # called. We expect #error! to be called, because if it isn't, - # that probably indicates the code isn't robust enough. This - # may need to be relaxed in the future. - def failing_shellout_double(stdout='', stderr='', exitstatus=1) - shellout = double(Mixlib::ShellOut) - shellout.stub(:environment).and_return({}) - shellout.stub(:run_command) - shellout.stub(:stdout).and_return(stdout) - shellout.stub(:stderr).and_return(stderr) - shellout.stub(:exitstatus).and_return(exitstatus) - exception = Mixlib::ShellOut::ShellCommandFailed.new( - "Expected process to exit with 0, " + - "but received '#{exitstatus}'" - ) - expect(shellout).to receive(:error!).and_raise(exception) - shellout - end - - # This stubs Mixlib::ShellOut.new with a sequence of doubles - # with a corresponding sequence of behaviours. This allows us - # to simulate the output of a series of shell commands being run - # via Mixlib::ShellOut. Each double either mimics a successful - # command execution whose #stdout method returns the given - # string, or a failed execution with the given exit code and - # STDOUT/STDERR. - # - # results is an Array describing the sequence of behaviours; - # each element is either a string mimicking STDOUT from - # successful command execution, or a [stdout, stderr, exitcode] - # status mimicking command execution failure. - # - # For example, "crm configure show" is executed by - # #load_current_resource, and again later on for the :create - # action, to see whether to create or modify. So the first - # double in the sequence would return an empty definition if we - # wanted to test creation of a new CIB object, or an existing - # definition if we wanted to test modification of an existing - # one. If the test needs subsequent doubles to return different - # values then stdout_strings can have more than one element. - def stub_shellout(*results) - doubles = results.map { |result| - result.is_a?(String) ? - succeeding_shellout_double(result) - : failing_shellout_double(*result) - } - Mixlib::ShellOut.stub(:new).and_return(*doubles) - end - end - end -end +require File.expand_path('shellout', this_dir) shared_examples "a CIB object" do - include Chef::RSpec::Pacemaker::CIBObject + include Chef::RSpec::Mixlib::ShellOut def expect_to_match_fixture(obj) expect(obj.class).to eq(pacemaker_object_class) diff --git a/spec/helpers/provider.rb b/spec/helpers/provider.rb index 4f29a73..591f89a 100644 --- a/spec/helpers/provider.rb +++ b/spec/helpers/provider.rb @@ -1,7 +1,8 @@ # Shared code used to test providers of CIB objects this_dir = File.dirname(__FILE__) -require File.expand_path('../helpers/cib_object', this_dir) +require File.expand_path('shellout', this_dir) +require File.expand_path('cib_object', this_dir) shared_context "a Pacemaker LWRP" do before(:each) do @@ -26,6 +27,8 @@ end module Chef::RSpec module Pacemaker module CIBObject + include Chef::RSpec::Mixlib::ShellOut + def test_modify(expected_cmds) yield @@ -43,7 +46,7 @@ module Chef::RSpec end shared_examples "action on non-existent resource" do |action, cmd, expected_error| - include Chef::RSpec::Pacemaker::CIBObject + include Chef::RSpec::Mixlib::ShellOut it "should not attempt to #{action.to_s} a non-existent resource" do stub_shellout("") diff --git a/spec/helpers/runnable_resource.rb b/spec/helpers/runnable_resource.rb index c527e2d..5695d35 100644 --- a/spec/helpers/runnable_resource.rb +++ b/spec/helpers/runnable_resource.rb @@ -4,7 +4,8 @@ # and stopped) but constraints cannot. this_dir = File.dirname(__FILE__) -require File.expand_path('../helpers/provider', this_dir) +require File.expand_path('provider', this_dir) +require File.expand_path('shellout', this_dir) shared_examples "a runnable resource" do |fixture| def expect_running(running) @@ -13,6 +14,8 @@ shared_examples "a runnable resource" do |fixture| .and_return(running) end + include Chef::RSpec::Mixlib::ShellOut + describe ":delete action" do it_should_behave_like "action on non-existent resource", \ :delete, "crm configure delete #{fixture.name}", nil diff --git a/spec/helpers/shellout.rb b/spec/helpers/shellout.rb new file mode 100644 index 0000000..ffc4e93 --- /dev/null +++ b/spec/helpers/shellout.rb @@ -0,0 +1,68 @@ +require 'mixlib/shellout' + +module Chef::RSpec + module Mixlib + module ShellOut + # Return a Mixlib::ShellOut double which mimics successful + # execution of a command, returning the given string on STDOUT. + def succeeding_shellout_double(string) + shellout = double(Mixlib::ShellOut) + shellout.stub(:environment).and_return({}) + shellout.stub(:run_command) + shellout.stub(:error!) + expect(shellout).to receive(:stdout).and_return(string) + shellout + end + + # Return a Mixlib::ShellOut double which mimics failed + # execution of a command, raising an exception when #error! is + # called. We expect #error! to be called, because if it isn't, + # that probably indicates the code isn't robust enough. This + # may need to be relaxed in the future. + def failing_shellout_double(stdout='', stderr='', exitstatus=1) + shellout = double(Mixlib::ShellOut) + shellout.stub(:environment).and_return({}) + shellout.stub(:run_command) + shellout.stub(:stdout).and_return(stdout) + shellout.stub(:stderr).and_return(stderr) + shellout.stub(:exitstatus).and_return(exitstatus) + exception = ::Mixlib::ShellOut::ShellCommandFailed.new( + "Expected process to exit with 0, " + + "but received '#{exitstatus}'" + ) + expect(shellout).to receive(:error!).and_raise(exception) + shellout + end + + # This stubs Mixlib::ShellOut.new with a sequence of doubles + # with a corresponding sequence of behaviours. This allows us + # to simulate the output of a series of shell commands being run + # via Mixlib::ShellOut. Each double either mimics a successful + # command execution whose #stdout method returns the given + # string, or a failed execution with the given exit code and + # STDOUT/STDERR. + # + # results is an Array describing the sequence of behaviours; + # each element is either a string mimicking STDOUT from + # successful command execution, or a [stdout, stderr, exitcode] + # status mimicking command execution failure. + # + # For example, "crm configure show" is executed by + # #load_current_resource, and again later on for the :create + # action, to see whether to create or modify. So the first + # double in the sequence would return an empty definition if we + # wanted to test creation of a new CIB object, or an existing + # definition if we wanted to test modification of an existing + # one. If the test needs subsequent doubles to return different + # values then stdout_strings can have more than one element. + def stub_shellout(*results) + doubles = results.map { |result| + result.is_a?(String) ? + succeeding_shellout_double(result) + : failing_shellout_double(*result) + } + ::Mixlib::ShellOut.stub(:new).and_return(*doubles) + end + end + end +end diff --git a/spec/providers/clone_spec.rb b/spec/providers/clone_spec.rb index fb4f4f6..4431d1b 100644 --- a/spec/providers/clone_spec.rb +++ b/spec/providers/clone_spec.rb @@ -27,8 +27,6 @@ describe "Chef::Provider::PacemakerClone" do Pacemaker::Resource::Clone end - include Chef::RSpec::Pacemaker::CIBObject - describe ":create action" do include Chef::RSpec::Pacemaker::CIBObject diff --git a/spec/providers/colocation_spec.rb b/spec/providers/colocation_spec.rb index 2bd2e2f..541c07b 100644 --- a/spec/providers/colocation_spec.rb +++ b/spec/providers/colocation_spec.rb @@ -27,8 +27,6 @@ describe "Chef::Provider::PacemakerColocation" do Pacemaker::Constraint::Colocation end - include Chef::RSpec::Pacemaker::CIBObject - describe ":create action" do include Chef::RSpec::Pacemaker::CIBObject diff --git a/spec/providers/group_spec.rb b/spec/providers/group_spec.rb index c6b34b9..c9ba86e 100644 --- a/spec/providers/group_spec.rb +++ b/spec/providers/group_spec.rb @@ -27,8 +27,6 @@ describe "Chef::Provider::PacemakerGroup" do Pacemaker::Resource::Group end - include Chef::RSpec::Pacemaker::CIBObject - describe ":create action" do include Chef::RSpec::Pacemaker::CIBObject diff --git a/spec/providers/location_spec.rb b/spec/providers/location_spec.rb index 7783b0d..80566c8 100644 --- a/spec/providers/location_spec.rb +++ b/spec/providers/location_spec.rb @@ -27,8 +27,6 @@ describe "Chef::Provider::PacemakerLocation" do Pacemaker::Constraint::Location end - include Chef::RSpec::Pacemaker::CIBObject - describe ":create action" do include Chef::RSpec::Pacemaker::CIBObject diff --git a/spec/providers/ms_spec.rb b/spec/providers/ms_spec.rb index 3fdfbbd..45aca27 100644 --- a/spec/providers/ms_spec.rb +++ b/spec/providers/ms_spec.rb @@ -27,8 +27,6 @@ describe "Chef::Provider::PacemakerMs" do Pacemaker::Resource::MasterSlave end - include Chef::RSpec::Pacemaker::CIBObject - describe ":create action" do include Chef::RSpec::Pacemaker::CIBObject diff --git a/spec/providers/primitive_spec.rb b/spec/providers/primitive_spec.rb index b264fc8..8b7c528 100644 --- a/spec/providers/primitive_spec.rb +++ b/spec/providers/primitive_spec.rb @@ -27,10 +27,9 @@ describe "Chef::Provider::PacemakerPrimitive" do Pacemaker::Resource::Primitive end - include Chef::RSpec::Pacemaker::CIBObject - describe ":create action" do include Chef::RSpec::Pacemaker::CIBObject + include Chef::RSpec::Mixlib::ShellOut it "should modify the primitive if it has different params" do expected_configure_cmd_args = [