Skip to content

Commit e790633

Browse files
authored
Merge pull request #743 from StatensPensjonskasse/fix-replset-acceptance-testing
Fix replset and sharding integration tests
2 parents dbe9194 + 9636c73 commit e790633

File tree

9 files changed

+296
-151
lines changed

9 files changed

+296
-151
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ jobs:
2121
uses: voxpupuli/gha-puppet/.github/workflows/beaker.yml@v2
2222
with:
2323
pidfile_workaround: 'false'
24+
beaker_hosts: 'host1:shard.ma;host2:slave,router.a'
2425
beaker_facter: 'mongodb_repo_version:MongoDB:4.4,5.0,6.0,7.0'

.sync.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
appveyor.yml:
33
delete: true
44
.github/workflows/ci.yml:
5-
beaker_facter: 'mongodb_repo_version:MongoDB:4.4,5.0,6.0,7.0'
5+
with:
6+
beaker_hosts: 'host1:shard.ma;host2:slave,router.a'
7+
beaker_facter: 'mongodb_repo_version:MongoDB:4.4,5.0,6.0,7.0'

lib/puppet/provider/mongodb_database/mongodb.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
def self.instances
1010
require 'json'
1111

12-
pre_cmd = 'db.getMongo().setReadPref("primaryPreferred")'
13-
dbs = JSON.parse mongo_eval("#{pre_cmd};EJSON.stringify(db.getMongo().getDBs())")
12+
if db_ismaster
13+
dbs = JSON.parse mongo_eval('EJSON.stringify(db.getMongo().getDBs())')
1414

15-
dbs['databases'].map do |db|
16-
new(name: db['name'],
17-
ensure: :present)
15+
dbs['databases'].map do |db|
16+
new(name: db['name'],
17+
ensure: :present)
18+
end
19+
else
20+
Puppet.warning 'Database info is available only from master host'
21+
[]
1822
end
1923
end
2024

lib/puppet/provider/mongodb_replset/mongo.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,14 @@ def create_replica_set(alive_hosts)
284284
retry_sleep = 3
285285

286286
retry_limit.times do |n|
287-
if db_ismaster(alive_hosts[0]['host'])['ismaster']
288-
Puppet.debug 'Replica set initialization has successfully ended'
289-
return true
290-
else
291-
Puppet.debug "Waiting for replica initialization. Retry: #{n}"
292-
sleep retry_sleep
293-
next
287+
alive_hosts.each do |alive_host|
288+
if db_ismaster(alive_host['host'])['ismaster']
289+
Puppet.debug 'Replica set initialization has successfully ended'
290+
return true
291+
end
294292
end
293+
Puppet.debug "Waiting for replica initialization. Retry: #{n}"
294+
sleep retry_sleep
295295
end
296296
raise Puppet::Error, "rs.initiate() failed for replicaset #{name}"
297297
end

lib/puppet/provider/mongodb_shard/mongo.rb

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def self.shard_collection_details(obj, shard_name)
102102

103103
def self.shard_properties(shard)
104104
properties = {}
105-
output = mongo_command('sh.status()')
105+
output = mongo_command('sh.status()')['value']
106106
output['shards'].each do |s|
107107
next unless s['_id'] == shard
108108

@@ -118,7 +118,7 @@ def self.shard_properties(shard)
118118
end
119119

120120
def self.shards_properties
121-
output = mongo_command('sh.status()')
121+
output = mongo_command('sh.status()')['value']
122122
properties = if output['shards'].empty?
123123
[]
124124
else
@@ -163,64 +163,6 @@ def self.mongo_command(command, host = nil, _retries = 4)
163163
retry
164164
end
165165

166-
# NOTE: (spredzy) : sh.status()
167-
# does not return a json stream
168-
# we jsonify it so it is easier
169-
# to parse and deal with it
170-
if command == 'sh.status()'
171-
myarr = output.split("\n")
172-
myarr.shift
173-
myarr.pop
174-
myarr.pop
175-
final_stream = []
176-
prev_line = nil
177-
in_shard_list = 0
178-
in_chunk = 0
179-
myarr.each do |line|
180-
line.gsub!(%r{sharding version:}, '{ "sharding version":')
181-
line.gsub!(%r{shards:}, ',"shards":[')
182-
line.gsub!(%r{databases:}, '], "databases":[')
183-
line.gsub!(%r{"clusterId" : ObjectId\("(.*)"\)}, '"clusterId" : "ObjectId(\'\1\')"')
184-
line.gsub!(%r{\{ "_id" :}, ',{ "_id" :') if %r{_id} =~ prev_line
185-
# Modification for shard
186-
line = '' if line =~ %r{on :.*Timestamp}
187-
if line =~ %r{_id} && in_shard_list == 1
188-
in_shard_list = 0
189-
last_line = final_stream.pop.strip
190-
proper_line = "#{last_line}]},"
191-
final_stream << proper_line
192-
end
193-
if line =~ %r{shard key} && in_shard_list == 1
194-
shard_name = final_stream.pop.strip
195-
proper_line = ",{\"#{shard_name}\":"
196-
final_stream << proper_line
197-
end
198-
if line =~ %r{shard key} && in_shard_list.zero?
199-
in_shard_list = 1
200-
shard_name = final_stream.pop.strip
201-
id_line = "#{final_stream.pop[0..-2]}, \"shards\": "
202-
proper_line = "[{\"#{shard_name}\":"
203-
final_stream << id_line
204-
final_stream << proper_line
205-
end
206-
if in_chunk == 1
207-
in_chunk = 0
208-
line = "\"#{line.strip}\"}}"
209-
end
210-
in_chunk = 1 if line =~ %r{chunks} && in_chunk.zero?
211-
line.gsub!(%r{shard key}, '{"shard key"')
212-
line.gsub!(%r{chunks}, ',"chunks"')
213-
final_stream << line unless line.empty?
214-
prev_line = line
215-
end
216-
final_stream << ' ] }' if in_shard_list == 1
217-
final_stream << ' ] }'
218-
output = final_stream.join("\n")
219-
end
220-
221-
# Hack to avoid non-json empty sets
222-
output = '{}' if output == "null\n"
223-
output.gsub!(%r{\s*}, '')
224166
JSON.parse(output)
225167
end
226168
end

0 commit comments

Comments
 (0)