From 16144d7c2aa3872aa884a245a652ada3e1ca051e Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Mon, 30 Jan 2023 14:03:26 +0900 Subject: [PATCH] Replace mocha by rspec-mocks puppetlabs_spec_helper recommends rspec-mocks instead of mocha[1] and it uses rspec-mocks by default instead of mocha since v 5.0.0[2] This is the prep work to adapt to that migration. [1] https://github.com/puppetlabs/puppetlabs_spec_helper/#mock_with [2] https://github.com/puppetlabs/puppetlabs_spec_helper/commit/493f0cbc1c96a3fa67591f3936430a70af74847f Closes-Bug: #2004135 Change-Id: Ibaad33d7113c9ea9da77786647da6f615980faff --- spec/spec_helper.rb | 2 + .../puppet/provider/pacemaker_common_spec.rb | 28 +-- .../puppet/provider/service/pacemaker_spec.rb | 183 +++++++++--------- 3 files changed, 106 insertions(+), 107 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4a5c46c..e90909f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,8 @@ RSpec.configure do |c| c.module_path = File.join(fixture_path, 'modules') c.manifest_dir = File.join(fixture_path, 'manifests') + + c.mock_with :rspec end at_exit { RSpec::Puppet::Coverage.report! } diff --git a/spec/unit/puppet/provider/pacemaker_common_spec.rb b/spec/unit/puppet/provider/pacemaker_common_spec.rb index 388274e..bf2aa8f 100644 --- a/spec/unit/puppet/provider/pacemaker_common_spec.rb +++ b/spec/unit/puppet/provider/pacemaker_common_spec.rb @@ -44,8 +44,8 @@ describe Puppet::Provider::Pacemaker_common do before(:each) do @class = subject - @class.stubs(:raw_cib).returns raw_cib - @class.stubs(:pcs).returns true + allow(@class).to receive(:raw_cib).and_return raw_cib + allow(@class).to receive(:pcs).and_return true end context 'configuration parser' do @@ -162,29 +162,29 @@ describe Puppet::Provider::Pacemaker_common do context 'cluster control' do it 'can enable maintenance mode' do - @class.expects(:pcs).with 'property', 'set', 'maintenance-mode=true' + expect(@class).to receive(:pcs).with 'property', 'set', 'maintenance-mode=true' @class.maintenance_mode 'true' end it 'can disable maintenance mode' do - @class.expects(:pcs).with 'property', 'set', 'maintenance-mode=false' + expect(@class).to receive(:pcs).with 'property', 'set', 'maintenance-mode=false' @class.maintenance_mode 'false' end it 'can set no-quorum policy' do - @class.expects(:pcs).with 'property', 'set', 'no-quorum-policy=ignore' + expect(@class).to receive(:pcs).with 'property', 'set', 'no-quorum-policy=ignore' @class.no_quorum_policy 'ignore' end end context 'constraints control' do it 'can add location constraint' do - @class.expects(:cibadmin).returns(true) + expect(@class).to receive(:cibadmin).and_return(true) @class.constraint_location_add 'myprimitive', 'mynode', '200' end it 'can remove location constraint' do - @class.expects(:pcs).with 'constraint', 'location', 'remove', 'myprimitive_on_mynode' + expect(@class).to receive(:pcs).with 'constraint', 'location', 'remove', 'myprimitive_on_mynode' @class.constraint_location_remove 'myprimitive', 'mynode' end end @@ -195,25 +195,25 @@ describe Puppet::Provider::Pacemaker_common do end it 'waits for Pacemaker to become ready' do - @class.stubs(:is_online?).returns true + allow(@class).to receive(:is_online?).and_return true @class.wait_for_online end it 'waits for status to become known' do - @class.stubs(:cib_reset).returns true - @class.stubs(:primitive_status).returns 'stopped' + allow(@class).to receive(:cib_reset).and_return true + allow(@class).to receive(:primitive_status).and_return 'stopped' @class.wait_for_status 'myprimitive' end it 'waits for the service to start' do - @class.stubs(:cib_reset).returns true - @class.stubs(:primitive_is_running?).with('myprimitive', nil).returns true + allow(@class).to receive(:cib_reset).and_return true + allow(@class).to receive(:primitive_is_running?).with('myprimitive', nil).and_return true @class.wait_for_start 'myprimitive' end it 'waits for the service to stop' do - @class.stubs(:cib_reset).returns true - @class.stubs(:primitive_is_running?).with('myprimitive', nil).returns false + allow(@class).to receive(:cib_reset).and_return true + allow(@class).to receive(:primitive_is_running?).with('myprimitive', nil).and_return false @class.wait_for_stop 'myprimitive' end end diff --git a/spec/unit/puppet/provider/service/pacemaker_spec.rb b/spec/unit/puppet/provider/service/pacemaker_spec.rb index 9474d4e..8b4d068 100644 --- a/spec/unit/puppet/provider/service/pacemaker_spec.rb +++ b/spec/unit/puppet/provider/service/pacemaker_spec.rb @@ -13,78 +13,78 @@ describe Puppet::Type.type(:service).provider(:pacemaker) do before :each do @class = provider - @class.stubs(:title).returns(title) - @class.stubs(:hostname).returns(hostname) - @class.stubs(:name).returns(name) - @class.stubs(:full_name).returns(full_name) - @class.stubs(:basic_service_name).returns(title) - @class.stubs(:primitive_class).returns(primitive_class) + allow(@class).to receive(:title).and_return(title) + allow(@class).to receive(:hostname).and_return(hostname) + allow(@class).to receive(:name).and_return(name) + allow(@class).to receive(:full_name).and_return(full_name) + allow(@class).to receive(:basic_service_name).and_return(title) + allow(@class).to receive(:primitive_class).and_return(primitive_class) - @class.stubs(:cib_reset).returns(true) + allow(@class).to receive(:cib_reset).and_return(true) - @class.stubs(:wait_for_online).returns(true) - @class.stubs(:wait_for_status).returns(true) - @class.stubs(:wait_for_start).returns(true) - @class.stubs(:wait_for_stop).returns(true) + allow(@class).to receive(:wait_for_online).and_return(true) + allow(@class).to receive(:wait_for_status).and_return(true) + allow(@class).to receive(:wait_for_start).and_return(true) + allow(@class).to receive(:wait_for_stop).and_return(true) - @class.stubs(:disable_basic_service).returns(true) - @class.stubs(:get_primitive_puppet_status).returns(:started) - @class.stubs(:get_primitive_puppet_enable).returns(:true) + allow(@class).to receive(:disable_basic_service).and_return(true) + allow(@class).to receive(:get_primitive_puppet_status).and_return(:started) + allow(@class).to receive(:get_primitive_puppet_enable).and_return(:true) - @class.stubs(:primitive_is_managed?).returns(true) - @class.stubs(:primitive_is_running?).returns(true) - @class.stubs(:primitive_has_failures?).returns(false) - @class.stubs(:primitive_is_complex?).returns(false) - @class.stubs(:primitive_is_multistate?).returns(false) - @class.stubs(:primitive_is_clone?).returns(false) + allow(@class).to receive(:primitive_is_managed?).and_return(true) + allow(@class).to receive(:primitive_is_running?).and_return(true) + allow(@class).to receive(:primitive_has_failures?).and_return(false) + allow(@class).to receive(:primitive_is_complex?).and_return(false) + allow(@class).to receive(:primitive_is_multistate?).and_return(false) + allow(@class).to receive(:primitive_is_clone?).and_return(false) - @class.stubs(:unban_primitive).returns(true) - @class.stubs(:ban_primitive).returns(true) - @class.stubs(:start_primitive).returns(true) - @class.stubs(:stop_primitive).returns(true) - @class.stubs(:cleanup_primitive).returns(true) - @class.stubs(:enable).returns(true) - @class.stubs(:disable).returns(true) + allow(@class).to receive(:unban_primitive).and_return(true) + allow(@class).to receive(:ban_primitive).and_return(true) + allow(@class).to receive(:start_primitive).and_return(true) + allow(@class).to receive(:stop_primitive).and_return(true) + allow(@class).to receive(:cleanup_primitive).and_return(true) + allow(@class).to receive(:enable).and_return(true) + allow(@class).to receive(:disable).and_return(true) - @class.stubs(:constraint_location_add).returns(true) - @class.stubs(:constraint_location_remove).returns(true) + allow(@class).to receive(:constraint_location_add).and_return(true) + allow(@class).to receive(:constraint_location_remove).and_return(true) - @class.stubs(:get_cluster_debug_report).returns(true) + allow(@class).to receive(:get_cluster_debug_report).and_return(true) end context 'service name mangling' do it 'uses title as the service name if it is found in CIB' do - @class.unstub(:name) - @class.stubs(:primitive_exists?).with(title).returns(true) + allow(@class).to receive(:name).and_call_original + allow(@class).to receive(:primitive_exists?).with(title).and_return(true) expect(@class.name).to eq(title) end it 'uses "p_" prefix with name if found name with prefix' do - @class.unstub(:name) - @class.stubs(:primitive_exists?).with(title).returns(false) - @class.stubs(:primitive_exists?).with(name).returns(true) + allow(@class).to receive(:name).and_call_original + allow(@class).to receive(:primitive_exists?).with(title).and_return(false) + allow(@class).to receive(:primitive_exists?).with(name).and_return(true) expect(@class.name).to eq(name) end it 'uses name without "p_" to disable basic service' do - @class.stubs(:name).returns(name) + allow(@class).to receive(:name).and_return(name) expect(@class.basic_service_name).to eq(title) end end context '#status' do it 'should wait for pacemaker to become online' do - @class.expects(:wait_for_online) + expect(@class).to receive(:wait_for_online) @class.status end it 'should reset cib mnemoization on every call' do - @class.expects(:cib_reset) + expect(@class).to receive(:cib_reset) @class.status end it 'gets service status locally' do - @class.expects(:get_primitive_puppet_status).with name, hostname + expect(@class).to receive(:get_primitive_puppet_status).with name, hostname @class.status end @@ -92,139 +92,136 @@ describe Puppet::Type.type(:service).provider(:pacemaker) do context '#start' do it 'tries to enable service if it is not enabled to work with it' do - @class.stubs(:primitive_is_managed?).returns(false) - @class.expects(:enable).once + allow(@class).to receive(:primitive_is_managed?).and_return(false) + expect(@class).to receive(:enable).once @class.start - @class.stubs(:primitive_is_managed?).returns(true) - @class.unstub(:enable) - @class.expects(:enable).never + allow(@class).to receive(:primitive_is_managed?).and_return(true) + allow(@class).to receive(:enable).and_call_original + expect(@class).to receive(:enable).never @class.start end it 'tries to disable a basic service with the same name' do - @class.expects(:disable_basic_service) + expect(@class).to receive(:disable_basic_service) @class.start end it 'should cleanup a primitive' do - @class.stubs(:primitive_has_failures?).returns(true) - @class.expects(:cleanup_primitive).with(full_name, hostname).once + allow(@class).to receive(:primitive_has_failures?).and_return(true) + expect(@class).to receive(:cleanup_primitive).with(full_name, hostname).once @class.start end it 'tries to unban the service on the node by the name' do - @class.expects(:unban_primitive).with(name, hostname) + expect(@class).to receive(:unban_primitive).with(name, hostname) @class.start end it 'tries to start the service by its full name' do - @class.expects(:start_primitive).with(full_name) + expect(@class).to receive(:start_primitive).with(full_name) @class.start end it 'adds a location constraint for the service by its full_name' do - @class.expects(:constraint_location_add).with(full_name, hostname) + expect(@class).to receive(:constraint_location_add).with(full_name, hostname) @class.start end it 'waits for the service to start locally if primitive is clone' do - @class.stubs(:primitive_is_clone?).returns(true) - @class.stubs(:primitive_is_multistate?).returns(false) - @class.stubs(:primitive_is_complex?).returns(true) - @class.expects(:wait_for_start).with name + allow(@class).to receive(:primitive_is_clone?).and_return(true) + allow(@class).to receive(:primitive_is_multistate?).and_return(false) + allow(@class).to receive(:primitive_is_complex?).and_return(true) + expect(@class).to receive(:wait_for_start).with name @class.start end it 'waits for the service to start master anywhere if primitive is multistate' do - @class.stubs(:primitive_is_clone?).returns(false) - @class.stubs(:primitive_is_multistate?).returns(true) - @class.stubs(:primitive_is_complex?).returns(true) - @class.expects(:wait_for_master).with name + allow(@class).to receive(:primitive_is_clone?).and_return(false) + allow(@class).to receive(:primitive_is_multistate?).and_return(true) + allow(@class).to receive(:primitive_is_complex?).and_return(true) + expect(@class).to receive(:wait_for_master).with name @class.start end it 'waits for the service to start anywhere if primitive is simple' do - @class.stubs(:primitive_is_clone?).returns(false) - @class.stubs(:primitive_is_multistate?).returns(false) - @class.stubs(:primitive_is_complex?).returns(false) - @class.expects(:wait_for_start).with name + allow(@class).to receive(:primitive_is_clone?).and_return(false) + allow(@class).to receive(:primitive_is_multistate?).and_return(false) + allow(@class).to receive(:primitive_is_complex?).and_return(false) + expect(@class).to receive(:wait_for_start).with name @class.start end end context '#stop' do it 'tries to disable service if it is not enabled to work with it' do - @class.stubs(:primitive_is_managed?).returns(false) - @class.expects(:enable).once + allow(@class).to receive(:primitive_is_managed?).and_return(false) + expect(@class).to receive(:enable).once @class.stop - @class.stubs(:primitive_is_managed?).returns(true) - @class.unstub(:enable) - @class.expects(:enable).never + allow(@class).to receive(:primitive_is_managed?).and_return(true) + allow(@class).to receive(:enable).and_call_original + expect(@class).to receive(:enable).never @class.stop end it 'should cleanup a primitive on stop' do - @class.expects(:cleanup_primitive).with(full_name, hostname).once.once + expect(@class).to receive(:cleanup_primitive).with(full_name, hostname).once.once @class.stop end it 'uses Ban to stop the service and waits for it to stop locally if service is complex' do - @class.stubs(:primitive_is_complex?).returns(true) - @class.expects(:wait_for_stop).with name, hostname - @class.expects(:ban_primitive).with name, hostname + allow(@class).to receive(:primitive_is_complex?).and_return(true) + expect(@class).to receive(:wait_for_stop).with name, hostname + expect(@class).to receive(:ban_primitive).with name, hostname @class.stop end it 'uses Stop to stop the service and waits for it to stop globally if service is simple' do - @class.stubs(:primitive_is_complex?).returns(false) - @class.expects(:wait_for_stop).with name - @class.expects(:stop_primitive).with name + allow(@class).to receive(:primitive_is_complex?).and_return(false) + expect(@class).to receive(:wait_for_stop).with name + expect(@class).to receive(:stop_primitive).with name @class.stop end end context '#restart' do it 'does not stop or start the service if it is not locally running' do - @class.stubs(:primitive_is_running?).with(name, hostname).returns(false) - @class.unstub(:stop) - @class.unstub(:start) - @class.expects(:stop).never - @class.expects(:start).never + allow(@class).to receive(:primitive_is_running?).with(name, hostname).and_return(false) + expect(@class).to receive(:stop).never + expect(@class).to receive(:start).never @class.restart end it 'stops and start the service if it is locally running' do - @class.stubs(:primitive_is_running?).with(name, hostname).returns(true) - restart_sequence = sequence('restart') - @class.expects(:stop).in_sequence(restart_sequence) - @class.expects(:start).in_sequence(restart_sequence) + allow(@class).to receive(:primitive_is_running?).with(name, hostname).and_return(true) + expect(@class).to receive(:stop).ordered + expect(@class).to receive(:start).ordered @class.restart end end context 'basic service handling' do before :each do - @class.unstub(:disable_basic_service) - @class.extra_provider.stubs(:enableable?).returns true - @class.extra_provider.stubs(:enabled?).returns :true - @class.extra_provider.stubs(:disable).returns true - @class.extra_provider.stubs(:stop).returns true - @class.extra_provider.stubs(:status).returns :running + allow(@class).to receive(:disable_basic_service).and_call_original + allow(@class.extra_provider).to receive(:enableable?).and_return true + allow(@class.extra_provider).to receive(:enabled?).and_return :true + allow(@class.extra_provider).to receive(:disable).and_return true + allow(@class.extra_provider).to receive(:stop).and_return true + allow(@class.extra_provider).to receive(:status).and_return :running end it 'tries to disable the basic service if it is enabled' do - @class.extra_provider.expects(:disable) + expect(@class.extra_provider).to receive(:disable) @class.disable_basic_service end it 'tries to stop the service if it is running' do - @class.extra_provider.expects(:stop) + expect(@class.extra_provider).to receive(:stop) @class.disable_basic_service end it 'does not try to stop a systemd running service' do - @class.stubs(:primitive_class).returns('systemd') - @class.extra_provider.expects(:stop).never + allow(@class).to receive(:primitive_class).and_return('systemd') + expect(@class.extra_provider).to receive(:stop).never @class.disable_basic_service end end