Created
November 9, 2016 18:40
-
-
Save davidglivar/deddcde9897ed621724e84b8c59304a5 to your computer and use it in GitHub Desktop.
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
const { exec } = require('child_process'); | |
const createContainer = () => ( | |
new Promise((resolve, reject) => { | |
exec('docker create --name test --label test busybox', (err, stdout) => { | |
if (err) return reject(err); | |
resolve(stdout); | |
}); | |
}) | |
); | |
const listContainers = () => ( | |
new Promise((resolve, reject) => { | |
exec('docker ps -a -q --filter "label=test"', (err, stdout) => { | |
if (err) return reject(err); | |
resolve(stdout); | |
}); | |
}) | |
); | |
const removeContainers = ids => ( | |
new Promise((resolve, reject) => { | |
if (ids.trim() === '') { | |
return process.nextTick(resolve.bind(null, '')); | |
} | |
exec(`docker rm -f ${ids}`, (err, stdout) => { | |
if (err) return reject(err); | |
resolve(stdout); | |
}); | |
}) | |
); | |
const startContainer = id => ( | |
new Promise((resolve, reject) => { | |
exec(`docker start ${id}`, (err, stdout) => { | |
if (err) return reject(err); | |
resolve(stdout); | |
}); | |
}) | |
) | |
const stopContainers = ids => ( | |
new Promise((resolve, reject) => { | |
if (ids.trim() === '') { | |
return process.nextTick(resolve.bind(null, '')); | |
} | |
exec(`docker stop ${ids}`, (err, stdout) => { | |
if (err) return reject(err); | |
resolve(ids); // chain ids | |
}); | |
}) | |
); | |
const attempts = process.env.ATTEMPTS || 10; | |
const loop = counter => { | |
console.log(`Attempt ${counter} of ${attempts}...`); | |
listContainers() | |
.then(ids => stopContainers(ids)) | |
.then(ids => removeContainers(ids)) | |
.then(() => createContainer()) | |
.then(id => startContainer(id)) | |
.then(() => { | |
if (counter >= attempts) { | |
return console.log('test complete.'); | |
} | |
loop(counter + 1); | |
}) | |
.catch(console.error); | |
}; | |
loop(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment