From 08f5d019130dac1fd734a7baf032b6bedf308c3f Mon Sep 17 00:00:00 2001 From: ernest-tpximpact Date: Fri, 1 Dec 2023 15:28:12 +0000 Subject: [PATCH] Add basic apps to test --- .github/workflows/.gitignore | 45 ++++++++++++++++++++++++++++++++++++ server.js | 42 +++++++++++++++++++++++++++++++++ tests.js | 39 +++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 .github/workflows/.gitignore create mode 100644 server.js create mode 100644 tests.js diff --git a/.github/workflows/.gitignore b/.github/workflows/.gitignore new file mode 100644 index 00000000..7254fa2f --- /dev/null +++ b/.github/workflows/.gitignore @@ -0,0 +1,45 @@ +__pycache__/ +*.py[cod] + +.cache +.coverage +build +dist +specification +*.egg-info +tmp/ +var/ +.DS_Store +*.swp +.eggs +*.gfs +.venv +.vscode + +tf/*/.terraform* +tf/**/tfplan + +/.python-version +.idea + +node_modules + +.editorconfig +pyrightconfig.json +tags +__pycache__ +.mypy_cache + +.env +.env.test +.db-data +static + +docker-compose.override.yml +.junitxml +.envrc +.env.staging +.env.sandbox +.env.production +coverage.xml +.secrets diff --git a/server.js b/server.js new file mode 100644 index 00000000..ced190ff --- /dev/null +++ b/server.js @@ -0,0 +1,42 @@ +const express = require('express') +const tests = require('./tests') +const {createLogger, transports, format} = require('winston') + +const logger = createLogger({ + level: 'info', + format: format.json(), + defaultMeta: {service: 'status'}, + transports: [new transports.Console()], +}); + +const app = express() + +app.get('/', (req, res) => { + Promise.all(tests.map(test => new Promise((resolve) => { + test.check().then(result => { + resolve([test.print, {status: result ? 'success' : 'failure', result}]) + }).catch(error => { + logger.error('error', { + test: test.print, + error, + }); + resolve([test.print, {status: 'failure', error: error.message}]) + }) + }))).then(checks => { + if (checks.filter(([_, v]) => v.status === 'failure').length) res.statusCode = 500 + + logger.info(res.statusCode === 500 ? 'failure' : 'success', { + results: Object.fromEntries(checks), + }); + res.json(Object.fromEntries(checks)); + }) +}) + +app.get('/health', (req, res) => { + logger.info('healthcheck'); + res.status(200).json({applicationHealth: 'ok'}) +}) + +app.listen(process.env.PORT || 80, () => { + logger.info(`Status application listening on port ${process.env.PORT || 80}`); +}) diff --git a/tests.js b/tests.js new file mode 100644 index 00000000..befe7adb --- /dev/null +++ b/tests.js @@ -0,0 +1,39 @@ +const {readFileSync} = require("fs"); +const {join} = require("path"); +const {Client} = require("pg"); +const {S3, STS} = require("aws-sdk"); + +module.exports = [{ + print: "DEPLOYMENT:TIME", + check: async () => readFileSync(join(process.cwd(), 'DEPLOY_TIME'), 'utf-8').replace('\n', ''), +}, { + print: "DB:READ", + check: async () => { + const client = new Client({connectionString: process.env.READ_DATABASE_URL}); + await client.connect(); + await client.end(); + return true; + }, +}, { + print: "DB:WRITE", + check: async () => { + const client = new Client({connectionString: process.env.WRITE_DATABASE_URL}); + await client.connect(); + await client.end(); + return true; + }, +}, { + print: "BUCKET:READ", + check: async () => { + const client = new S3({region: 'eu-west-2'}); + await client.getObject({Bucket: process.env.COLLECTION_DATA_BUCKET, Key: 'index.html'}).promise(); + return true; + }, +}, { + print: "ROLE:APPLICATION", + check: async () => { + const client = new STS({region: 'eu-west-2'}); + const response = await client.getCallerIdentity({}).promise(); + return response.Arn.indexOf('task-application-role') !== -1; + }, +}]