-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorm_jsonrpc.js
110 lines (96 loc) · 3.26 KB
/
storm_jsonrpc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import fs from 'fs'
import path from 'path'
import StormRunner from 'storm'
const RpcServer = require('http-jsonrpc-server')
import log from './logfile.js'
var testCasePath
// To get the location of the testcase within testcases folder which needs to executed
function getTestcasePath(dirPath, testCase, arrayOfFiles) {
let files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(function(file) {
if (fs.statSync(dirPath + '/' + file).isDirectory()) {
arrayOfFiles = getTestcasePath(dirPath + '/' + file, testCase, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, '/', file))
if (file == testCase) {
testCasePath = path.join(__dirname, dirPath, '/', file)
}
}
})
return arrayOfFiles
}
// To start the test case execution
function execute_testcase(params) {
process.chdir(__dirname)
let log_dir = './logs'
if (!fs.existsSync(log_dir)) {
//Modified for solving permission issue while writing logs
fs.mkdirSync(log_dir, '766')
}
testCasePath = ''
const testCase = params.test
const deviceIp = params.device
const execId = params.execid
getTestcasePath('../testcases/Storm-Testcases/src/tests/', testCase)
let stormJsonrpcLog = fs.createWriteStream(
__dirname + '/logs/' + testCase + '_' + execId + '_Serverconsole.log'
)
if (!testCase || !fs.existsSync(testCasePath)) {
process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog)
console.log('Test case ' + testCase + ' not found')
result = 'Test case ' + testCase + ' not found'
return result
}
if (!deviceIp) {
process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog)
console.log('Device IP is not available')
result = 'Device IP is not available'
return result
}
let testCaseLog = fs.createWriteStream(
__dirname + '/logs/' + testCase + '_' + execId + '_Execution.log'
)
process.stdout.write = process.stderr.write = stormJsonrpcLog.write.bind(stormJsonrpcLog)
import(testCasePath)
.then(testCase => {
StormRunner(testCase.default, log(testCaseLog.path, stormJsonrpcLog), deviceIp)
.then(console.log)
.catch(console.error)
})
.catch(console.error)
var result = { ExecutionLog: testCaseLog.path, ServerLog: stormJsonrpcLog.path }
return result
}
// To check the json-rpc server status
function check_rpcserver_status() {
if (rpcServer.server.listening) {
console.log('JSON-RPC server is in listening mode')
return 'LISTEN'
} else {
console.log('JSON-RPC server is not running')
return 'DOWN'
}
}
// To Stop the json-rpc server
function stop_rpcserver() {
rpcServer.close().then(() => {
console.log('JSON-RPC server stopped')
return 'STOPPED'
})
}
// Registering json-rpc methods and start the server
const rpcServer = new RpcServer({
path: '/storm-jsonrpc',
methods: {
execute_testcase,
check_rpcserver_status,
stop_rpcserver,
},
})
rpcServer.setMethod('executeTest', execute_testcase)
rpcServer.setMethod('checkRpcServer', check_rpcserver_status)
rpcServer.setMethod('stopRpcServer', stop_rpcserver)
rpcServer.listen(9091, '127.0.0.1').then(() => {
console.log('JSON RPC server is listening at http://127.0.0.1:9091/storm-jsonrpc')
})