2017-02-06 13:40:28 -05:00
|
|
|
// Imports
|
|
|
|
import * as cp from 'child_process';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as shell from 'shelljs';
|
2018-05-10 08:56:07 -04:00
|
|
|
import {AIO_DOWNLOADS_DIR, HIDDEN_DIR_PREFIX} from '../common/constants';
|
|
|
|
import {
|
|
|
|
AIO_BUILDS_DIR,
|
|
|
|
AIO_NGINX_PORT_HTTP,
|
|
|
|
AIO_NGINX_PORT_HTTPS,
|
|
|
|
AIO_WWW_USER,
|
|
|
|
} from '../common/env-variables';
|
2018-08-27 11:07:25 -04:00
|
|
|
import {computeShortSha, Logger} from '../common/utils';
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
// Interfaces - Types
|
2017-09-23 08:24:04 -04:00
|
|
|
export interface CmdResult { success: boolean; err: Error | null; stdout: string; stderr: string; }
|
2017-02-06 13:40:28 -05:00
|
|
|
export interface FileSpecs { content?: string; size?: number; }
|
2017-02-27 04:35:34 -05:00
|
|
|
|
|
|
|
export type CleanUpFn = () => void;
|
|
|
|
export type TestSuiteFactory = (scheme: string, port: number) => void;
|
|
|
|
export type VerifyCmdResultFn = (result: CmdResult) => void;
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
// Classes
|
|
|
|
class Helper {
|
2018-05-12 10:39:16 -04:00
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
// Properties - Protected
|
|
|
|
protected cleanUpFns: CleanUpFn[] = [];
|
|
|
|
protected portPerScheme: {[scheme: string]: number} = {
|
2018-05-10 08:56:07 -04:00
|
|
|
http: AIO_NGINX_PORT_HTTP,
|
|
|
|
https: AIO_NGINX_PORT_HTTPS,
|
2017-02-06 13:40:28 -05:00
|
|
|
};
|
|
|
|
|
2018-08-27 11:07:25 -04:00
|
|
|
private logger = new Logger('TestHelper');
|
2018-05-12 10:39:16 -04:00
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
// Constructor
|
|
|
|
constructor() {
|
2018-05-10 08:56:07 -04:00
|
|
|
shell.mkdir('-p', AIO_BUILDS_DIR);
|
|
|
|
shell.exec(`chown -R ${AIO_WWW_USER} ${AIO_BUILDS_DIR}`);
|
|
|
|
shell.mkdir('-p', AIO_DOWNLOADS_DIR);
|
|
|
|
shell.exec(`chown -R ${AIO_WWW_USER} ${AIO_DOWNLOADS_DIR}`);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Methods - Public
|
2018-08-10 06:10:22 -04:00
|
|
|
public cleanUp(): void {
|
2017-02-06 13:40:28 -05:00
|
|
|
while (this.cleanUpFns.length) {
|
|
|
|
// Clean-up fns remove themselves from the list.
|
|
|
|
this.cleanUpFns[0]();
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
const leftoverDownloads = fs.readdirSync(AIO_DOWNLOADS_DIR);
|
|
|
|
const leftoverBuilds = fs.readdirSync(AIO_BUILDS_DIR);
|
2017-02-06 13:40:28 -05:00
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
if (leftoverDownloads.length) {
|
2018-05-12 10:39:16 -04:00
|
|
|
this.logger.log(`Downloads directory '${AIO_DOWNLOADS_DIR}' is not empty after clean-up.`, leftoverDownloads);
|
2018-05-10 08:56:07 -04:00
|
|
|
shell.rm('-rf', `${AIO_DOWNLOADS_DIR}/*`);
|
|
|
|
}
|
2017-02-06 13:40:28 -05:00
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
if (leftoverBuilds.length) {
|
2018-05-12 10:39:16 -04:00
|
|
|
this.logger.log(`Builds directory '${AIO_BUILDS_DIR}' is not empty after clean-up.`, leftoverBuilds);
|
2018-05-10 08:56:07 -04:00
|
|
|
shell.rm('-rf', `${AIO_BUILDS_DIR}/*`);
|
|
|
|
}
|
2017-02-06 13:40:28 -05:00
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
if (leftoverBuilds.length || leftoverDownloads.length) {
|
|
|
|
throw new Error(`Unexpected test files not cleaned up.`);
|
|
|
|
}
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
2018-08-10 06:10:22 -04:00
|
|
|
public createDummyBuild(pr: number, sha: string, isPublic = true, force = false, legacy = false): CleanUpFn {
|
2017-06-20 13:22:32 -04:00
|
|
|
const prDir = this.getPrDir(pr, isPublic);
|
2017-06-25 15:13:03 -04:00
|
|
|
const shaDir = this.getShaDir(prDir, sha, legacy);
|
2017-02-06 13:40:28 -05:00
|
|
|
const idxPath = path.join(shaDir, 'index.html');
|
|
|
|
const barPath = path.join(shaDir, 'foo', 'bar.js');
|
|
|
|
|
|
|
|
this.writeFile(idxPath, {content: `PR: ${pr} | SHA: ${sha} | File: /index.html`}, force);
|
|
|
|
this.writeFile(barPath, {content: `PR: ${pr} | SHA: ${sha} | File: /foo/bar.js`}, force);
|
2018-05-10 08:56:07 -04:00
|
|
|
shell.exec(`chown -R ${AIO_WWW_USER} ${prDir}`);
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
return this.createCleanUpFn(() => shell.rm('-rf', prDir));
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
public getPrDir(pr: number, isPublic: boolean): string {
|
|
|
|
const prDirName = isPublic ? '' + pr : HIDDEN_DIR_PREFIX + pr;
|
|
|
|
return path.join(AIO_BUILDS_DIR, prDirName);
|
2017-06-20 13:22:32 -04:00
|
|
|
}
|
|
|
|
|
2017-06-25 15:13:03 -04:00
|
|
|
public getShaDir(prDir: string, sha: string, legacy = false): string {
|
2018-05-10 08:56:07 -04:00
|
|
|
return path.join(prDir, legacy ? sha : computeShortSha(sha));
|
2017-06-25 15:13:03 -04:00
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
public readBuildFile(pr: number, sha: string, relFilePath: string, isPublic = true, legacy = false): string {
|
2017-06-25 15:13:03 -04:00
|
|
|
const shaDir = this.getShaDir(this.getPrDir(pr, isPublic), sha, legacy);
|
|
|
|
const absFilePath = path.join(shaDir, relFilePath);
|
2017-02-06 13:40:28 -05:00
|
|
|
return fs.readFileSync(absFilePath, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
public runCmd(cmd: string, opts: cp.ExecFileOptions = {}): Promise<CmdResult> {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const proc = cp.exec(cmd, opts, (err, stdout, stderr) => resolve({success: !err, err, stdout, stderr}));
|
|
|
|
this.createCleanUpFn(() => proc.kill());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:10:22 -04:00
|
|
|
public runForAllSupportedSchemes(suiteFactory: TestSuiteFactory): void {
|
2017-02-27 05:11:55 -05:00
|
|
|
Object.keys(this.portPerScheme).forEach(scheme => suiteFactory(scheme, this.portPerScheme[scheme]));
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
2018-08-25 17:29:14 -04:00
|
|
|
public verifyResponse(status: number | [number, string], regex: string | RegExp = /^/): VerifyCmdResultFn {
|
2017-02-06 13:40:28 -05:00
|
|
|
let statusCode: number;
|
|
|
|
let statusText: string;
|
|
|
|
|
|
|
|
if (Array.isArray(status)) {
|
2017-02-27 04:59:15 -05:00
|
|
|
statusCode = status[0];
|
|
|
|
statusText = status[1];
|
2017-02-06 13:40:28 -05:00
|
|
|
} else {
|
|
|
|
statusCode = status;
|
2017-09-23 08:24:04 -04:00
|
|
|
statusText = http.STATUS_CODES[statusCode] || 'UNKNOWN_STATUS_CODE';
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (result: CmdResult) => {
|
|
|
|
const [headers, body] = result.stdout.
|
|
|
|
split(/(?:\r?\n){2,}/).
|
|
|
|
map(s => s.trim()).
|
2017-06-20 13:22:32 -04:00
|
|
|
slice(-2); // In case of redirect, discard the previous headers.
|
|
|
|
// Only keep the last to sections (final headers and body).
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
if (!result.success) {
|
2018-05-12 10:39:16 -04:00
|
|
|
this.logger.log('Stdout:', result.stdout);
|
|
|
|
this.logger.error('Stderr:', result.stderr);
|
|
|
|
this.logger.error('Error:', result.err);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(result.success).toBe(true);
|
|
|
|
expect(headers).toContain(`${statusCode} ${statusText}`);
|
|
|
|
expect(body).toMatch(regex);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
public writeBuildFile(pr: number, sha: string, relFilePath: string, content: string, isPublic = true,
|
2018-08-10 06:10:22 -04:00
|
|
|
legacy = false): void {
|
2017-06-25 15:13:03 -04:00
|
|
|
const shaDir = this.getShaDir(this.getPrDir(pr, isPublic), sha, legacy);
|
|
|
|
const absFilePath = path.join(shaDir, relFilePath);
|
2018-05-10 08:56:07 -04:00
|
|
|
this.writeFile(absFilePath, {content}, true);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
2018-08-10 06:10:22 -04:00
|
|
|
public writeFile(filePath: string, {content, size}: FileSpecs, force = false): void {
|
2017-02-06 13:40:28 -05:00
|
|
|
if (!force && fs.existsSync(filePath)) {
|
|
|
|
throw new Error(`Refusing to overwrite existing file '${filePath}'.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cleanUpTarget = filePath;
|
|
|
|
while (!fs.existsSync(path.dirname(cleanUpTarget))) {
|
|
|
|
cleanUpTarget = path.dirname(cleanUpTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
shell.mkdir('-p', path.dirname(filePath));
|
|
|
|
if (size) {
|
|
|
|
// Create a file of the specified size.
|
|
|
|
cp.execSync(`fallocate -l ${size} ${filePath}`);
|
|
|
|
} else {
|
|
|
|
// Create a file with the specified content.
|
|
|
|
fs.writeFileSync(filePath, content || '');
|
|
|
|
}
|
2018-05-10 08:56:07 -04:00
|
|
|
shell.exec(`chown ${AIO_WWW_USER} ${filePath}`);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Methods - Protected
|
2017-09-23 08:24:04 -04:00
|
|
|
protected createCleanUpFn(fn: () => void): CleanUpFn {
|
2017-02-06 13:40:28 -05:00
|
|
|
const cleanUpFn = () => {
|
|
|
|
const idx = this.cleanUpFns.indexOf(cleanUpFn);
|
|
|
|
if (idx !== -1) {
|
|
|
|
this.cleanUpFns.splice(idx, 1);
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.cleanUpFns.push(cleanUpFn);
|
|
|
|
|
|
|
|
return cleanUpFn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 17:29:14 -04:00
|
|
|
interface DefaultCurlOptions {
|
|
|
|
defaultMethod?: CurlOptions['method'];
|
|
|
|
defaultOptions?: CurlOptions['options'];
|
|
|
|
defaultHeaders?: CurlOptions['headers'];
|
|
|
|
defaultData?: CurlOptions['data'];
|
|
|
|
defaultExtraPath?: CurlOptions['extraPath'];
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
interface CurlOptions {
|
|
|
|
method?: string;
|
|
|
|
options?: string;
|
2018-08-25 17:29:14 -04:00
|
|
|
headers?: string[];
|
2018-05-10 08:56:07 -04:00
|
|
|
data?: any;
|
|
|
|
url?: string;
|
|
|
|
extraPath?: string;
|
|
|
|
}
|
|
|
|
|
2018-08-25 17:29:14 -04:00
|
|
|
export function makeCurl(baseUrl: string, {
|
|
|
|
defaultMethod = 'POST',
|
|
|
|
defaultOptions = '',
|
|
|
|
defaultHeaders = ['Content-Type: application/json'],
|
|
|
|
defaultData = {},
|
|
|
|
defaultExtraPath = '',
|
|
|
|
}: DefaultCurlOptions = {}) {
|
2018-05-10 08:56:07 -04:00
|
|
|
return function curl({
|
2018-08-25 17:29:14 -04:00
|
|
|
method = defaultMethod,
|
|
|
|
options = defaultOptions,
|
|
|
|
headers = defaultHeaders,
|
|
|
|
data = defaultData,
|
2018-05-10 08:56:07 -04:00
|
|
|
url = baseUrl,
|
2018-08-25 17:29:14 -04:00
|
|
|
extraPath = defaultExtraPath,
|
2018-05-10 08:56:07 -04:00
|
|
|
}: CurlOptions) {
|
|
|
|
const dataString = data ? JSON.stringify(data) : '';
|
|
|
|
const cmd = `curl -iLX ${method} ` +
|
|
|
|
`${options} ` +
|
2018-08-25 17:29:14 -04:00
|
|
|
headers.map(header => `--header "${header}" `).join('') +
|
2018-05-10 08:56:07 -04:00
|
|
|
`--data '${dataString}' ` +
|
|
|
|
`${url}${extraPath}`;
|
|
|
|
return helper.runCmd(cmd);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:10:22 -04:00
|
|
|
export interface PayloadData {
|
|
|
|
data: {
|
|
|
|
payload: {
|
|
|
|
build_num: number,
|
|
|
|
build_parameters: {
|
|
|
|
CIRCLE_JOB: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function payload(buildNum: number): PayloadData {
|
2018-05-10 08:56:07 -04:00
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
payload: {
|
|
|
|
build_num: buildNum,
|
|
|
|
build_parameters: { CIRCLE_JOB: 'aio_preview' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
// Exports
|
|
|
|
export const helper = new Helper();
|