Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Jun 26, 2024
1 parent e62aa0a commit 3b6219d
Show file tree
Hide file tree
Showing 8 changed files with 4,510 additions and 646 deletions.
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
56 changes: 56 additions & 0 deletions __tests__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import assert from 'node:assert'
import { dirname, join } from 'node:path'
import { describe, it } from 'node:test'
import { execa, ExecaError } from 'execa'
import { fileURLToPath } from 'node:url'

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

const signal = 'SIGINT'
// const engines = ['elasticsearch', 'opensearch']
const engines = ['elasticsearch']

engines.forEach((engine) =>
describe(engine, () => {
it('starts and stops in sandbox mode', async () => {
// FIXME: replace with import.meta.resolve once it is stable in Node.js
const cwd = join(dirname(fileURLToPath(import.meta.url)), engine)
const process = execa('arc', ['sandbox'], {
preferLocal: true,
cwd,
stdin: 'inherit',
stdout: 'inherit',
stderr: 'inherit',
})

let result
try {
for (let i = 0; i < 30 && !result; i++) {
try {
const response = await fetch('http://localhost:3333/')
if (!response.ok) throw new Error(`HTTP ${response.status}`)
result = await response.json()
} catch {
await sleep(1000)
}
}
assert.deepStrictEqual(result?.body.cluster_name, 'elasticsearch')
} finally {
assert.ok(process.kill(signal))
}

let error
try {
await process
} catch (e) {
error = e
}
assert.notStrictEqual(error, undefined)
assert.ok(error instanceof ExecaError)
assert.strictEqual(error.isTerminated, true)
assert.strictEqual(error.signal, signal)
})
})
)
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const sandbox = {
},

async end() {
console.log('****stopping')
await local.stop()
},
}
Loading

0 comments on commit 3b6219d

Please sign in to comment.