// resetAdminPasswordAndFetchDBNames logs into the database with a // plausible password and returns all the database's db names. We need // to try several passwords because we don't know what state the mongo // server is in when Reset is called. If the test has set a custom // password, we're out of luck, but if they are using // DefaultStatePassword, we can succeed. func resetAdminPasswordAndFetchDBNames(session *mgo.Session) ([]string, bool) { // First try with no password dbnames, err := session.DatabaseNames() if err == nil { return dbnames, true } if !isUnauthorized(err) { panic(err) } // Then try the two most likely passwords in turn. for _, password := range []string{ DefaultMongoPassword, utils.UserPasswordHash(DefaultMongoPassword, utils.CompatSalt), } { admin := session.DB("admin") if err := admin.Login("admin", password); err != nil { logger.Infof("failed to log in with password %q", password) continue } dbnames, err := session.DatabaseNames() if err == nil { if err := admin.RemoveUser("admin"); err != nil { panic(err) } return dbnames, true } if !isUnauthorized(err) { panic(err) } logger.Infof("unauthorized access when getting database names; password %q", password) } return nil, false }
func listDbs(session *mgo.Session) { dbNames, err := session.DatabaseNames() if err != nil { panic(err) } for i := 0; i < len(dbNames); i++ { fmt.Println(dbNames[i]) } }
// dropAll drops all databases apart from admin, local and config. func dropAll(session *mgo.Session) (err error) { names, err := session.DatabaseNames() if err != nil { return err } for _, name := range names { switch name { case "admin", "local", "config": default: err = session.DB(name).DropDatabase() if err != nil { return err } } } return nil }