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

Add unit tests #45

Merged
merged 2 commits into from
Jul 2, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ on:
jobs:
build:
uses: nasa-gcn/.github/.github/workflows/node.yml@main
with:
test-options:
12 changes: 12 additions & 0 deletions __tests__/elasticsearch/app.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@app
project

@http
get /

@search
sandboxEngine elasticsearch

@plugins
nasa-gcn/architect-plugin-search
src ../../index.js
28 changes: 28 additions & 0 deletions __tests__/elasticsearch/src/http/get-index/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { search } from '@nasa-gcn/architect-functions-search'
import { ConnectionError } from '@opensearch-project/opensearch/lib/errors.js'

export async function handler() {
let statusCode = 200,
result = {}

const client = await search()
try {
result = await client.info()
} catch (e) {
if (e instanceof ConnectionError) {
statusCode = 503
} else {
throw e
}
}

return {
statusCode,
body: JSON.stringify(result),
headers: {
'cache-control':
'no-cache, no-store, must-revalidate, max-age=0, s-maxage=0',
'content-type': 'application/json; charset=utf8',
},
}
}
12 changes: 12 additions & 0 deletions __tests__/opensearch/app.arc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@app
project

@http
get /

@search
sandboxEngine opensearch

@plugins
nasa-gcn/architect-plugin-search
src ../../index.js
1 change: 1 addition & 0 deletions __tests__/opensearch/src
76 changes: 76 additions & 0 deletions __tests__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import assert from 'node:assert'
import { dirname, join } from 'node:path'
import { afterEach, beforeEach, describe, test } from 'node:test'
import { fileURLToPath } from 'node:url'
import { ExecaError, execa } from 'execa'

async function sleep(timeout: number) {
return new Promise((resolve) => setTimeout(resolve, timeout))
}

async function fetchRetry(
...props: Parameters<typeof fetch>
): ReturnType<typeof fetch> {
let response
try {
response = await fetch(...props)
} catch (e) {
if (!(e instanceof TypeError)) throw e
}

if (response?.ok) {
return response
} else {
await sleep(1000)
return await fetchRetry(...props)
}
}

const signals = ['SIGTERM'] as const
const engines = ['elasticsearch', 'opensearch']

engines.forEach((engine) =>
signals.forEach((signal) => {
describe(`${engine} stops when sent signal ${signal}`, () => {
let process: ReturnType<typeof execa> | undefined

beforeEach(async () => {
// FIXME: replace with import.meta.resolve once it is stable in Node.js
const cwd = join(dirname(fileURLToPath(import.meta.url)), engine)

process = execa('arc', ['sandbox'], {
cwd,
preferLocal: true,
forceKillAfterDelay: false,
stdio: 'inherit',
})
})

afterEach(async () => {
if (process) {
assert.ok(process.kill(signal))
// Make sure arc sandbox is dead
try {
await process
} catch (e) {
if (!(e instanceof ExecaError)) throw e
}
// Give subprocesses some time to die
await sleep(1000)

// Make sure that arc sandbox and opensearch/elasticseasrch are both
// down and not responding to HTTP requests any more
for (const port of [3333, 9200]) {
assert.rejects(fetch(`http://localhost:${port}/`), TypeError)
}
}
})

test('connection was alive', async () => {
const response = await fetchRetry('http://localhost:3333/')
const result = await response.json()
assert.deepStrictEqual(result?.meta.connection.status, 'alive')
})
})
})
)
Loading
Loading