diff --git a/lib/puppet/provider/mongodb_database/mongodb.rb b/lib/puppet/provider/mongodb_database/mongodb.rb index b96845216..74667682b 100644 --- a/lib/puppet/provider/mongodb_database/mongodb.rb +++ b/lib/puppet/provider/mongodb_database/mongodb.rb @@ -22,7 +22,7 @@ def self.instances catch (err) { rs.slaveOk() } - ' + '.squeeze(" ") mongo_eval_result = mongo_eval("#{pre_cmd};JSON.stringify(db.getMongo().getDBs())") dbs = JSON.parse mongo_eval_result diff --git a/spec/acceptance/server_spec.rb b/spec/acceptance/server_spec.rb index e889254d2..2970acca5 100644 --- a/spec/acceptance/server_spec.rb +++ b/spec/acceptance/server_spec.rb @@ -25,6 +25,13 @@ service_name = 'mongod' package_name = 'mongodb-org-server' end + major_version=fact('mongodb_version').split('.')[0].to_i + if major_version>=5 + mongo_cli=mongosh + else + mongo_cli=mongo + end + describe 'installation' do it 'works with no errors' do @@ -143,7 +150,9 @@ class { 'mongodb::client': } its(:stdout) { is_expected.to match '13' } end - describe file('/root/.mongorc.js') do + + + describe file("/root/.#{mongo_cli}.js") do it { is_expected.to be_file } it { is_expected.to be_owned_by 'root' } it { is_expected.to be_grouped_into 'root' } @@ -151,7 +160,7 @@ class { 'mongodb::client': } it { is_expected.to contain 'db.auth(\'admin\', \'password\')' } end - describe command("mongo admin --quiet --eval \"load('/root/.mongorc.js');printjson(db.getUser('admin')['customData'])\"") do + describe command("#{mongo_cli} admin --quiet --eval \"load('/root/.#{mongo_cli}.js');printjson(db.getUser('admin')['customData'])\"") do its(:exit_status) { is_expected.to eq 0 } its(:stdout) { is_expected.to match "{ \"createdBy\" : \"Puppet Mongodb_user['User admin on db admin']\" }\n" } end diff --git a/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb b/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb index c783d6898..dea509888 100644 --- a/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb +++ b/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb @@ -38,7 +38,21 @@ tmp = Tempfile.new('test') mongodconffile = tmp.path allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile) - allow(provider.class).to receive(:mongo_eval).with('try { rs.secondaryOk() } catch (err) { rs.slaveOk() };printjson(db.getMongo().getDBs())').and_return(raw_dbs) + mongodb_eval_command = ' + try { + version=parseInt(db.version().split(\'.\')[0]) + if (version>=5){ + db.getMongo().setReadPref(\'nearest\') + } + else{ + rs.secondaryOk() + } + } + catch (err) { + rs.slaveOk() + } + ;JSON.stringify(db.getMongo().getDBs())'.squeeze(" ") + allow(provider.class).to receive(:mongo_eval).with(mongodb_eval_command).and_return(raw_dbs) allow(provider.class).to receive(:db_ismaster).and_return(true) end