Skip to content

Instantly share code, notes, and snippets.

@davidglivar
Created November 9, 2016 18:40
Show Gist options
  • Save davidglivar/deddcde9897ed621724e84b8c59304a5 to your computer and use it in GitHub Desktop.
Save davidglivar/deddcde9897ed621724e84b8c59304a5 to your computer and use it in GitHub Desktop.
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