Skip to content

Commit

Permalink
Merge pull request #130 from dappnode/dev
Browse files Browse the repository at this point in the history
minor bug fixing

Former-commit-id: d2116a2
  • Loading branch information
eduadiez authored Nov 14, 2018
2 parents 83b1958 + 64c44d2 commit 5893ecb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 30 deletions.
23 changes: 15 additions & 8 deletions build/src/src/calls/restartPackageVolumes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs');
const getPath = require('utils/getPath');
const parse = require('utils/parse');
const params = require('params');
const docker = require('modules/docker');
const dockerList = require('modules/dockerList');
const {eventBus, eventBusTag} = require('eventBus');


Expand All @@ -18,34 +18,41 @@ const {eventBus, eventBusTag} = require('eventBus');
async function restartPackageVolumes({
id,
}) {
const dnpList = await dockerList.listContainers();
const dnp = dnpList.find((_dnp) => _dnp.name && _dnp.name.includes(id));
if (!dnp) {
throw Error(`Could not found an container with the name: ${id}`);
}
const dockerComposePath = getPath.dockerComposeSmart(id, params);
if (!fs.existsSync(dockerComposePath)) {
throw Error('No docker-compose found: ' + dockerComposePath);
}

if (id.includes('dappmanager.dnp.dappnode.eth')) {
throw Error('The installer cannot be restarted');
}

const packageVolumes = parse.serviceVolumes(dockerComposePath, id);

// If there are no volumes don't do anything
if (!packageVolumes.length) {
if (!dnp.volumes || !dnp.volumes.length) {
return {
message: id+' has no volumes ',
};
}

await docker.compose.down(dockerComposePath, {volumes: true});

if (dnp.isCORE) {
// docker-compose down can't be called because of the shared network
await docker.compose.rm(dockerComposePath);
await docker.volume.rm(dnp.volumes.map((v) => v.name).join(' '));
} else {
await docker.compose.down(dockerComposePath, {volumes: true});
}
// Restart docker to apply changes
await docker.compose.up(dockerComposePath);

// Emit packages update
eventBus.emit(eventBusTag.emitPackages);

return {
message: 'Restarted '+id+' volumes: ' + packageVolumes.join(', '),
message: 'Restarted '+id+' volumes',
logMessage: true,
userAction: true,
};
Expand Down
1 change: 1 addition & 0 deletions build/src/src/modules/dockerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function format(c) {
name: name,
shortName: shortName,
ports: mapPorts(c.Ports),
volumes: c.Mounts.map(({Name, Source}) => ({name: Name, path: Source})),
state: c.State,
running: !/^Exited /i.test(c.Status),
};
Expand Down
3 changes: 2 additions & 1 deletion build/src/src/utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ function envFile(envFileData) {
.split('\n')
.filter((row) => row.length > 0 )
.map((row) => {
res[row.split('=')[0]] = row.split('=')[1];
const [key, value] = row.split(/=(.+)/);
res[key] = value;
});

return res;
Expand Down
77 changes: 57 additions & 20 deletions build/src/test/calls/restartPackageVolumes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const sinon = require('sinon');
const fs = require('fs');
const getPath = require('utils/getPath');
const validate = require('utils/validate');
const docker = require('modules/docker');
const parse = require('utils/parse');

chai.should();

Expand All @@ -17,36 +15,75 @@ describe('Call function: restartPackageVolumes', function() {
};

const PACKAGE_NAME = 'test.dnp.dappnode.eth';
const CORE_PACKAGE_NAME = 'testCore.dnp.dappnode.eth';
const DOCKERCOMPOSE_PATH = getPath.dockerCompose(PACKAGE_NAME, params);
const CORE_DOCKERCOMPOSE_PATH = getPath.dockerCompose(CORE_PACKAGE_NAME, params);

const docker = {
compose: {
rm: sinon.stub(),
down: sinon.stub(),
up: sinon.stub(),
},
volume: {
rm: sinon.stub(),
},
};
// Declare stub behaviour. If done chaining methods, sinon returns an erorr:

const dockerList = {
listContainers: async () => ([
{
name: CORE_PACKAGE_NAME,
isCORE: true,
volumes: [
{name: 'vol1'},
{name: 'vol2'},
],
},
{
name: PACKAGE_NAME,
volumes: [
{name: 'vol3'},
],
},
]),
};

const restartPackageVolumes = proxyquire('calls/restartPackageVolumes', {
'modules/docker': docker,
'modules/dockerList': dockerList,
'params': params,
});

before(() => {
validate.path(DOCKERCOMPOSE_PATH);
fs.writeFileSync(DOCKERCOMPOSE_PATH, 'docker-compose');
for (const path of [DOCKERCOMPOSE_PATH, CORE_DOCKERCOMPOSE_PATH]) {
validate.path(path);
fs.writeFileSync(path, 'docker-compose');
}
});

it('should remove the package volumes of a CORE', async () => {
const res = await restartPackageVolumes({id: CORE_PACKAGE_NAME});
// sinon.assert.called(docker.compose.rm);
sinon.assert.called(docker.compose.rm);
sinon.assert.calledWith(docker.volume.rm, 'vol1 vol2');
sinon.assert.called(docker.compose.up);
expect(res).to.be.ok;
expect(res).to.have.property('message');
});

it('should remove the package volumes', async () => {
// Mock docker
sinon.replace(docker.volume, 'rm', sinon.fake());
sinon.replace(docker.compose, 'rm', sinon.fake());
sinon.replace(docker.compose, 'up', sinon.fake());
// Mock parse
const packageVolumes = ['vol1', 'vol2'];
sinon.replace(parse, 'serviceVolumes', sinon.fake.returns(packageVolumes));
const restartPackageVolumes = proxyquire('calls/restartPackageVolumes', {
'utils/parse': parse,
'modules/docker': docker,
'params': params,
});
let res = await restartPackageVolumes({id: PACKAGE_NAME});
it('should remove the package volumes of a NOT CORE', async () => {
const res = await restartPackageVolumes({id: PACKAGE_NAME});
// sinon.assert.called(docker.compose.rm);
sinon.assert.calledWith(docker.volume.rm.firstCall, 'vol1');
sinon.assert.calledWith(docker.volume.rm.secondCall, 'vol2');
sinon.assert.called(docker.compose.down);
sinon.assert.called(docker.compose.up);
expect(res).to.be.ok;
expect(res).to.have.property('message');
});

after(() => {
fs.unlinkSync(DOCKERCOMPOSE_PATH);
fs.unlinkSync(CORE_DOCKERCOMPOSE_PATH);
});
});
1 change: 0 additions & 1 deletion build/src/test/calls/updatePackageEnv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe('Call function: updatePackageEnv', function() {
});
// Verify
// docker.compose should be used to reset the package
sinon.assert.calledWith(docker.compose.down, dockerComposePath);
sinon.assert.calledWith(docker.compose.up, dockerComposePath);
// The envs should have been written
let envString = fs.readFileSync(envFilePath, 'utf8');
Expand Down

0 comments on commit 5893ecb

Please sign in to comment.