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] 493f0cbc1c

Closes-Bug: #2004135
Change-Id: Ibaad33d7113c9ea9da77786647da6f615980faff
This commit is contained in:
Takashi Kajinami 2023-01-30 14:03:26 +09:00
parent bb3d7c05e5
commit 16144d7c2a
3 changed files with 106 additions and 107 deletions

View File

@ -10,6 +10,8 @@ RSpec.configure do |c|
c.module_path = File.join(fixture_path, 'modules') c.module_path = File.join(fixture_path, 'modules')
c.manifest_dir = File.join(fixture_path, 'manifests') c.manifest_dir = File.join(fixture_path, 'manifests')
c.mock_with :rspec
end end
at_exit { RSpec::Puppet::Coverage.report! } at_exit { RSpec::Puppet::Coverage.report! }

View File

@ -44,8 +44,8 @@ describe Puppet::Provider::Pacemaker_common do
before(:each) do before(:each) do
@class = subject @class = subject
@class.stubs(:raw_cib).returns raw_cib allow(@class).to receive(:raw_cib).and_return raw_cib
@class.stubs(:pcs).returns true allow(@class).to receive(:pcs).and_return true
end end
context 'configuration parser' do context 'configuration parser' do
@ -162,29 +162,29 @@ describe Puppet::Provider::Pacemaker_common do
context 'cluster control' do context 'cluster control' do
it 'can enable maintenance mode' 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' @class.maintenance_mode 'true'
end end
it 'can disable maintenance mode' do 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' @class.maintenance_mode 'false'
end end
it 'can set no-quorum policy' do 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' @class.no_quorum_policy 'ignore'
end end
end end
context 'constraints control' do context 'constraints control' do
it 'can add location constraint' 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' @class.constraint_location_add 'myprimitive', 'mynode', '200'
end end
it 'can remove location constraint' do 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' @class.constraint_location_remove 'myprimitive', 'mynode'
end end
end end
@ -195,25 +195,25 @@ describe Puppet::Provider::Pacemaker_common do
end end
it 'waits for Pacemaker to become ready' do 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 @class.wait_for_online
end end
it 'waits for status to become known' do it 'waits for status to become known' do
@class.stubs(:cib_reset).returns true allow(@class).to receive(:cib_reset).and_return true
@class.stubs(:primitive_status).returns 'stopped' allow(@class).to receive(:primitive_status).and_return 'stopped'
@class.wait_for_status 'myprimitive' @class.wait_for_status 'myprimitive'
end end
it 'waits for the service to start' do it 'waits for the service to start' do
@class.stubs(:cib_reset).returns true allow(@class).to receive(:cib_reset).and_return true
@class.stubs(:primitive_is_running?).with('myprimitive', nil).returns true allow(@class).to receive(:primitive_is_running?).with('myprimitive', nil).and_return true
@class.wait_for_start 'myprimitive' @class.wait_for_start 'myprimitive'
end end
it 'waits for the service to stop' do it 'waits for the service to stop' do
@class.stubs(:cib_reset).returns true allow(@class).to receive(:cib_reset).and_return true
@class.stubs(:primitive_is_running?).with('myprimitive', nil).returns false allow(@class).to receive(:primitive_is_running?).with('myprimitive', nil).and_return false
@class.wait_for_stop 'myprimitive' @class.wait_for_stop 'myprimitive'
end end
end end

View File

@ -13,78 +13,78 @@ describe Puppet::Type.type(:service).provider(:pacemaker) do
before :each do before :each do
@class = provider @class = provider
@class.stubs(:title).returns(title) allow(@class).to receive(:title).and_return(title)
@class.stubs(:hostname).returns(hostname) allow(@class).to receive(:hostname).and_return(hostname)
@class.stubs(:name).returns(name) allow(@class).to receive(:name).and_return(name)
@class.stubs(:full_name).returns(full_name) allow(@class).to receive(:full_name).and_return(full_name)
@class.stubs(:basic_service_name).returns(title) allow(@class).to receive(:basic_service_name).and_return(title)
@class.stubs(:primitive_class).returns(primitive_class) 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) allow(@class).to receive(:wait_for_online).and_return(true)
@class.stubs(:wait_for_status).returns(true) allow(@class).to receive(:wait_for_status).and_return(true)
@class.stubs(:wait_for_start).returns(true) allow(@class).to receive(:wait_for_start).and_return(true)
@class.stubs(:wait_for_stop).returns(true) allow(@class).to receive(:wait_for_stop).and_return(true)
@class.stubs(:disable_basic_service).returns(true) allow(@class).to receive(:disable_basic_service).and_return(true)
@class.stubs(:get_primitive_puppet_status).returns(:started) allow(@class).to receive(:get_primitive_puppet_status).and_return(:started)
@class.stubs(:get_primitive_puppet_enable).returns(:true) allow(@class).to receive(:get_primitive_puppet_enable).and_return(:true)
@class.stubs(:primitive_is_managed?).returns(true) allow(@class).to receive(:primitive_is_managed?).and_return(true)
@class.stubs(:primitive_is_running?).returns(true) allow(@class).to receive(:primitive_is_running?).and_return(true)
@class.stubs(:primitive_has_failures?).returns(false) allow(@class).to receive(:primitive_has_failures?).and_return(false)
@class.stubs(:primitive_is_complex?).returns(false) allow(@class).to receive(:primitive_is_complex?).and_return(false)
@class.stubs(:primitive_is_multistate?).returns(false) allow(@class).to receive(:primitive_is_multistate?).and_return(false)
@class.stubs(:primitive_is_clone?).returns(false) allow(@class).to receive(:primitive_is_clone?).and_return(false)
@class.stubs(:unban_primitive).returns(true) allow(@class).to receive(:unban_primitive).and_return(true)
@class.stubs(:ban_primitive).returns(true) allow(@class).to receive(:ban_primitive).and_return(true)
@class.stubs(:start_primitive).returns(true) allow(@class).to receive(:start_primitive).and_return(true)
@class.stubs(:stop_primitive).returns(true) allow(@class).to receive(:stop_primitive).and_return(true)
@class.stubs(:cleanup_primitive).returns(true) allow(@class).to receive(:cleanup_primitive).and_return(true)
@class.stubs(:enable).returns(true) allow(@class).to receive(:enable).and_return(true)
@class.stubs(:disable).returns(true) allow(@class).to receive(:disable).and_return(true)
@class.stubs(:constraint_location_add).returns(true) allow(@class).to receive(:constraint_location_add).and_return(true)
@class.stubs(:constraint_location_remove).returns(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 end
context 'service name mangling' do context 'service name mangling' do
it 'uses title as the service name if it is found in CIB' do it 'uses title as the service name if it is found in CIB' do
@class.unstub(:name) allow(@class).to receive(:name).and_call_original
@class.stubs(:primitive_exists?).with(title).returns(true) allow(@class).to receive(:primitive_exists?).with(title).and_return(true)
expect(@class.name).to eq(title) expect(@class.name).to eq(title)
end end
it 'uses "p_" prefix with name if found name with prefix' do it 'uses "p_" prefix with name if found name with prefix' do
@class.unstub(:name) allow(@class).to receive(:name).and_call_original
@class.stubs(:primitive_exists?).with(title).returns(false) allow(@class).to receive(:primitive_exists?).with(title).and_return(false)
@class.stubs(:primitive_exists?).with(name).returns(true) allow(@class).to receive(:primitive_exists?).with(name).and_return(true)
expect(@class.name).to eq(name) expect(@class.name).to eq(name)
end end
it 'uses name without "p_" to disable basic service' do 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) expect(@class.basic_service_name).to eq(title)
end end
end end
context '#status' do context '#status' do
it 'should wait for pacemaker to become online' do it 'should wait for pacemaker to become online' do
@class.expects(:wait_for_online) expect(@class).to receive(:wait_for_online)
@class.status @class.status
end end
it 'should reset cib mnemoization on every call' do it 'should reset cib mnemoization on every call' do
@class.expects(:cib_reset) expect(@class).to receive(:cib_reset)
@class.status @class.status
end end
it 'gets service status locally' do 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 @class.status
end end
@ -92,139 +92,136 @@ describe Puppet::Type.type(:service).provider(:pacemaker) do
context '#start' do context '#start' do
it 'tries to enable service if it is not enabled to work with it' do it 'tries to enable service if it is not enabled to work with it' do
@class.stubs(:primitive_is_managed?).returns(false) allow(@class).to receive(:primitive_is_managed?).and_return(false)
@class.expects(:enable).once expect(@class).to receive(:enable).once
@class.start @class.start
@class.stubs(:primitive_is_managed?).returns(true) allow(@class).to receive(:primitive_is_managed?).and_return(true)
@class.unstub(:enable) allow(@class).to receive(:enable).and_call_original
@class.expects(:enable).never expect(@class).to receive(:enable).never
@class.start @class.start
end end
it 'tries to disable a basic service with the same name' do 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 @class.start
end end
it 'should cleanup a primitive' do it 'should cleanup a primitive' do
@class.stubs(:primitive_has_failures?).returns(true) allow(@class).to receive(:primitive_has_failures?).and_return(true)
@class.expects(:cleanup_primitive).with(full_name, hostname).once expect(@class).to receive(:cleanup_primitive).with(full_name, hostname).once
@class.start @class.start
end end
it 'tries to unban the service on the node by the name' do 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 @class.start
end end
it 'tries to start the service by its full name' do 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 @class.start
end end
it 'adds a location constraint for the service by its full_name' do 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 @class.start
end end
it 'waits for the service to start locally if primitive is clone' do it 'waits for the service to start locally if primitive is clone' do
@class.stubs(:primitive_is_clone?).returns(true) allow(@class).to receive(:primitive_is_clone?).and_return(true)
@class.stubs(:primitive_is_multistate?).returns(false) allow(@class).to receive(:primitive_is_multistate?).and_return(false)
@class.stubs(:primitive_is_complex?).returns(true) allow(@class).to receive(:primitive_is_complex?).and_return(true)
@class.expects(:wait_for_start).with name expect(@class).to receive(:wait_for_start).with name
@class.start @class.start
end end
it 'waits for the service to start master anywhere if primitive is multistate' do it 'waits for the service to start master anywhere if primitive is multistate' do
@class.stubs(:primitive_is_clone?).returns(false) allow(@class).to receive(:primitive_is_clone?).and_return(false)
@class.stubs(:primitive_is_multistate?).returns(true) allow(@class).to receive(:primitive_is_multistate?).and_return(true)
@class.stubs(:primitive_is_complex?).returns(true) allow(@class).to receive(:primitive_is_complex?).and_return(true)
@class.expects(:wait_for_master).with name expect(@class).to receive(:wait_for_master).with name
@class.start @class.start
end end
it 'waits for the service to start anywhere if primitive is simple' do it 'waits for the service to start anywhere if primitive is simple' do
@class.stubs(:primitive_is_clone?).returns(false) allow(@class).to receive(:primitive_is_clone?).and_return(false)
@class.stubs(:primitive_is_multistate?).returns(false) allow(@class).to receive(:primitive_is_multistate?).and_return(false)
@class.stubs(:primitive_is_complex?).returns(false) allow(@class).to receive(:primitive_is_complex?).and_return(false)
@class.expects(:wait_for_start).with name expect(@class).to receive(:wait_for_start).with name
@class.start @class.start
end end
end end
context '#stop' do context '#stop' do
it 'tries to disable service if it is not enabled to work with it' do it 'tries to disable service if it is not enabled to work with it' do
@class.stubs(:primitive_is_managed?).returns(false) allow(@class).to receive(:primitive_is_managed?).and_return(false)
@class.expects(:enable).once expect(@class).to receive(:enable).once
@class.stop @class.stop
@class.stubs(:primitive_is_managed?).returns(true) allow(@class).to receive(:primitive_is_managed?).and_return(true)
@class.unstub(:enable) allow(@class).to receive(:enable).and_call_original
@class.expects(:enable).never expect(@class).to receive(:enable).never
@class.stop @class.stop
end end
it 'should cleanup a primitive on stop' do 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 @class.stop
end end
it 'uses Ban to stop the service and waits for it to stop locally if service is complex' do 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) allow(@class).to receive(:primitive_is_complex?).and_return(true)
@class.expects(:wait_for_stop).with name, hostname expect(@class).to receive(:wait_for_stop).with name, hostname
@class.expects(:ban_primitive).with name, hostname expect(@class).to receive(:ban_primitive).with name, hostname
@class.stop @class.stop
end end
it 'uses Stop to stop the service and waits for it to stop globally if service is simple' do 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) allow(@class).to receive(:primitive_is_complex?).and_return(false)
@class.expects(:wait_for_stop).with name expect(@class).to receive(:wait_for_stop).with name
@class.expects(:stop_primitive).with name expect(@class).to receive(:stop_primitive).with name
@class.stop @class.stop
end end
end end
context '#restart' do context '#restart' do
it 'does not stop or start the service if it is not locally running' 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) allow(@class).to receive(:primitive_is_running?).with(name, hostname).and_return(false)
@class.unstub(:stop) expect(@class).to receive(:stop).never
@class.unstub(:start) expect(@class).to receive(:start).never
@class.expects(:stop).never
@class.expects(:start).never
@class.restart @class.restart
end end
it 'stops and start the service if it is locally running' do it 'stops and start the service if it is locally running' do
@class.stubs(:primitive_is_running?).with(name, hostname).returns(true) allow(@class).to receive(:primitive_is_running?).with(name, hostname).and_return(true)
restart_sequence = sequence('restart') expect(@class).to receive(:stop).ordered
@class.expects(:stop).in_sequence(restart_sequence) expect(@class).to receive(:start).ordered
@class.expects(:start).in_sequence(restart_sequence)
@class.restart @class.restart
end end
end end
context 'basic service handling' do context 'basic service handling' do
before :each do before :each do
@class.unstub(:disable_basic_service) allow(@class).to receive(:disable_basic_service).and_call_original
@class.extra_provider.stubs(:enableable?).returns true allow(@class.extra_provider).to receive(:enableable?).and_return true
@class.extra_provider.stubs(:enabled?).returns :true allow(@class.extra_provider).to receive(:enabled?).and_return :true
@class.extra_provider.stubs(:disable).returns true allow(@class.extra_provider).to receive(:disable).and_return true
@class.extra_provider.stubs(:stop).returns true allow(@class.extra_provider).to receive(:stop).and_return true
@class.extra_provider.stubs(:status).returns :running allow(@class.extra_provider).to receive(:status).and_return :running
end end
it 'tries to disable the basic service if it is enabled' do 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 @class.disable_basic_service
end end
it 'tries to stop the service if it is running' do 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 @class.disable_basic_service
end end
it 'does not try to stop a systemd running service' do it 'does not try to stop a systemd running service' do
@class.stubs(:primitive_class).returns('systemd') allow(@class).to receive(:primitive_class).and_return('systemd')
@class.extra_provider.expects(:stop).never expect(@class.extra_provider).to receive(:stop).never
@class.disable_basic_service @class.disable_basic_service
end end
end end