1
0
Fork 0
mirror of https://github.com/mmumshad/ansible-playable.git synced 2025-03-09 23:38:54 +00:00

Update server side test cases and disable client side tests for now. Integrate later.

This commit is contained in:
Mumshad Mannambeth 2017-07-07 12:23:45 -04:00
parent ab295a073a
commit 9c880c0bba
13 changed files with 588 additions and 318 deletions

View file

@ -93,43 +93,38 @@ export function index(req, res) {
.catch(handleError(res));*/
}
// Gets a single CustomModule from the DB
// Gets a single CustomModule or a module_template from DB
export function show(req, res) {
console.log("Show " + req.params.custom_module);
var ansibleEngine = req.body.ansibleEngine;
if(!ansibleEngine.customModules){
res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
return res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
var command = 'cat "' + ansibleEngine.customModules + '"/' + req.params.custom_module;
// If request is for module template, return module_template from default path
if(req.params.custom_module === 'template.py'){
//command = 'cat ' + '/opt/ehc-builder-scripts/ansible_modules/template.py';
return require('fs').readFile('./helpers/module_template.py', (err, data) => {
if (err) res.status(500).send(data);
if (err) return res.status(500).send(data);
res.send(data);
});
}else{
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
/*return CustomModule.findById(req.params.custom_module).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));*/
}
// Test Module
@ -145,7 +140,7 @@ export function testModule(req, res) {
var test_module = '/tmp/test-module';
var command = 'chmod 755 ' + test_module + '; ' + test_module + ' -m "' + ansibleEngine.customModules + '/' + req.params.custom_module + "\" -a '" + JSON.stringify(moduleArgs) + "'";
var command = 'chmod 755 ' + test_module + '; python ' + test_module + ' -m "' + ansibleEngine.customModules + '/' + req.params.custom_module + "\" -a '" + JSON.stringify(moduleArgs) + "'";
scp2_exec.copyFileToScriptEngine('./helpers/test-module',test_module,ansibleEngine,function(){
console.log("Command=" + command);
@ -174,8 +169,6 @@ export function testModule(req, res) {
// Creates a new CustomModule in the DB
export function create(req, res) {
console.log("Create");
var custom_module_name = req.params.custom_module;
var custom_module_code = req.body.custom_module_code;

View file

@ -4,187 +4,168 @@
var app = require('../..');
import request from 'supertest';
import User from '../user/user.model';
var newCustomModule;
describe('CustomModule API:', function() {
describe('GET /api/custom_modules', function() {
var token;
var user;
// Clear users before testing
before(function() {
return User.remove().then(function() {
user = new User({
name: 'Fake User',
email: 'test@example.com',
password: 'password'
});
return user.save();
});
});
// Clear users after testing
after(function() {
return User.remove();
});
describe('GET /api/users/me', function() {
before(function(done) {
request(app)
.post('/auth/local')
.send({
email: 'test@example.com',
password: 'password'
})
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
token = res.body.token;
done();
});
});
it('should respond with a user profile when authenticated', function(done) {
request(app)
.get('/api/users/me')
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
expect(res.body._id.toString()).to.equal(user._id.toString());
done();
});
});
it('should respond with a 401 when not authenticated', function(done) {
request(app)
.get('/api/users/me')
.expect(401)
.end(done);
});
});
describe('POST /api/custom_modules/template.py/get', function() {
var customModules;
beforeEach(function(done) {
request(app)
.get('/api/custom_modules')
.post('/api/custom_modules/template.py/get')
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
customModules = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(customModules).to.be.instanceOf(Array);
});
});
describe('POST /api/custom_modules', function() {
beforeEach(function(done) {
request(app)
.post('/api/custom_modules')
.send({
name: 'New CustomModule',
info: 'This is the brand new customModule!!!'
ansibleEngine: {
host : '',
customModules: '/'
}
})
.expect(201)
.expect('Content-Type', /json/)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newCustomModule = res.body;
customModules = res.text;
done();
});
});
it('should respond with the newly created customModule', function() {
expect(newCustomModule.name).to.equal('New CustomModule');
expect(newCustomModule.info).to.equal('This is the brand new customModule!!!');
it('should respond with module_template', function() {
expect(customModules).to.contain('import AnsibleModule');
});
});
describe('GET /api/custom_modules/:id', function() {
var customModule;
describe('POST /api/custom_modules/test_module.py', function() {
var customModules;
beforeEach(function(done) {
request(app)
.get(`/api/custom_modules/${newCustomModule._id}`)
.post('/api/custom_modules/test_module.py')
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
customModule = res.body;
done();
});
});
afterEach(function() {
customModule = {};
});
it('should respond with the requested customModule', function() {
expect(customModule.name).to.equal('New CustomModule');
expect(customModule.info).to.equal('This is the brand new customModule!!!');
});
});
describe('PUT /api/custom_modules/:id', function() {
var updatedCustomModule;
beforeEach(function(done) {
request(app)
.put(`/api/custom_modules/${newCustomModule._id}`)
.send({
name: 'Updated CustomModule',
info: 'This is the updated customModule!!!'
ansibleEngine: {
host : '',
customModules: '/tmp'
},
custom_module_code: '#!/usr/bin/python\n' +
'\n' +
'import datetime\n' +
'import json\n' +
'\n' +
'date = str(datetime.datetime.now())\n' +
'print(json.dumps({\n' +
'"time" : date\n' +
'}))'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedCustomModule = res.body;
done();
});
});
afterEach(function() {
updatedCustomModule = {};
});
it('should respond with the updated customModule', function() {
expect(updatedCustomModule.name).to.equal('Updated CustomModule');
expect(updatedCustomModule.info).to.equal('This is the updated customModule!!!');
});
it('should respond with the updated customModule on a subsequent GET', function(done) {
request(app)
.get(`/api/custom_modules/${newCustomModule._id}`)
.expect(200)
.expect('Content-Type', /json/)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let customModule = res.body;
expect(customModule.name).to.equal('Updated CustomModule');
expect(customModule.info).to.equal('This is the updated customModule!!!');
customModules = res.text;
done();
});
});
it('should respond with "Saved"', function() {
expect(customModules).to.contain('Saved');
});
});
describe('PATCH /api/custom_modules/:id', function() {
var patchedCustomModule;
describe('POST /api/custom_modules/test_module.py/test', function() {
var result;
beforeEach(function(done) {
request(app)
.patch(`/api/custom_modules/${newCustomModule._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched CustomModule' },
{ op: 'replace', path: '/info', value: 'This is the patched customModule!!!' }
])
.post('/api/custom_modules/test_module.py/test')
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
.send({
ansibleEngine: {
host : '',
customModules: '/tmp'
},
moduleArgs: {}
})
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
patchedCustomModule = res.body;
result = res;
done();
});
});
afterEach(function() {
patchedCustomModule = {};
});
it('should respond with the patched customModule', function() {
expect(patchedCustomModule.name).to.equal('Patched CustomModule');
expect(patchedCustomModule.info).to.equal('This is the patched customModule!!!');
it('should respond with 200', function() {
expect(result.status).to.equal(200);
});
});
describe('DELETE /api/custom_modules/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/custom_modules/${newCustomModule._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
//TODO: Add more test cases
it('should respond with 404 when customModule does not exist', function(done) {
request(app)
.delete(`/api/custom_modules/${newCustomModule._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -5,7 +5,7 @@ var controller = require('./custom_module.controller');
var router = express.Router();
router.post('/query', controller.index);
router.post('/list', controller.index);
router.post('/:custom_module/test', controller.testModule);
router.post('/:custom_module/get', controller.show);
router.post('/:custom_module', controller.create);

View file

@ -36,10 +36,10 @@ describe('CustomModule API Router:', function() {
expect(customModuleIndex).to.equal(routerStub);
});
describe('GET /api/custom_modules/query', function() {
describe('GET /api/custom_modules/list', function() {
it('should route to customModule.controller.index', function() {
expect(routerStub.post
.withArgs('/query', 'customModuleCtrl.index')
.withArgs('/list', 'customModuleCtrl.index')
).to.have.been.calledOnce;
});
});