-
Notifications
You must be signed in to change notification settings - Fork 8
/
availability.js
49 lines (41 loc) · 1.54 KB
/
availability.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
const got = require('got');
const chalk = require('chalk');
const Check = require('./check');
class AvailabilityCheck extends Check {
constructor(connector, reporter) {
super(connector, reporter);
this.timeout = 3000;
}
async check(context, args) {
let ip = await this.connector.getContainerIp(context);
let address = `${args.port == 443 ? 'https' : 'http'}://${ip}:${args.port}/${args.url === '/' ? '' : args.url}`;
let state = null;
if (args.setup) {
state = await this.connector.setup(context, args.setup);
}
let status = await this.endpoint(address, args.status);
let results = {
host: await this.connector.getName(context),
address,
status,
expected: args.status,
};
await this.connector.tearDown(state);
return results;
}
async endpoint(address, expectedStatus) {
return this.requestStatus(address);
}
async requestStatus(address) {
try {
return (await got(address, {followRedirect: true, rejectUnauthorized: false, timeout: this.timeout})).statusCode;
} catch (err) {
return err.errorCode || err;
}
}
async report(results) {
let message = chalk`{gray [${results.host}] ${results.address}} {white expected:} {gray ${results.expected}} {white actual:} {gray ${results.status}}`;
this.reporter.report(message, results.expected === results.status);
}
}
module.exports = AvailabilityCheck;