Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite mocking and stubbing to only rspec #496

Merged
merged 1 commit into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,6 @@ Style/FormatStringToken:
# are located.
RSpec/FilePath:
Enabled: false

RSpec/MessageSpies:
EnforcedStyle: receive
2 changes: 1 addition & 1 deletion .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
appveyor.yml:
delete: true
spec/spec_helper.rb:
allow_deprecations: true
mock_with: :rspec
spec_overrides: "require 'spec_helper_local'"
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# This file is managed via modulesync
# https://github.com/voxpupuli/modulesync
# https://github.com/voxpupuli/modulesync_config
RSpec.configure do |c|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c.mock_with :rspec
end

require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'
include RspecPuppetFacts
Expand Down
4 changes: 0 additions & 4 deletions spec/spec_helper_local.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
RSpec.configure do |config|
config.mock_with :rspec
end

def with_debian_facts
let :facts do
{
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/mongodb_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
describe 'mongodb_version' do
context 'with value' do
before do
Facter::Core::Execution.stubs(:which).with('mongo').returns(true)
Facter::Core::Execution.stubs(:execute).with('mongo --version 2>&1').returns('MongoDB shell version: 3.2.1')
allow(Facter::Core::Execution).to receive(:which).with('mongo').and_return(true)
allow(Facter::Core::Execution).to receive(:execute).with('mongo --version 2>&1').and_return('MongoDB shell version: 3.2.1')
end
it {
expect(Facter.fact(:mongodb_version).value).to eq('3.2.1')
Expand Down
11 changes: 4 additions & 7 deletions spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
}.to_json
end

let(:parsed_dbs) { %w[admin local] }

let(:resource) do
Puppet::Type.type(:mongodb_database).new(
ensure: :present,
Expand All @@ -38,27 +36,26 @@
tmp = Tempfile.new('test')
mongodconffile = tmp.path
allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile)
provider.class.stubs(:mongo_eval).with('rs.slaveOk();printjson(db.getMongo().getDBs())').returns(raw_dbs)
allow(provider.class).to receive(:mongo_eval).with('rs.slaveOk();printjson(db.getMongo().getDBs())').and_return(raw_dbs)
allow(provider.class).to receive(:db_ismaster).and_return(true)
end

describe 'self.instances' do
it 'returns an array of dbs' do
dbs = provider.class.instances.map(&:name)
expect(parsed_dbs).to match_array(dbs)
expect(provider.class.instances.map(&:name)).to match_array(%w[admin local])
end
end

describe 'create' do
it 'makes a database' do
provider.expects(:mongo_eval)
expect(provider).to receive(:mongo_eval)
provider.create
end
end

describe 'destroy' do
it 'removes a database' do
provider.expects(:mongo_eval)
expect(provider).to receive(:mongo_eval)
provider.destroy
end
end
Expand Down
11 changes: 6 additions & 5 deletions spec/unit/puppet/provider/mongodb_replset/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
EOT
end

# rubocop:disable RSpec/MessageSpies
# rubocop:disable RSpec/MultipleExpectations
it 'creates a replicaset' do
allow(provider.class).to receive(:replset_properties)
allow(provider).to receive(:alive_members).and_return(valid_members)
allow(provider).to receive(:master_host).and_return(false)
expect(provider.class).to receive(:replset_properties)
expect(provider).to receive(:get_hosts_status).and_return([valid_members, []])
expect(provider).to receive(:master_host).and_return(false)
expect(provider).to receive(:rs_initiate).with('{"_id":"rs_test","members":[{"host":"mongo1:27017","_id":0},{"host":"mongo2:27017","_id":1},{"host":"mongo3:27017","_id":2}],"settings":{}}', 'mongo1:27017').and_return('info' => 'Config now saved locally. Should come online in about a minute.', 'ok' => 1)
allow(provider).to receive(:db_ismaster).and_return('{"ismaster" : true}')
expect(provider).to receive(:db_ismaster).and_return('{"ismaster" : true}')
provider.create
provider.flush
end
# rubocop:enable RSpec/MultipleExpectations
end

describe '#exists?' do
Expand Down
8 changes: 5 additions & 3 deletions spec/unit/puppet/provider/mongodb_shard/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

before do
provider.class.stubs(:mongo_command).with('sh.status()').returns(raw_shards)
allow(provider.class).to receive(:mongo_command).with('sh.status()').and_return(raw_shards)
end

describe 'self.instances' do
Expand All @@ -48,17 +48,19 @@
end

describe '#create' do
# rubocop:disable RSpec/MultipleExpectations
it 'makes a shard' do
provider.expects('sh_addshard').with('rs_test/mongo1:27018').returns(
expect(provider).to receive(:sh_addshard).with('rs_test/mongo1:27018').and_return(
'shardAdded' => 'rs_test',
'ok' => 1
)
provider.expects('sh_enablesharding').with('rs_test').returns(
expect(provider).to receive(:sh_enablesharding).with('rs_test').and_return(
'ok' => 1
)
provider.create
provider.flush
end
# rubocop:enable RSpec/MultipleExpectations
end

describe 'destroy' do
Expand Down
22 changes: 12 additions & 10 deletions spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
tmp = Tempfile.new('test')
mongodconffile = tmp.path
allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile)
provider.class.stubs(:mongo_eval).with('printjson(db.system.users.find().toArray())').returns(raw_users)
provider.class.stubs(:mongo_version).returns('2.6.x')
allow(provider.class).to receive(:mongo_eval).with('printjson(db.system.users.find().toArray())').and_return(raw_users)
allow(provider.class).to receive(:mongo_version).and_return('2.6.x')
allow(provider.class).to receive(:db_ismaster).and_return(true)
end

Expand Down Expand Up @@ -61,14 +61,14 @@
}
EOS

provider.expects(:mongo_eval).with("db.runCommand(#{cmd_json})", 'new_database')
expect(provider).to receive(:mongo_eval).with("db.runCommand(#{cmd_json})", 'new_database')
provider.create
end
end

describe 'destroy' do
it 'removes a user' do
provider.expects(:mongo_eval).with("db.dropUser('new_user')")
expect(provider).to receive(:mongo_eval).with("db.dropUser('new_user')")
provider.destroy
end
end
Expand All @@ -94,7 +94,7 @@
"digestPassword": false
}
EOS
provider.expects(:mongo_eval).
expect(provider).to receive(:mongo_eval).
with("db.runCommand(#{cmd_json})", 'new_database')
provider.password_hash = 'newpass'
end
Expand All @@ -121,32 +121,34 @@
describe 'roles=' do
it 'changes nothing' do
resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2])
provider.expects(:mongo_eval).times(0)
expect(provider).not_to receive(:mongo_eval)
provider.roles = %w[role1 role2]
end

it 'grant a role' do
resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2])
provider.expects(:mongo_eval).
expect(provider).to receive(:mongo_eval).
with("db.getSiblingDB('new_database').grantRolesToUser('new_user', [\"role3\"])")
provider.roles = %w[role1 role2 role3]
end

it 'revokes a role' do
resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2])
provider.expects(:mongo_eval).
expect(provider).to receive(:mongo_eval).
with("db.getSiblingDB('new_database').revokeRolesFromUser('new_user', [\"role1\"])")
provider.roles = ['role2']
end

# rubocop:disable RSpec/MultipleExpectations
it 'exchanges a role' do
resource.provider.set(name: 'new_user', ensure: :present, roles: %w[role1 role2])
provider.expects(:mongo_eval).
expect(provider).to receive(:mongo_eval).
with("db.getSiblingDB('new_database').revokeRolesFromUser('new_user', [\"role1\"])")
provider.expects(:mongo_eval).
expect(provider).to receive(:mongo_eval).
with("db.getSiblingDB('new_database').grantRolesToUser('new_user', [\"role3\"])")

provider.roles = %w[role2 role3]
end
# rubocop:enable RSpec/MultipleExpectations
end
end