Skip to content

Commit 8a8033b

Browse files
author
Johan De Wit
committed
[test] Fix unit and acceptance tests
* Fix linter and regenerate REFERENCE.md * Fix rubocop * Re-add opsmanager unit test * Use both new mongosh and mongoshrc in acceptance * Client package is now intalled by the server (mandatory for mongosh) * Fix some tests due to changed output mongosh * add some more rspec tests, and fix template
1 parent 07eac43 commit 8a8033b

File tree

20 files changed

+203
-174
lines changed

20 files changed

+203
-174
lines changed

lib/puppet/provider/mongodb.rb

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,10 @@ def self.db_ismaster
141141
db = 'admin'
142142

143143
begin
144-
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
144+
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
145145
rescue StandardError => e
146-
if mongorc_file && res =~ %r{Authentication failed}
147-
res = mongosh_cmd(db, conn_string, 'db.isMaster().ismaster').to_s.chomp
148-
end
146+
res = mongosh_cmd(db, conn_string, 'db.isMaster().ismaster').to_s.chomp if auth_enabled && e.message =~ %r{Authentication failed}
147+
end
149148

150149
res.eql?('true')
151150
end
@@ -160,23 +159,18 @@ def self.auth_enabled(config = nil)
160159
end
161160

162161
def self.rs_initiated?
162+
# TODO: not used yet, generates a stack level to deep error
163163
cmd_status = "rs.status('localhost').set"
164164
cmd_status = mongoshrc_file + cmd_status if mongoshrc_file
165165
db = 'admin'
166-
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
166+
res = mongosh_cmd(db, conn_string, cmd_status).to_s.split(%r{\n}).last.chomp
167167

168168
# Retry command without authentication when mongorc_file is set and authentication failed
169-
if mongorc_file && res =~ %r{Authentication failed}
170-
res = mongosh_cmd(db, conn_string, "rs.status('localhost').set").to_s.chomp
171-
end
169+
res = mongosh_cmd(db, conn_string, "rs.status('localhost').set").to_s.chomp if mongorc_file && res =~ %r{Authentication failed}
172170

173171
res == @resource[:name]
174172
end
175173

176-
def rs_initiated?
177-
self.rs_initiated?
178-
end
179-
180174
# Mongo Command Wrapper
181175
def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
182176
retry_count = retries
@@ -194,7 +188,7 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
194188
rescue StandardError => e
195189
# When using the rc file, we get this eror because in most cases the admin user is not created yet
196190
# Can/must we move this out of the resue block ?
197-
if self.auth_enabled && e.message =~ %r{Authentication failed}
191+
if auth_enabled && e.message =~ %r{Authentication failed}
198192
out = if host
199193
mongosh_cmd(db, host, no_auth_cmd)
200194
else
@@ -227,31 +221,4 @@ def self.mongo_version
227221
def mongo_version
228222
self.class.mongo_version
229223
end
230-
231-
def self.mongo_4?
232-
v = mongo_version
233-
!v[%r{^4\.}].nil?
234-
end
235-
236-
def mongo_4?
237-
self.class.mongo_4?
238-
end
239-
240-
def self.mongo_5?
241-
v = mongo_version
242-
!v[%r{^5\.}].nil?
243-
end
244-
245-
def mongo_5?
246-
self.class.mongo_5?
247-
end
248-
249-
def self.mongo_6?
250-
v = mongo_version
251-
!v[%r{^6\.}].nil?
252-
end
253-
254-
def mongo_6?
255-
self.class.mongo_6?
256-
end
257224
end

lib/puppet/provider/mongodb_database/mongodb.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def self.instances
1616
new(name: db['name'],
1717
ensure: :present)
1818
end
19-
rescue => e
20-
Puppet.warning("Getting instances of mongodb_database failed: #{e}")
21-
[]
19+
rescue StandardError => e
20+
Puppet.warning("Getting instances of mongodb_database failed: #{e}")
21+
[]
2222
end
2323

2424
# Assign prefetched dbs based on name.
@@ -39,12 +39,12 @@ def create
3939
begin
4040
out = mongo_eval('db.dummyData.insertOne({"created_by_puppet": 1})', @resource[:name])
4141
rescue StandardError => e
42-
if auth_enabled && e.message =~ %r{not authorized on admin to execute commanda} && @resource[:name] == 'admin'
42+
if auth_enabled && e.message =~ %r{not authorized on admin to execute command} && @resource[:name] == 'admin'
4343
Puppet.warning 'Skipping database creation for admin, need admin user first when security is enabled'
4444
@property_hash[:ensure] = :present
4545
@property_hash[:name] = @resource[:name]
46-
else
47-
raise "Failed to create DB '#{@resource[:name]}'\n#{out}" if %r{writeError} =~ out
46+
elsif %r{writeError} =~ out
47+
raise "Failed to create DB '#{@resource[:name]}'\n#{out}"
4848
end
4949
end
5050
else

lib/puppet/provider/mongodb_replset/mongo.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ def get_hosts_status(members)
184184
case e.message
185185
when %r{no replset config has been received}
186186
Puppet.warning('No replicaset config received, needs initialisation')
187-
when /Authentication failed/, /not authorized on admin/, /Authentication failed/
187+
when %r{Authentication failed}, %r{not authorized on admin}
188188
Puppet.warning "Host #{host} is available, but you are unauthorized because of authentication is enabled: #{auth_enabled}"
189-
when /command replSetGetStatus requires authentication/
189+
when %r{command replSetGetStatus requires authentication}
190190
Puppet.warning("Node #{host} is reachable but requires authentication: ReplicaSet not initialized")
191191
end
192192
alive.push(member)

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,16 @@ def password_hash=(_value)
118118
end
119119
end
120120

121-
def password=(value)
122-
if mongo_26?
123-
mongo_eval("db.changeUserPassword(#{@resource[:username].to_json}, #{value.to_json})", @resource[:database])
124-
else
125-
command = {
126-
updateUser: @resource[:username],
127-
pwd: @resource[:password],
128-
digestPassword: true
129-
}
121+
def password=(_value)
122+
command = {
123+
updateUser: @resource[:username],
124+
pwd: @resource[:password],
125+
digestPassword: true
126+
}
130127

131-
if mongo_4? || mongo_5?
132-
command[:mechanisms] = @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1']
133-
end
128+
command[:mechanisms] = @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1']
134129

135-
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
136-
end
130+
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
137131
end
138132

139133
def roles=(roles)

manifests/opsmanager.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
}
150150
default: {
151151
$my_package_ensure = $package_ensure
152-
$file_ensure = 'present'
152+
$file_ensure = 'file'
153153
}
154154
}
155155

manifests/server.pp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
#
55
# For more details about configuration parameters consult the MongoDB Configuration File Options.
66
#
7-
# @example
8-
9-
7+
# @example Basic usage.
8+
# include mongodb::server
9+
#
10+
# @example Overrule settings
11+
# class {'mongodb::server':
12+
# port => 27018,
13+
# verbose => true,
14+
# }
15+
#
1016
# @param ensure
1117
# Used to ensure that the package is installed and the service is running, or that the package is
1218
# absent/purged and the service is stopped.

manifests/server/config.pp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@
126126
mode => '0644',
127127
}
128128

129+
# TODO: we kind of use this file to force x509 authentication in the providers when it exsists
130+
# Open for suggestions how to deal with this
131+
if $admin_auth_mechanism == 'x509' and $handle_creds {
132+
$_ensure = 'file'
133+
} else {
134+
$_ensure = 'absent'
135+
}
136+
137+
file { '/root/.mongosh.yaml':
138+
ensure => $_ensure,
139+
owner => 'root',
140+
group => 'root',
141+
mode => '0600',
142+
content => "---admin:\n name:${admin_username}\n auth_mechanism: ${admin_auth_mechanism}\n tlsCertificateKeyFile: ${admin_tls_key}\n",
143+
}
144+
129145
file { $dbpath:
130146
ensure => directory,
131147
mode => '0750',

spec/acceptance/database_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
it 'compiles with no errors' do
99
pp = <<-EOS
1010
class { 'mongodb::server': }
11-
-> class { 'mongodb::client': }
1211
-> mongodb::db { 'testdb1':
1312
user => 'testuser',
1413
password => 'testpass',
@@ -23,8 +22,8 @@ class { 'mongodb::server': }
2322
end
2423

2524
it 'creates the databases' do
26-
shell("mongo testdb1 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
27-
shell("mongo testdb2 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
25+
shell("mongosh testdb1 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
26+
shell("mongosh testdb2 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
2827
end
2928
end
3029

@@ -34,7 +33,6 @@ class { 'mongodb::server': }
3433
class { 'mongodb::server':
3534
port => 27018,
3635
}
37-
-> class { 'mongodb::client': }
3836
-> mongodb::db { 'testdb1':
3937
user => 'testuser',
4038
password => 'testpass',
@@ -50,8 +48,8 @@ class { 'mongodb::server':
5048
end
5149

5250
it 'creates the database' do
53-
shell("mongo testdb1 --port 27018 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
54-
shell("mongo testdb2 --port 27018 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
51+
shell("mongosh testdb1 --port 27018 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
52+
shell("mongosh testdb2 --port 27018 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
5553
end
5654
end
5755
end

spec/acceptance/mongos_spec.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class { 'mongodb::server':
2525
replset_members => ['127.0.0.1:27019'],
2626
port => 27019,
2727
}
28-
-> class { 'mongodb::client': }
2928
-> class { 'mongodb::mongos':
3029
configdb => ['test/127.0.0.1:27019'],
3130
}
@@ -56,7 +55,7 @@ class { 'mongodb::server':
5655
it { is_expected.to be_listening }
5756
end
5857

59-
describe command('mongo --version') do
58+
describe command('mongod --version') do
6059
its(:exit_status) { is_expected.to eq 0 }
6160
end
6261
end
@@ -73,9 +72,6 @@ class { 'mongodb::mongos':
7372
service_ensure => stopped,
7473
service_enable => false
7574
}
76-
-> class { 'mongodb::client':
77-
ensure => absent,
78-
}
7975
EOS
8076
apply_manifest(pp, catch_failures: true)
8177
apply_manifest(pp, catch_changes: true)

0 commit comments

Comments
 (0)