Skip to content

Commit 961ee15

Browse files
h-haaksbastelfreak
authored andcommitted
Fix sharding acceptance testing
1 parent 2abddb3 commit 961ee15

File tree

3 files changed

+61
-95
lines changed

3 files changed

+61
-95
lines changed

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

spec/acceptance/sharding_spec.rb

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,41 @@
77

88
if hosts.length > 1 && supported_version?(default[:platform], repo_version)
99
describe 'mongodb_shard resource' do
10+
after :all do
11+
pp = <<-EOS
12+
class { 'mongodb::globals':
13+
#{repo_ver_param}
14+
}
15+
-> class { 'mongodb::mongos' :
16+
package_ensure => purged,
17+
service_ensure => stopped
18+
}
19+
-> class { 'mongodb::server':
20+
ensure => absent,
21+
package_ensure => purged,
22+
service_ensure => stopped
23+
}
24+
-> class { 'mongodb::client':
25+
ensure => purged
26+
}
27+
EOS
28+
29+
apply_manifest_on(hosts.reverse, pp, catch_failures: true)
30+
end
31+
1032
it 'configures the shard server' do
1133
pp = <<-EOS
1234
class { 'mongodb::globals':
1335
#{repo_ver_param}
1436
}
37+
-> class { 'mongodb::client': }
1538
-> class { 'mongodb::server':
16-
bind_ip => '0.0.0.0',
39+
bind_ip => ['0.0.0.0'],
1740
replset => 'foo',
1841
shardsvr => true,
19-
}->
20-
mongodb_replset { 'foo' :
21-
members => ["shard:27018"],
2242
}
23-
if $::osfamily == 'RedHat' {
24-
class { 'mongodb::client': }
43+
-> mongodb_replset { 'foo' :
44+
members => [#{hosts_as('shard').map { |x| "'#{x}:27018'" }.join(',')}],
2545
}
2646
EOS
2747

@@ -34,27 +54,29 @@ class { 'mongodb::client': }
3454
class { 'mongodb::globals':
3555
#{repo_ver_param}
3656
}
57+
-> class { 'mongodb::client': }
3758
-> class { 'mongodb::server':
38-
bind_ip => '0.0.0.0',
59+
bind_ip => ['0.0.0.0'],
60+
replset => 'conf',
3961
configsvr => true,
40-
} ->
41-
class { 'mongodb::mongos' :
42-
configdb => ["router:27019"],
43-
} ->
44-
exec { '/bin/sleep 20' :
45-
} ->
46-
mongodb_shard { 'foo':
47-
member => 'foo/shard:27018',
48-
keys => [{'foo.toto' => {'name' => 1}}]
4962
}
50-
if $::osfamily == 'RedHat' {
51-
class { 'mongodb::client': }
63+
-> mongodb_replset { 'conf' :
64+
members => [#{hosts_as('router').map { |x| "'#{x}:27019'" }.join(',')}],
65+
}
66+
-> class { 'mongodb::mongos' :
67+
configdb => ["conf/#{hosts_as('router').map { |x| "#{x}:27019" }.join(',')}"],
68+
}
69+
-> exec { '/bin/sleep 20' :
70+
}
71+
-> mongodb_shard { 'foo':
72+
member => "foo/#{hosts_as('shard').map { |x| "#{x}:27018" }.join(',')}",
73+
keys => [{'foo.toto' => {'name' => 1}}]
5274
}
5375
EOS
5476

55-
apply_manifest_on(hosts_as('router'), pp, catch_failures: true)
77+
apply_manifest_on(hosts_as('router'), pp, catch_failures: true, trace: true)
5678
on(hosts_as('router'), 'mongosh --quiet --eval "EJSON.stringify(sh.status())"') do |r|
57-
expect(r.stdout).to match %r{foo/shard:27018}
79+
expect(r.stdout).to match %r{foo/#{hosts[0]}:27018}
5880
expect(r.stdout).to match %r{foo\.toto}
5981
end
6082
end

spec/unit/puppet/provider/mongodb_shard/mongodb_spec.rb

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@
2020

2121
let(:raw_shards) do
2222
{
23-
'sharding version' => {
24-
'_id' => 1,
25-
'version' => 4,
26-
'minCompatibleVersion' => 4,
27-
'currentVersion' => 5,
28-
'clusterId' => "ObjectId('548e9110f3aca177c94c5e49')"
29-
},
30-
'shards' => [
31-
{ '_id' => 'rs_test', 'host' => 'rs_test/mongo1:27018' }
32-
],
33-
'databases' => [
34-
{ '_id' => 'admin', 'partitioned' => false, 'primary' => 'config' },
35-
{ '_id' => 'test', 'partitioned' => false, 'primary' => 'rs_test' },
36-
{ '_id' => 'rs_test', 'partitioned' => true, 'primary' => 'rs_test' }
37-
]
23+
'value' => {
24+
'sharding version' => {
25+
'_id' => 1,
26+
'version' => 4,
27+
'minCompatibleVersion' => 4,
28+
'currentVersion' => 5,
29+
'clusterId' => "ObjectId('548e9110f3aca177c94c5e49')"
30+
},
31+
'shards' => [
32+
{ '_id' => 'rs_test', 'host' => 'rs_test/mongo1:27018' }
33+
],
34+
'databases' => [
35+
{ '_id' => 'admin', 'partitioned' => false, 'primary' => 'config' },
36+
{ '_id' => 'test', 'partitioned' => false, 'primary' => 'rs_test' },
37+
{ '_id' => 'rs_test', 'partitioned' => true, 'primary' => 'rs_test' }
38+
]
39+
}
3840
}
3941
end
4042

0 commit comments

Comments
 (0)