Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a better summary to the end #9

Merged
merged 3 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const backup = async () => {

// Backup containers
return Promise.all(containers.map(
logAndReturnErrors(backupContainer),
async container => ({
name: container,
result: await logAndReturnErrors(backupContainer)(container),
}),
));
};

Expand All @@ -24,7 +27,10 @@ const restore = async () => {

// Restore containers
return Promise.all(containers.map(
logAndReturnErrors(restoreContainer),
async container => ({
name: container,
result: await logAndReturnErrors(restoreContainer)(container),
}),
));
};

Expand Down
17 changes: 11 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ module.exports = async () => {
// eslint-disable-next-line no-console
console.log('== Done ==');

// Check if we had any errors and throw them if we did
const errors = results.filter(result => result instanceof Error);
if (errors.length) {
const errorHeader = '\nThe following errors occurred during the run (this does not include errors from the tar command used for volume backup/restore):\n';
const errorMessages = errors.map(e => e.message).join('\n');
throw new Error(errorHeader + errorMessages);
// Print a summary of the results if there are any
if (results.length) {
// eslint-disable-next-line no-console
console.log('\nSummary (does not include errors from tar command used for volume operations):');
results.forEach(({ name, result }) => {
const success = !(result instanceof Error);
const mark = success ? '✔' : '✖';
const message = success ? 'Success!' : result.message;
// eslint-disable-next-line no-console
console.log(` ${mark} ${name}: ${message}`);
});
}

return results;
Expand Down
50 changes: 24 additions & 26 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ describe('backup', () => {

const result = await main();

expect(result).toEqual([true, true, true]);
expect(result).toEqual([
{ name: 6, result: true },
{ name: 7, result: true },
{ name: 8, result: true },
]);
expect(docker.getContainers).toHaveBeenCalledTimes(1);
expect(docker.backupContainer).toHaveBeenCalledTimes(3);
expect(docker.backupContainer)
.toHaveBeenLastCalledWith(8, expect.any(Number), expect.any(Array));
expect(docker.backupContainer).toHaveBeenLastCalledWith(8);
});

it('should backup the given container', async () => {
Expand All @@ -32,24 +35,20 @@ describe('backup', () => {

expect(docker.getContainers).toHaveBeenCalledTimes(0);
expect(docker.backupContainer).toHaveBeenCalledTimes(1);
expect(docker.backupContainer)
.toHaveBeenLastCalledWith('pear', expect.any(Number), expect.any(Array));
expect(docker.backupContainer).toHaveBeenLastCalledWith('pear');
});

it('should catch errors and return false', async () => {
expect.assertions(1);
it('should print summary', async () => {
global.console.log = jest.fn();
const options = require('../src/modules/options');
options.containers = ['pear'];
const docker = require('../src/modules/docker');
docker.backupContainer = () => { throw new Error('Mock backup error'); };
const main = require('../src/main');

try {
await main();
} catch (e) {
expect(e.message)
.toBe('\nThe following errors occurred during the run (this does not include errors from the tar command used for volume backup/restore):\nMock backup error');
}
await main();

expect(global.console.log).toHaveBeenLastCalledWith(' ✖ pear: Mock backup error');
});
});

Expand All @@ -64,12 +63,15 @@ describe('restore', () => {

const result = await main();

expect(result).toEqual([true, true, true]);
expect(result).toEqual([
{ name: 'a', result: true },
{ name: 'b', result: true },
{ name: 'c', result: true },
]);
expect(fs.readdirSync).toHaveBeenCalledTimes(1);
expect(fs.readdirSync).toHaveBeenCalledWith('/folder/containers');
expect(docker.restoreContainer).toHaveBeenCalledTimes(3);
expect(docker.restoreContainer)
.toHaveBeenLastCalledWith('c', expect.any(Number), expect.any(Array));
expect(docker.restoreContainer).toHaveBeenLastCalledWith('c');
});

it('should restore the given container', async () => {
Expand All @@ -84,24 +86,20 @@ describe('restore', () => {

expect(fs.readdir).toHaveBeenCalledTimes(0);
expect(docker.restoreContainer).toHaveBeenCalledTimes(1);
expect(docker.restoreContainer)
.toHaveBeenLastCalledWith('mango', expect.any(Number), expect.any(Array));
expect(docker.restoreContainer).toHaveBeenLastCalledWith('mango');
});

it('should catch errors and return false', async () => {
expect.assertions(1);
it('should print summary', async () => {
global.console.log = jest.fn();
const options = require('../src/modules/options');
options.operation = 'restore';
options.containers = ['pear'];
const docker = require('../src/modules/docker');
docker.restoreContainer = () => { throw new Error('Mock restore error'); };
const main = require('../src/main');

try {
await main();
} catch (e) {
expect(e.message)
.toBe('\nThe following errors occurred during the run (this does not include errors from the tar command used for volume backup/restore):\nMock restore error');
}
await main();

expect(global.console.log).toHaveBeenLastCalledWith(' ✖ pear: Mock restore error');
});
});