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

Events V2: Fix for how fake ES is set up #8317

Merged
merged 2 commits into from
Jan 10, 2025
Merged
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
9 changes: 6 additions & 3 deletions packages/events/src/service/indexing/indexing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/

import { getOrCreateClient } from '@events/storage/__mocks__/elasticsearch'
import {
getEventIndexName,
getOrCreateClient
} from '@events/storage/elasticsearch'
import { payloadGenerator } from '@events/tests/generators'
import { createTestClient } from '@events/tests/utils'
import { indexAllEvents } from './indexing'
Expand All @@ -27,7 +30,7 @@ test('indexes all records from MongoDB with one function call', async () => {
}

const body = await esClient.search({
index: 'events',
index: getEventIndexName(),
body: {
query: {
match_all: {}
Expand All @@ -43,7 +46,7 @@ test('records are automatically indexed when they are created', async () => {

const esClient = getOrCreateClient()
const body = await esClient.search({
index: 'events',
index: getEventIndexName(),
body: {
query: {
match_all: {}
Expand Down
33 changes: 21 additions & 12 deletions packages/events/src/service/indexing/indexing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/

const EVENTS_INDEX = 'events'

import {
EventDocument,
EventIndex,
Expand All @@ -19,7 +17,10 @@ import {

import { type estypes } from '@elastic/elasticsearch'
import { getClient } from '@events/storage'
import { getOrCreateClient } from '@events/storage/elasticsearch'
import {
getEventIndexName,
getOrCreateClient
} from '@events/storage/elasticsearch'
import { Transform } from 'stream'
import { z } from 'zod'

Expand All @@ -32,7 +33,7 @@ function eventToEventIndex(event: EventDocument): EventIndex {
*/
type EventIndexMapping = { [key in keyof EventIndex]: estypes.MappingProperty }

function createIndex(indexName: string) {
export function createIndex(indexName: string) {
const client = getOrCreateClient()
return client.indices.create({
index: indexName,
Expand All @@ -58,9 +59,15 @@ function createIndex(indexName: string) {
export async function indexAllEvents() {
const mongoClient = await getClient()
const esClient = getOrCreateClient()
await createIndex(EVENTS_INDEX)
const hasEventsIndex = await esClient.indices.exists({
index: getEventIndexName()
})

const stream = mongoClient.collection(EVENTS_INDEX).find().stream()
if (!hasEventsIndex) {
await createIndex(getEventIndexName())
}

const stream = mongoClient.collection(getEventIndexName()).find().stream()

const transformedStreamData = new Transform({
readableObjectMode: true,
Expand All @@ -76,7 +83,7 @@ export async function indexAllEvents() {
datasource: stream.pipe(transformedStreamData),
onDocument: (doc: EventIndex) => ({
index: {
_index: EVENTS_INDEX,
_index: getEventIndexName(),
_id: doc.id
}
}),
Expand All @@ -88,7 +95,7 @@ export async function indexEvent(event: EventDocument) {
const esClient = getOrCreateClient()

return esClient.update({
index: EVENTS_INDEX,
index: getEventIndexName(),
id: event.id,
body: {
doc: eventToEventIndex(event),
Expand All @@ -102,7 +109,7 @@ export async function deleteEventIndex(eventId: string) {
const esClient = getOrCreateClient()

const response = await esClient.delete({
index: EVENTS_INDEX,
index: getEventIndexName(),
id: eventId,
refresh: 'wait_for'
})
Expand All @@ -113,19 +120,21 @@ export async function deleteEventIndex(eventId: string) {
export async function getIndexedEvents() {
const esClient = getOrCreateClient()

const hasEventsIndex = await esClient.indices.exists({ index: EVENTS_INDEX })
const hasEventsIndex = await esClient.indices.exists({
index: getEventIndexName()
})

if (!hasEventsIndex) {
// @TODO: We probably want to create the index on startup or as part of the deployment process.
// eslint-disable-next-line no-console
console.error('Events index does not exist. Creating one.')
await createIndex(EVENTS_INDEX)
await createIndex(getEventIndexName())

return []
}

const response = await esClient.search({
index: EVENTS_INDEX,
index: getEventIndexName(),
size: 10000,
request_cache: false
})
Expand Down
35 changes: 3 additions & 32 deletions packages/events/src/storage/__mocks__/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,13 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import {
ElasticsearchContainer,
StartedElasticsearchContainer
} from '@testcontainers/elasticsearch'
import * as elasticsearch from '@elastic/elasticsearch'
let server: StartedElasticsearchContainer
import { inject, vi } from 'vitest'

export async function setupServer() {
if (server) {
return
}

server = await new ElasticsearchContainer('elasticsearch:8.14.3')
.withExposedPorts(9200)
.withStartupTimeout(120_000)
.withEnvironment({
'discovery.type': 'single-node',
'xpack.security.enabled': 'false',
'action.destructive_requires_name': 'false'
})
.start()
}

export async function resetServer() {
const host = server.getHost()
const port = server.getMappedPort(9200)
return fetch(`http://${host}:${port}/*`, {
method: 'DELETE'
})
}
export const getEventIndexName = vi.fn()

export function getOrCreateClient() {
const host = server.getHost()
const port = server.getMappedPort(9200)

return new elasticsearch.Client({
node: `http://${host}:${port}`
node: `http://${inject('ELASTICSEARCH_URI')}`
})
}
2 changes: 2 additions & 0 deletions packages/events/src/storage/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { env } from '@events/environment'

let client: elasticsearch.Client

export const getEventIndexName = () => 'events'

export const getOrCreateClient = () => {
if (!client) {
client = new elasticsearch.Client({
Expand Down
21 changes: 20 additions & 1 deletion packages/events/src/tests/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/

import { ElasticsearchContainer } from '@testcontainers/elasticsearch'
import { MongoMemoryServer } from 'mongodb-memory-server'
export type { ProvidedContext } from 'vitest'

declare module 'vitest' {
export interface ProvidedContext {
MONGO_URI: string
ELASTICSEARCH_URI: string
}
}

async function setupServer() {
return new ElasticsearchContainer('elasticsearch:8.14.3')
.withExposedPorts(9200)
.withStartupTimeout(120_000)
.withEnvironment({
'discovery.type': 'single-node',
'xpack.security.enabled': 'false',
'action.destructive_requires_name': 'false'
})
.start()
}

export default async function setup({ provide }: any) {
const mongod = await MongoMemoryServer.create()
const [mongod, es] = await Promise.all([
await MongoMemoryServer.create(),
await setupServer()
])
const uri = mongod.getUri()

provide('ELASTICSEARCH_URI', `${es.getHost()}:${es.getMappedPort(9200)}`)
provide('MONGO_URI', uri)

return async () => {
await es.stop()
await mongod.stop()
}
}
24 changes: 14 additions & 10 deletions packages/events/src/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { vi } from 'vitest'
import { resetServer as resetMongoServer } from '@events/storage/__mocks__/mongodb'
import { inject, vi } from 'vitest'

import { createIndex } from '@events/service/indexing/indexing'
import { tennisClubMembershipEvent } from '@opencrvs/commons/fixtures'
import {
resetServer as resetESServer,
setupServer as setupESServer
} from '@events/storage/__mocks__/elasticsearch'
import { mswServer } from './msw'

vi.mock('@events/storage/mongodb')
Expand All @@ -28,16 +25,23 @@ vi.mock('@events/service/config/config', () => ({
])
}))

beforeAll(() => Promise.all([setupESServer()]), 100000)
beforeEach(() => Promise.all([resetMongoServer(), resetESServer()]))
async function resetServer() {
// @ts-ignore "Cannot find module '@events/storage/elasticsearch' or its corresponding type declarations."
const { getEventIndexName } = await import('@events/storage/elasticsearch')
const index = 'events' + Date.now() + Math.random()
getEventIndexName.mockReturnValue(index)
await createIndex(index)
}

beforeEach(async () => {
return Promise.all([resetMongoServer(), resetServer()])
})

beforeAll(() =>
mswServer.listen({
onUnhandledRequest: (req) => {
const elasticRegex = /http:\/\/localhost:551\d{2}\/.*/

const isElasticResetCall =
req.method === 'DELETE' && elasticRegex.test(req.url)
req.method === 'DELETE' && req.url.includes(inject('ELASTICSEARCH_URI'))

if (!isElasticResetCall) {
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion packages/events/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"types": ["fhir", "jest"]
"types": ["vitest/globals"]
},
"include": ["src/**/*.ts", "typings", "tslint-rules"],
"exclude": ["node_modules", "build", "scripts", "acceptance-tests"]
Expand Down
Loading