This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (66 loc) · 2.29 KB
/
index.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
require('./config')
const amqp = require('amqplib')
const fs = require('fs')
const YAML = require('yaml')
const { client } = require('./utils').minio
const runner = require('./runner')
const { RABBITMQ_URL, RABBITMQ_USER, RABBITMQ_PASS } = process.env
const connURL = `amqp://${RABBITMQ_USER}:${RABBITMQ_PASS}@${RABBITMQ_URL}`
amqp
.connect(connURL)
.then(async conn => {
const channel = await conn.createChannel()
const emulatorConfig = await fs.promises
.readFile('machines.yaml')
.then(file => YAML.parse(file.toString()))
const queues = Object.keys(emulatorConfig).filter(
queue =>
emulatorConfig[queue].devices &&
emulatorConfig[queue].devices.length > 0
)
await channel.assertQueue('results', { durable: true })
queues.map(async queue => {
await channel.assertQueue(queue, { durable: true })
channel.prefetch(emulatorConfig[queue].devices.length)
channel.consume(queue, async msg => {
try {
const spec = JSON.parse(msg.content.toString())
const tmpDir = `${__dirname}/tmp/${spec._id}`
const fileDir = `${tmpDir}/${spec.buildFile}`
const screenshotDir = `${tmpDir}/screenshots`
if (!fs.existsSync(screenshotDir)) {
fs.mkdirSync(screenshotDir, { recursive: true })
}
await client.fGetObject(spec.owner, spec.buildFile, fileDir)
const results = await runner(spec.testCase, fileDir, screenshotDir)
await Promise.all(
results.state.map(step => {
return client.fPutObject(
spec.owner,
`${spec._id}/${step.screenshotFilename}`,
`${screenshotDir}/${step.screenshotFilename}`
)
})
)
const resultsMsg = {
test_id: spec._id,
results,
owner: spec.owner,
}
// file cleanup
fs.rmdirSync(tmpDir, { recursive: true })
channel.sendToQueue(
'results',
Buffer.from(JSON.stringify(resultsMsg)),
{
persistent: true,
}
)
channel.ack(msg) // just do ACK on the message received
} catch (error) {
channel.nack(msg)
}
})
})
})
.catch(console.error)