Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integration tests #13

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
npm-debug.log
npm-debug.log
docker-compose.*
Makefile
60 changes: 57 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
name: build
name: build-app

on:
push:

jobs:
build:
uses: decentraland/platform-actions/.github/workflows/apps-with-db-build.yml@main
validations:
runs-on: ubuntu-latest

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this? What's missing on the apps-with-db-build.yml?

steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: yarn
- name: install
run: yarn install --frozen-lockfile
- name: lint
run: yarn lint:check

test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_DB: db
POSTGRES_PASSWORD: pass1234
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
steps:
- uses: actions/checkout@v3
- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: yarn
- name: install
run: yarn install --frozen-lockfile
- name: create .env
run: echo "" >> .env
- name: build
run: yarn build
- name: test
run: yarn test
env:
PG_COMPONENT_PSQL_CONNECTION_STRING: postgres://postgres:pass1234@localhost/db
36 changes: 16 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
ARG RUN

FROM node:lts as builderenv
FROM node:18-alpine as builderenv

WORKDIR /app

# some packages require a build step
RUN apt-get update && apt-get -y -qq install build-essential

# We use Tini to handle signals and PID1 (https://github.com/krallin/tini, read why here https://github.com/krallin/tini/issues/8)
ENV TINI_VERSION v0.19.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini

# install dependencies
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
RUN yarn
RUN apk update && apk add wget

# build the app
COPY . /app
RUN yarn install --frozen-lockfile
RUN yarn build
RUN yarn test

# remove devDependencies, keep only used dependencies
RUN yarn install --frozen-lockfile --production
RUN yarn install --prod --frozen-lockfile

########################## END OF BUILD STAGE ##########################

FROM node:lts
FROM node:18-alpine

RUN apk update && apk add --update wget && apk add --update tini

# NODE_ENV is used to configure some runtime options, like JSON logger
ENV NODE_ENV production

ARG COMMIT_HASH=local
ENV COMMIT_HASH=${COMMIT_HASH:-local}

ARG CURRENT_VERSION=Unknown
ENV CURRENT_VERSION=${CURRENT_VERSION:-Unknown}

WORKDIR /app
COPY --from=builderenv /app /app
COPY --from=builderenv /tini /tini
# Please _DO NOT_ use a custom ENTRYPOINT because it may prevent signals
# (i.e. SIGTERM) to reach the service
# Read more here: https://aws.amazon.com/blogs/containers/graceful-shutdowns-with-ecs/
# and: https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/

RUN echo "" > /app/.env

ENTRYPOINT ["/tini", "--"]
# Run the program under Tini
CMD [ "/usr/local/bin/node", "--trace-warnings", "--abort-on-uncaught-exception", "--unhandled-rejections=strict", "dist/index.js" ]
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
DOCKER_COMPOSE_EXISTS = $(shell which docker-compose > /dev/null && echo 1 || echo 0)

RUN_SERVICES = docker-compose -f docker-compose.local.yml up -d && docker exec social_service_ea_db bash -c "until pg_isready; do sleep 1; done" > /dev/null && sleep 5
RUN_TEST_SERVICES = docker-compose -f docker-compose.test.yml up -d && docker exec social_service_ea_test_db bash -c "until pg_isready; do sleep 1; done" > /dev/null && sleep 5
LOCAL_DB = $(shell docker ps | grep social_service_ea_db > /dev/null && echo 1 || echo 0)

run-services:
ifeq ($(DOCKER_COMPOSE_EXISTS), 1)
@$(RUN_SERVICES)
else
@$(ERROR) "Install Docker in order to run the local DB"
@exit 1;
endif

run-test-services:
ifeq ($(DOCKER_COMPOSE_EXISTS), 1)
@$(RUN_TEST_SERVICES)
else
@$(ERROR) "Install Docker in order to run the local DB"
@exit 1;
endif

stop-services:
-@docker stop social_service_ea_db
-@docker stop social_service_ea_redis

stop-test-services:
-@docker stop social_service_ea_test_db
-@docker stop social_service_ea_test_redis
-@docker rm social_service_ea_test_db
-@docker rm social_service_ea_test_redis

# Local testing
tests:
ifeq ($(LOCAL_DB), 1)
@make stop-services
@make run-test-services
-@npm run test
@make stop-test-services
else
@make run-test-services
-@npm run test
@make stop-test-services
endif
23 changes: 23 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3.8'
services:
postgres:
container_name: 'social_service_ea_db'
image: 'postgres:latest'
restart: always
user: postgres
volumes:
- postgres_volume:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=social_service_ea
ports:
- '5432:5432'
redis:
container_name: 'social_service_ea_redis'
image: 'redis:latest'
restart: always
user: redis
ports:
- '6379:6379'
volumes:
postgres_volume:
19 changes: 19 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.8'
services:
postgres:
container_name: 'social_service_ea_test_db'
image: 'postgres:latest'
restart: always
user: postgres
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=social_service_ea
ports:
- '5432:5432'
redis:
container_name: 'social_service_ea_test_redis'
image: 'redis:latest'
restart: always
user: redis
ports:
- '6379:6379'
12 changes: 6 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
transform: {
"^.+\\.(ts|tsx)$": ["ts-jest", {tsconfig: "test/tsconfig.json"}]
'^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: 'test/tsconfig.json' }]
},
moduleFileExtensions: ["ts", "js"],
coverageDirectory: "coverage",
collectCoverageFrom: ["src/**/*.ts", "src/**/*.js"],
testMatch: ["**/*.spec.(ts)"],
testEnvironment: "node",
moduleFileExtensions: ['ts', 'js'],
coverageDirectory: 'coverage',
collectCoverageFrom: ['src/**/*.ts', 'src/**/*.js'],
testMatch: ['**/*.spec.(ts)'],
testEnvironment: 'node'
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@dcl/rpc": "^1.1.2",
"@well-known-components/env-config-provider": "^1.2.0",
"@well-known-components/fetch-component": "^2.0.2",
"@well-known-components/http-server": "^2.1.0",
"@well-known-components/interfaces": "^1.4.3",
"@well-known-components/logger": "^3.1.3",
"@well-known-components/metrics": "^2.1.0",
Expand Down
5 changes: 2 additions & 3 deletions src/adapters/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
async updateFriendshipStatus(friendshipId, isActive, txClient) {
logger.debug(`updating ${friendshipId} - ${isActive}`)
const query = SQL`UPDATE friendships SET is_active = ${isActive}, updated_at = now() WHERE id = ${friendshipId}`
console.log(query.text)
console.log(query.values)

if (txClient) {
const results = await txClient.query(query)
Expand Down Expand Up @@ -203,8 +201,9 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
} catch (error) {
logger.error(error as any)
await client.query('ROLLBACK')
client.release()
throw error
} finally {
client.release()
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/adapters/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ export default function createPubSubComponent(components: Pick<AppComponents, 'l
}
},
async stop() {
if (subClient.isReady) {
await subClient.disconnect()
}

if (pubClient.isReady) {
await pubClient.disconnect()
}
await subClient.unsubscribe(FRIENDSHIP_UPDATES_CHANNEL, friendshipUpdatesCb)
await subClient.disconnect()
await pubClient.disconnect()
},
async subscribeToFriendshipUpdates(cb) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { validateMetricsDeclaration } from '@well-known-components/metrics'
import { getDefaultHttpMetrics } from '@well-known-components/http-server'
import { getDefaultHttpMetrics } from '@well-known-components/uws-http-server'
import { metricDeclarations as logsMetricsDeclarations } from '@well-known-components/logger'

export const metricDeclarations = {
Expand Down
20 changes: 3 additions & 17 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import type {
IConfigComponent,
ILoggerComponent,
IHttpServerComponent,
IBaseComponent,
IMetricsComponent,
IFetchComponent
} from '@well-known-components/interfaces'
import { IPgComponent } from '@well-known-components/pg-component'
import { WebSocketServer } from 'ws'
import { Emitter } from 'mitt'
import { HttpRequest, HttpResponse, IUWsComponent, WebSocket } from '@well-known-components/uws-http-server'
import { metricDeclarations } from './metrics'
import { IDatabaseComponent } from './adapters/db'
import { IRedisComponent } from './adapters/redis'
import { IRPCServerComponent } from './adapters/rpcServer'
import { IPubSubComponent } from './adapters/pubsub'
import { HttpRequest, HttpResponse, IUWsComponent, WebSocket } from '@well-known-components/uws-http-server'
import { IUWebSocketEventMap } from './utils/UWebSocketTransport'
import { ISocialServiceRpcClientComponent } from '../test/rpc'

export type GlobalContext = {
components: BaseComponents
Expand All @@ -40,8 +40,7 @@ export type AppComponents = BaseComponents

// components used in tests
export type TestComponents = BaseComponents & {
// A fetch component that only hits the test server
localFetch: IFetchComponent
socialServiceClient: ISocialServiceRpcClientComponent
}

export type JsonBody = Record<string, any>
Expand Down Expand Up @@ -73,19 +72,6 @@ export type WsUserData =

export type InternalWebSocket = WebSocket<WsUserData>

// this type simplifies the typings of http handlers
export type HandlerContextWithPath<
ComponentNames extends keyof AppComponents,
Path extends string = any
> = IHttpServerComponent.PathAwareContext<
IHttpServerComponent.DefaultContext<{
components: Pick<AppComponents, ComponentNames>
}>,
Path
>

export type Context<Path extends string = any> = IHttpServerComponent.PathAwareContext<GlobalContext, Path>

export type IWebSocketComponent = IBaseComponent & {
ws: WebSocketServer
}
Expand Down
15 changes: 7 additions & 8 deletions test/components.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// This file is the "test-environment" analogous for src/components.ts
// Here we define the test components to be used in the testing environment

import { createRunner, createLocalFetchCompoment } from "@well-known-components/test-helpers"
import { createRunner, createLocalFetchCompoment } from '@well-known-components/test-helpers'

import { main } from "../src/service"
import { TestComponents } from "../src/types"
import { initComponents as originalInitComponents } from "../src/components"
import { main } from '../src/service'
import { TestComponents } from '../src/types'
import { initComponents as originalInitComponents } from '../src/components'
import { createSocialServiceRpcClientComponent } from './rpc'

/**
* Behaves like Jest "describe" function, used to describe a test for a
Expand All @@ -16,16 +17,14 @@ import { initComponents as originalInitComponents } from "../src/components"
*/
export const test = createRunner<TestComponents>({
main,
initComponents,
initComponents
})

async function initComponents(): Promise<TestComponents> {
const components = await originalInitComponents()

const { config } = components

return {
...components,
localFetch: await createLocalFetchCompoment(config),
socialServiceClient: await createSocialServiceRpcClientComponent({ logs: components.logs })
}
}
Loading
Loading