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

Fix replset and sharding integration tests #743

Merged
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ jobs:
uses: voxpupuli/gha-puppet/.github/workflows/beaker.yml@v2
with:
pidfile_workaround: 'false'
beaker_hosts: 'host1:shard.ma;host2:slave,router.a'
beaker_facter: 'mongodb_repo_version:MongoDB:4.4,5.0,6.0,7.0'
4 changes: 3 additions & 1 deletion .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
appveyor.yml:
delete: true
.github/workflows/ci.yml:
beaker_facter: 'mongodb_repo_version:MongoDB:4.4,5.0,6.0,7.0'
with:
beaker_hosts: 'host1:shard.ma;host2:slave,router.a'
beaker_facter: 'mongodb_repo_version:MongoDB:4.4,5.0,6.0,7.0'
14 changes: 9 additions & 5 deletions lib/puppet/provider/mongodb_database/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
def self.instances
require 'json'

pre_cmd = 'db.getMongo().setReadPref("primaryPreferred")'
dbs = JSON.parse mongo_eval("#{pre_cmd};EJSON.stringify(db.getMongo().getDBs())")
if db_ismaster
dbs = JSON.parse mongo_eval('EJSON.stringify(db.getMongo().getDBs())')
h-haaks marked this conversation as resolved.
Show resolved Hide resolved

dbs['databases'].map do |db|
new(name: db['name'],
ensure: :present)
dbs['databases'].map do |db|
new(name: db['name'],
ensure: :present)
end
else
Puppet.warning 'Database info is available only from master host'
[]
end
end

Expand Down
14 changes: 7 additions & 7 deletions lib/puppet/provider/mongodb_replset/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@ def create_replica_set(alive_hosts)
retry_sleep = 3

retry_limit.times do |n|
if db_ismaster(alive_hosts[0]['host'])['ismaster']
Puppet.debug 'Replica set initialization has successfully ended'
return true
else
Puppet.debug "Waiting for replica initialization. Retry: #{n}"
sleep retry_sleep
next
alive_hosts.each do |alive_host|
if db_ismaster(alive_host['host'])['ismaster']
Puppet.debug 'Replica set initialization has successfully ended'
return true
end
end
Puppet.debug "Waiting for replica initialization. Retry: #{n}"
sleep retry_sleep
end
raise Puppet::Error, "rs.initiate() failed for replicaset #{name}"
end
Expand Down
62 changes: 2 additions & 60 deletions lib/puppet/provider/mongodb_shard/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def self.shard_collection_details(obj, shard_name)

def self.shard_properties(shard)
properties = {}
output = mongo_command('sh.status()')
output = mongo_command('sh.status()')['value']
output['shards'].each do |s|
next unless s['_id'] == shard

Expand All @@ -118,7 +118,7 @@ def self.shard_properties(shard)
end

def self.shards_properties
output = mongo_command('sh.status()')
output = mongo_command('sh.status()')['value']
properties = if output['shards'].empty?
[]
else
Expand Down Expand Up @@ -163,64 +163,6 @@ def self.mongo_command(command, host = nil, _retries = 4)
retry
end

# NOTE: (spredzy) : sh.status()
# does not return a json stream
# we jsonify it so it is easier
# to parse and deal with it
if command == 'sh.status()'
myarr = output.split("\n")
myarr.shift
myarr.pop
myarr.pop
final_stream = []
prev_line = nil
in_shard_list = 0
in_chunk = 0
myarr.each do |line|
line.gsub!(%r{sharding version:}, '{ "sharding version":')
line.gsub!(%r{shards:}, ',"shards":[')
line.gsub!(%r{databases:}, '], "databases":[')
line.gsub!(%r{"clusterId" : ObjectId\("(.*)"\)}, '"clusterId" : "ObjectId(\'\1\')"')
line.gsub!(%r{\{ "_id" :}, ',{ "_id" :') if %r{_id} =~ prev_line
# Modification for shard
line = '' if line =~ %r{on :.*Timestamp}
if line =~ %r{_id} && in_shard_list == 1
in_shard_list = 0
last_line = final_stream.pop.strip
proper_line = "#{last_line}]},"
final_stream << proper_line
end
if line =~ %r{shard key} && in_shard_list == 1
shard_name = final_stream.pop.strip
proper_line = ",{\"#{shard_name}\":"
final_stream << proper_line
end
if line =~ %r{shard key} && in_shard_list.zero?
in_shard_list = 1
shard_name = final_stream.pop.strip
id_line = "#{final_stream.pop[0..-2]}, \"shards\": "
proper_line = "[{\"#{shard_name}\":"
final_stream << id_line
final_stream << proper_line
end
if in_chunk == 1
in_chunk = 0
line = "\"#{line.strip}\"}}"
end
in_chunk = 1 if line =~ %r{chunks} && in_chunk.zero?
line.gsub!(%r{shard key}, '{"shard key"')
line.gsub!(%r{chunks}, ',"chunks"')
final_stream << line unless line.empty?
prev_line = line
end
final_stream << ' ] }' if in_shard_list == 1
final_stream << ' ] }'
output = final_stream.join("\n")
end

# Hack to avoid non-json empty sets
output = '{}' if output == "null\n"
output.gsub!(%r{\s*}, '')
JSON.parse(output)
end
end
Loading