domotz-remote-pawn
Version:
Domotz Agent
144 lines (120 loc) • 5.11 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.error = console.log;
console.warn = console.log;
console.info = console.log;
console.debug = console.log;
console.verbose = console.log;
console.silly = console.log;
var q = require('q');
var xml2js = require('xml2js');
var DiscoveryTestRunnerFactory = require("./runners/discovery_test_runner");
var testSuite = process.argv[2] ? process.argv[2] : "default_discovery";
console.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ");
console.info("# ");
console.info("# EXECUTING TEST SUITE - %s - ", testSuite.toUpperCase());
console.info("# ");
console.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ");
var testSuiteDir = './test_scenarios/' + testSuite+ '/';
var testRunner = DiscoveryTestRunnerFactory(testSuiteDir, 'scenario');
var domotzApp = {
q: q,
configuration: {
discovery:{
ports: [
80, 8000, 8001, 8080, 8888, 5000, 3000, // HTTP
3389, // RDP
22, // SSH
23, // telnet
5900// vnc
],
retry_interval: 10
},
zabbix:{
items: {
}
}
},
xml2js: xml2js,
utils: {proc: testRunner.process},
event: {}
};
function setupBindDiscoveryFactory(resourceLocator) {
"use strict";
var device = require('../src/discovery/ip_network/device');
return require('../src/discovery/ip_network/bind_discovery').bindDiscoveryFactoryCreator(
require('../src/discovery/nmap/commands').factory(resourceLocator.configuration),
require('../src/discovery/nmap/xml2js').factory(resourceLocator.q, resourceLocator.xml2js),
require('../src/discovery/ip_network/parser').factory(device),
resourceLocator
);
}
function repeatablePerformDescovery(count){
var done = q.defer();
function _retry() {
if (count === 0){
done.resolve();
}
else{
count--;
console.info("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
console.info("- - - - - - - PERFORMING ITERATION ( STILL LEFT: %s) - - - - - - - ", count);
console.info("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
q.delay(1000).then(coordinator.performDiscovery).then(_retry);
}
}
q.nextTick(_retry);
return done.promise;
}
var engineRequestMock = {
sendWithPromise: function sendWithPromise(host, method, payload, undefined){
return q.resolve(null);
}
}
const interfacesBindingStorage = require('../src/discovery/ip_network/interfaces_binding').factory(engineRequestMock);
const engineReportingStrategy = require('../src/discovery/ip_network/engine_report_strategy').
engineIpDiscoveryReportStrategyFactory(testRunner.engine_requests,
interfacesBindingStorage,
domotzApp);
var zabbixUtilsMock = {
getHostByMAC: function getHostByMAC(mac){
return {
mac: mac,
ip : "XXX.XXX.XXX.XXX"
}
},
send: function (host, item, value) {
console.debug("-- Mock zabbix call performed: mac %s, ip %s, item %s, value %s", host.mac, host.ip, item, value);
testRunner.zabbix_requests.send(host, item, value);
}
}
var zabbixReportStrategy = require('../src/discovery/ip_network/zabbix_report_strategy')
.zabbixIpDiscoveryReportStrategyFactory(zabbixUtilsMock, interfacesBindingStorage, domotzApp.configuration);
var coordinator = require('../src/discovery/ip_network/discovery_coordinator').discoveryCoordinatorFactory(
domotzApp,
testRunner,
setupBindDiscoveryFactory(domotzApp),
interfacesBindingStorage,
engineReportingStrategy,
zabbixReportStrategy);
try {
var promise = repeatablePerformDescovery(testRunner.iterations);
promise.then(testRunner.assertNoUnresolvedExpectations);
promise.catch(testRunner.fail);
} catch (error) {
testRunner.fail(error);
}