domotz-remote-pawn
Version:
Domotz Agent
149 lines (131 loc) • 5.97 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
console.debug = console.log;
console.silly = console.log;
var q = require('q');
var xml2js = require('xml2js');
module.exports = function DiscoveryTestRunner(baseDir, scenario) {
"use strict";
var fs = require('fs');
var test_scenario = JSON.parse(fs.readFileSync(baseDir + scenario + '.json').toString());
var commands = test_scenario.expectations.commands;
var expectedCalls = test_scenario.expectations.engine_calls;
var zabbixCalls = test_scenario.expectations.zabbix_calls;
var test_failed = function (error) {
console.error("TEST FAILED: \n" + error.message);
require('process').exit(255);
};
return {
iterations: test_scenario.iterations ? test_scenario.iterations : 1,
interfaces: test_scenario.interfaces,
getInterfacesList: function () {
return test_scenario.interfaces;
},
process: {
exec: function (command) {
try {
var resultList = commands[command];
if (resultList === undefined) {
test_failed(Error("Command was not expected: `" + command + "`"));
}
if (resultList.length === 0) {
test_failed(Error("Command called too many times: `" + command + "`"));
}
var fileName = commands[command].shift();
var defer_1 = q.defer();
fs.readFile(baseDir + fileName, function (err, value) {
defer_1.resolve(value.toString());
});
return defer_1.promise;
} catch (error) {
test_failed(error);
}
}
},
engine_requests: {
send: function (url, method, body, success, error) {
try {
var expected = expectedCalls.shift();
if (!expected) {
test_failed(Error("Unexpected engine call " + url));
}
if (method !== 'PUT') {
test_failed(Error("Unexpected method " + method + " for call:" + url));
}
if (expected.url !== url) {
test_failed(Error("Wrong URL for engine call: \n" + expected.url + "\t<- expected\n" + url + "\t<- actual"));
}
fs.readFile(baseDir + 'engine_payloads/' + expected.body, function (err, contents) {
var expectedBody = JSON.stringify(JSON.parse(contents.toString()), null, 2);
var actualBody = JSON.stringify(body, null, 2);
if (actualBody !== expectedBody) {
test_failed(Error("Body differ: Expected=\n" + expectedBody + "\n--\nActual=\n" + actualBody));
}
success();
});
} catch (error) {
test_failed(error);
}
}
},
zabbix_requests: {
send: function (host, item, value) {
try {
var expected = zabbixCalls.shift();
if (!expected) {
test_failed(Error("Unexpected zabbix call "));
}
if (host.mac !== expected.mac) {
test_failed(Error("Unexpected mac " + host.mac + " (for : " + expected.host.mac +" ) in zabbix call"));
}
if (host.ip !== expected.ip) {
test_failed(Error("Unexpected ip " + host.ip + " (for : " + expected.host.ip +" ) in zabbix call"));
}
if (value !== expected.value) {
test_failed(Error("Unexpected value " + value + " (for : " + expected.value +" ) in zabbix call"));
}
} catch (error) {
test_failed(error);
}
}
},
fail: test_failed,
assertNoUnresolvedExpectations: function () {
var commands = test_scenario.expectations.commands;
var calls = test_scenario.expectations.engine_calls;
var zabbix_calls = test_scenario.expectations.zabbix_calls;
var msg = '';
for (var key in commands) {
if (commands.hasOwnProperty(key)) {
if (commands[key].length > 0) {
msg += "`" + key + "` shell command still not run, pending calls: " + commands[key].length + "\n";
}
}
}
calls.forEach(function (call) {
msg += "Engine call to URL: " + call.url + " never executed\n";
});
zabbix_calls.forEach(function (zabbix_call){
msg += "Zabbix call : " + zabbix_call.mac + "(ip " + zabbix_call.ip + ") with value "
+ zabbix_call.value + " never executed\n";
});
if (msg !== '') {
test_failed(Error(msg));
}
}
};
}