Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Jul 2, 2024
1 parent a68ff59 commit fc556a4
Show file tree
Hide file tree
Showing 8 changed files with 4,396 additions and 711 deletions.
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
75 changes: 75 additions & 0 deletions __tests__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 { spawn, untilTerminated } from '../processes'
import { ChildProcess } from 'node:child_process'

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: ChildProcess

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 = await spawn('npx', ['arc', 'sandbox'], {
cwd,
stdio: 'inherit',
})
})

afterEach(async () => {
const processIsDead = untilTerminated(process)

assert.ok(process.kill(signal))
// Make sure arc sandbox is dead
await processIsDead
// 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,
'fetch failed'
)
}
})

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

0 comments on commit fc556a4

Please sign in to comment.