-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
303 lines (272 loc) · 8.85 KB
/
app.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const fetch = require('node-fetch')
const {URLSearchParams} = require('url')
const parse = require('url-parse')
const fs = require('fs')
const {spawn, execSync} = require("child_process")
const STORAGE_PREFIX = 'repo/iiidevops/sideex/'
const DEFAULT_VARIABLES = 'Global Variables.json'
// const SUITES_PREFIX = 'suites/'
// const VAR_PREFIX = 'vars/'
const git = {
url: process.env['git_url'],
pUrl: parse(process.env['git_url']),
branch: process.env['git_branch'],
commit_id: process.env['git_commit_id']
}
const verbose = process.env['verbose'] === 'true'
const global = {
jwtToken: null,
repoId: -1,
projectId: -1,
total: 0,
failed: 0,
report: {}
}
if (verbose) {
process.env.TZ = 'Asia/Taipei'
console.log('Current time is ' + (new Date().toLocaleString()))
console.log('Log into API server...')
}
try {
(async () => {
await login()
if (await checkPluginDisabled('sideex')) {
console.log('Sideex plugin is disabled.')
return
}
console.log('Mention API Server test started.')
const testId = await mentionStart()
console.log(`Test id is ${testId} , starting Selenium...`)
const selenium = spawn('java', ['-jar', 'selenium-server-standalone-3.141.59.jar'])
try {
await waitSeleniumUp(1)
} catch (err) {
console.log('Selenium is not up in 30 seconds!')
process.exit(1)
}
const config = JSON.parse(fs.readFileSync('config.json'))
console.log('Inserting test suites...')
const testSuites = []
console.log("Read Test Suites Json File")
const suites = fs.readdirSync(STORAGE_PREFIX)
for (const file of suites) {
if (file.endsWith('.json') && file !== DEFAULT_VARIABLES) {
console.log(file)
testSuites.push(STORAGE_PREFIX + file)
}
}
config.input.testSuites = testSuites
console.log("Read Variables Json File")
var testVariables = {}
const targetOrigin = process.env['target_origin']
const variable_files = fs.readdirSync(STORAGE_PREFIX)
for (const variable_file of variable_files) {
if (variable_file === DEFAULT_VARIABLES) {
testVariables = Object.assign(testVariables, JSON.parse(fs.readFileSync(STORAGE_PREFIX + variable_file)));
}
}
testVariables["target_origin"] = targetOrigin
console.log(testVariables)
fs.writeFileSync(STORAGE_PREFIX + DEFAULT_VARIABLES, JSON.stringify(testVariables))
var variable_file = STORAGE_PREFIX + DEFAULT_VARIABLES
config.input.variables = [variable_file]
fs.writeFileSync('config.json', JSON.stringify(config))
console.log('Running Sideex...')
fs.rmdirSync("report", {recursive: true})
fs.mkdirSync('report')
execSync('sideex-runner --config config.json', {stdio: 'inherit'})
console.log('Uploading result...')
let reportHTML = null
let reportJSON = null
// Should only have one html and one json report file
const files = fs.readdirSync('report')
files.forEach(file => {
if (file.endsWith('.html')) {
reportHTML = `report/${file}`
}
if (file.endsWith('.json')) {
reportJSON = `report/${file}`
}
})
selenium.kill()
if (! reportHTML) {
console.log('Cannot find report file!')
process.exit(1)
}
const data = fs.readFileSync(reportJSON, 'utf-8')
const report = fs.readFileSync(reportHTML, 'utf-8')
const result = analyze(data)
console.log(result)
console.log('Uploading to API server...')
await mentionFinish(testId, JSON.stringify(result), report)
console.log('Job done.')
})()
} catch (e) {
process.exit(1)
}
function login() {
return new Promise((resolve, reject) => {
apiPost('/user/login', null, {
username: process.env['username'],
password: process.env['password']
}).then(json => {
global.jwtToken = json.data.token
if (verbose)
console.log('Retrieving repo_id...')
apiGet(`/repositories/id?repository_url=${
git.url
}`).then(json => {
global.projectId = json.data.project_id
global.repoId = json.data.repository_id
if (verbose)
console.log('repo_id is ' + global.repoId)
resolve()
})
})
})
}
async function checkPluginDisabled(pluginName) {
const data = (await apiGet('/plugins')).data
for (const d of data) {
if (d.name === pluginName) {
return d.disabled
}
}
return false
}
async function mentionStart() {
const json = await apiPost('/sideex', null, {
project_name: process.env['project_name'],
branch: process.env['git_branch'],
commit_id: process.env['git_commit_id']
})
return json.data.test_id
}
function waitSeleniumUp(trial) {
return new Promise((resolve, reject) => {
console.log('Waiting for selenium to be up, trail ' + trial)
if (trial > 30) {
reject()
return
}
setTimeout(async function () {
const result = await checkSelenium()
if (result) {
resolve()
} else {
try {
await waitSeleniumUp(trial + 1)
resolve()
} catch (err) {
reject()
}
}
}, 1000)
})
}
async function checkSelenium() {
try {
const res = await fetch('http://localhost:4444', {method: 'GET'})
return true
} catch (err) {
return false
}
}
async function mentionFinish(test_id, result, report) {
await apiPut(`/sideex`, null, {test_id, result, report})
}
function analyze(data) {
const json = JSON.parse(data)
const ret = {
suitesPassed: json.successSuiteNum,
suitesTotal: json.failureSuiteNum + json.successSuiteNum,
casesPassed: json.successCaseNum,
casesTotal: json.failureCaseNum + json.successCaseNum,
suites: {}
}
for (const suite of json.suites) {
const title = suite.title
const result = {
passed: 0,
total: 0,
name: title,
cases: []
}
for (const caseIndex of suite.cases) {
let passed = true
let case_object = {
"title": json.cases[caseIndex].title,
"status": json.cases[caseIndex].status
}
// Record Steps
for (const record of json.cases[caseIndex].records) {
if (record.status === 'fail') {
passed = false
break
}
}
if (passed) {
result.passed ++
}
result.total ++
result.cases.push(case_object)
ret.suites[title] = result
}
}
return ret
}
// ----------------- API functions -----------------
function apiGet(path, headers) {
return new Promise((resolve, reject) => {
if (! headers) {
headers = {}
}
headers['Authorization'] = `Bearer ${
global.jwtToken
}`
const opts = {
headers
}
fetch(process.env['api_origin'] + path, opts).then(res => res.json()).then(json => resolve(json)).catch(err => console.error(err))
})
}
function apiPost(path, headers, body) {
return new Promise((resolve, reject) => {
if (! headers) {
headers = {}
}
headers['Authorization'] = `Bearer ${
global.jwtToken
}`
const params = new URLSearchParams()
for (let key in body) {
params.append(key, body[key])
}
const opts = {
method: 'POST',
headers,
body: params
}
fetch(process.env['api_origin'] + path, opts).then(res => res.json()).then(json => resolve(json)).catch(err => console.error(err))
})
}
function apiPut(path, headers, body) {
return new Promise((resolve, reject) => {
if (! headers) {
headers = {}
}
headers['Authorization'] = `Bearer ${
global.jwtToken
}`
const params = new URLSearchParams()
for (let key in body) {
params.append(key, body[key])
}
const opts = {
method: 'PUT',
headers,
body: params
}
fetch(process.env['api_origin'] + path, opts).then(res => res.json()).then(json => resolve(json)).catch(err => console.error(err))
})
}