@@ -72,6 +72,277 @@ createMongoArgs = function( binaryName , args ){
7272 return fullArgs ;
7373}
7474
75+
76+ MongoRunner = function ( ) { }
77+
78+ MongoRunner . dataDir = "/data/db"
79+ MongoRunner . dataPath = "/data/db/"
80+ MongoRunner . usedPortMap = { }
81+ MongoRunner . logicalOptions = { runId : true ,
82+ pathOpts : true ,
83+ remember : true ,
84+ noRemember : true ,
85+ appendOptions : true ,
86+ restart : true ,
87+ noCleanData : true ,
88+ cleanData : true ,
89+ forceLock : true ,
90+ useLogFiles : true ,
91+ useHostName : true ,
92+ useHostname : true }
93+
94+ MongoRunner . toRealPath = function ( path , pathOpts ) {
95+
96+ // Replace all $pathOptions with actual values
97+ pathOpts = pathOpts || { }
98+ path = path . replace ( / \$ d a t a P a t h / g, MongoRunner . dataPath )
99+ path = path . replace ( / \$ d a t a D i r / g, MongoRunner . dataDir )
100+ for ( key in pathOpts ) {
101+ path = path . replace ( RegExp ( "\\$" + key , "g" ) , pathOpts [ key ] )
102+ }
103+
104+ // Relative path
105+ if ( ! path . startsWith ( "/" ) ) {
106+ if ( path != "" && ! path . endsWith ( "/" ) )
107+ path += "/"
108+
109+ path = MongoRunner . dataPath + path
110+ }
111+
112+ return path
113+
114+ }
115+
116+ MongoRunner . toRealDir = function ( path , pathOpts ) {
117+
118+ path = MongoRunner . toRealPath ( path , pathOpts )
119+
120+ if ( path . endsWith ( "/" ) )
121+ path = path . substring ( 0 , path . length - 1 )
122+
123+ return path
124+ }
125+
126+ MongoRunner . toRealFile = MongoRunner . toRealDir
127+
128+ MongoRunner . nextOpenPort = function ( ) {
129+
130+ var i = 0 ;
131+ while ( __usedPortMap [ "" + ( 27000 + i ) ] ) i ++ ;
132+ return 27000 + i
133+
134+ }
135+
136+ MongoRunner . arrOptions = function ( binaryName , args ) {
137+
138+ var fullArgs = [ binaryName ]
139+
140+ if ( isObject ( args ) || ( args . length == 1 && isObject ( args [ 0 ] ) ) ) {
141+
142+ var o = isObject ( args ) ? args : args [ 0 ]
143+ for ( var k in o ) {
144+
145+ if ( ! o . hasOwnProperty ( k ) || k in MongoRunner . logicalOptions ) continue
146+
147+ if ( ( k == "v" || k == "verbose" ) && isNumber ( o [ k ] ) ) {
148+ var n = o [ k ]
149+ if ( n > 0 ) {
150+ if ( n > 10 ) n = 10
151+ var temp = "-"
152+ while ( n -- > 0 ) temp += "v"
153+ fullArgs . push ( temp )
154+ }
155+ }
156+ else {
157+ if ( o [ k ] == undefined || o [ k ] == null ) continue
158+ fullArgs . push ( "--" + k )
159+ if ( o [ k ] != "" )
160+ fullArgs . push ( "" + o [ k ] )
161+ }
162+ }
163+ }
164+ else {
165+ for ( var i = 0 ; i < args . length ; i ++ )
166+ fullArgs . push ( args [ i ] )
167+ }
168+
169+ return fullArgs
170+ }
171+
172+ MongoRunner . arrToOpts = function ( arr ) {
173+
174+ var opts = { }
175+ for ( var i = 1 ; i < arr . length ; i ++ ) {
176+ if ( arr [ i ] . startsWith ( "-" ) ) {
177+ var opt = arr [ i ] . replace ( / ^ - / , "" ) . replace ( / ^ - / , "" )
178+
179+ if ( arr . length > i + 1 && ! arr [ i + 1 ] . startsWith ( "-" ) ) {
180+ opts [ opt ] = arr [ i + 1 ]
181+ i ++
182+ }
183+ else {
184+ opts [ opt ] = ""
185+ }
186+
187+ if ( opt . replace ( / v / g, "" ) == "" ) {
188+ opts [ "verbose" ] = opt . length
189+ }
190+ }
191+ }
192+
193+ return opts
194+ }
195+
196+ MongoRunner . savedOptions = { }
197+
198+ MongoRunner . mongoOptions = function ( opts ) {
199+
200+ // Initialize and create a copy of the opts
201+ opts = Object . merge ( opts || { } , { } )
202+
203+ if ( ! opts . restart ) opts . restart = false
204+
205+ // RunId can come from a number of places
206+ if ( isObject ( opts . restart ) ) {
207+ opts . runId = opts . restart
208+ opts . restart = true
209+ }
210+
211+ if ( isObject ( opts . remember ) ) {
212+ opts . runId = opts . remember
213+ opts . remember = true
214+ }
215+
216+ if ( opts . restart && opts . remember ) opts = Object . merge ( MongoRunner . savedOptions [ opts . runId ] , opts )
217+
218+ // Create a new runId
219+ opts . runId = opts . runId || ObjectId ( )
220+
221+ var shouldRemember = ( ! opts . restart && ! opts . noRemember ) || ( opts . restart && opts . appendOptions )
222+
223+ if ( shouldRemember ) {
224+ MongoRunner . savedOptions [ opts . runId ] = Object . merge ( opts , { } )
225+ }
226+
227+ opts . port = opts . port || nextOpenPort
228+ opts . pathOpts = Object . merge ( opts . pathOpts || { } , { port : "" + opts . port , runId : "" + opts . runId } )
229+
230+ return opts
231+ }
232+
233+ MongoRunner . mongodOptions = function ( opts ) {
234+
235+ opts = MongoRunner . mongoOptions ( opts )
236+
237+ opts . dbpath = MongoRunner . toRealDir ( opts . dbpath || "$dataDir/mongod-$port" ,
238+ opts . pathOpts )
239+
240+ opts . pathOpts = Object . merge ( opts . pathOpts , { dbpath : opts . dbpath } )
241+
242+ if ( ! opts . logFile && opts . useLogFiles ) {
243+ opts . logFile = opts . dbpath + "/mongod.log"
244+ }
245+ else if ( opts . logFile ) {
246+ opts . logFile = MongoRunner . toRealFile ( opts . logFile , opts . pathOpts )
247+ }
248+
249+ if ( jsTestOptions ( ) . noJournalPrealloc )
250+ opts . nopreallocj = ""
251+
252+ if ( jsTestOptions ( ) . noJournal )
253+ opts . nojournal = ""
254+
255+ return opts
256+ }
257+
258+ MongoRunner . mongosOptions = function ( opts ) {
259+
260+ opts = MongoRunner . mongoOptions ( opts )
261+
262+ opts . pathOpts = Object . merge ( opts . pathOpts ,
263+ { configdb : opts . configdb . replace ( / : | , / g, "-" ) } )
264+
265+ if ( ! opts . logFile && opts . useLogFiles ) {
266+ opts . logFile = MongoRunner . toRealFile ( "$dataDir/mongos-$configdb-$port.log" ,
267+ opts . pathOpts )
268+ }
269+ else if ( opts . logFile ) {
270+ opts . logFile = MongoRunner . toRealFile ( opts . logFile , opts . pathOpts )
271+ }
272+
273+ return opts
274+ }
275+
276+ MongoRunner . runMongod = function ( opts ) {
277+
278+ var useHostName = false
279+ var runId = null
280+ if ( isObject ( opts ) ) {
281+
282+ opts = MongoRunner . mongodOptions ( opts )
283+
284+ useHostName = opts . useHostName || opts . useHostname
285+ runId = opts . runId
286+
287+ if ( opts . forceLock ) removeFile ( opts . dbpath + "/mongod.lock" )
288+ if ( opts . cleanData || ( ! opts . restart && ! opts . noCleanData ) ) resetDbpath ( opts . dbpath )
289+
290+ opts = MongoRunner . arrOptions ( "mongod" , opts )
291+ }
292+
293+ var mongod = startMongoProgram . apply ( null , opts )
294+ mongod . commandLine = MongoRunner . arrToOpts ( opts )
295+ mongod . name = ( useHostName ? getHostName ( ) : "localhost" ) + ":" + mongod . commandLine . port
296+ mongod . host = mongod . name
297+ mongod . port = parseInt ( mongod . commandLine . port )
298+ mongod . runId = runId || ObjectId ( )
299+ mongod . savedOptions = MongoRunner . savedOptions [ mongod . runId ]
300+
301+ return mongod
302+ }
303+
304+ MongoRunner . runMongos = function ( opts ) {
305+
306+ var useHostName = false
307+ var runId = null
308+ if ( isObject ( opts ) ) {
309+
310+ opts = MongoRunner . mongosOptions ( opts )
311+
312+ useHostName = opts . useHostName || opts . useHostname
313+ runId = opts . runId
314+
315+ opts = MongoRunner . arrOptions ( "mongos" , opts )
316+ }
317+
318+ var mongos = startMongoProgram . apply ( null , opts )
319+ mongos . commandLine = MongoRunner . arrToOpts ( opts )
320+ mongos . name = ( useHostName ? getHostName ( ) : "localhost" ) + ":" + mongos . commandLine . port
321+ mongos . host = mongos . name
322+ mongos . port = parseInt ( mongos . commandLine . port )
323+ mongos . runId = runId || ObjectId ( )
324+ mongos . savedOptions = MongoRunner . savedOptions [ mongos . runId ]
325+
326+ return mongos
327+ }
328+
329+ MongoRunner . stopMongod = function ( port , signal ) {
330+
331+ signal = signal || 15
332+
333+ if ( port . port )
334+ port = parseInt ( port . port )
335+
336+ if ( port instanceof ObjectId ) {
337+ var opts = MongoRunner . savedOptions ( port )
338+ if ( opts ) port = parseInt ( opts . port )
339+ }
340+
341+ return stopMongod ( parseInt ( port ) , parseInt ( signal ) )
342+ }
343+
344+ MongoRunner . stopMongos = MongoRunner . stopMongod
345+
75346__nextPort = 27000 ;
76347startMongodTest = function ( port , dirname , restart , extraOptions ) {
77348 if ( ! port )
0 commit comments