Skip to content

Instantly share code, notes, and snippets.

@mpobrien
Created February 24, 2014 02:47
Show Gist options
  • Save mpobrien/9181129 to your computer and use it in GitHub Desktop.
Save mpobrien/9181129 to your computer and use it in GitHub Desktop.
load('multi_rs.js')
//Set of sample users to create on each test run
users = [
{user:"user1", roles: ["read"], pwd:"pwd"},
{user:"user2", roles: ["readWrite"], pwd:"pwd"},
{user:"user3", roles: ["dbAdmin"], pwd:"pwd"},
{user:"user4", roles: ["userAdmin"], pwd:"pwd"},
{user:"user5", roles: ["clusterAdmin"], pwd:"pwd"},
{user:"user6", roles: ["readAnyDatabase"], pwd:"pwd"},
{user:"user7", roles: ["readWriteAnyDatabase"], pwd:"pwd"},
{user:"user8", roles: ["userAdminAnyDatabase"], pwd:"pwd"},
{user:"user9", roles: ["dbAdminAnyDatabase"], pwd:"pwd"},
]
rscount = 1
//Helper function to generate a new name + port number for each test.
function newReplSetInfo(){
rscount++
return {name:"rs" + rscount, portOffset: rscount*10}
}
//Helper function to insert the test users into a replset.
//if "useOld" is true, it calls addUser
//otherwise, it uses createUser
function addTestUsers(replset, useOld){
//Create a bunch of users in our source database.
print("Adding users")
for(var i=0;i<users.length;i++){
if(useOld){
replset.getPrimary().getDB("admin").addUser(users[i])
}else{
replset.getPrimary().getDB("admin").createUser(users[i])
}
}
print("Done adding users")
}
function runDumpRestoreTest(options, asserts){
print("TESTING WITH VERSIONS:")
printjson(options)
var sourceVersion = options.s
var targetVersion= options.t
var dumpVersion=options.d
var restoreVersion=options.r
var oldAuth = options.oldSchema
var sourceInfo = newReplSetInfo()
var source = new ReplSetTest({name:sourceInfo.name, nodes:3, keyFile:"testKeyFile", startPort:40000 + sourceInfo.portOffset })
var targetInfo = newReplSetInfo()
var target = new ReplSetTest({name:targetInfo.name, nodes:3, keyFile:"testKeyFile", startPort:50000 + targetInfo.portOffset})
sourceStartVersion = sourceVersion
targetStartVersion = targetVersion
//If we want to use the old auth schema, we may need to start with 2.4 binaries even if the source/target is 2.6
if(sourceStartVersion == "2.6" && oldAuth){
sourceStartVersion = "2.4"
}
if(targetStartVersion == "2.6" && oldAuth){
targetStartVersion = "2.4"
}
print("source version " + sourceVersion + " source start version " + sourceStartVersion)
print("target version " + targetVersion + " target start version " + targetStartVersion)
source.startSet({binVersion:sourceStartVersion})
source.initiate()
target.startSet({binVersion:targetStartVersion})
target.initiate()
//Set up the all-powerful user we will run the dump/restore programs as.
//Need to pick command to use (createUser vs. addUser) based on version, since createUser doesn't exist in 2.4.x.
print("Creating users in source database (" + sourceVersion + ")")
if(sourceVersion != "2.4"){
if(oldAuth){ //start the cluster on 2.4, add the user then upgrade the binaries so the auth schema is still old.
print("old auth source version")
source.getPrimary().getDB("admin").addUser({user:"user", roles:["userAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"], pwd:"pwd"})
source.getPrimary().getDB("admin").auth("user","pwd")
addTestUsers(source, true)
source.upgradeSet(sourceVersion, {auth:{"user":"user","pwd":"pwd"}})
} else{
print("not old auth source version")
source.getPrimary().getDB("admin").createUser({user:"user", roles:["userAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"], pwd:"pwd"})
source.getPrimary().getDB("admin").auth("user","pwd")
addTestUsers(source, false)
}
}else{
source.getPrimary().getDB("admin").addUser({user:"user", roles:["userAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"], pwd:"pwd"})
source.getPrimary().getDB("admin").auth("user","pwd")
addTestUsers(source, true)
}
source.getPrimary().getDB("admin").auth("user", "pwd")
if(targetVersion != "2.4"){
if(oldAuth){ //start the cluster on 2.4, add the user then upgrade the binaries so the auth schema is still old.
print("old auth target version")
print("calling adduser right here2")
target.getPrimary().getDB("admin").addUser({user:"user", roles:["userAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"], pwd:"pwd"})
target.getPrimary().getDB("admin").auth("user","pwd")
target.upgradeSet(sourceVersion, {"auth":{"user":"user", pwd:"pwd"}})
} else{
print("not old auth target version")
print("calling createuser right here2")
target.getPrimary().getDB("admin").createUser({user:"user", roles:["userAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"], pwd:"pwd"})
}
}else{
print("calling adduser right here")
target.getPrimary().getDB("admin").addUser({user:"user", roles:["userAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin"], pwd:"pwd"})
}
target.getPrimary().getDB("admin").auth("user", "pwd")
print("done initializing replsets for:")
printjson(options)
print("SOURCE VERSION: ")
printjson(source.getPrimary().getDB("admin").system.version.find().toArray())
print("TARGET VERSION: ")
printjson(target.getPrimary().getDB("admin").system.version.find().toArray())
print("SOURCE DB VER: "+ source.getPrimary().getDB("admin").version())
print("TARGET DB VER: "+ target.getPrimary().getDB("admin").version())
/*
//Create a bunch of users in our source database.
for(var i=0;i<users.length;i++){
var func = source.getPrimary().getDB("admin").addUser
if(sourceVersion != "2.4"){
if(oldAuth){
print("trying adduser1")
source.getPrimary().getDB("admin").addUser(users[i])
}else{
print("trying createuser")
source.getPrimary().getDB("admin").createUser(users[i])
}
}else{
print("trying adduser")
source.getPrimary().getDB("admin").addUser(users[i])
}
}
print("Done adding users")
*/
//Dump the data from the source server.
print("Running mongodump from source database (" + sourceVersion + ") using mongodump version: " + dumpVersion)
MongoRunner.runMongoTool("mongodump-" + dumpVersion, {host:source.getURL(), authenticationDatabase: "admin", username:"user", password:"pwd" })
//Restore the data into the target server.
//
var targetAdmin = target.getPrimary().getDB("admin")
printjson(targetAdmin.auth("user", "pwd"))
print("Running mongorestore to target database (" + targetVersion + ") using mongorestore version: " + restoreVersion)
MongoRunner.runMongoTool("mongorestore-" + restoreVersion, {host:target.getURL() , username:"__system", password:"abcdefghijklmnopqrstuvwxyz", authenticationDatabase:"local"})
print("MONGORESTORE is done.")
print("authing to target db")
try{
printjson(targetAdmin.auth("user", "pwd"))
}catch(e){
print("couldn't log in!")
}
sleep(10000)
var targetUsers = targetAdmin.system.users.find().toArray()
var sourceUsers = source.getPrimary().getDB("admin").system.users.find().toArray()
print("RUNNING ASSERTIONS FOR TEST CONFIG")
printjson(options)
printjson(asserts)
printjson(sourceUsers)
printjson(targetUsers)
//Check users existence.
var assertFunc = asserts.usersShouldExist ? assert.neq : assert.eq
for(var i=0;i<users.length;i++){
var userDoc = targetAdmin.system.users.findOne({user:users[i].user})
assertFunc(userDoc, null)
}
//Check users existence.
var assertFunc = asserts.loginsShouldWork ? assert.neq : assert.eq
for(var i=0;i<users.length;i++){
targetAdmin.logout()
assertFunc(targetAdmin.auth(users[i].user, users[i].pwd), 0)
}
/*
//For cases where source is 2.4 and target is 2.6,
//or vice versa
if(sourceVersion != targetVersion){
if(oldAuth){
//If the old auth schema was used, all users should have been restored
printjson(options)
assert.eq(targetUsers.length, sourceUsers.length, "target users count was expected to match source")
}else{
//If the *new* auth schema was used,
//inserts against the differing auth schema should have been dropped.
//So the only user in "targetUsers" is the one we created at start
printjson(options)
printjson(targetUsers)
printjson(sourceUsers)
assert.eq(targetUsers.length, 1, "target users count was expected to be 1")
}
}else{
//Going from either 2.4 -> 2.4 or 2.6 -> 2.6
//So all users should definitely have been dumped/restored.
printjson(options)
assert.eq(targetUsers.length, sourceUsers.length, "user counts did not match")
}
*/
source.stopSet()
target.stopSet()
print("DONE TESTING FOR:")
printjson(options)
}
assertions = {usersShouldExist:false, loginsShouldWork:false}
runDumpRestoreTest({s:"2.6",t: "2.6",d: "2.4",r: "2.4", oldSchema:true}, assertions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment