2020-03-13 12:14:06 -04:00
|
|
|
const streamifier = require('streamifier')
|
2022-05-31 10:30:21 -04:00
|
|
|
const fetch = require('node-fetch')
|
2020-03-13 12:14:06 -04:00
|
|
|
|
|
|
|
const { expect } = require('chai')
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
uploadStringToPersistor,
|
|
|
|
getStringFromPersistor,
|
|
|
|
expectPersistorToHaveFile,
|
|
|
|
expectPersistorNotToHaveFile,
|
|
|
|
streamToString,
|
2021-07-13 07:04:46 -04:00
|
|
|
getMetric,
|
2020-03-13 12:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getMetric(filestoreUrl, metric) {
|
2022-05-31 10:30:21 -04:00
|
|
|
const res = await fetch(`${filestoreUrl}/metrics`)
|
|
|
|
expect(res.status).to.equal(200)
|
2024-10-24 07:42:20 -04:00
|
|
|
const metricRegex = new RegExp(`^${metric}{[^}]+} ([0-9]+)$`, 'gm')
|
2022-05-31 10:30:21 -04:00
|
|
|
const body = await res.text()
|
2024-10-24 07:42:20 -04:00
|
|
|
let v = 0
|
|
|
|
// Sum up size="lt-128KiB" and size="gte-128KiB"
|
|
|
|
for (const [, found] of body.matchAll(metricRegex)) {
|
|
|
|
v += parseInt(found, 10) || 0
|
|
|
|
}
|
|
|
|
return v
|
2020-03-13 12:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function streamToString(stream) {
|
|
|
|
const chunks = []
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-07-13 07:04:46 -04:00
|
|
|
stream.on('data', chunk => chunks.push(chunk))
|
2020-03-13 12:14:06 -04:00
|
|
|
stream.on('error', reject)
|
|
|
|
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
|
|
|
|
stream.resume()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uploadStringToPersistor(persistor, bucket, key, content) {
|
|
|
|
const fileStream = streamifier.createReadStream(content)
|
2020-07-07 08:49:54 -04:00
|
|
|
await persistor.sendStream(bucket, key, fileStream)
|
2020-03-13 12:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getStringFromPersistor(persistor, bucket, key) {
|
2020-07-07 08:49:54 -04:00
|
|
|
const stream = await persistor.getObjectStream(bucket, key, {})
|
2024-05-22 05:37:08 -04:00
|
|
|
return await streamToString(stream)
|
2020-03-13 12:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function expectPersistorToHaveFile(persistor, bucket, key, content) {
|
|
|
|
const foundContent = await getStringFromPersistor(persistor, bucket, key)
|
|
|
|
expect(foundContent).to.equal(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function expectPersistorNotToHaveFile(persistor, bucket, key) {
|
|
|
|
await expect(
|
|
|
|
getStringFromPersistor(persistor, bucket, key)
|
|
|
|
).to.eventually.have.been.rejected.with.property('name', 'NotFoundError')
|
|
|
|
}
|