Skip to content

Commit

Permalink
Switch from mocha to rspec mocks (#93)
Browse files Browse the repository at this point in the history
* Switch from mocha to rspec mocks

* Drop calls to regexp_matches

* Remove use of expects method
  • Loading branch information
silug authored Jun 20, 2024
1 parent de196e8 commit 7a6ddeb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def set_hieradata(hieradata)
}

c.mock_framework = :rspec
c.mock_with :mocha
c.mock_with :rspec

c.module_path = File.join(fixture_path, 'modules')
c.manifest_dir = File.join(fixture_path, 'manifests') if c.respond_to?(:manifest_dir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,33 @@
let(:provider) { resource.provider }

before :each do
Puppet.stubs(:[]).with(:confdir).returns(target_dir)
Puppet.stubs(:[]).with(:environmentpath).returns(nil)
Puppet.stubs(:[]).with(:environment).returns('')
Puppet::Util.stubs(:which).with('kadmin.local').returns('/usr/sbin/kadmin.local')

provider.stubs(:execute).with(regexp_matches(%r{list_principals})).returns(list_principals)
provider.stubs(:execute).with(regexp_matches(%r{get_principal})).returns(get_principal)
provider.stubs(:execute).with(regexp_matches(%r{add_principal})).returns(add_principal)
provider.stubs(:execute).with(regexp_matches(%r{ktadd})).returns(add_keytab)

provider.stubs(:introspect_hosts).returns([test_host])
provider.stubs(:clean_files).returns(true)

File.stubs(:exist?).with(regexp_matches(%r{\.keytab})).returns(true)
File.stubs(:exist?).with(regexp_matches(%r{\.kvno})).returns(false)
File.stubs(:open).with(regexp_matches(%r{\.kvno}), 'w').returns(true)

FileUtils.stubs(:mkdir_p).returns(true)
FileUtils.stubs(:chmod).returns(true)
FileUtils.stubs(:chown).returns(true)
FileUtils.stubs(:mv).returns(true)
allow(Puppet).to receive(:[]).with(:confdir).and_return(target_dir)
allow(Puppet).to receive(:[]).with(:environmentpath).and_return(nil)
allow(Puppet).to receive(:[]).with(:environment).and_return('')
allow(Puppet::Util).to receive(:which).with('kadmin.local').and_return('/usr/sbin/kadmin.local')

allow(provider).to receive(:execute).with(%r{list_principals}).and_return(list_principals)
allow(provider).to receive(:execute).with(%r{get_principal}).and_return(get_principal)
allow(provider).to receive(:execute).with(%r{add_principal}).and_return(add_principal)
allow(provider).to receive(:execute).with(%r{ktadd}).and_return(add_keytab)

allow(provider).to receive(:introspect_hosts).and_return([test_host])
allow(provider).to receive(:clean_files).and_return(true)

allow(File).to receive(:exist?).with(%r{\.keytab}).and_return(true)
allow(File).to receive(:exist?).with(%r{\.kvno}).and_return(false)
allow(File).to receive(:open).with(%r{\.kvno}, 'w').and_return(true)

allow(FileUtils).to receive(:mkdir_p).and_return(true)
allow(FileUtils).to receive(:chmod).and_return(true)
allow(FileUtils).to receive(:chown).and_return(true)
allow(FileUtils).to receive(:mv).and_return(true)
end

context 'when generating keytabs' do
it 'does not have any errors' do
provider.expects(:execute).with(regexp_matches(%r{ktadd.*.+/#{test_host}@#{test_realm}}))
provider.expects(:execute).with(regexp_matches(%r{add_principal.*.+/#{test_host}@#{test_realm}})).never
expect(provider).to receive(:execute).with(%r{ktadd.*.+/#{test_host}@#{test_realm}})
expect(provider).to_not receive(:execute).with(%r{add_principal.*.+/#{test_host}@#{test_realm}})

provider.exists?
provider.sync_keytabs
Expand All @@ -105,11 +105,11 @@

context 'with a matching kvno file' do
it 'does not have any errors' do
File.stubs(:exist?).with(regexp_matches(%r{\.kvno})).returns(true)
File.stubs(:read).with(regexp_matches(%r{\.kvno})).returns("1\n1\n")
allow(File).to receive(:exist?).with(%r{\.kvno}).and_return(true)
allow(File).to receive(:read).with(%r{\.kvno}).and_return("1\n1\n")

provider.expects(:execute).with(regexp_matches(%r{ktadd.*.+/#{test_host}@#{test_realm}})).never
provider.expects(:execute).with(regexp_matches(%r{add_principal.*.+/#{test_host}@#{test_realm}})).never
expect(provider).to_not receive(:execute).with(%r{ktadd.*.+/#{test_host}@#{test_realm}})
expect(provider).to_not receive(:execute).with(%r{add_principal.*.+/#{test_host}@#{test_realm}})

provider.exists?
provider.sync_keytabs
Expand All @@ -118,11 +118,11 @@

context 'with a non-matching kvno file' do
it 'does not have any errors' do
File.stubs(:exist?).with(regexp_matches(%r{\.kvno})).returns(true)
File.stubs(:read).with(regexp_matches(%r{\.kvno})).returns("1\n2\n")
allow(File).to receive(:exist?).with(%r{\.kvno}).and_return(true)
allow(File).to receive(:read).with(%r{\.kvno}).and_return("1\n2\n")

provider.expects(:execute).with(regexp_matches(%r{ktadd.*.+/#{test_host}@#{test_realm}})).once
provider.expects(:execute).with(regexp_matches(%r{add_principal.*.+/#{test_host}@#{test_realm}})).never
expect(provider).to receive(:execute).with(%r{ktadd.*.+/#{test_host}@#{test_realm}}).once
expect(provider).to_not receive(:execute).with(%r{add_principal.*.+/#{test_host}@#{test_realm}})

provider.exists?
provider.sync_keytabs
Expand All @@ -139,8 +139,8 @@
end

it 'does not have any errors' do
provider.expects(:execute).with(regexp_matches(%r{ktadd.*.+/#{test_host}@#{test_realm}})).once
provider.expects(:execute).with(regexp_matches(%r{add_principal.*.+/#{test_host}@#{test_realm}})).once
expect(provider).to receive(:execute).with(%r{ktadd.*.+/#{test_host}@#{test_realm}}).once
expect(provider).to receive(:execute).with(%r{add_principal.*.+/#{test_host}@#{test_realm}}).once

provider.exists?
provider.sync_keytabs
Expand All @@ -160,10 +160,10 @@
end

it 'does not have any errors' do
provider.stubs(:introspect_hosts).returns([])
allow(provider).to receive(:introspect_hosts).and_return([])

provider.expects(:execute).with(regexp_matches(%r{ktadd.*.+/#{test_host}@#{test_realm}})).twice
provider.expects(:execute).with(regexp_matches(%r{add_principal.*.+/#{test_host}@#{test_realm}})).never
expect(provider).to receive(:execute).with(%r{ktadd.*.+/#{test_host}@#{test_realm}}).twice
expect(provider).to receive(:execute).with(%r{add_principal.*.+/#{test_host}@#{test_realm}}).never

provider.exists?
provider.sync_keytabs
Expand Down

0 comments on commit 7a6ddeb

Please sign in to comment.