feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -15,10 +15,11 @@
*/
import {Logger} from '@openstapps/logger';
import {execSync} from 'child_process';
import * as Dockerode from 'dockerode';
import type {ContainerInfo} from 'dockerode';
import mustache from 'mustache';
import {asyncReadFile, asyncWriteFile} from './common.js';
import {getContainers, getTemplateView} from './main.js';
import {readFile, writeFile} from 'fs/promises';
import {configFile} from './common.js';
/* eslint-disable unicorn/prefer-module */
@@ -39,14 +40,11 @@ async function updateNginxConfig() {
const containers = await getContainers();
const containerHash = containers
.map((container: Dockerode.ContainerInfo) => {
.map((container: ContainerInfo) => {
return container.Id;
})
.join(',');
delete require.cache[require.resolve('config')];
// eslint-disable-next-line @typescript-eslint/no-var-requires
const configFile = require('config');
const configHash = JSON.stringify(configFile);
// if containers changed -> write config file, reload nginx
@@ -57,7 +55,7 @@ async function updateNginxConfig() {
// render nginx config file
const nginxConfig = mustache.render(
await asyncReadFile('nginx.conf.template', 'utf8'),
await readFile('nginx.conf.template', 'utf8'),
await getTemplateView(containers),
);
@@ -67,7 +65,7 @@ async function updateNginxConfig() {
Logger.log(`Writing new config file "${configFile.output}"`);
// overwrite nginx config file with our rendered one
await asyncWriteFile(configFile.output, nginxConfig, 'utf8');
await writeFile(configFile.output, nginxConfig, 'utf8');
Logger.log('Executing "nginx -s reload" to tell nginx to reload the configuration file');

View File

@@ -15,15 +15,11 @@
*/
import {Logger, SMTP} from '@openstapps/logger';
import config from 'config';
import {existsSync, readFile, writeFile} from 'fs';
import {promisify} from 'util';
import {existsSync} from 'fs';
// set transport on logger
Logger.setTransport(SMTP.getInstance());
export const asyncReadFile = promisify(readFile);
export const asyncWriteFile = promisify(writeFile);
/**
* A representation of the file paths of the needed ssl certificates
*/

View File

@@ -20,8 +20,7 @@ import mustache from 'mustache';
import path from 'path';
import * as semver from 'semver';
import {
asyncReadFile,
ConfigFile,
configFile,
isFileType,
protocolHardeningParameters,
SSLFilePaths,
@@ -29,10 +28,9 @@ import {
SupportedLogFormats,
TemplateView,
} from './common.js';
// @ts-expect-error missing type defs
import nodePortScanner from 'node-port-scanner';
import {readFile} from 'fs/promises';
import PortScanner from './port-scanner.js';
/* eslint-disable unicorn/prefer-module */
/* eslint-disable unicorn/no-await-expression-member */
/**
@@ -90,9 +88,7 @@ Please expose a port if the container should be accessible by NGINX.`);
// Get a routable network connection
for (const network in container.NetworkSettings.Networks) {
const scan = await nodePortScanner(container.NetworkSettings.Networks[network].IPAddress, [port]);
if ((scan.ports.open as Array<number>).includes(port)) {
if (await PortScanner.isPortFree(port, container.NetworkSettings.Networks[network].IPAddress)) {
Logger.info(
`${container.Names[0]} reachable via ${container.NetworkSettings.Networks[network].IPAddress}:${port}`,
);
@@ -260,7 +256,7 @@ export async function generateMetricsServer(logFormat: string, enableMetrics?: b
* @param view Data to render template with
*/
async function renderTemplate(path: string, view: unknown): Promise<string> {
const content = await asyncReadFile(path, 'utf8');
const content = await readFile(path, 'utf8');
return mustache.render(content, view);
}
@@ -283,12 +279,7 @@ function generateRateLimitAllowList(entries: string[]): string {
* @param containers List of container info
*/
export async function getTemplateView(containers: Dockerode.ContainerInfo[]): Promise<TemplateView> {
delete require.cache[require.resolve('config')];
// eslint-disable-next-line @typescript-eslint/no-var-requires
const config = require('config');
const configFile = config as ConfigFile;
const cors = await asyncReadFile('./fixtures/cors.template', 'utf8');
const cors = await readFile('./fixtures/cors.template', 'utf8');
const visibleRoutesPromises = ['/'].map(async route => {
return renderTemplate(path.join('fixtures', 'visibleRoute.template'), {

View File

@@ -0,0 +1,22 @@
import {createServer, Server} from 'net';
/**
* Checks if a port is in use
*/
async function isPortFree(port: number, hostname?: string): Promise<boolean> {
return new Promise((resolve, reject) => {
const server: Server = createServer()
.once('error', error => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((error as any).code === 'EADDRINUSE') {
resolve(true);
} else {
reject(error);
}
})
.once('listening', () => server.once('close', () => resolve(false)).close())
.listen(port, hostname);
});
}
export default {isPortFree};