Testing
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import test from 'brittle'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { PearDataModel } from '../server/db/model.js'
|
||||
import Corestore from 'corestore'
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
const TMP = path.join(__dirname, '..', 'tmp-hyperdb-test')
|
||||
|
||||
async function withModel(fn) {
|
||||
fs.rmSync(TMP, { recursive: true, force: true })
|
||||
const store = new Corestore(path.join(TMP, 'corestore'))
|
||||
await store.ready()
|
||||
const core = store.get({ name: 'peardata-meta' })
|
||||
const model = new PearDataModel(core, { autoUpdate: true })
|
||||
await model.ready()
|
||||
try {
|
||||
await fn(model)
|
||||
} finally {
|
||||
await model.close().catch(() => {})
|
||||
await store.close().catch(() => {})
|
||||
fs.rmSync(TMP, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
test('hyperdb put/get node', async (t) => {
|
||||
await withModel(async (db) => {
|
||||
await db.putNode({
|
||||
nodeId: 'node-a',
|
||||
hostname: 'lab',
|
||||
publicKeyHex: 'ab'.repeat(32),
|
||||
cpus: 4,
|
||||
updatedAt: Date.now(),
|
||||
})
|
||||
const row = await db.getNode('node-a')
|
||||
t.is(row.hostname, 'lab')
|
||||
t.is(row.cpus, 4)
|
||||
})
|
||||
})
|
||||
|
||||
test('hyperdb peer links', async (t) => {
|
||||
await withModel(async (db) => {
|
||||
const remote = 'cd'.repeat(32)
|
||||
await db.putPeerLink({
|
||||
localNodeId: 'local',
|
||||
remotePublicKey: remote,
|
||||
syncMode: 'pull',
|
||||
alias: 'nas',
|
||||
})
|
||||
const links = await db.listPeerLinks('local')
|
||||
t.is(links.length, 1)
|
||||
t.is(links[0].alias, 'nas')
|
||||
await db.deletePeerLink('local', remote)
|
||||
t.is((await db.listPeerLinks('local')).length, 0)
|
||||
})
|
||||
})
|
||||
|
||||
test('hyperdb warm metric points', async (t) => {
|
||||
await withModel(async (db) => {
|
||||
const ts = Date.now()
|
||||
await db.putMetricPoints([
|
||||
{
|
||||
chart: 'system.cpu',
|
||||
context: 'system.cpu',
|
||||
ts: ts - 1000,
|
||||
values: { user: 10, idle: 90 },
|
||||
tier: 1,
|
||||
},
|
||||
{
|
||||
chart: 'system.cpu',
|
||||
context: 'system.cpu',
|
||||
ts,
|
||||
values: { user: 20, idle: 80 },
|
||||
tier: 1,
|
||||
},
|
||||
])
|
||||
const rows = await db.queryMetricPoints({
|
||||
chart: 'system.cpu',
|
||||
afterMs: ts - 5000,
|
||||
beforeMs: ts + 1000,
|
||||
})
|
||||
t.is(rows.length, 2)
|
||||
t.is(rows[1].values.user, 20)
|
||||
})
|
||||
})
|
||||
|
||||
test('hyperdb alert event', async (t) => {
|
||||
await withModel(async (db) => {
|
||||
const ts = Date.now()
|
||||
await db.putAlertEvent({
|
||||
id: 'cpu_user_high',
|
||||
ts,
|
||||
chart: 'system.cpu',
|
||||
dimension: 'user',
|
||||
severity: 'warning',
|
||||
value: 90,
|
||||
threshold: 80,
|
||||
message: 'high',
|
||||
})
|
||||
const evs = await db.listAlertEvents({ chart: 'system.cpu', limit: 10 })
|
||||
t.ok(evs.length >= 1)
|
||||
t.is(evs[0].severity, 'warning')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user