Skip to content

Commit

Permalink
Fixing the disabled Ansible tests and enabling them (#592)
Browse files Browse the repository at this point in the history
Fixing the disabled Ansible tests and enabling them.
  • Loading branch information
amaljg authored Feb 20, 2019
1 parent 9cc089e commit a87e458
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as ansibleUtils from './ansibleUtils';
import { RemoteCommandOptions } from './ansibleUtils'
import { ansibleTaskParameters } from './ansibleTaskParameters';

var uuid = require('uuid/v4'); //randomly generated uid
var os = require('os');
var shell = require('shelljs');

Expand Down Expand Up @@ -131,7 +130,7 @@ export class ansibleCommandLineInterface extends ansibleInterface {

return new Promise<string>(async (resolve, reject) => {
try {
let remoteInventory = '/tmp/' + uuid() + 'inventory.ini';
let remoteInventory = ansibleUtils.getTemporaryInventoryFilePath();
let remoteInventoryPath = '"' + remoteInventory + '"';
tl.debug('RemoteInventoryPath = ' + remoteInventoryPath);

Expand Down
6 changes: 6 additions & 0 deletions Extensions/Ansible/Src/Tasks/Ansible/ansibleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import tl = require("vsts-task-lib/task");
import Q = require("q");
import util = require("util");
import querystring = require('querystring');

var uuid = require('uuid/v4');
var httpClient = require('vso-node-api/HttpClient');
var httpObj = new httpClient.HttpCallbackClient(tl.getVariable("AZURE_HTTP_USER_AGENT"));

Expand Down Expand Up @@ -195,6 +197,10 @@ function beginRequestInternal(request: WebRequest): Promise<WebResponse> {
});
}

export function getTemporaryInventoryFilePath(): string {
return '/tmp/' + uuid() + 'inventory.ini';
}

function toWebResponse(response, body): WebResponse {
var res = new WebResponse();

Expand Down
85 changes: 43 additions & 42 deletions Extensions/Ansible/Tests/Tasks/Ansible/_suite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert = require('assert');
import path = require('path');
import {TestGuid} from './mockAnsibleUtils';

var mocktest = require('vsts-task-lib/mock-test');

Expand Down Expand Up @@ -73,28 +74,28 @@ describe('Ansible Suite', function () {
}
});

// it('should run when inventory is inline content for remote machine', (done: MochaDone) => {
// this.timeout(1000);
// let testPath = path.join(__dirname, 'testInventoryToBeInlineForRemoteMachine');
// let runner = new mocktest.MockTestRunner(testPath);
// runner.run();

// try {
// assert(runner.succeeded, "Should have succeeded");
// assert(runner.stdOutContained('cmd run on remote machine = echo "DUMMY_IP_ADDRESS" > /tmp/inventory.ini'), 'should able to copy the inline content to inventory.ini file on remote machine');
// assert(runner.stdOutContained('cmd run on remote machine = chmod +x /tmp/inventory.ini'), 'should able to make the inventory.ini file as executable for dynamic inventory');
// assert(runner.stdOutContained('cmd run on remote machine = ansible-playbook -i /tmp/inventory.ini /path/to/ansiblePlaybookRoot/ansiblePlaybook.yml'), 'should able to run playbook on remote machine');
// assert(runner.stdOutContained('connection to dummy client established'), 'should able connect to client');
// assert(runner.stdOutContained('connection to dummy client terminated'), 'should able disconnect to client');
// assert(runner.stdOutContained('cmd run on remote machine = rm -f /tmp/inventory.ini', 'should clean all the temporary inventory file on remote machine'));
// done();
// }
// catch (error) {
// console.log("STDERR", runner.stderr);
// console.log("STDOUT", runner.stdout);
// done(error);
// }
// });
it('should run when inventory is inline content for remote machine', (done: MochaDone) => {
this.timeout(1000);
let testPath = path.join(__dirname, 'testInventoryToBeInlineForRemoteMachine');
let runner = new mocktest.MockTestRunner(testPath);
runner.run();

try {
assert(runner.succeeded, "Should have succeeded");
assert(runner.stdOutContained(`cmd run on remote machine = echo "DUMMY_IP_ADDRESS" > /tmp/${TestGuid}inventory.ini`), 'should able to copy the inline content to inventory.ini file on remote machine');
assert(runner.stdOutContained(`cmd run on remote machine = chmod +x /tmp/${TestGuid}inventory.ini`), 'should able to make the inventory.ini file as executable for dynamic inventory');
assert(runner.stdOutContained(`cmd run on remote machine = ansible-playbook -i /tmp/${TestGuid}inventory.ini /path/to/ansiblePlaybookRoot/ansiblePlaybook.yml`), 'should able to run playbook on remote machine');
assert(runner.stdOutContained('connection to dummy client established'), 'should able connect to client');
assert(runner.stdOutContained('connection to dummy client terminated'), 'should able disconnect to client');
assert(runner.stdOutContained(`cmd run on remote machine = rm -f /tmp/${TestGuid}inventory.ini`, 'should clean all the temporary inventory file on remote machine'));
done();
}
catch (error) {
console.log("STDERR", runner.stderr);
console.log("STDOUT", runner.stdout);
done(error);
}
});

it('should run when sudo and additional parameters is present for remote machine', (done: MochaDone) => {
this.timeout(1000);
Expand Down Expand Up @@ -171,24 +172,24 @@ describe('Ansible Suite', function () {
}
});

// it('should run when inventory is inline content for agent machine', (done: MochaDone) => {
// this.timeout(1000);
// let testPath = path.join(__dirname, 'testInventoryToBeInlineForAgentMachine');
// let runner = new mocktest.MockTestRunner(testPath);
// runner.run();

// try {
// assert(runner.succeeded, "Should have succeeded");
// assert(runner.stdOutContained('cmd run on agent machine = echo "DUMMY_IP_ADDRESS" > /tmp/inventory.ini'), 'should able to copy the inline content to inventory.ini file on remote machine');
// assert(runner.stdOutContained('cmd run on agent machine = chmod +x /tmp/inventory.ini'), 'should able to make the inventory.ini file as executable for dynamic inventory');
// assert(runner.stdOutContained('cmd run on agent machine = ansible-playbook -i /tmp/inventory.ini /path/to/ansiblePlaybookRoot/ansiblePlaybook.yml'), 'should able to run playbook on remote machine');
// assert(runner.stdOutContained('cmd run on agent machine = rm -f /tmp/inventory.ini', 'should clean all the temporary inventory file on remote machine'));
// done();
// }
// catch (error) {
// console.log("STDERR", runner.stderr);
// console.log("STDOUT", runner.stdout);
// done(error);
// }
// });
it('should run when inventory is inline content for agent machine', (done: MochaDone) => {
this.timeout(1000);
let testPath = path.join(__dirname, 'testInventoryToBeInlineForAgentMachine');
let runner = new mocktest.MockTestRunner(testPath);
runner.run();

try {
assert(runner.succeeded, "Should have succeeded");
assert(runner.stdOutContained(`cmd run on agent machine = echo "DUMMY_IP_ADDRESS" > /tmp/${TestGuid}inventory.ini`), 'should able to copy the inline content to inventory.ini file on remote machine');
assert(runner.stdOutContained(`cmd run on agent machine = chmod +x /tmp/${TestGuid}inventory.ini`), 'should able to make the inventory.ini file as executable for dynamic inventory');
assert(runner.stdOutContained(`cmd run on agent machine = ansible-playbook -i /tmp/${TestGuid}inventory.ini /path/to/ansiblePlaybookRoot/ansiblePlaybook.yml`), 'should able to run playbook on remote machine');
assert(runner.stdOutContained(`cmd run on agent machine = rm -f /tmp/${TestGuid}inventory.ini`, 'should clean all the temporary inventory file on remote machine'));
done();
}
catch (error) {
console.log("STDERR", runner.stderr);
console.log("STDOUT", runner.stdout);
done(error);
}
});
});
6 changes: 6 additions & 0 deletions Extensions/Ansible/Tests/Tasks/Ansible/mockAnsibleUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Q = require("q");

export const TestGuid: string = "7d9b7f32-41e5-43ed-8b0e-c6f2874836f8";

export function _writeLine(str) {
console.log(str);
}
Expand Down Expand Up @@ -151,4 +153,8 @@ export function beginRequest(request) {
res.body = JSON.parse(body);
return res;
}
}

export function getTemporaryInventoryFilePath(): string {
return '/tmp/' + TestGuid + 'inventory.ini';
}

0 comments on commit a87e458

Please sign in to comment.