Last active
October 25, 2018 08:42
-
-
Save jonbern/ef9802d3fad84335ef544503a060a5e5 to your computer and use it in GitHub Desktop.
MongoDB replicaset for tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
const execSync = require('child_process').execSync; | |
const path = require('path'); | |
module.exports = version => { | |
const isWindows = process.platform === 'win32'; | |
const mongoPath = path.resolve(__dirname, `../../data/${version}`); | |
const mongod = `${mongoPath}/mongod${isWindows ? '.exe' : ''}`; | |
if (fs.existsSync(mongoPath)) { | |
return mongod; | |
} | |
const [downloadDirectory, mongodbArchiveFilename] = getPlatformSpecificFilePath(version); | |
const os = process.platform === 'darwin' ? 'osx' : process.platform; | |
console.log('MongoDB not found in ./data folder.'); | |
console.log(`Downloading MongoDB ${version}.`); | |
if (isWindows) { | |
execSync('powershell.exe -nologo -noprofile -command "&{' + | |
'Add-Type -AssemblyName System.IO.Compression.FileSystem;' + | |
`(New-Object Net.WebClient).DownloadFile('http://downloads.mongodb.org/${os}/${mongodbArchiveFilename}', '${mongodbArchiveFilename}');` + | |
`[System.IO.Compression.ZipFile]::ExtractToDirectory('${mongodbArchiveFilename}','.');` + | |
`mv '${path.resolve(__dirname, `../../${downloadDirectory}/bin`)}' '${mongoPath}';` + | |
`rd -r './${downloadDirectory}';` + | |
`rm '${path.resolve(__dirname, '../../filename')}';}"` | |
); | |
} else { | |
execSync(`curl -OL http://downloads.mongodb.org/${os}/${mongodbArchiveFilename}`); | |
execSync(`tar -zxvf ${mongodbArchiveFilename}`); | |
execSync(`mv ${path.resolve(__dirname, `../../${downloadDirectory}`)}/bin ${mongoPath}`); | |
execSync(`rm -rf ${downloadDirectory}`); | |
execSync(`rm ${path.resolve(__dirname, `../../${mongodbArchiveFilename}`)}`); | |
} | |
console.log(`Copied MongoDB ${version} to '${mongoPath}'`); | |
return mongod; | |
}; | |
const getPlatformSpecificFilePath = (version) => { | |
const os = process.platform; | |
switch(os) { | |
case 'linux': | |
return [`mongodb-linux-x86_64-${version}`, `mongodb-linux-x86_64-${version}.tgz`]; | |
case 'darwin': | |
return [`mongodb-osx-x86_64-${version}`, `mongodb-osx-ssl-x86_64-${version}.tgz`]; | |
case 'win32': | |
return [`mongodb-win32-x86_64-2008plus-ssl-${version}`, `mongodb-win32-x86_64-2008plus-ssl-${version}.zip`]; | |
default: | |
throw new Error(`Unrecognized os ${os}`); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const {ReplSet} = require('mongodb-topology-manager'); | |
const mongodbUri = require('mongodb-uri'); | |
const ensureMongoDB = require('./ensureMongoDB'); | |
const config = require('../config'); | |
let topology; | |
const start = async mongoUri => { | |
const mongod = ensureMongoDB(config.mongodb.version); | |
const uriParts = mongodbUri.parse(mongoUri); | |
const nodes = uriParts.hosts.map(host => { | |
return { | |
options: { | |
bind_ip: host.host, port: host.port, dbpath: `./data/${host.port}` | |
} | |
}; | |
}); | |
const replSetOptions = { | |
replSet: uriParts.options.replicaSet | |
}; | |
topology = new ReplSet(mongod, nodes, replSetOptions); | |
const info = await topology.discover(); | |
console.log('Replicaset info', info); | |
console.log('Purging replicaset...'); | |
await topology.purge(); | |
console.log(`Starting replicaset with ${uriParts.hosts.length} nodes...`, mongoUri); | |
await topology.start(); | |
console.log('Waiting for primary to be available...'); | |
await topology.waitForPrimary(); | |
console.log('Replicaset started'); | |
}; | |
const stop = async () => { | |
console.log('Shutting down replicaset...'); | |
await topology.stop(); | |
console.log('Replicaset stopped'); | |
}; | |
module.exports = { | |
start, | |
stop | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const config = require('./config'); | |
const replicaset = require('./replicaset'); | |
describe('Tests', () => { | |
before(async function() { | |
this.timeout(1000 * 60 * 5); | |
await replicaset.start(config.mongodb.uri); | |
}); | |
after(async function() { | |
this.timeout(1000 * 60 * 1); | |
await replicaset.stop(); | |
}); | |
// your tests here... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment