Skip to content

Commit

Permalink
chore(cb2-11222): upgrade to use serverless-dynamodb (#231)
Browse files Browse the repository at this point in the history
* chore(cb2-11222): update readme

* chore(CB2-11222): update packages

* chore(cb2-11222): update serverless-plugin-typescript

* chore(cb2-11222): package-lock update

* chore(cb2-11222): change port number

---------

Co-authored-by: Ruairidh Taylor <[email protected]>
  • Loading branch information
luluharvey and Ruairidh-BJSS authored Mar 21, 2024
1 parent e324b25 commit 6261664
Show file tree
Hide file tree
Showing 11 changed files with 5,565 additions and 7,930 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ The test-types microservice contains some reference data to be used for CVS serv

## Dependencies

The project runs on node >18.x with typescript and serverless framework. For further details about project dependencies, please refer to the `package.json` file.
- The project runs on node >18.x with typescript and serverless framework. For further details about project dependencies, please refer to the `package.json` file.
[nvm](https://github.com/nvm-sh/nvm/blob/master/README.md) is used to managed node versions and configuration explicitly done per project using an `.npmrc` file.

- [Java Development Kit (JDK)](https://docs.oracle.com/en/java/javase/11/) (version 11 or higher)

### Prerequisites

Please install and run the following securiy programs as part of your development process:
Expand All @@ -20,6 +22,9 @@ Please install and run the following securiy programs as part of your developmen

These will be run as part of your projects hooks so you don't accidentally introduce any new security vulnerabilities.

- [aws configure](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-methods)
Run `aws configure` and set random credentials

## Architecture

Data is used is made available to VTA for searching a vehicle.
Expand Down
13,393 changes: 5,528 additions & 7,865 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@
"npm": "9.*"
},
"scripts": {
"start": "BRANCH=local serverless offline start",
"start": "BRANCH=local SLS_DEBUG=* serverless offline start --noPrependStageInUrl",
"build-start": "npm run build && npm run start",
"debug": "SLS_DEBUG=* BRANCH=local node --inspect ./node_modules/serverless/bin/serverless offline start",
"build": "node_modules/typescript/bin/tsc --rootDir ./ --outDir .build --sourceMap false && npm run build:copy",
"build:copy": "find src -type f \\( -name \"*.yml\" -or -name \"*.json\" \\) | cpio -pdm .build && find tests -type f \\( -name \"*.yml\" -or -name \"*.json\" \\) | cpio -pdm .build",
"test": "npm run test:unit -- --coverage",
"test:unit": "BRANCH=local SLS_DEBUG=* AWS_XRAY_CONTEXT_MISSING=LOG_ERROR jest --testMatch=\"**/*.unitTest.ts\" --runInBand",
"test:integration": "BRANCH=local jest --testMatch=\"**/*.intTest.ts\" --runInBand",
"test-i": "npm run test:integration -- --globalSetup='./scripts/setup.ts' --globalTeardown='./scripts/teardown.ts'",
"test:integration": "BRANCH=local jest --testMatch=\"**/*.intTest.ts\" --runInBand --detectOpenHandles",
"test-i": "AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1 npm run test:integration -- --globalSetup='./scripts/setup.ts' --globalTeardown='./scripts/teardown.ts'",
"prepush": "npm run test && npm run build && npm run test-i",
"security-checks": "git secrets --scan",
"lint": "tslint src/**/*.ts tests/**/*.ts -q",
"format": "prettier --write .",
"sonar-scanner": "sonar-scanner",
"audit": "npm audit --prod",
"package": "mkdir ${ZIP_NAME} && cp package.json package-lock.json ${ZIP_NAME}/ && cp -r .build/src/* ${ZIP_NAME}/ && cd ${ZIP_NAME} && npm ci --production && rm package.json package-lock.json && zip -qr ../${ZIP_NAME}.zip .",
"tools-setup": "node_modules/.bin/sls dynamodb install",
"postinstall": "patch-package"
"tools-setup": "sls dynamodb install"
},
"author": "",
"license": "MIT",
Expand Down Expand Up @@ -58,11 +57,10 @@
"lambda-tester": "^4.0.1",
"lodash": "^4.17.21",
"prettier": "^2.3.2",
"serverless": "^2.43.1",
"serverless-dynamodb-local": "^0.2.38",
"serverless-offline": "5.12.0",
"serverless": "^3.28.1",
"serverless-offline": "12.0.4",
"serverless-plugin-tracing": "^2.0.0",
"serverless-plugin-typescript": "^1.1.9",
"serverless-plugin-typescript": "^2.1.4",
"sonar-scanner": "^3.1.0",
"standard": "^14.3.1",
"supertest": "4.0.2",
Expand All @@ -77,9 +75,9 @@
"aws-xray-sdk": "^3.3.4",
"joi": "^14.3.1",
"node-yaml": "^4.0.1",
"patch-package": "^8.0.0",
"path-parser": "^4.2.0",
"reflect-metadata": "^0.1.13"
"reflect-metadata": "^0.1.13",
"serverless-dynamodb": "^0.2.50"
},
"jestSonar": {
"reportPath": ".reports",
Expand Down
34 changes: 0 additions & 34 deletions patches/dynamodb-localhost+0.0.9.patch

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/getDBPid.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lsof -i:8002 | awk '{print $2}' | grep -v '^PID'
lsof -i:8002 | awk '{print $2}' | tail -1
2 changes: 1 addition & 1 deletion scripts/getServerPid.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lsof -i:3002 | awk '{print $2}' | grep -v '^PID'
lsof -i:3005 | awk '{print $2}' | grep -v '^PID'
16 changes: 10 additions & 6 deletions scripts/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn } from "child_process";
import { exec } from "child_process";

// We hook to serverless offline when firing its process
const SERVER_OK = `Offline [HTTP] listening on http://localhost:3002`;
const SERVER_OK = `Server ready: http://localhost:3005 🚀`;
// Serverless fires a local dynamo-db instance which is killed once the parent process is terminated
// the current serverless script checks whether a local instance is running but does not error when binding fails
// we force throwing an error so we always start from a clean slate if java.io.IOException: Failed to bind to 0.0.0.0/0.0.0.0:8006
Expand All @@ -10,17 +10,17 @@ const DYNAMO_LOCAL_ERROR_THREAD = `Exception in thread "main"`;
const setupServer = (process: any) => {
return new Promise((resolve, reject) => {
process.stdout.setEncoding("utf-8").on("data", (stream: any) => {
console.log(stream);
console.log(`stdout: ${stream}`);
if (stream.includes(SERVER_OK)) {
resolve(process);
}
});

process.stderr.setEncoding("utf-8").on("data", (stream: any) => {
console.log(`stderr: ${stream}`);
if (stream.includes(DYNAMO_LOCAL_ERROR_THREAD)) {
throw new Error("Internal Java process crashed");
}
if (stream.includes(SERVER_OK)) {
} else if (stream.includes(SERVER_OK)) {
resolve(process);
}
});
Expand All @@ -35,7 +35,11 @@ const setupServer = (process: any) => {
});
};

const server = spawn("npm", ["run", "start"], {});
const server = exec('npm run start &', (error) => {
if (error) {
console.error(`error starting server: ${error}`);
}
});

// setupServer(server);

Expand Down
10 changes: 6 additions & 4 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
service: cvs-svc-test-types

provider:
lambdaHashingVersion: 20201221
name: aws
runtime: nodejs18.x
region: eu-west-1
stage: dev
tracing:
lambda: true
Expand All @@ -20,12 +20,14 @@ provider:

plugins:
- serverless-offline
- serverless-dynamodb-local
- serverless-dynamodb
- serverless-plugin-tracing
- serverless-plugin-typescript

functions:
getTestTypes:
environment:
BRANCH: local
handler: src/handler.handler
events:
- http:
Expand Down Expand Up @@ -66,8 +68,8 @@ resources:
WriteCapacityUnits: 1
custom:
serverless-offline:
port: 3002
dynamodb:
httpPort: 3005
serverless-dynamodb:
start:
port: 8002
inMemory: true
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ dynamodb:
table: cvs-${BRANCH}-test-types
serverless:
basePath: /${BRANCH}
port: 3002
port: 3005
3 changes: 1 addition & 2 deletions tests/integration/testTypesDbEmpty.intTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import supertest from "supertest";
import { ITestType } from "../../src/models/ITestType";
import { HTTPRESPONSE } from "../../src/assets/Enums";
import { emptyDatabase, populateDatabase } from "../util/dbOperations";
const url = "http://localhost:3002/";
const url = "http://localhost:3005/";

describe("get testTypes", () => {
const request = supertest(url);
Expand Down
6 changes: 2 additions & 4 deletions tests/integration/testTypesService.intTest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* global describe it context before after beforeEach afterEach */
import supertest from "supertest";
import { TestTypesService } from "../../src/services/TestTypesService";
import TestTypesDAO from "../../src/models/TestTypesDAO";
import { TestTypesService } from "../../src/services/TestTypesService";
import TestTypes from "../resources/test-types.json";
import { emptyDatabase, populateDatabase } from "../util/dbOperations";
import LambdaTester from "lambda-tester";
import { getTestTypes } from "../../src/functions/getTestTypes";
import recursiveFind from "../util/recursiveFind";
const url = "http://localhost:3002/";
const url = "http://localhost:3005/";

describe("test types", () => {
const request = supertest(url);
Expand Down

0 comments on commit 6261664

Please sign in to comment.