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

@ -86,7 +86,11 @@ export function command(req, res) {
)
}
// Creates a new Ansible in the DB
/**
* List Ansible Modules
* @param req
* @param res
*/
export function modules(req, res) {
var ansibleEngine = req.body.ansibleEngine;
@ -108,6 +112,7 @@ export function modules(req, res) {
// Gets a single Deploy from the DB
export function getLogs(req, res) {
console.log("Param ID " + req.params.id);
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(function(entity){
@ -169,6 +174,7 @@ export function execute(req, res) {
var resultSent = false;
// Execute Ansible Playbook and return immediately with a new Job (Ansible) object
ansibleTool.executeAnsible(logfilename, project_folder, playbook_name, inventory_file_name, tags_joined, limit_to_hosts_joined, verbose,check_mode,
function(data){
//res.write(data)
@ -244,6 +250,7 @@ export function playbook_create(req, res) {
function(data){
//res.write(data);
//res.end()
console.log("data = " + data);
if(!resultSent){
resultSent = true;
res.send(data)
@ -251,6 +258,7 @@ export function playbook_create(req, res) {
},
function(data){
//res.write(data)
console.log("data = " + data);
if(!resultSent){
resultSent = true;
res.status(500).send(data)

View file

@ -4,187 +4,318 @@
var app = require('../..');
import request from 'supertest';
import User from '../user/user.model';
var newAnsible;
describe('Ansible API:', function() {
describe('GET /api/ansible', function() {
var ansibles;
var token;
var user;
var ansible_job;
beforeEach(function(done) {
// 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)
.get('/api/ansible')
.post('/auth/local')
.send({
email: 'test@example.com',
password: 'password'
})
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansibles = res.body;
token = res.body.token;
done();
});
});
it('should respond with JSON array', function() {
expect(ansibles).to.be.instanceOf(Array);
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/ansible', function() {
describe('POST /modules/list', function() {
var modules;
beforeEach(function(done) {
request(app)
.post('/api/ansible')
.post('/api/ansible/modules/list')
.timeout(10000)
.set('authorization', `Bearer ${token}`)
.send({
name: 'New Ansible',
info: 'This is the brand new ansible!!!'
ansibleEngine: {
'host' : ''
}
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
modules = res.text;
done();
});
});
it('should respond with list of Ansible Modules (containing ping module)', function() {
expect(modules).to.contain('ping');
});
});
describe('POST /command to execute a sample command - echo "Hello World"', function() {
var modules;
beforeEach(function(done) {
request(app)
.post('/api/ansible/command')
.set('authorization', `Bearer ${token}`)
.send({
command: 'echo "Hello World"'
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
modules = res.text;
done();
});
});
it('should respond with the result of command', function() {
expect(modules).to.contain('Hello World');
});
});
describe('POST /inventory/create', function() {
var result;
beforeEach(function(done) {
request(app)
.post('/api/ansible/inventory/create')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
inventoryName: 'inventory.txt',
inventoryFileContents: 'localhost ansible_connection=local'
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
result = res.text;
done();
});
});
it('should respond with "file written"', function() {
expect(result).to.contain('file written');
});
});
describe('POST /playbook/create', function() {
var result;
beforeEach(function(done) {
request(app)
.post('/api/ansible/playbook/create')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
playbookName: 'test_playbook.yml',
playbookFileContents: '-\n' +
' name: "Test Play1"\n' +
' hosts: localhost\n' +
' tasks:\n' +
' - name: "Test Task1"\n' +
' ping:\n'
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
result = res.text;
done();
});
});
it('should respond with "file written"', function() {
expect(result).to.contain('file written');
});
});
describe('POST /execute', function() {
beforeEach(function(done) {
request(app)
.post('/api/ansible/execute')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp/'
},
selectedPlaybook: 'test_playbook.yml',
inventory_file_name: 'inventory.txt',
})
.expect(201)
.expect('Content-Type', /json/)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newAnsible = res.body;
ansible_job = res.body;
done();
});
});
it('should respond with the newly created ansible', function() {
expect(newAnsible.name).to.equal('New Ansible');
expect(newAnsible.info).to.equal('This is the brand new ansible!!!');
it('should respond with an Ansible Job object', function() {
expect(ansible_job.selectedPlaybook).to.equal('test_playbook.yml');
});
});
describe('GET /api/ansible/:id', function() {
var ansible;
describe('GET /:id', function() {
beforeEach(function(done) {
request(app)
.get(`/api/ansible/${newAnsible._id}`)
.get('/api/ansible/' + ansible_job._id)
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansible = res.body;
ansible_job = res.body;
console.log("ansible_job " + JSON.stringify(ansible_job));
done();
});
});
afterEach(function() {
ansible = {};
});
it('should respond with the requested ansible', function() {
expect(ansible.name).to.equal('New Ansible');
expect(ansible.info).to.equal('This is the brand new ansible!!!');
it('should respond with an Ansible Job object', function() {
expect(ansible_job.selectedPlaybook).to.equal('test_playbook.yml');
});
});
describe('PUT /api/ansible/:id', function() {
var updatedAnsible;
describe('POST /playbook/delete', function() {
var result;
beforeEach(function(done) {
request(app)
.put(`/api/ansible/${newAnsible._id}`)
.post('/api/ansible/playbook/delete')
.set('authorization', `Bearer ${token}`)
.send({
name: 'Updated Ansible',
info: 'This is the updated ansible!!!'
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
playbookName: 'test_playbook.yml',
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedAnsible = res.body;
done();
});
});
afterEach(function() {
updatedAnsible = {};
});
it('should respond with the updated ansible', function() {
expect(updatedAnsible.name).to.equal('Updated Ansible');
expect(updatedAnsible.info).to.equal('This is the updated ansible!!!');
});
it('should respond with the updated ansible on a subsequent GET', function(done) {
request(app)
.get(`/api/ansible/${newAnsible._id}`)
.expect(200)
.expect('Content-Type', /json/)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let ansible = res.body;
expect(ansible.name).to.equal('Updated Ansible');
expect(ansible.info).to.equal('This is the updated ansible!!!');
result = res;
done();
});
});
it('should respond with status code 200', function() {
expect(result.status).to.equal(200);
});
});
describe('PATCH /api/ansible/:id', function() {
var patchedAnsible;
describe('POST /inventory/delete', function() {
var result;
beforeEach(function(done) {
request(app)
.patch(`/api/ansible/${newAnsible._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Ansible' },
{ op: 'replace', path: '/info', value: 'This is the patched ansible!!!' }
])
.post('/api/ansible/inventory/delete')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
inventoryName: 'inventory.txt',
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
patchedAnsible = res.body;
result = res;
done();
});
});
afterEach(function() {
patchedAnsible = {};
});
it('should respond with the patched ansible', function() {
expect(patchedAnsible.name).to.equal('Patched Ansible');
expect(patchedAnsible.info).to.equal('This is the patched ansible!!!');
it('should respond with status code 200', function() {
expect(result.status).to.equal(200);
});
});
describe('DELETE /api/ansible/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/ansible/${newAnsible._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
//TODO: Add more Ansible test cases here
it('should respond with 404 when ansible does not exist', function(done) {
request(app)
.delete(`/api/ansible/${newAnsible._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -5,9 +5,22 @@ var controller = require('./ansible.controller');
var router = express.Router();
router.get('/', controller.index);
// List, create and get Ansible Jobs
router.get('/runs', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
router.post('/modules', controller.modules);
// Ansible Command line APIs
// - Create and modify inventory files
// - Create and modify playbooks
// - Create and modify roles
// - List tags
// - Create and modify files
// - Create and modify Var files
router.post('/modules/list', controller.modules);
router.post('/command', controller.command);
router.post('/execute', controller.execute);
@ -50,10 +63,5 @@ router.post('/vars/roles/get', controller.get_roles_vars);
router.get('/logs/:id', controller.getLogs);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -10,7 +10,15 @@ var ansibleCtrlStub = {
create: 'ansibleCtrl.create',
upsert: 'ansibleCtrl.upsert',
patch: 'ansibleCtrl.patch',
destroy: 'ansibleCtrl.destroy'
destroy: 'ansibleCtrl.destroy',
modules: 'ansibleCtrl.modules',
command: 'ansibleCtrl.command',
execute: 'ansibleCtrl.execute',
project_files: 'ansibleCtrl.project_files',
playbook_get: 'ansibleCtrl.playbook_get',
playbook_create: 'ansibleCtrl.playbook_create',
playbook_delete: 'ansibleCtrl.playbook_delete',
playbook_list: 'ansibleCtrl.playbook_list',
};
var routerStub = {
@ -18,7 +26,15 @@ var routerStub = {
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
delete: sinon.spy(),
modules: sinon.spy(),
command: sinon.spy(),
execute: sinon.spy(),
project_files: sinon.spy(),
playbook_get: sinon.spy(),
playbook_create: sinon.spy(),
playbook_delete: sinon.spy(),
playbook_list: sinon.spy()
};
// require the index with our stubbed out modules
@ -36,51 +52,70 @@ describe('Ansible API Router:', function() {
expect(ansibleIndex).to.equal(routerStub);
});
describe('GET /api/ansible', function() {
it('should route to ansible.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'ansibleCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/ansible/:id', function() {
it('should route to ansible.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'ansibleCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/ansible', function() {
it('should route to ansible.controller.create', function() {
describe('POST /api/ansible/modules/list', function() {
it('should route to ansible.controller.modules', function() {
expect(routerStub.post
.withArgs('/', 'ansibleCtrl.create')
.withArgs('/modules/list', 'ansibleCtrl.modules')
).to.have.been.calledOnce;
});
});
describe('PUT /api/ansible/:id', function() {
it('should route to ansible.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'ansibleCtrl.upsert')
).to.have.been.calledOnce;
describe('POST /api/ansible/command', function() {
it('should route to ansible.controller.command', function() {
expect(routerStub.post
.withArgs('/command', 'ansibleCtrl.command')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/ansible/:id', function() {
it('should route to ansible.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'ansibleCtrl.patch')
).to.have.been.calledOnce;
describe('POST /api/ansible/execute', function() {
it('should route to ansible.controller.execute', function() {
expect(routerStub.post
.withArgs('/execute', 'ansibleCtrl.execute')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/ansible/:id', function() {
it('should route to ansible.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'ansibleCtrl.destroy')
).to.have.been.calledOnce;
describe('POST /api/ansible/project/files', function() {
it('should route to ansible.controller.project_files', function() {
expect(routerStub.post
.withArgs('/project/files', 'ansibleCtrl.project_files')
).to.have.been.calledOnce;
});
});
describe('POST /api/ansible/playbook/get', function() {
it('should route to ansible.controller.playbook_get', function() {
expect(routerStub.post
.withArgs('/playbook/get', 'ansibleCtrl.playbook_get')
).to.have.been.calledOnce;
});
});
describe('POST /api/ansible/playbook/create', function() {
it('should route to ansible.controller.playbook_create', function() {
expect(routerStub.post
.withArgs('/playbook/create', 'ansibleCtrl.playbook_create')
).to.have.been.calledOnce;
});
});
describe('POST /api/ansible/playbook/delete', function() {
it('should route to ansible.controller.playbook_delete', function() {
expect(routerStub.post
.withArgs('/playbook/delete', 'ansibleCtrl.playbook_delete')
).to.have.been.calledOnce;
});
});
describe('POST /api/ansible/playbook/list', function() {
it('should route to ansible.controller.playbook_list', function() {
expect(routerStub.post
.withArgs('/playbook/list', 'ansibleCtrl.playbook_list')
).to.have.been.calledOnce;
});
});
//TODO: Add the remaining test cases here
});