Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/sequelize/managers/iofog-provision-key-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*
*/

const BaseManager = require('../managers/base-manager')
const BaseManager = require('../managers/base-manager');
const models = require('./../models');
const FogProvisionKey = models.FogProvisionKey
const FogProvisionKey = models.FogProvisionKey;

class FogProvisionKeyManager extends BaseManager {
getEntity() {
return FogProvisionKey
}
}

const instance = new FogProvisionKeyManager()
module.exports = instance
const instance = new FogProvisionKeyManager();
module.exports = instance;
8 changes: 4 additions & 4 deletions src/sequelize/managers/iofog-version-command-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*
*/

const BaseManager = require('../managers/base-manager')
const BaseManager = require('../managers/base-manager');
const models = require('./../models');
const FogVersionCommand = models.FogVersionCommand
const FogVersionCommand = models.FogVersionCommand;

class FogVersionCommandManager extends BaseManager {
getEntity() {
return FogVersionCommand
}
}

const instance = new FogVersionCommandManager()
module.exports = instance
const instance = new FogVersionCommandManager();
module.exports = instance;
8 changes: 5 additions & 3 deletions src/services/agent-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const formidable = require('formidable');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

const IncomingForm = formidable.IncomingForm;

const agentProvision = async function (provisionData, transaction) {

await Validator.validate(provisionData, Validator.schemas.agentProvision);
Expand Down Expand Up @@ -398,16 +400,16 @@ const putImageSnapshot = async function (req, fog, transaction) {
throw new Errors.ValidationError(ErrorMessages.INVALID_CONTENT_TYPE);
}

const form = new formidable.IncomingForm(opts);
const form = new IncomingForm(opts);
form.uploadDir = path.join(appRoot, '../') + 'data';
if (!fs.existsSync(form.uploadDir)) {
fs.mkdirSync(form.uploadDir);
}
await saveSnapShot(req, form,fog, transaction);
await _saveSnapShot(req, form, fog, transaction);
return {};
};

const saveSnapShot = function (req, form, fog, transaction) {
const _saveSnapShot = function (req, form, fog, transaction) {
return new Promise((resolve, reject) => {
form.parse(req, async function (error, fields, files) {
const file = files['upstream'];
Expand Down
306 changes: 305 additions & 1 deletion test/src/services/agent-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ const TunnelManager = require('../../../src/sequelize/managers/tunnel-manager');
const StraceManager = require('../../../src/sequelize/managers/strace-manager');
const ioFogVersionCommandManager = require('../../../src/sequelize/managers/iofog-version-command-manager');
const ioFogProvisionKeyManager = require('../../../src/sequelize/managers/iofog-provision-key-manager');
const BaseManager = require('../../../src/sequelize/managers/base-manager');
const HWInfoManager = require('../../../src/sequelize/managers/hw-info-manager');
const USBInfoManager = require('../../../src/sequelize/managers/usb-info-manager');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const path = require('path');
const formidable = ('./incoming_form');
const IncomingForm = formidable.IncomingForm;

global.appRoot = path.resolve(__dirname);


describe('Agent Service', () => {
Expand Down Expand Up @@ -1046,6 +1052,304 @@ describe('Agent Service', () => {
});
});

describe('.getAgentChangeVersionCommand()', () => {
const transaction = {};
const error = 'Error!';

def('uuid', () => 'testUuid');

def('fog', () => ({
uuid: $uuid
}));

def('versionCommandLine', () => 'testVersionCommand');
def('versionCommand', () => ({
versionCommand: $versionCommandLine
}));

def('provisionKey', () => 'testKey');
def('expirationTime', () => 12535352525);
def('provision', () => ({
provisionKey: $provisionKey,
expirationTime: $expirationTime
}));

def('response', () => ({
versionCommand: $versionCommandLine,
provisionKey: $provisionKey,
expirationTime: $expirationTime
}));

def('subject', () => $subject.getAgentChangeVersionCommand($fog, transaction));

def('findCommandResponse', () => Promise.resolve($versionCommand));
def('findProvisionResponse', () => Promise.resolve($provision));

beforeEach(() => {
$sandbox.stub(ioFogVersionCommandManager, 'findOne').returns($findCommandResponse);
$sandbox.stub(ioFogProvisionKeyManager, 'findOne').returns($findProvisionResponse);
});

it('calls ioFogVersionCommandManager#findOne() with correct args', async () => {
await $subject;
expect(ioFogVersionCommandManager.findOne).to.have.been.calledWith({
iofogUuid: $uuid
}, transaction);
});

context('when ioFogVersionCommandManager#findOne() fails', () => {
def('findCommandResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.rejectedWith(error);
})
});

context('when ioFogVersionCommandManager#findOne() succeeds', () => {
it('calls ioFogProvisionKeyManager#findOne() with correct args', async () => {
await $subject;
expect(ioFogProvisionKeyManager.findOne).to.have.been.calledWith({
iofogUuid: $uuid
}, transaction);

context('when ioFogProvisionKeyManager#findOne() fails', () => {
def('findProvisionResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.equal(undefined);
})
});

context('when ioFogProvisionKeyManager#findOne() succeeds', () => {
it(`succeeds`, () => {
return expect($subject).to.equal($response);
})
});
});
});
});

describe('.updateHalHardwareInfo()', () => {
const transaction = {};
const error = 'Error!';

def('uuid', () => 'testUuid');

def('fog', () => ({
uuid: $uuid
}));

def('info', () => 'testInfo');
def('hardwareData', () => ({
info: $info
}));

def('response', () => ({
versionCommand: $versionCommandLine,
provisionKey: $provisionKey,
expirationTime: $expirationTime
}));

def('subject', () => $subject.updateHalHardwareInfo($hardwareData, $fog, transaction));

def('validatorResponse', () => Promise.resolve(true));
def('hwResponse', () => Promise.resolve());

beforeEach(() => {
$sandbox.stub(Validator, 'validate').returns($validatorResponse);
$sandbox.stub(HWInfoManager, 'updateOrCreate').returns($hwResponse);
});

it('calls Validator#validate() with correct args', async () => {
await $subject;
expect(Validator.validate).to.have.been.calledWith($hardwareData, Validator.schemas.updateHardwareInfo);
});

context('when Validator#validate() fails', () => {
def('validatorResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.rejectedWith(error);
})
});

context('when Validator#validate() succeeds', () => {
it('calls HWInfoManager#updateOrCreate() with correct args', async () => {
await $subject;
expect(HWInfoManager.updateOrCreate).to.have.been.calledWith({
iofogUuid: $uuid
}, $hardwareData, transaction);

context('when HWInfoManager#updateOrCreate() fails', () => {
def('hwResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.equal(undefined);
})
});

context('when HWInfoManager#updateOrCreate() succeeds', () => {
it(`succeeds`, () => {
return expect($subject).to.equal(undefined);
})
});
});
});
});

describe('.updateHalUsbInfo()', () => {
const transaction = {};
const error = 'Error!';

def('uuid', () => 'testUuid');

def('fog', () => ({
uuid: $uuid
}));

def('info', () => 'testInfo');
def('usbData', () => ({
info: $info
}));

def('response', () => ({
versionCommand: $versionCommandLine,
provisionKey: $provisionKey,
expirationTime: $expirationTime
}));

def('subject', () => $subject.updateHalUsbInfo($usbData, $fog, transaction));

def('validatorResponse', () => Promise.resolve(true));
def('usbResponse', () => Promise.resolve());

beforeEach(() => {
$sandbox.stub(Validator, 'validate').returns($validatorResponse);
$sandbox.stub(USBInfoManager, 'updateOrCreate').returns($usbResponse);
});

it('calls Validator#validate() with correct args', async () => {
await $subject;
expect(Validator.validate).to.have.been.calledWith($usbData, Validator.schemas.updateUsbInfo);
});

context('when Validator#validate() fails', () => {
def('validatorResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.rejectedWith(error);
})
});

context('when Validator#validate() succeeds', () => {
it('calls USBInfoManager#updateOrCreate() with correct args', async () => {
await $subject;
expect(USBInfoManager.updateOrCreate).to.have.been.calledWith({
iofogUuid: $uuid
}, $usbData, transaction);

context('when USBInfoManager#updateOrCreate() fails', () => {
def('usbResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.equal(undefined);
})
});

context('when USBInfoManager#updateOrCreate() succeeds', () => {
it(`succeeds`, () => {
return expect($subject).to.equal(undefined);
})
});
});
});
});

describe('.deleteNode()', () => {
const transaction = {};
const error = 'Error!';

def('uuid', () => 'testUuid');

def('fog', () => ({
uuid: $uuid
}));

def('subject', () => $subject.deleteNode($fog, transaction));

def('deleteResponse', () => Promise.resolve($getStracesData));

beforeEach(() => {
$sandbox.stub(ioFogManager, 'delete').returns($deleteResponse);
});

it('calls ioFogManager#delete() with correct args', async () => {
await $subject;
expect(ioFogManager.delete).to.have.been.calledWith({
uuid: $uuid
}, transaction);
});

context('when ioFogManager#delete() fails', () => {
def('deleteResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.rejectedWith(error);
})
});

context('when ioFogManager#delete() succeeds', () => {
it(`succeeds`, () => {
return expect($subject).to.eventually.equal(undefined);
})
});
});

describe('.getImageSnapshot()', () => {
const transaction = {};
const error = 'Error!';

def('uuid', () => 'testUuid');

def('fog', () => ({
uuid: $uuid
}));

def('microserviceUuid', () => 'testMicroserviceUuid');

def('microserviceResponse', () => ({
uuid: $microserviceUuid
}));

def('subject', () => $subject.getImageSnapshot($fog, transaction));

def('findResponse', () => Promise.resolve($microserviceResponse));

beforeEach(() => {
$sandbox.stub(MicroserviceManager, 'findOne').returns($findResponse);
});

it('calls MicroserviceManager#delete() with correct args', async () => {
await $subject;
expect(MicroserviceManager.findOne).to.have.been.calledWith({
iofogUuid: $uuid,
imageSnapshot: 'get_image'
}, transaction);
});

context('when MicroserviceManager#delete() fails', () => {
def('findResponse', () => Promise.reject(error));

it(`fails with ${error}`, () => {
return expect($subject).to.be.rejectedWith(error);
})
});

context('when MicroserviceManager#delete() succeeds', () => {
it(`succeeds`, () => {
return expect($subject).to.eventually.have.property('uuid');
})
});
});

});