I am trying to add compatibility with Mongoid5 to
https://github.com/dblock/mongoid-shell (current WIP in
https://github.com/dblock/mongoid-shell/tree/mongoid-5). This gem examines the guts of server configuration to produce command lines that work with mongo command line tools. We use it a lot in rake tasks.
Question 1: I used to be able to load a real mongoid configuration session by name in a test and make it disconnected like so:
allow_any_instance_of(Moped::Address).to receive(:resolve).and_return(false)
config = File.join(File.dirname(__FILE__), '../../support/config/mongoid34.yml')
Mongoid.load! config, :production
session = Mongoid::Sessions.with_name(name)
allow(session.cluster.nodes.last).to receive(:primary?).and_return(true)
session
How do I do this now? I got this far, but it looks like cluster.servers ends up blank every time.
socket = Mongo::Socket::TCP.new('127.0.0.1', 27017, 30, Socket::PF_INET)
allow_any_instance_of(Mongo::Address).to receive(:socket).and_return(socket)
config = File.join(File.dirname(__FILE__), '../../support/config/mongoid5.yml')
Mongoid.load! config, :production
session = Mongoid::Clients.with_name(name)
server = session.cluster.servers.last
allow(server).to receive(:primary?).and_return(true)
session
Question 2: When a configuration uses a username/password, how can I fetch the auth from a server? It used to be something like this:
node = session.cluster.nodes.first
return nil if !node.credentials.key?(db) || node.credentials[db].empty?
node.credentials[db][1]
I understand this is a bit hacky, any other suggestion appreciated.
Thanks!
-dB.