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

Allocate fixes #528

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions app/controllers/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class ResourcesController extends DefaultController {
next();
}

static alloc(req, res) {
req.Resource.alloc((error) => {
static allocate(req, res) {
req.Resource.allocate((error) => {
if (error) {
res.status(500).json({error: error});
} else {
Expand Down
25 changes: 13 additions & 12 deletions app/routes/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,15 @@ function Route(app) {
.get(controller.find.bind(controller))
.post(controller.create.bind(controller));

router.route('/api/v0/resources/allocation')
// .get( controller.list_allocs )
.put(ResourceController.allocMultiple);

// router.route('/api/v0/resources/allocation/:Alloc')
// .get( controller.list_alloc_resources );

router.route('/api/v0/resources/allocation/:Alloc/release')
.put(ResourceController.releaseMultiple);
router.route('/api/v0/resources/:Resource/allocate')
.put(ResourceController.allocate);

router.route('/api/v0/resources/:Resource.:format?')
.all(controller.all.bind(controller))
.get(controller.get.bind(controller))
.put(controller.update.bind(controller))
.delete(controller.remove.bind(controller));

router.route('/api/v0/resources/:Resource/alloc')
.put(ResourceController.alloc);

router.route('/api/v0/resources/:Resource/release')
.put(ResourceController.release);

Expand All @@ -41,6 +31,17 @@ function Route(app) {
router.route('/api/v0/resources/:Resource/route')
.get(ResourceController.solveRoute);

// allocations api
router.route('/api/v0/allocations')
// .get( controller.list_allocs )
.put(ResourceController.allocMultiple);

// router.route('/api/v0/allocations/:Alloc')
// .get( controller.list_alloc_resources );

router.route('/api/v0/allocations/:Alloc/release')
.put(ResourceController.releaseMultiple);

app.use(router);
}

Expand Down
10 changes: 9 additions & 1 deletion app/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ function Route(app) {
User.isEmpty()
.then((empty) => {
if (empty) { return createDefaultAdmin(); }
return Promise.resolve();
return User
.find({name: 'admin'}).select('+password')
.then(([user]) => {
if (!user.password) {
logger.debug('Admin doesnt have password, set default one: "admin"');
user.password = 'admin';
return user.save();
}
});
})
.catch(error => logger.warn(error));

Expand Down
18 changes: 6 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 39 additions & 49 deletions test/tests_api/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,39 @@ const getResource = resourceId => superagent.get(`${api}/resources/${resourceId}
return res.body;
});

function createResource({sn}) {
const body = {name: 'dev1', type: 'dut', hw: {sn}};
return superagent.post(`${api}/resources`).send(body);
}

describe('Resource', function () {
let resourceId;

let authString;
before(function () {
authString = getTestUserToken();
});
beforeEach(async function () {
const {body} = await createResource({sn: 'SerialNumber'});
resourceId = body._id;
});
afterEach(async function () {
await superagent.del(`${api}/resources/${resourceId}`);
resourceId = null;
});

it('add resource', function () {
const body = {
name: 'dev1',
type: 'dut',
hw: {
sn: 'SerialNumber'
}
};
return superagent.post(`${api}/resources`)
.send(body)
.end(function (error, res) {
expect(error).to.equal(null);
expect(res).to.be.a('Object');
if (res.status === 300) {
logger.warn('Seems that your DB is not clean!');
process.exit(1);
}
expect(res).to.have.property('status', 200);
expect(res.body).to.have.property('name');
expect(res.body).to.have.property('type');
expect(res.body).to.have.property('_id');
expect(res.body._id).to.be.an('string');
expect(res.body.name).to.equal('dev1');
expect(res.body.type).to.equal('dut');
resourceId = res.body._id;
});
it('add resource', async function () {
const res = await createResource({sn: 'SerialNumber2'})
expect(res).to.be.a('Object');
expect(res).to.have.property('status', 200);
expect(res.body).to.have.property('name');
expect(res.body).to.have.property('type');
expect(res.body).to.have.property('_id');
expect(res.body._id).to.be.an('string');
expect(res.body.name).to.equal('dev1');
expect(res.body.type).to.equal('dut');

await superagent.del(`${api}/resources/${res.body._id}`);
});

it('get resource', function () {
Expand Down Expand Up @@ -133,36 +132,18 @@ describe('Resource', function () {
return getResource(resourceId)
.then(doUpdate);
});
it('remove resource', function () {
return superagent.del(`${api}/resources/${resourceId}`)
it('remove resource', async function () {
const {body} = await createResource({sn: '1'})
const {_id} = body
return superagent.del(`${api}/resources/${_id}`)
.end(function (error, res) {
expect(error).to.equal(null);
expect(res).to.be.a('Object');
expect(res.status).to.be.equal(200);
});
});
describe('events', function () {
before(function () {
const body = {
name: 'dev1',
type: 'dut',
hw: {
sn: 'SerialNumber'
}
};
return superagent.post(`${api}/resources`)
.send(body)
.end(function (error, res) {
resourceId = res.body._id;
});
});
after(function () {
return superagent.del(`${api}/resources/${resourceId}`)
.end(function (error, res) {
expect(error).to.equal(null);
expect(res.status).to.be.equal(200);
});
});

it('events', function () {
return superagent
.get(`${api}/resources/${resourceId}/events`)
Expand Down Expand Up @@ -192,4 +173,13 @@ describe('Resource', function () {
});
});
});
describe('allocation', function () {
it('allocate and release', async function () {
let url = `${api}/resources/${resourceId}/allocate`;
await superagent.put(url).send({});

url = `${api}/resources/${resourceId}/release`;
await superagent.put(url).send({});
});
});
});
12 changes: 6 additions & 6 deletions test/unittests/controller/resources.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable func-names, prefer-arrow-callback, no-unused-expressions */

// Third party components
const chai = require('chai');
const sinon = require('sinon');
const Promise = require('bluebird');

// Local components
const chai = require('../../chai');
const {setup, reset, teardown} = require('../../utils/mongomock');
require('./../../../app/models/resource.js');
const ResourceController = require('./../../../app/controllers/resources.js');
Expand Down Expand Up @@ -207,10 +207,10 @@ describe('controllers/resources.js', function () {
]);
});

it('alloc', function () {
it('allocate', function () {
// Allocate returns error
const errorPromise = new Promise((resolve) => {
const req = {Resource: {alloc: (cb) => {
const req = {Resource: {allocate: (cb) => {
cb(new Error('Cannot allocate this resource'));
}}};

Expand All @@ -221,12 +221,12 @@ describe('controllers/resources.js', function () {
expect(value).to.equal(500);
});

ResourceController.alloc(req, res);
ResourceController.allocate(req, res);
});

// Allocate is successful
const successPromise = new Promise((resolve) => {
const req = {Resource: {alloc: (cb) => {
const req = {Resource: {allocate: (cb) => {
req.allocated = 'allocation';
cb(undefined, 'document');
}}};
Expand All @@ -236,7 +236,7 @@ describe('controllers/resources.js', function () {
resolve();
});

ResourceController.alloc(req, res);
ResourceController.allocate(req, res);
});

// Resolve all promises
Expand Down