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: I273711da547446f2c37be84656d0e3be2a3f9ec7
This commit is contained in:
Takashi Kajinami
2023-01-30 12:38:05 +09:00
parent ee63d152c3
commit eaf7c98ae4
10 changed files with 161 additions and 159 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

@@ -6,7 +6,7 @@ describe 'os_workers_heat_engine' do
context 'with processorcount=1' do context 'with processorcount=1' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 1}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1})
end end
it 'returns a minimum of 2' do it 'returns a minimum of 2' do
@@ -16,7 +16,7 @@ describe 'os_workers_heat_engine' do
context 'with processorcount=8' do context 'with processorcount=8' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 8}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8})
end end
it 'returns processorcount/2' do it 'returns processorcount/2' do
@@ -26,7 +26,7 @@ describe 'os_workers_heat_engine' do
context 'with processorcount=64' do context 'with processorcount=64' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 64}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 64})
end end
it 'returns a maximum of 24' do it 'returns a maximum of 24' do

View File

@@ -6,7 +6,7 @@ describe 'os_workers_keystone' do
context 'with processorcount=1' do context 'with processorcount=1' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 1}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1})
end end
it 'returns a minimum of 4' do it 'returns a minimum of 4' do
@@ -16,7 +16,7 @@ describe 'os_workers_keystone' do
context 'with processorcount=8' do context 'with processorcount=8' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 8}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8})
end end
it 'returns processorcount' do it 'returns processorcount' do
@@ -26,7 +26,7 @@ describe 'os_workers_keystone' do
context 'with processorcount=32' do context 'with processorcount=32' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 32}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 32})
end end
it 'returns a maximum of 24' do it 'returns a maximum of 24' do

View File

@@ -6,7 +6,7 @@ describe 'os_workers_large' do
context 'with processorcount=1' do context 'with processorcount=1' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 1}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1})
end end
it 'returns a minimum of 1' do it 'returns a minimum of 1' do
@@ -16,7 +16,7 @@ describe 'os_workers_large' do
context 'with processorcount=8' do context 'with processorcount=8' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 8}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8})
end end
it 'returns processorcount/2' do it 'returns processorcount/2' do

View File

@@ -6,7 +6,7 @@ describe 'os_workers_small' do
context 'with processorcount=1' do context 'with processorcount=1' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 1}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1})
end end
it 'returns a minimum of 2' do it 'returns a minimum of 2' do
@@ -16,7 +16,7 @@ describe 'os_workers_small' do
context 'with processorcount=16' do context 'with processorcount=16' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 16}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 16})
end end
it 'returns processorcount/4' do it 'returns processorcount/4' do
@@ -26,7 +26,7 @@ describe 'os_workers_small' do
context 'with processorcount=32' do context 'with processorcount=32' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 32}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 32})
end end
it 'returns a maximum of 8' do it 'returns a maximum of 8' do

View File

@@ -6,7 +6,7 @@ describe 'os_workers' do
context 'with processorcount=1' do context 'with processorcount=1' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 1}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 1})
end end
it 'returns a minimum of 2' do it 'returns a minimum of 2' do
@@ -16,7 +16,7 @@ describe 'os_workers' do
context 'with processorcount=8' do context 'with processorcount=8' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 8}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 8})
end end
it 'returns processorcount/2' do it 'returns processorcount/2' do
@@ -26,7 +26,7 @@ describe 'os_workers' do
context 'with processorcount=32' do context 'with processorcount=32' do
before do before do
Facter.fact(:processors).stubs(:value).returns({'count' => 32}) allow(Facter.fact(:processors)).to receive(:value).and_return({'count' => 32})
end end
it 'returns a maximum of 12' do it 'returns a maximum of 12' do

View File

@@ -88,7 +88,7 @@ describe Puppet::Provider::Openstack::Auth do
describe '#get_os_vars_from_cloudsfile' do describe '#get_os_vars_from_cloudsfile' do
context 'with a clouds.yaml present' do context 'with a clouds.yaml present' do
it 'provides a hash' 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') response = klass.get_os_vars_from_cloudsfile('project')
expect(response).to eq({ expect(response).to eq({
@@ -100,8 +100,8 @@ describe Puppet::Provider::Openstack::Auth do
context 'with a admin-clouds.yaml present' do context 'with a admin-clouds.yaml present' do
it 'provides a hash' do it 'provides a hash' do
File.expects(:exists?).with('/etc/openstack/puppet/clouds.yaml').returns(false) expect(File).to receive(:exists?).with('/etc/openstack/puppet/clouds.yaml').and_return(false)
File.expects(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').returns(true) expect(File).to receive(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').and_return(true)
response = klass.get_os_vars_from_cloudsfile('project') response = klass.get_os_vars_from_cloudsfile('project')
expect(response).to eq({ expect(response).to eq({
@@ -113,8 +113,8 @@ describe Puppet::Provider::Openstack::Auth do
context 'with a clouds.yaml not present' do context 'with a clouds.yaml not present' do
it 'provides an empty hash' do it 'provides an empty hash' do
File.expects(:exists?).with('/etc/openstack/puppet/clouds.yaml').returns(false) expect(File).to receive(:exists?).with('/etc/openstack/puppet/clouds.yaml').and_return(false)
File.expects(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').returns(false) expect(File).to receive(:exists?).with('/etc/openstack/puppet/admin-clouds.yaml').and_return(false)
response = klass.get_os_vars_from_cloudsfile('project') response = klass.get_os_vars_from_cloudsfile('project')
expect(response).to eq({}) expect(response).to eq({})
@@ -125,10 +125,10 @@ describe Puppet::Provider::Openstack::Auth do
describe '#get_os_vars_from_rcfile' do describe '#get_os_vars_from_rcfile' do
context 'with a valid RC file' do context 'with a valid RC file' do
it 'provides a hash' 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' filename = 'file'
File.expects(:exists?).with('file').returns(true) expect(File).to receive(:exists?).with('file').and_return(true)
File.expects(:open).with('file').returns(StringIO.new(mock)) expect(File).to receive(:open).with('file').and_return(StringIO.new(content))
response = klass.get_os_vars_from_rcfile(filename) response = klass.get_os_vars_from_rcfile(filename)
expect(response).to eq({ 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 context 'with a valid RC file with extra code in it' do
it 'provides a hash' 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' filename = 'file'
File.expects(:exists?).with('file').returns(true) expect(File).to receive(:exists?).with('file').and_return(true)
File.expects(:open).with('file').returns(StringIO.new(mock)) expect(File).to receive(:open).with('file').and_return(StringIO.new(content))
response = klass.get_os_vars_from_rcfile(filename) response = klass.get_os_vars_from_rcfile(filename)
expect(response).to eq({ expect(response).to eq({
@@ -158,8 +158,8 @@ describe Puppet::Provider::Openstack::Auth do
context 'with an empty file' do context 'with an empty file' do
it 'provides an empty hash' do it 'provides an empty hash' do
filename = 'file' filename = 'file'
File.expects(:exists?).with(filename).returns(true) expect(File).to receive(:exists?).with(filename).and_return(true)
File.expects(:open).with(filename).returns(StringIO.new("")) expect(File).to receive(:open).with(filename).and_return(StringIO.new(""))
response = klass.get_os_vars_from_rcfile(filename) response = klass.get_os_vars_from_rcfile(filename)
expect(response).to eq({}) expect(response).to eq({})
@@ -169,12 +169,12 @@ describe Puppet::Provider::Openstack::Auth do
context 'with a nonexistent file' do context 'with a nonexistent file' do
it 'should get default rcfile when no environment or openrc file' do it 'should get default rcfile when no environment or openrc file' do
ENV.clear 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' filename = '/root/openrc'
File.expects(:exists?).with("#{ENV['HOME']}/openrc").returns(false) expect(File).to receive(:exists?).with("#{ENV['HOME']}/openrc").and_return(false)
File.expects(:exists?).with(filename).returns(true) expect(File).to receive(:exists?).with(filename).and_return(true)
File.expects(:open).with(filename).returns(StringIO.new(mock)) expect(File).to receive(:open).with(filename).and_return(StringIO.new(content))
expect(klass.get_os_vars_from_rcfile("#{ENV['HOME']}/openrc")).to eq({ expect(klass.get_os_vars_from_rcfile("#{ENV['HOME']}/openrc")).to eq({
'OS_USERNAME' => 'user', 'OS_USERNAME' => 'user',
@@ -202,15 +202,15 @@ describe Puppet::Provider::Openstack::Auth do
context 'with user credentials in env' do context 'with user credentials in env' do
it 'is successful' do it 'is successful' do
klass.expects(:get_os_vars_from_env) expect(klass).to receive(:get_os_vars_from_env)
.returns({ 'OS_USERNAME' => 'test', .and_return({ 'OS_USERNAME' => 'test',
'OS_PASSWORD' => 'abc123', 'OS_PASSWORD' => 'abc123',
'OS_PROJECT_NAME' => 'test', 'OS_PROJECT_NAME' => 'test',
'OS_AUTH_URL' => 'http://127.0.0.1:5000', 'OS_AUTH_URL' => 'http://127.0.0.1:5000',
'OS_NOT_VALID' => 'notvalid' }) 'OS_NOT_VALID' => 'notvalid' })
klass.expects(:openstack) expect(klass).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.returns('"ID","Name","Description","Enabled" .and_return('"ID","Name","Description","Enabled"
"1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True
') ')
response = klass.request('project', 'list', ['--long']) response = klass.request('project', 'list', ['--long'])
@@ -227,13 +227,13 @@ describe Puppet::Provider::Openstack::Auth do
context 'with service token credentials in env' do context 'with service token credentials in env' do
it 'is successful' do it 'is successful' do
klass.expects(:get_os_vars_from_env) expect(klass).to receive(:get_os_vars_from_env)
.returns({ 'OS_TOKEN' => 'test', .and_return({ 'OS_TOKEN' => 'test',
'OS_ENDPOINT' => 'http://127.0.0.1:5000', 'OS_ENDPOINT' => 'http://127.0.0.1:5000',
'OS_NOT_VALID' => 'notvalid' }) 'OS_NOT_VALID' => 'notvalid' })
klass.expects(:openstack) expect(klass).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.returns('"ID","Name","Description","Enabled" .and_return('"ID","Name","Description","Enabled"
"1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True
') ')
response = klass.request('project', 'list', ['--long']) response = klass.request('project', 'list', ['--long'])
@@ -249,13 +249,13 @@ describe Puppet::Provider::Openstack::Auth do
context 'with clouds.yaml file' do context 'with clouds.yaml file' do
it 'is successful' do it 'is successful' do
# return incomplete creds from env # return incomplete creds from env
klass.expects(:get_os_vars_from_env) expect(klass).to receive(:get_os_vars_from_env)
.returns({ 'OS_USERNAME' => 'incompleteusername', .and_return({ 'OS_USERNAME' => 'incompleteusername',
'OS_AUTH_URL' => 'incompleteauthurl' }) 'OS_AUTH_URL' => 'incompleteauthurl' })
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)
klass.expects(:openstack) expect(klass).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.returns('"ID","Name","Description","Enabled" .and_return('"ID","Name","Description","Enabled"
"1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True
') ')
response = provider.class.request('project', 'list', ['--long']) 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 context 'with a RC file containing user credentials' do
it 'is successful' do it 'is successful' do
# return incomplete creds from env # return incomplete creds from env
klass.expects(:get_os_vars_from_env) expect(klass).to receive(:get_os_vars_from_env)
.returns({ 'OS_USERNAME' => 'incompleteusername', .and_return({ 'OS_USERNAME' => 'incompleteusername',
'OS_AUTH_URL' => 'incompleteauthurl' }) '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'" 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'"
File.expects(:exists?).with("/etc/openstack/puppet/clouds.yaml").returns(false) expect(File).to receive(:exists?).with("/etc/openstack/puppet/clouds.yaml").and_return(false)
File.expects(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").returns(false) expect(File).to receive(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").and_return(false)
File.expects(:exists?).with("#{ENV['HOME']}/openrc").returns(true) expect(File).to receive(:exists?).with("#{ENV['HOME']}/openrc").and_return(true)
File.expects(:open).with("#{ENV['HOME']}/openrc").returns(StringIO.new(mock)) expect(File).to receive(:open).with("#{ENV['HOME']}/openrc").and_return(StringIO.new(content))
klass.expects(:openstack) expect(klass).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.returns('"ID","Name","Description","Enabled" .and_return('"ID","Name","Description","Enabled"
"1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True
') ')
response = provider.class.request('project', 'list', ['--long']) 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 context 'with a RC file containing service token credentials' do
it 'is successful' do it 'is successful' do
# return incomplete creds from env # return incomplete creds from env
klass.expects(:get_os_vars_from_env) expect(klass).to receive(:get_os_vars_from_env)
.returns({ 'OS_TOKEN' => 'incomplete' }) .and_return({ 'OS_TOKEN' => 'incomplete' })
mock = "export OS_TOKEN='test'\nexport OS_ENDPOINT='abc123'\nexport OS_NOT_VALID='notvalid'\n" content = "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) expect(File).to receive(:exists?).with("/etc/openstack/puppet/clouds.yaml").and_return(false)
File.expects(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").returns(false) expect(File).to receive(:exists?).with("/etc/openstack/puppet/admin-clouds.yaml").and_return(false)
File.expects(:exists?).with("#{ENV['HOME']}/openrc").returns(true) expect(File).to receive(:exists?).with("#{ENV['HOME']}/openrc").and_return(true)
File.expects(:open).with("#{ENV['HOME']}/openrc").returns(StringIO.new(mock)) expect(File).to receive(:open).with("#{ENV['HOME']}/openrc").and_return(StringIO.new(content))
klass.expects(:openstack) expect(klass).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.returns('"ID","Name","Description","Enabled" .and_return('"ID","Name","Description","Enabled"
"1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True "1cb05cfed7c24279be884ba4f6520262","test","Test tenant",True
') ')
response = klass.request('project', 'list', ['--long']) response = klass.request('project', 'list', ['--long'])

View File

@@ -18,8 +18,8 @@ describe Puppet::Provider::Openstack do
end end
let(:credentials) do let(:credentials) do
credentials = mock('credentials') credentials = double('credentials')
credentials.stubs(:to_env).returns({ allow(credentials).to receive(:to_env).and_return({
'OS_USERNAME' => 'user', 'OS_USERNAME' => 'user',
'OS_PASSWORD' => 'password', 'OS_PASSWORD' => 'password',
'OS_PROJECT_NAME' => 'project', 'OS_PROJECT_NAME' => 'project',
@@ -56,31 +56,31 @@ name="test"
end end
it 'makes a successful list request' do it 'makes a successful list request' do
provider.class.expects(:openstack) expect(provider.class).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.returns list_data .and_return list_data
response = Puppet::Provider::Openstack.request('project', 'list', ['--long']) response = Puppet::Provider::Openstack.request('project', 'list', ['--long'])
expect(response.first[:description]).to eq 'Test tenant' expect(response.first[:description]).to eq 'Test tenant'
end end
it 'makes a successful show request' do it 'makes a successful show request' do
provider.class.expects(:openstack) expect(provider.class).to receive(:openstack)
.with('project', 'show', '--format', 'shell', ['1cb05cfed7c24279be884ba4f6520262']) .with('project', 'show', '--format', 'shell', ['1cb05cfed7c24279be884ba4f6520262'])
.returns show_data .and_return show_data
response = Puppet::Provider::Openstack.request('project', 'show', ['1cb05cfed7c24279be884ba4f6520262']) response = Puppet::Provider::Openstack.request('project', 'show', ['1cb05cfed7c24279be884ba4f6520262'])
expect(response[:description]).to eq 'Test tenant' expect(response[:description]).to eq 'Test tenant'
end end
it 'makes a successful set request' do it 'makes a successful set request' do
provider.class.expects(:openstack) expect(provider.class).to receive(:openstack)
.with('project', 'set', ['--name', 'new name', '1cb05cfed7c24279be884ba4f6520262']) .with('project', 'set', ['--name', 'new name', '1cb05cfed7c24279be884ba4f6520262'])
.returns '' .and_return ''
response = Puppet::Provider::Openstack.request('project', 'set', ['--name', 'new name', '1cb05cfed7c24279be884ba4f6520262']) response = Puppet::Provider::Openstack.request('project', 'set', ['--name', 'new name', '1cb05cfed7c24279be884ba4f6520262'])
expect(response).to eq '' expect(response).to eq ''
end end
it 'uses provided credentials' do 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) Puppet::Provider::Openstack.request('project', 'list', ['--long'], credentials)
end end
@@ -96,8 +96,8 @@ name="test"
end end
it 'redacts password in execution output on exception' do it 'redacts password in execution output on exception' do
provider.class.stubs(:execute) allow(provider.class).to receive(: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") .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 expect do
Puppet::Provider::Openstack.request('user', 'create', ['hello', '--password', 'world', '--enable', '--email', 'foo@example.com', '--domain', 'Default']) 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" 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 context 'on connection errors' do
it 'retries the failed command' do it 'retries the failed command' do
provider.class.stubs(:openstack) allow(provider.class).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.raises(Puppet::ExecutionFailure, 'Unable to establish connection') .and_invoke(
.then lambda { |*args| raise Puppet::ExecutionFailure, 'Unable to establish connection' },
.returns list_data lambda { |*args| return list_data }
provider.class.expects(:sleep).with(3).returns(nil) )
expect(provider.class).to receive(:sleep).with(3).and_return(nil)
response = Puppet::Provider::Openstack.request('project', 'list', ['--long']) response = Puppet::Provider::Openstack.request('project', 'list', ['--long'])
expect(response.first[:description]).to eq 'Test tenant' expect(response.first[:description]).to eq 'Test tenant'
end end
it 'fails after the timeout and redacts' do it 'fails after the timeout and redacts' do
provider.class.expects(:execute) expect(provider.class).to receive(:execute)
.raises(Puppet::ExecutionFailure, "Execution of 'openstack user create foo --password secret' returned 1: command failed") .and_raise(Puppet::ExecutionFailure, "Execution of 'openstack user create foo --password secret' returned 1: command failed")
.times(3) .exactly(3).times
provider.class.stubs(:sleep) allow(provider.class).to receive(:sleep)
provider.class.stubs(:current_time) allow(provider.class).to receive(:current_time)
.returns(0, 10, 10, 20, 20, 200, 200) .and_return(0, 10, 10, 20, 20, 200, 200)
expect do expect do
Puppet::Provider::Openstack.request('project', 'list', ['--long']) 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.to raise_error Puppet::ExecutionFailure, /Execution of \'openstack user create foo --password \[redacted secret\]\' returned 1/
end end
it 'fails after the timeout' do it 'fails after the timeout' do
provider.class.expects(:openstack) expect(provider.class).to receive(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long']) .with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.raises(Puppet::ExecutionFailure, 'Unable to establish connection') .and_raise(Puppet::ExecutionFailure, 'Unable to establish connection')
.times(3) .exactly(3).times
provider.class.stubs(:sleep) allow(provider.class).to receive(:sleep)
provider.class.stubs(:current_time) allow(provider.class).to receive(:current_time)
.returns(0, 10, 10, 20, 20, 200, 200) .and_return(0, 10, 10, 20, 20, 200, 200)
expect do expect do
Puppet::Provider::Openstack.request('project', 'list', ['--long']) Puppet::Provider::Openstack.request('project', 'list', ['--long'])
end.to raise_error Puppet::ExecutionFailure, /Unable to establish connection/ end.to raise_error Puppet::ExecutionFailure, /Unable to establish connection/
end end
it 'does not retry non-idempotent commands' do it 'does not retry non-idempotent commands' do
provider.class.expects(:openstack) expect(provider.class).to receive(:openstack)
.with('project', 'create', '--format', 'shell', ['--quiet']) .with('project', 'create', '--format', 'shell', ['--quiet'])
.raises(Puppet::ExecutionFailure, 'Unable to establish connection') .and_raise(Puppet::ExecutionFailure, 'Unable to establish connection')
.then .exactly(1).times
.returns list_data expect(provider.class).to receive(:sleep).never
provider.class.expects(:sleep).never
expect do expect do
Puppet::Provider::Openstack.request('project', 'create', ['--quiet']) Puppet::Provider::Openstack.request('project', 'create', ['--quiet'])
end.to raise_error Puppet::ExecutionFailure, /Unable to establish connection/ end.to raise_error Puppet::ExecutionFailure, /Unable to establish connection/
@@ -160,18 +160,18 @@ name="test"
ENV['OS_PASSWORD'] = 'abc123' ENV['OS_PASSWORD'] = 'abc123'
ENV['OS_PROJECT_NAME'] = 'test' ENV['OS_PROJECT_NAME'] = 'test'
ENV['OS_AUTH_URL'] = 'http://127.0.0.1:5000' 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']) .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 expect do
Puppet::Provider::Openstack.request('project', 'list', ['--long']) Puppet::Provider::Openstack.request('project', 'list', ['--long'])
end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/) end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/)
end end
it 'should raise an error with not authorized to perform' do 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']) .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 expect do
Puppet::Provider::Openstack.request('role', 'list', ['--long']) Puppet::Provider::Openstack.request('role', 'list', ['--long'])
end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/) end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/)

View File

@@ -27,109 +27,109 @@ describe provider_class do
describe 'managing policy' do describe 'managing policy' do
describe '#create' do describe '#create' do
it 'creates a policy when policy-rc.d doesnt exist' do it 'creates a policy when policy-rc.d doesnt exist' do
file = mock('file') file = double('file')
provider.stubs(:policy_rcd).returns(file) allow(provider).to receive(:policy_rcd).and_return(file)
File.expects(:exist?).with(file).returns(false) expect(File).to receive(:exist?).with(file).and_return(false)
content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" 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 provider.create
end end
it 'creates a policy when policy-rc.d exists' do it 'creates a policy when policy-rc.d exists' do
file = mock('file') file = double('file')
provider.stubs(:policy_rcd).returns(file) allow(provider).to receive(:policy_rcd).and_return(file)
File.expects(:exist?).with(file).returns(true) expect(File).to receive(:exist?).with(file).and_return(true)
content = "[[ \"$1\" == \"service\" ]] && exit 101\n" 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 provider.create
end end
end end
describe '#destroy' do describe '#destroy' do
it 'destroy a policy' do it 'destroy a policy' do
file = mock('file') file = double('file')
file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n"
provider.stubs(:policy_rcd).returns(file) allow(provider).to receive(:policy_rcd).and_return(file)
File.expects(:exist?).with(file).returns(true) expect(File).to receive(:exist?).with(file).and_return(true)
provider.stubs(:file_lines).returns(file_content.split("\n")) allow(provider).to receive(:file_lines).and_return(file_content.split("\n"))
provider.class.expects(:write_to_file).with(file, ['#!/bin/bash', '# THIS FILE MANAGED BY PUPPET'], true) expect(provider.class).to receive(:write_to_file).with(file, ['#!/bin/bash', '# THIS FILE MANAGED BY PUPPET'], true)
provider.destroy provider.destroy
end end
end end
describe '#flush' do describe '#flush' do
it 'update a policy' do it 'update a policy' do
file = mock('file') file = double('file')
provider.stubs(:policy_rcd).returns(file) allow(provider).to receive(:policy_rcd).and_return(file)
file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 102\n" file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 102\n"
provider.stubs(:file_lines).returns(file_content.split("\n")) allow(provider).to receive(:file_lines).and_return(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) 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 provider.flush
end end
it 'dont update a policy' do it 'dont update a policy' do
file = mock('file') file = double('file')
file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n"
provider.stubs(:policy_rcd).returns(file) allow(provider).to receive(:policy_rcd).and_return(file)
provider.stubs(:file_lines).returns(file_content.split("\n")) allow(provider).to receive(:file_lines).and_return(file_content.split("\n"))
provider.flush provider.flush
end end
end end
describe '#exists?' do describe '#exists?' do
it 'should exists on Debian family' do it 'should exists on Debian family' do
provider.stubs(:check_os).returns(true) allow(provider).to receive(:check_os).and_return(true)
file = mock('file') file = double('file')
file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n" file_content = "#{header}[[ \"$1\" == \"service\" ]] && exit 101\n"
provider.stubs(:policy_rcd).returns(file) allow(provider).to receive(:policy_rcd).and_return(file)
provider.stubs(:check_policy_rcd).returns(true) allow(provider).to receive(:check_policy_rcd).and_return(true)
provider.stubs(:file_lines).returns(file_content.split("\n")) allow(provider).to receive(:file_lines).and_return(file_content.split("\n"))
expect(provider.exists?).to be_truthy expect(provider.exists?).to be_truthy
end end
it 'should not exists on Debian family when file is present' do it 'should not exists on Debian family when file is present' do
provider.stubs(:check_os).returns(true) allow(provider).to receive(:check_os).and_return(true)
file = mock('file') file = double('file')
file_content = "#{header}[[ \"$1\" == \"new-service\" ]] && exit 101\n" file_content = "#{header}[[ \"$1\" == \"new-service\" ]] && exit 101\n"
provider.stubs(:policy_rcd).returns(file) allow(provider).to receive(:policy_rcd).and_return(file)
provider.stubs(:check_policy_rcd).returns(true) allow(provider).to receive(:check_policy_rcd).and_return(true)
provider.stubs(:file_lines).returns(file_content.split("\n")) allow(provider).to receive(:file_lines).and_return(file_content.split("\n"))
expect(provider.exists?).to be_falsey expect(provider.exists?).to be_falsey
end end
it 'should not exists on Debian family when file is not present' do it 'should not exists on Debian family when file is not present' do
provider.stubs(:check_os).returns(true) allow(provider).to receive(:check_os).and_return(true)
provider.stubs(:check_policy_rcd).returns(false) allow(provider).to receive(:check_policy_rcd).and_return(false)
expect(provider.exists?).to be_falsey expect(provider.exists?).to be_falsey
end end
it 'should exists on non-Debian family' do 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 expect(provider.exists?).to be_truthy
end end
end end
describe 'write_to_file' do describe 'write_to_file' do
it 'should write to file' do it 'should write to file' do
file = mock file = double
policy = mock policy = double
content = 'some_content' content = 'some_content'
File.expects(:open).with(file, 'a+').returns(policy) expect(File).to receive(:open).with(file, 'a+').and_return(policy)
policy.expects(:puts).with(content) expect(policy).to receive(:puts).with(content)
policy.expects(:close) expect(policy).to receive(:close)
File.expects(:chmod).with(0744, file) expect(File).to receive(:chmod).with(0744, file)
provider.class.write_to_file(file, content) provider.class.write_to_file(file, content)
end end
it 'should truncate file' do it 'should truncate file' do
file = mock file = double
policy = mock policy = double
content = 'some_content' content = 'some_content'
File.expects(:truncate).with(file, 0) expect(File).to receive(:truncate).with(file, 0)
File.expects(:open).with(file, 'a+').returns(policy) expect(File).to receive(:open).with(file, 'a+').and_return(policy)
policy.expects(:puts).with(content) expect(policy).to receive(:puts).with(content)
policy.expects(:close) expect(policy).to receive(:close)
File.expects(:chmod).with(0744, file) expect(File).to receive(:chmod).with(0744, file)
provider.class.write_to_file(file, content, true) provider.class.write_to_file(file, content, true)
end end
end end

View File

@@ -15,7 +15,7 @@ describe Puppet::Util::OpenStackConfig do
end end
before :each do before :each do
Puppet::Util::OpenStackConfig.stubs(:readlines).returns(sample_content) allow(Puppet::Util::OpenStackConfig).to receive(:readlines).and_return(sample_content)
end end
context "when parsing a file" do context "when parsing a file" do