From eaf7c98ae4aa4e712ece73bd3ddfe9706a6c660e Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Mon, 30 Jan 2023 12:38:05 +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: I273711da547446f2c37be84656d0e3be2a3f9ec7 --- spec/spec_helper.rb | 2 + .../facter/os_workers_heat_engine_spec.rb | 6 +- spec/unit/facter/os_workers_keystone_spec.rb | 6 +- spec/unit/facter/os_workers_large_spec.rb | 4 +- spec/unit/facter/os_workers_small_spec.rb | 6 +- spec/unit/facter/os_workers_spec.rb | 6 +- spec/unit/provider/openstack/auth_spec.rb | 112 +++++++++--------- spec/unit/provider/openstack_spec.rb | 84 ++++++------- .../provider/policy_rcd/policy_rcd_spec.rb | 92 +++++++------- spec/unit/puppet/util/openstackconfig_spec.rb | 2 +- 10 files changed, 161 insertions(+), 159 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4a5c46c3..e90909f8 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/facter/os_workers_heat_engine_spec.rb b/spec/unit/facter/os_workers_heat_engine_spec.rb index 8b95739b..b891b98d 100644 --- a/spec/unit/facter/os_workers_heat_engine_spec.rb +++ b/spec/unit/facter/os_workers_heat_engine_spec.rb @@ -6,7 +6,7 @@ describe 'os_workers_heat_engine' do context 'with processorcount=1' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 1}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1}) end it 'returns a minimum of 2' do @@ -16,7 +16,7 @@ describe 'os_workers_heat_engine' do context 'with processorcount=8' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 8}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8}) end it 'returns processorcount/2' do @@ -26,7 +26,7 @@ describe 'os_workers_heat_engine' do context 'with processorcount=64' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 64}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 64}) end it 'returns a maximum of 24' do diff --git a/spec/unit/facter/os_workers_keystone_spec.rb b/spec/unit/facter/os_workers_keystone_spec.rb index 84209c14..75543ad7 100644 --- a/spec/unit/facter/os_workers_keystone_spec.rb +++ b/spec/unit/facter/os_workers_keystone_spec.rb @@ -6,7 +6,7 @@ describe 'os_workers_keystone' do context 'with processorcount=1' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 1}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1}) end it 'returns a minimum of 4' do @@ -16,7 +16,7 @@ describe 'os_workers_keystone' do context 'with processorcount=8' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 8}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8}) end it 'returns processorcount' do @@ -26,7 +26,7 @@ describe 'os_workers_keystone' do context 'with processorcount=32' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 32}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 32}) end it 'returns a maximum of 24' do diff --git a/spec/unit/facter/os_workers_large_spec.rb b/spec/unit/facter/os_workers_large_spec.rb index 4ec239f7..0fedc849 100644 --- a/spec/unit/facter/os_workers_large_spec.rb +++ b/spec/unit/facter/os_workers_large_spec.rb @@ -6,7 +6,7 @@ describe 'os_workers_large' do context 'with processorcount=1' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 1}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1}) end it 'returns a minimum of 1' do @@ -16,7 +16,7 @@ describe 'os_workers_large' do context 'with processorcount=8' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 8}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8}) end it 'returns processorcount/2' do diff --git a/spec/unit/facter/os_workers_small_spec.rb b/spec/unit/facter/os_workers_small_spec.rb index e4cd22f7..a698edd5 100644 --- a/spec/unit/facter/os_workers_small_spec.rb +++ b/spec/unit/facter/os_workers_small_spec.rb @@ -6,7 +6,7 @@ describe 'os_workers_small' do context 'with processorcount=1' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 1}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1}) end it 'returns a minimum of 2' do @@ -16,7 +16,7 @@ describe 'os_workers_small' do context 'with processorcount=16' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 16}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 16}) end it 'returns processorcount/4' do @@ -26,7 +26,7 @@ describe 'os_workers_small' do context 'with processorcount=32' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 32}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 32}) end it 'returns a maximum of 8' do diff --git a/spec/unit/facter/os_workers_spec.rb b/spec/unit/facter/os_workers_spec.rb index 131e3221..e5d70f71 100644 --- a/spec/unit/facter/os_workers_spec.rb +++ b/spec/unit/facter/os_workers_spec.rb @@ -6,7 +6,7 @@ describe 'os_workers' do context 'with processorcount=1' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 1}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1}) end it 'returns a minimum of 2' do @@ -16,7 +16,7 @@ describe 'os_workers' do context 'with processorcount=8' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 8}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8}) end it 'returns processorcount/2' do @@ -26,7 +26,7 @@ describe 'os_workers' do context 'with processorcount=32' do before do - Facter.fact(:processors).stubs(:value).returns({'count' => 32}) + allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 32}) end it 'returns a maximum of 12' do diff --git a/spec/unit/provider/openstack/auth_spec.rb b/spec/unit/provider/openstack/auth_spec.rb index 53988703..6f33d35e 100644 --- a/spec/unit/provider/openstack/auth_spec.rb +++ b/spec/unit/provider/openstack/auth_spec.rb @@ -88,7 +88,7 @@ describe Puppet::Provider::Openstack::Auth do describe '#get_os_vars_from_cloudsfile' do context 'with a clouds.yaml present' do it 'provides a hash' do - File.expects(:exists?).with('/etc/openstack/puppet/clouds.yaml').returns(true) + expect(File).to receive(:exists?).with('/etc/openstack/puppet/clouds.yaml').and_return(true) response = klass.get_os_vars_from_cloudsfile('project') expect(response).to eq({ @@ -100,8 +100,8 @@ describe Puppet::Provider::Openstack::Auth do context 'with a admin-clouds.yaml present' do it 'provides a hash' do - File.expects(:exists?).with('/etc/openstack/puppet/clouds.yaml').returns(false) - File.expects(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').returns(true) + expect(File).to receive(:exists?).with('/etc/openstack/puppet/clouds.yaml').and_return(false) + expect(File).to receive(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').and_return(true) response = klass.get_os_vars_from_cloudsfile('project') expect(response).to eq({ @@ -113,8 +113,8 @@ describe Puppet::Provider::Openstack::Auth do context 'with a clouds.yaml not present' do it 'provides an empty hash' do - File.expects(:exists?).with('/etc/openstack/puppet/clouds.yaml').returns(false) - File.expects(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').returns(false) + expect(File).to receive(:exists?).with('/etc/openstack/puppet/clouds.yaml').and_return(false) + expect(File).to receive(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').and_return(false) response = klass.get_os_vars_from_cloudsfile('project') expect(response).to eq({}) @@ -125,10 +125,10 @@ describe Puppet::Provider::Openstack::Auth do describe '#get_os_vars_from_rcfile' do context 'with a valid RC file' do it 'provides a hash' do - mock = "export OS_USERNAME='test'\nexport OS_PASSWORD='abc123'\nexport OS_PROJECT_NAME='test'\nexport OS_AUTH_URL='http://127.0.0.1:5000'" + content = "export OS_USERNAME='test'\nexport OS_PASSWORD='abc123'\nexport OS_PROJECT_NAME='test'\nexport OS_AUTH_URL='http://127.0.0.1:5000'" filename = 'file' - File.expects(:exists?).with('file').returns(true) - File.expects(:open).with('file').returns(StringIO.new(mock)) + expect(File).to receive(:exists?).with('file').and_return(true) + expect(File).to receive(:open).with('file').and_return(StringIO.new(content)) response = klass.get_os_vars_from_rcfile(filename) expect(response).to eq({ @@ -141,10 +141,10 @@ describe Puppet::Provider::Openstack::Auth do context 'with a valid RC file with extra code in it' do it 'provides a hash' do - mock = "export OS_USERNAME='test'\nexport OS_PASSWORD='abc123'\nexport OS_PROJECT_NAME='test'\nexport OS_AUTH_URL='http://127.0.0.1:5000'\n_openstack() {\n foo\n} " + content = "export OS_USERNAME='test'\nexport OS_PASSWORD='abc123'\nexport OS_PROJECT_NAME='test'\nexport OS_AUTH_URL='http://127.0.0.1:5000'\n_openstack() {\n foo\n} " filename = 'file' - File.expects(:exists?).with('file').returns(true) - File.expects(:open).with('file').returns(StringIO.new(mock)) + expect(File).to receive(:exists?).with('file').and_return(true) + expect(File).to receive(:open).with('file').and_return(StringIO.new(content)) response = klass.get_os_vars_from_rcfile(filename) expect(response).to eq({ @@ -158,8 +158,8 @@ describe Puppet::Provider::Openstack::Auth do context 'with an empty file' do it 'provides an empty hash' do filename = 'file' - File.expects(:exists?).with(filename).returns(true) - File.expects(:open).with(filename).returns(StringIO.new("")) + expect(File).to receive(:exists?).with(filename).and_return(true) + expect(File).to receive(:open).with(filename).and_return(StringIO.new("")) response = klass.get_os_vars_from_rcfile(filename) expect(response).to eq({}) @@ -169,12 +169,12 @@ describe Puppet::Provider::Openstack::Auth do context 'with a nonexistent file' do it 'should get default rcfile when no environment or openrc file' do ENV.clear - mock = "export OS_USERNAME='user'\nexport OS_PASSWORD='secret'\nexport OS_PROJECT_NAME='project'\nexport OS_AUTH_URL='http://127.0.0.1:5000'" + content = "export OS_USERNAME='user'\nexport OS_PASSWORD='secret'\nexport OS_PROJECT_NAME='project'\nexport OS_AUTH_URL='http://127.0.0.1:5000'" filename = '/root/openrc' - File.expects(:exists?).with("#{ENV['HOME']}/openrc").returns(false) - File.expects(:exists?).with(filename).returns(true) - File.expects(:open).with(filename).returns(StringIO.new(mock)) + expect(File).to receive(:exists?).with("#{ENV['HOME']}/openrc").and_return(false) + expect(File).to receive(:exists?).with(filename).and_return(true) + expect(File).to receive(:open).with(filename).and_return(StringIO.new(content)) expect(klass.get_os_vars_from_rcfile("#{ENV['HOME']}/openrc")).to eq({ 'OS_USERNAME' => 'user', @@ -202,15 +202,15 @@ describe Puppet::Provider::Openstack::Auth do context 'with user credentials in env' do it 'is successful' do - klass.expects(:get_os_vars_from_env) - .returns({ 'OS_USERNAME' => 'test', - 'OS_PASSWORD' => 'abc123', - 'OS_PROJECT_NAME' => 'test', - 'OS_AUTH_URL' => 'http://127.0.0.1:5000', - 'OS_NOT_VALID' => 'notvalid' }) - klass.expects(:openstack) + expect(klass).to receive(:get_os_vars_from_env) + .and_return({ 'OS_USERNAME' => 'test', + 'OS_PASSWORD' => 'abc123', + 'OS_PROJECT_NAME' => 'test', + 'OS_AUTH_URL' => 'http://127.0.0.1:5000', + 'OS_NOT_VALID' => 'notvalid' }) + expect(klass).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .returns('"ID","Name","Description","Enabled" + .and_return('"ID","Name","Description","Enabled" "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True ') response = klass.request('project', 'list', ['--long']) @@ -227,13 +227,13 @@ describe Puppet::Provider::Openstack::Auth do context 'with service token credentials in env' do it 'is successful' do - klass.expects(:get_os_vars_from_env) - .returns({ 'OS_TOKEN' => 'test', - 'OS_ENDPOINT' => 'http://127.0.0.1:5000', - 'OS_NOT_VALID' => 'notvalid' }) - klass.expects(:openstack) + expect(klass).to receive(:get_os_vars_from_env) + .and_return({ 'OS_TOKEN' => 'test', + 'OS_ENDPOINT' => 'http://127.0.0.1:5000', + 'OS_NOT_VALID' => 'notvalid' }) + expect(klass).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .returns('"ID","Name","Description","Enabled" + .and_return('"ID","Name","Description","Enabled" "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True ') response = klass.request('project', 'list', ['--long']) @@ -249,13 +249,13 @@ describe Puppet::Provider::Openstack::Auth do context 'with clouds.yaml file' do it 'is successful' do # return incomplete creds from env - klass.expects(:get_os_vars_from_env) - .returns({ 'OS_USERNAME' => 'incompleteusername', - 'OS_AUTH_URL' => 'incompleteauthurl' }) - File.expects(:exists?).with('/etc/openstack/puppet/clouds.yaml').returns(true) - klass.expects(:openstack) + expect(klass).to receive(:get_os_vars_from_env) + .and_return({ 'OS_USERNAME' => 'incompleteusername', + 'OS_AUTH_URL' => 'incompleteauthurl' }) + expect(File).to receive(:exists?).with('/etc/openstack/puppet/clouds.yaml').and_return(true) + expect(klass).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .returns('"ID","Name","Description","Enabled" + .and_return('"ID","Name","Description","Enabled" "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True ') response = provider.class.request('project', 'list', ['--long']) @@ -271,17 +271,17 @@ describe Puppet::Provider::Openstack::Auth do context 'with a RC file containing user credentials' do it 'is successful' do # return incomplete creds from env - klass.expects(:get_os_vars_from_env) - .returns({ 'OS_USERNAME' => 'incompleteusername', - 'OS_AUTH_URL' => 'incompleteauthurl' }) - mock = "export OS_USERNAME='test'\nexport OS_PASSWORD='abc123'\nexport OS_PROJECT_NAME='test'\nexport OS_AUTH_URL='http://127.0.0.1:5000'\nexport OS_NOT_VALID='notvalid'" - File.expects(:exists?).with("/etc/openstack/puppet/clouds.yaml").returns(false) - File.expects(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").returns(false) - File.expects(:exists?).with("#{ENV['HOME']}/openrc").returns(true) - File.expects(:open).with("#{ENV['HOME']}/openrc").returns(StringIO.new(mock)) - klass.expects(:openstack) + expect(klass).to receive(:get_os_vars_from_env) + .and_return({ 'OS_USERNAME' => 'incompleteusername', + 'OS_AUTH_URL' => 'incompleteauthurl' }) + content = "export OS_USERNAME='test'\nexport OS_PASSWORD='abc123'\nexport OS_PROJECT_NAME='test'\nexport OS_AUTH_URL='http://127.0.0.1:5000'\nexport OS_NOT_VALID='notvalid'" + expect(File).to receive(:exists?).with("/etc/openstack/puppet/clouds.yaml").and_return(false) + expect(File).to receive(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").and_return(false) + expect(File).to receive(:exists?).with("#{ENV['HOME']}/openrc").and_return(true) + expect(File).to receive(:open).with("#{ENV['HOME']}/openrc").and_return(StringIO.new(content)) + expect(klass).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .returns('"ID","Name","Description","Enabled" + .and_return('"ID","Name","Description","Enabled" "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True ') response = provider.class.request('project', 'list', ['--long']) @@ -299,16 +299,16 @@ describe Puppet::Provider::Openstack::Auth do context 'with a RC file containing service token credentials' do it 'is successful' do # return incomplete creds from env - klass.expects(:get_os_vars_from_env) - .returns({ 'OS_TOKEN' => 'incomplete' }) - mock = "export OS_TOKEN='test'\nexport OS_ENDPOINT='abc123'\nexport OS_NOT_VALID='notvalid'\n" - File.expects(:exists?).with("/etc/openstack/puppet/clouds.yaml").returns(false) - File.expects(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").returns(false) - File.expects(:exists?).with("#{ENV['HOME']}/openrc").returns(true) - File.expects(:open).with("#{ENV['HOME']}/openrc").returns(StringIO.new(mock)) - klass.expects(:openstack) + expect(klass).to receive(:get_os_vars_from_env) + .and_return({ 'OS_TOKEN' => 'incomplete' }) + content = "export OS_TOKEN='test'\nexport OS_ENDPOINT='abc123'\nexport OS_NOT_VALID='notvalid'\n" + expect(File).to receive(:exists?).with("/etc/openstack/puppet/clouds.yaml").and_return(false) + expect(File).to receive(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").and_return(false) + expect(File).to receive(:exists?).with("#{ENV['HOME']}/openrc").and_return(true) + expect(File).to receive(:open).with("#{ENV['HOME']}/openrc").and_return(StringIO.new(content)) + expect(klass).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .returns('"ID","Name","Description","Enabled" + .and_return('"ID","Name","Description","Enabled" "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True ') response = klass.request('project', 'list', ['--long']) diff --git a/spec/unit/provider/openstack_spec.rb b/spec/unit/provider/openstack_spec.rb index 145bd1f2..81b636aa 100644 --- a/spec/unit/provider/openstack_spec.rb +++ b/spec/unit/provider/openstack_spec.rb @@ -18,13 +18,13 @@ describe Puppet::Provider::Openstack do end let(:credentials) do - credentials = mock('credentials') - credentials.stubs(:to_env).returns({ - 'OS_USERNAME' => 'user', - 'OS_PASSWORD' => 'password', - 'OS_PROJECT_NAME' => 'project', - 'OS_AUTH_URL' => 'http://url', - }) + credentials = double('credentials') + allow(credentials).to receive(:to_env).and_return({ + 'OS_USERNAME' => 'user', + 'OS_PASSWORD' => 'password', + 'OS_PROJECT_NAME' => 'project', + 'OS_AUTH_URL' => 'http://url', + }) credentials end @@ -56,31 +56,31 @@ name="test" end it 'makes a successful list request' do - provider.class.expects(:openstack) + expect(provider.class).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .returns list_data + .and_return list_data response = Puppet::Provider::Openstack.request('project', 'list', ['--long']) expect(response.first[:description]).to eq 'Test tenant' end it 'makes a successful show request' do - provider.class.expects(:openstack) + expect(provider.class).to receive(:openstack) .with('project', 'show', '--format', 'shell', ['1cb05cfed7c24279be884ba4f6520262']) - .returns show_data + .and_return show_data response = Puppet::Provider::Openstack.request('project', 'show', ['1cb05cfed7c24279be884ba4f6520262']) expect(response[:description]).to eq 'Test tenant' end it 'makes a successful set request' do - provider.class.expects(:openstack) + expect(provider.class).to receive(:openstack) .with('project', 'set', ['--name', 'new name', '1cb05cfed7c24279be884ba4f6520262']) - .returns '' + .and_return '' response = Puppet::Provider::Openstack.request('project', 'set', ['--name', 'new name', '1cb05cfed7c24279be884ba4f6520262']) expect(response).to eq '' end it 'uses provided credentials' do - Puppet::Util.expects(:withenv).with(credentials.to_env) + expect(Puppet::Util).to receive(:withenv).with(credentials.to_env) Puppet::Provider::Openstack.request('project', 'list', ['--long'], credentials) end @@ -96,8 +96,8 @@ name="test" end it 'redacts password in execution output on exception' do - provider.class.stubs(:execute) - .raises(Puppet::ExecutionFailure, "Execution of '/usr/bin/openstack user create --format shell hello --password world --enable --email foo@example.com --domain Default' returned 1: command failed") + allow(provider.class).to receive(:execute) + .and_raise(Puppet::ExecutionFailure, "Execution of '/usr/bin/openstack user create --format shell hello --password world --enable --email foo@example.com --domain Default' returned 1: command failed") expect do Puppet::Provider::Openstack.request('user', 'create', ['hello', '--password', 'world', '--enable', '--email', 'foo@example.com', '--domain', 'Default']) end.to raise_error Puppet::ExecutionFailure, "Execution of '/usr/bin/openstack user create --format shell hello --password [redacted secret] --enable --email foo@example.com --domain Default' returned 1: command failed" @@ -105,48 +105,48 @@ name="test" context 'on connection errors' do it 'retries the failed command' do - provider.class.stubs(:openstack) + allow(provider.class).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .raises(Puppet::ExecutionFailure, 'Unable to establish connection') - .then - .returns list_data - provider.class.expects(:sleep).with(3).returns(nil) + .and_invoke( + lambda { |*args| raise Puppet::ExecutionFailure, 'Unable to establish connection' }, + lambda { |*args| return list_data } + ) + expect(provider.class).to receive(:sleep).with(3).and_return(nil) response = Puppet::Provider::Openstack.request('project', 'list', ['--long']) expect(response.first[:description]).to eq 'Test tenant' end it 'fails after the timeout and redacts' do - provider.class.expects(:execute) - .raises(Puppet::ExecutionFailure, "Execution of 'openstack user create foo --password secret' returned 1: command failed") - .times(3) - provider.class.stubs(:sleep) - provider.class.stubs(:current_time) - .returns(0, 10, 10, 20, 20, 200, 200) + expect(provider.class).to receive(:execute) + .and_raise(Puppet::ExecutionFailure, "Execution of 'openstack user create foo --password secret' returned 1: command failed") + .exactly(3).times + allow(provider.class).to receive(:sleep) + allow(provider.class).to receive(:current_time) + .and_return(0, 10, 10, 20, 20, 200, 200) expect do Puppet::Provider::Openstack.request('project', 'list', ['--long']) end.to raise_error Puppet::ExecutionFailure, /Execution of \'openstack user create foo --password \[redacted secret\]\' returned 1/ end it 'fails after the timeout' do - provider.class.expects(:openstack) + expect(provider.class).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .raises(Puppet::ExecutionFailure, 'Unable to establish connection') - .times(3) - provider.class.stubs(:sleep) - provider.class.stubs(:current_time) - .returns(0, 10, 10, 20, 20, 200, 200) + .and_raise(Puppet::ExecutionFailure, 'Unable to establish connection') + .exactly(3).times + allow(provider.class).to receive(:sleep) + allow(provider.class).to receive(:current_time) + .and_return(0, 10, 10, 20, 20, 200, 200) expect do Puppet::Provider::Openstack.request('project', 'list', ['--long']) end.to raise_error Puppet::ExecutionFailure, /Unable to establish connection/ end it 'does not retry non-idempotent commands' do - provider.class.expects(:openstack) + expect(provider.class).to receive(:openstack) .with('project', 'create', '--format', 'shell', ['--quiet']) - .raises(Puppet::ExecutionFailure, 'Unable to establish connection') - .then - .returns list_data - provider.class.expects(:sleep).never + .and_raise(Puppet::ExecutionFailure, 'Unable to establish connection') + .exactly(1).times + expect(provider.class).to receive(:sleep).never expect do Puppet::Provider::Openstack.request('project', 'create', ['--quiet']) end.to raise_error Puppet::ExecutionFailure, /Unable to establish connection/ @@ -160,18 +160,18 @@ name="test" ENV['OS_PASSWORD'] = 'abc123' ENV['OS_PROJECT_NAME'] = 'test' ENV['OS_AUTH_URL'] = 'http://127.0.0.1:5000' - provider.class.stubs(:openstack) + allow(provider.class).to receive(:openstack) .with('project', 'list', '--quiet', '--format', 'csv', ['--long']) - .raises(Puppet::ExecutionFailure, 'Could not find user: test (HTTP 401)') + .and_raise(Puppet::ExecutionFailure, 'Could not find user: test (HTTP 401)') expect do Puppet::Provider::Openstack.request('project', 'list', ['--long']) end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/) end it 'should raise an error with not authorized to perform' do - provider.class.stubs(:openstack) + allow(provider.class).to receive(:openstack) .with('role', 'list', '--quiet', '--format', 'csv', ['--long']) - .raises(Puppet::ExecutionFailure, 'You are not authorized to perform the requested action: identity:list_grants (HTTP 403)') + .and_raise(Puppet::ExecutionFailure, 'You are not authorized to perform the requested action: identity:list_grants (HTTP 403)') expect do Puppet::Provider::Openstack.request('role', 'list', ['--long']) end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/) diff --git a/spec/unit/provider/policy_rcd/policy_rcd_spec.rb b/spec/unit/provider/policy_rcd/policy_rcd_spec.rb index 396f96ab..e94097d3 100644 --- a/spec/unit/provider/policy_rcd/policy_rcd_spec.rb +++ b/spec/unit/provider/policy_rcd/policy_rcd_spec.rb @@ -27,109 +27,109 @@ describe provider_class do describe 'managing policy' do describe '#create' do it 'creates a policy when policy-rc.d doesnt exist' do - file = mock('file') - provider.stubs(:policy_rcd).returns(file) - File.expects(:exist?).with(file).returns(false) + file = double('file') + allow(provider).to receive(:policy_rcd).and_return(file) + expect(File).to receive(:exist?).with(file).and_return(false) content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" - provider.class.expects(:write_to_file).with(file, content) + expect(provider.class).to receive(:write_to_file).with(file, content) provider.create end it 'creates a policy when policy-rc.d exists' do - file = mock('file') - provider.stubs(:policy_rcd).returns(file) - File.expects(:exist?).with(file).returns(true) + file = double('file') + allow(provider).to receive(:policy_rcd).and_return(file) + expect(File).to receive(:exist?).with(file).and_return(true) content = "[[ \"$1\" == \"service\" ]] && exit 101\n" - provider.class.expects(:write_to_file).with(file, content) + expect(provider.class).to receive(:write_to_file).with(file, content) provider.create end end describe '#destroy' do it 'destroy a policy' do - file = mock('file') + file = double('file') file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" - provider.stubs(:policy_rcd).returns(file) - File.expects(:exist?).with(file).returns(true) - provider.stubs(:file_lines).returns(file_content.split("\n")) - provider.class.expects(:write_to_file).with(file, ['#!/bin/bash', '# THIS FILE MANAGED BY PUPPET'], true) + allow(provider).to receive(:policy_rcd).and_return(file) + expect(File).to receive(:exist?).with(file).and_return(true) + allow(provider).to receive(:file_lines).and_return(file_content.split("\n")) + expect(provider.class).to receive(:write_to_file).with(file, ['#!/bin/bash', '# THIS FILE MANAGED BY PUPPET'], true) provider.destroy end end describe '#flush' do it 'update a policy' do - file = mock('file') - provider.stubs(:policy_rcd).returns(file) + file = double('file') + allow(provider).to receive(:policy_rcd).and_return(file) file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 102\n" - provider.stubs(:file_lines).returns(file_content.split("\n")) - provider.class.expects(:write_to_file).with(file, ['#!/bin/bash', "# THIS FILE MANAGED BY PUPPET", "[[ \"$1\" == \"service\" ]] && exit 101\n"], true) + allow(provider).to receive(:file_lines).and_return(file_content.split("\n")) + expect(provider.class).to receive(:write_to_file).with(file, ['#!/bin/bash', "# THIS FILE MANAGED BY PUPPET", "[[ \"$1\" == \"service\" ]] && exit 101\n"], true) provider.flush end it 'dont update a policy' do - file = mock('file') + file = double('file') file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" - provider.stubs(:policy_rcd).returns(file) - provider.stubs(:file_lines).returns(file_content.split("\n")) + allow(provider).to receive(:policy_rcd).and_return(file) + allow(provider).to receive(:file_lines).and_return(file_content.split("\n")) provider.flush end end describe '#exists?' do it 'should exists on Debian family' do - provider.stubs(:check_os).returns(true) - file = mock('file') + allow(provider).to receive(:check_os).and_return(true) + file = double('file') file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" - provider.stubs(:policy_rcd).returns(file) - provider.stubs(:check_policy_rcd).returns(true) - provider.stubs(:file_lines).returns(file_content.split("\n")) + allow(provider).to receive(:policy_rcd).and_return(file) + allow(provider).to receive(:check_policy_rcd).and_return(true) + allow(provider).to receive(:file_lines).and_return(file_content.split("\n")) expect(provider.exists?).to be_truthy end it 'should not exists on Debian family when file is present' do - provider.stubs(:check_os).returns(true) - file = mock('file') + allow(provider).to receive(:check_os).and_return(true) + file = double('file') file_content = "#{header}[[ \"$1\" == \"new-service\" ]] && exit 101\n" - provider.stubs(:policy_rcd).returns(file) - provider.stubs(:check_policy_rcd).returns(true) - provider.stubs(:file_lines).returns(file_content.split("\n")) + allow(provider).to receive(:policy_rcd).and_return(file) + allow(provider).to receive(:check_policy_rcd).and_return(true) + allow(provider).to receive(:file_lines).and_return(file_content.split("\n")) expect(provider.exists?).to be_falsey end it 'should not exists on Debian family when file is not present' do - provider.stubs(:check_os).returns(true) - provider.stubs(:check_policy_rcd).returns(false) + allow(provider).to receive(:check_os).and_return(true) + allow(provider).to receive(:check_policy_rcd).and_return(false) expect(provider.exists?).to be_falsey end it 'should exists on non-Debian family' do - provider.stubs(:check_os).returns(false) + allow(provider).to receive(:check_os).and_return(false) expect(provider.exists?).to be_truthy end end describe 'write_to_file' do it 'should write to file' do - file = mock - policy = mock + file = double + policy = double content = 'some_content' - File.expects(:open).with(file, 'a+').returns(policy) - policy.expects(:puts).with(content) - policy.expects(:close) - File.expects(:chmod).with(0744, file) + expect(File).to receive(:open).with(file, 'a+').and_return(policy) + expect(policy).to receive(:puts).with(content) + expect(policy).to receive(:close) + expect(File).to receive(:chmod).with(0744, file) provider.class.write_to_file(file, content) end it 'should truncate file' do - file = mock - policy = mock + file = double + policy = double content = 'some_content' - File.expects(:truncate).with(file, 0) - File.expects(:open).with(file, 'a+').returns(policy) - policy.expects(:puts).with(content) - policy.expects(:close) - File.expects(:chmod).with(0744, file) + expect(File).to receive(:truncate).with(file, 0) + expect(File).to receive(:open).with(file, 'a+').and_return(policy) + expect(policy).to receive(:puts).with(content) + expect(policy).to receive(:close) + expect(File).to receive(:chmod).with(0744, file) provider.class.write_to_file(file, content, true) end end diff --git a/spec/unit/puppet/util/openstackconfig_spec.rb b/spec/unit/puppet/util/openstackconfig_spec.rb index 75794833..cd7f38d3 100644 --- a/spec/unit/puppet/util/openstackconfig_spec.rb +++ b/spec/unit/puppet/util/openstackconfig_spec.rb @@ -15,7 +15,7 @@ describe Puppet::Util::OpenStackConfig do end before :each do - Puppet::Util::OpenStackConfig.stubs(:readlines).returns(sample_content) + allow(Puppet::Util::OpenStackConfig).to receive(:readlines).and_return(sample_content) end context "when parsing a file" do