2019-12-23 08:36:12 -05:00
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const fs = require('fs')
|
2021-07-12 12:47:19 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-12-23 08:36:12 -05:00
|
|
|
const Path = require('path')
|
|
|
|
const FilestoreApp = require('./FilestoreApp')
|
2020-03-13 12:14:06 -04:00
|
|
|
const TestHelper = require('./TestHelper')
|
2019-12-23 08:36:12 -05:00
|
|
|
const rp = require('request-promise-native').defaults({
|
2021-07-13 07:04:46 -04:00
|
|
|
resolveWithFullResponse: true,
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
2020-01-07 10:05:51 -05:00
|
|
|
const S3 = require('aws-sdk/clients/s3')
|
2019-12-23 08:36:12 -05:00
|
|
|
const Stream = require('stream')
|
|
|
|
const request = require('request')
|
|
|
|
const { promisify } = require('util')
|
2020-02-12 06:01:51 -05:00
|
|
|
const { Storage } = require('@google-cloud/storage')
|
2020-02-17 09:04:42 -05:00
|
|
|
const streamifier = require('streamifier')
|
2019-12-23 08:36:12 -05:00
|
|
|
chai.use(require('chai-as-promised'))
|
2020-03-10 13:54:09 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-03-14 10:11:17 -04:00
|
|
|
const tk = require('timekeeper')
|
2020-04-02 06:58:43 -04:00
|
|
|
const ChildProcess = require('child_process')
|
2019-12-23 08:36:12 -05:00
|
|
|
|
|
|
|
const fsWriteFile = promisify(fs.writeFile)
|
|
|
|
const fsStat = promisify(fs.stat)
|
|
|
|
const pipeline = promisify(Stream.pipeline)
|
2020-04-02 06:58:43 -04:00
|
|
|
const exec = promisify(ChildProcess.exec)
|
|
|
|
const msleep = promisify(setTimeout)
|
2019-12-23 08:36:12 -05:00
|
|
|
|
2020-02-17 09:04:42 -05:00
|
|
|
if (!process.env.AWS_ACCESS_KEY_ID) {
|
|
|
|
throw new Error('please provide credentials for the AWS S3 test server')
|
|
|
|
}
|
|
|
|
|
2021-07-13 07:04:46 -04:00
|
|
|
process.on('unhandledRejection', e => {
|
2020-03-26 18:07:37 -04:00
|
|
|
// eslint-disable-next-line no-console
|
2020-03-26 11:11:22 -04:00
|
|
|
console.log('** Unhandled Promise Rejection **\n', e)
|
|
|
|
throw e
|
|
|
|
})
|
|
|
|
|
2019-12-23 08:36:12 -05:00
|
|
|
// store settings for multiple backends, so that we can test each one.
|
|
|
|
// fs will always be available - add others if they are configured
|
2020-02-12 06:01:51 -05:00
|
|
|
const BackendSettings = require('./TestConfig')
|
2019-12-23 08:36:12 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('Filestore', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
this.timeout(1000 * 10)
|
|
|
|
const filestoreUrl = `http://localhost:${Settings.internal.filestore.port}`
|
|
|
|
|
2020-04-02 06:58:43 -04:00
|
|
|
const seenSockets = []
|
|
|
|
async function expectNoSockets() {
|
|
|
|
try {
|
|
|
|
await msleep(1000)
|
|
|
|
const { stdout } = await exec('ss -tnH')
|
|
|
|
|
|
|
|
const badSockets = []
|
|
|
|
for (const socket of stdout.split('\n')) {
|
2021-07-13 07:04:46 -04:00
|
|
|
const fields = socket.split(' ').filter(part => part !== '')
|
2020-04-02 06:58:43 -04:00
|
|
|
if (
|
|
|
|
fields.length > 2 &&
|
|
|
|
parseInt(fields[1]) &&
|
|
|
|
!seenSockets.includes(socket)
|
|
|
|
) {
|
|
|
|
badSockets.push(socket)
|
|
|
|
seenSockets.push(socket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badSockets.length) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(
|
|
|
|
'ERR: Sockets still have receive buffer after connection closed'
|
|
|
|
)
|
|
|
|
for (const socket of badSockets) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(socket)
|
|
|
|
}
|
|
|
|
throw new Error('Sockets still open after connection closed')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
expect(err).not.to.exist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 08:36:12 -05:00
|
|
|
// redefine the test suite for every available backend
|
2021-07-13 07:04:46 -04:00
|
|
|
Object.keys(BackendSettings).forEach(backend => {
|
2020-08-10 12:01:12 -04:00
|
|
|
describe(backend, function () {
|
2020-02-12 06:01:51 -05:00
|
|
|
let app, previousEgress, previousIngress, metricPrefix, projectId
|
2019-12-23 08:36:12 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
before(async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
// create the app with the relevant filestore settings
|
|
|
|
Settings.filestore = BackendSettings[backend]
|
|
|
|
app = new FilestoreApp()
|
|
|
|
await app.runServer()
|
|
|
|
})
|
|
|
|
|
2020-02-12 06:01:51 -05:00
|
|
|
if (BackendSettings[backend].gcs) {
|
2020-08-10 12:01:12 -04:00
|
|
|
before(async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
const storage = new Storage(Settings.filestore.gcs.endpoint)
|
2020-02-12 06:01:51 -05:00
|
|
|
await storage.createBucket(process.env.GCS_USER_FILES_BUCKET_NAME)
|
|
|
|
await storage.createBucket(process.env.GCS_PUBLIC_FILES_BUCKET_NAME)
|
|
|
|
await storage.createBucket(process.env.GCS_TEMPLATE_FILES_BUCKET_NAME)
|
2020-03-13 12:14:06 -04:00
|
|
|
await storage.createBucket(
|
|
|
|
`${process.env.GCS_USER_FILES_BUCKET_NAME}-deleted`
|
|
|
|
)
|
|
|
|
await storage.createBucket(
|
|
|
|
`${process.env.GCS_PUBLIC_FILES_BUCKET_NAME}-deleted`
|
|
|
|
)
|
|
|
|
await storage.createBucket(
|
|
|
|
`${process.env.GCS_TEMPLATE_FILES_BUCKET_NAME}-deleted`
|
|
|
|
)
|
2020-02-12 06:01:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
after(async function () {
|
2020-04-02 06:58:43 -04:00
|
|
|
await msleep(3000)
|
|
|
|
await app.stop()
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(async function () {
|
2020-02-12 06:01:51 -05:00
|
|
|
// retrieve previous metrics from the app
|
|
|
|
if (['s3', 'gcs'].includes(Settings.filestore.backend)) {
|
|
|
|
metricPrefix = Settings.filestore.backend
|
2020-03-13 12:14:06 -04:00
|
|
|
previousEgress = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_egress`
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
}
|
2020-03-10 13:54:09 -04:00
|
|
|
projectId = ObjectId().toString()
|
2019-12-23 08:36:12 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 200 for the status endpoint', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const response = await rp(`${filestoreUrl}/status`)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.body).to.contain('filestore')
|
|
|
|
expect(response.body).to.contain('up')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 200 for the health-check endpoint', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const response = await rp(`${filestoreUrl}/health_check`)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(response.body).to.equal('OK')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a file on the server', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let fileId, fileUrl, constantFileContent
|
2019-12-23 08:36:12 -05:00
|
|
|
|
|
|
|
const localFileReadPath =
|
|
|
|
'/tmp/filestore_acceptance_tests_file_read.txt'
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-10 13:54:09 -04:00
|
|
|
fileId = ObjectId().toString()
|
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
constantFileContent = [
|
|
|
|
'hello world',
|
|
|
|
`line 2 goes here ${Math.random()}`,
|
2021-07-13 07:04:46 -04:00
|
|
|
'there are 3 lines in all',
|
2020-02-17 09:04:42 -05:00
|
|
|
].join('\n')
|
|
|
|
|
|
|
|
await fsWriteFile(localFileReadPath, constantFileContent)
|
2019-12-23 08:36:12 -05:00
|
|
|
|
|
|
|
const writeStream = request.post(fileUrl)
|
|
|
|
const readStream = fs.createReadStream(localFileReadPath)
|
2020-01-06 09:14:22 -05:00
|
|
|
// hack to consume the result to ensure the http request has been fully processed
|
2019-12-23 08:36:12 -05:00
|
|
|
const resultStream = fs.createWriteStream('/dev/null')
|
|
|
|
await pipeline(readStream, writeStream, resultStream)
|
|
|
|
})
|
|
|
|
|
2020-02-28 06:27:06 -05:00
|
|
|
beforeEach(async function retrievePreviousIngressMetrics() {
|
|
|
|
// The upload request can bump the ingress metric.
|
|
|
|
// The content hash validation might require a full download
|
|
|
|
// in case the ETag field of the upload response is not a md5 sum.
|
2020-02-12 06:01:51 -05:00
|
|
|
if (['s3', 'gcs'].includes(Settings.filestore.backend)) {
|
2020-03-13 12:14:06 -04:00
|
|
|
previousIngress = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_ingress`
|
|
|
|
)
|
2020-02-28 06:27:06 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should return 404 for a non-existant id', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const options = { uri: fileUrl + '___this_is_clearly_wrong___' }
|
|
|
|
await expect(
|
|
|
|
rp.get(options)
|
|
|
|
).to.eventually.be.rejected.and.have.property('statusCode', 404)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should return the file size on a HEAD request', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const expectedLength = Buffer.byteLength(constantFileContent)
|
|
|
|
const res = await rp.head(fileUrl)
|
|
|
|
expect(res.statusCode).to.equal(200)
|
|
|
|
expect(res.headers['content-length']).to.equal(
|
|
|
|
expectedLength.toString()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able get the file back', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const res = await rp.get(fileUrl)
|
|
|
|
expect(res.body).to.equal(constantFileContent)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not leak a socket', async function () {
|
2020-04-02 06:58:43 -04:00
|
|
|
await rp.get(fileUrl)
|
|
|
|
await expectNoSockets()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to get back the first 9 bytes of the file', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const options = {
|
|
|
|
uri: fileUrl,
|
|
|
|
headers: {
|
2021-07-13 07:04:46 -04:00
|
|
|
Range: 'bytes=0-8',
|
|
|
|
},
|
2019-12-23 08:36:12 -05:00
|
|
|
}
|
|
|
|
const res = await rp.get(options)
|
|
|
|
expect(res.body).to.equal('hello wor')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to get back bytes 4 through 10 of the file', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const options = {
|
|
|
|
uri: fileUrl,
|
|
|
|
headers: {
|
2021-07-13 07:04:46 -04:00
|
|
|
Range: 'bytes=4-10',
|
|
|
|
},
|
2019-12-23 08:36:12 -05:00
|
|
|
}
|
|
|
|
const res = await rp.get(options)
|
|
|
|
expect(res.body).to.equal('o world')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to delete the file', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const response = await rp.del(fileUrl)
|
|
|
|
expect(response.statusCode).to.equal(204)
|
|
|
|
await expect(
|
|
|
|
rp.get(fileUrl)
|
|
|
|
).to.eventually.be.rejected.and.have.property('statusCode', 404)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to copy files', async function () {
|
2020-03-10 13:54:09 -04:00
|
|
|
const newProjectID = ObjectId().toString()
|
|
|
|
const newFileId = ObjectId().toString()
|
|
|
|
const newFileUrl = `${filestoreUrl}/project/${newProjectID}/file/${newFileId}`
|
2019-12-23 08:36:12 -05:00
|
|
|
const opts = {
|
|
|
|
method: 'put',
|
|
|
|
uri: newFileUrl,
|
|
|
|
json: {
|
|
|
|
source: {
|
2020-02-17 09:04:42 -05:00
|
|
|
project_id: projectId,
|
2021-07-13 07:04:46 -04:00
|
|
|
file_id: fileId,
|
|
|
|
},
|
|
|
|
},
|
2019-12-23 08:36:12 -05:00
|
|
|
}
|
|
|
|
let response = await rp(opts)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
response = await rp.del(fileUrl)
|
|
|
|
expect(response.statusCode).to.equal(204)
|
|
|
|
response = await rp.get(newFileUrl)
|
|
|
|
expect(response.body).to.equal(constantFileContent)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to overwrite the file', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const newContent = `here is some different content, ${Math.random()}`
|
|
|
|
const writeStream = request.post(fileUrl)
|
|
|
|
const readStream = streamifier.createReadStream(newContent)
|
|
|
|
// hack to consume the result to ensure the http request has been fully processed
|
|
|
|
const resultStream = fs.createWriteStream('/dev/null')
|
|
|
|
await pipeline(readStream, writeStream, resultStream)
|
|
|
|
|
|
|
|
const response = await rp.get(fileUrl)
|
|
|
|
expect(response.body).to.equal(newContent)
|
|
|
|
})
|
|
|
|
|
2020-02-12 06:01:51 -05:00
|
|
|
if (['S3Persistor', 'GcsPersistor'].includes(backend)) {
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an egress metric for the upload', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_egress`
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
expect(metric - previousEgress).to.equal(constantFileContent.length)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an ingress metric when downloading the file', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
await rp.get(fileUrl)
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_ingress`
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
expect(metric - previousIngress).to.equal(
|
|
|
|
constantFileContent.length
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an ingress metric for a partial download', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const options = {
|
|
|
|
uri: fileUrl,
|
|
|
|
headers: {
|
2021-07-13 07:04:46 -04:00
|
|
|
Range: 'bytes=0-8',
|
|
|
|
},
|
2019-12-23 08:36:12 -05:00
|
|
|
}
|
|
|
|
await rp.get(options)
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_ingress`
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
expect(metric - previousIngress).to.equal(9)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with multiple files', function () {
|
2020-03-16 07:34:45 -04:00
|
|
|
let fileIds, fileUrls, projectUrl
|
2020-01-06 09:14:22 -05:00
|
|
|
const localFileReadPaths = [
|
|
|
|
'/tmp/filestore_acceptance_tests_file_read_1.txt',
|
2021-07-13 07:04:46 -04:00
|
|
|
'/tmp/filestore_acceptance_tests_file_read_2.txt',
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
const constantFileContents = [
|
|
|
|
[
|
|
|
|
'hello world',
|
|
|
|
`line 2 goes here ${Math.random()}`,
|
2021-07-13 07:04:46 -04:00
|
|
|
'there are 3 lines in all',
|
2020-01-06 09:14:22 -05:00
|
|
|
].join('\n'),
|
|
|
|
[
|
|
|
|
`for reference: ${Math.random()}`,
|
|
|
|
'cats are the best animals',
|
2021-07-13 07:04:46 -04:00
|
|
|
'wombats are a close second',
|
|
|
|
].join('\n'),
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
before(async function () {
|
2020-01-06 09:14:22 -05:00
|
|
|
return Promise.all([
|
|
|
|
fsWriteFile(localFileReadPaths[0], constantFileContents[0]),
|
2021-07-13 07:04:46 -04:00
|
|
|
fsWriteFile(localFileReadPaths[1], constantFileContents[1]),
|
2020-01-06 09:14:22 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-16 07:34:45 -04:00
|
|
|
projectUrl = `${filestoreUrl}/project/${projectId}`
|
2020-03-10 13:54:09 -04:00
|
|
|
fileIds = [ObjectId().toString(), ObjectId().toString()]
|
2020-01-06 09:14:22 -05:00
|
|
|
fileUrls = [
|
2020-03-16 07:34:45 -04:00
|
|
|
`${projectUrl}/file/${fileIds[0]}`,
|
2021-07-13 07:04:46 -04:00
|
|
|
`${projectUrl}/file/${fileIds[1]}`,
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
const writeStreams = [
|
|
|
|
request.post(fileUrls[0]),
|
2021-07-13 07:04:46 -04:00
|
|
|
request.post(fileUrls[1]),
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
const readStreams = [
|
|
|
|
fs.createReadStream(localFileReadPaths[0]),
|
2021-07-13 07:04:46 -04:00
|
|
|
fs.createReadStream(localFileReadPaths[1]),
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
// hack to consume the result to ensure the http request has been fully processed
|
|
|
|
const resultStreams = [
|
|
|
|
fs.createWriteStream('/dev/null'),
|
2021-07-13 07:04:46 -04:00
|
|
|
fs.createWriteStream('/dev/null'),
|
2020-01-06 09:14:22 -05:00
|
|
|
]
|
|
|
|
return Promise.all([
|
|
|
|
pipeline(readStreams[0], writeStreams[0], resultStreams[0]),
|
2021-07-13 07:04:46 -04:00
|
|
|
pipeline(readStreams[1], writeStreams[1], resultStreams[1]),
|
2020-01-06 09:14:22 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should get the directory size', async function () {
|
2020-01-06 09:14:22 -05:00
|
|
|
const response = await rp.get(
|
2020-02-17 09:04:42 -05:00
|
|
|
`${filestoreUrl}/project/${projectId}/size`
|
2020-01-06 09:14:22 -05:00
|
|
|
)
|
|
|
|
expect(parseInt(JSON.parse(response.body)['total bytes'])).to.equal(
|
|
|
|
constantFileContents[0].length + constantFileContents[1].length
|
|
|
|
)
|
|
|
|
})
|
2020-03-16 07:34:45 -04:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should store the files', async function () {
|
2020-03-16 07:34:45 -04:00
|
|
|
for (const index in fileUrls) {
|
|
|
|
await expect(rp.get(fileUrls[index])).to.eventually.have.property(
|
|
|
|
'body',
|
|
|
|
constantFileContents[index]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to delete the project', async function () {
|
2020-03-16 07:34:45 -04:00
|
|
|
await expect(rp.delete(projectUrl)).to.eventually.have.property(
|
|
|
|
'statusCode',
|
|
|
|
204
|
|
|
|
)
|
|
|
|
|
|
|
|
for (const index in fileUrls) {
|
|
|
|
await expect(
|
|
|
|
rp.get(fileUrls[index])
|
|
|
|
).to.eventually.be.rejected.and.have.property('statusCode', 404)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not delete a partial project id', async function () {
|
2020-03-16 07:34:45 -04:00
|
|
|
await expect(
|
|
|
|
rp.delete(`${filestoreUrl}/project/5`)
|
|
|
|
).to.eventually.be.rejected.and.have.property('statusCode', 400)
|
|
|
|
})
|
2020-01-06 09:14:22 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a large file', function () {
|
2020-02-17 07:13:31 -05:00
|
|
|
let fileId, fileUrl, largeFileContent, error
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-10 13:54:09 -04:00
|
|
|
fileId = ObjectId().toString()
|
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 07:13:31 -05:00
|
|
|
|
|
|
|
largeFileContent = '_wombat_'.repeat(1024 * 1024) // 8 megabytes
|
|
|
|
largeFileContent += Math.random()
|
|
|
|
|
|
|
|
const writeStream = request.post(fileUrl)
|
|
|
|
const readStream = streamifier.createReadStream(largeFileContent)
|
|
|
|
// hack to consume the result to ensure the http request has been fully processed
|
|
|
|
const resultStream = fs.createWriteStream('/dev/null')
|
|
|
|
|
|
|
|
try {
|
|
|
|
await pipeline(readStream, writeStream, resultStream)
|
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able to get the file back', async function () {
|
2020-02-17 07:13:31 -05:00
|
|
|
const response = await rp.get(fileUrl)
|
|
|
|
expect(response.body).to.equal(largeFileContent)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not throw an error', function () {
|
2020-02-17 07:13:31 -05:00
|
|
|
expect(error).not.to.exist
|
|
|
|
})
|
2020-04-02 06:58:43 -04:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not leak a socket', async function () {
|
2020-04-02 06:58:43 -04:00
|
|
|
await rp.get(fileUrl)
|
|
|
|
await expectNoSockets()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not leak a socket if the connection is aborted', async function () {
|
2020-04-02 06:58:43 -04:00
|
|
|
this.timeout(20000)
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
// test is not 100% reliable, so repeat
|
|
|
|
// create a new connection and have it time out before reading any data
|
2021-07-13 07:04:46 -04:00
|
|
|
await new Promise(resolve => {
|
2020-04-02 06:58:43 -04:00
|
|
|
const streamThatHangs = new Stream.PassThrough()
|
|
|
|
const stream = request({ url: fileUrl, timeout: 1000 })
|
|
|
|
stream.pipe(streamThatHangs)
|
|
|
|
stream.on('error', () => {
|
|
|
|
stream.destroy()
|
|
|
|
streamThatHangs.destroy()
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
await expectNoSockets()
|
|
|
|
}
|
|
|
|
})
|
2020-02-17 07:13:31 -05:00
|
|
|
})
|
|
|
|
|
2020-03-19 07:10:31 -04:00
|
|
|
if (backend === 'S3Persistor' || backend === 'FallbackGcsToS3Persistor') {
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a file in a specific bucket', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let constantFileContent, fileId, fileUrl, bucketName
|
2020-01-07 10:05:51 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
constantFileContent = `This is a file in a different S3 bucket ${Math.random()}`
|
2020-03-10 13:54:09 -04:00
|
|
|
fileId = ObjectId().toString()
|
|
|
|
bucketName = ObjectId().toString()
|
2020-01-07 10:05:51 -05:00
|
|
|
fileUrl = `${filestoreUrl}/bucket/${bucketName}/key/${fileId}`
|
|
|
|
|
|
|
|
const s3ClientSettings = {
|
|
|
|
credentials: {
|
2020-02-28 06:26:46 -05:00
|
|
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
2021-07-13 07:04:46 -04:00
|
|
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
2020-01-07 10:05:51 -05:00
|
|
|
},
|
|
|
|
endpoint: process.env.AWS_S3_ENDPOINT,
|
|
|
|
sslEnabled: false,
|
2021-07-13 07:04:46 -04:00
|
|
|
s3ForcePathStyle: true,
|
2020-01-07 10:05:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const s3 = new S3(s3ClientSettings)
|
|
|
|
await s3
|
|
|
|
.createBucket({
|
2021-07-13 07:04:46 -04:00
|
|
|
Bucket: bucketName,
|
2020-01-07 10:05:51 -05:00
|
|
|
})
|
|
|
|
.promise()
|
|
|
|
await s3
|
|
|
|
.upload({
|
|
|
|
Bucket: bucketName,
|
|
|
|
Key: fileId,
|
2021-07-13 07:04:46 -04:00
|
|
|
Body: constantFileContent,
|
2020-01-07 10:05:51 -05:00
|
|
|
})
|
|
|
|
.promise()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should get the file from the specified bucket', async function () {
|
2020-01-07 10:05:51 -05:00
|
|
|
const response = await rp.get(fileUrl)
|
2020-02-17 09:04:42 -05:00
|
|
|
expect(response.body).to.equal(constantFileContent)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
if (backend === 'GcsPersistor') {
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when deleting a file in GCS', function () {
|
2020-03-14 10:11:17 -04:00
|
|
|
let fileId, fileUrl, content, error, date
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-14 10:11:17 -04:00
|
|
|
date = new Date()
|
|
|
|
tk.freeze(date)
|
2020-03-13 12:14:06 -04:00
|
|
|
fileId = ObjectId()
|
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
content = '_wombat_' + Math.random()
|
|
|
|
|
|
|
|
const writeStream = request.post(fileUrl)
|
|
|
|
const readStream = streamifier.createReadStream(content)
|
|
|
|
// hack to consume the result to ensure the http request has been fully processed
|
|
|
|
const resultStream = fs.createWriteStream('/dev/null')
|
|
|
|
|
|
|
|
try {
|
|
|
|
await pipeline(readStream, writeStream, resultStream)
|
|
|
|
await rp.delete(fileUrl)
|
|
|
|
} catch (err) {
|
|
|
|
error = err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
afterEach(function () {
|
2020-03-14 10:11:17 -04:00
|
|
|
tk.reset()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not throw an error', function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
expect(error).not.to.exist
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should copy the file to the deleted-files bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
|
|
|
app.persistor,
|
|
|
|
`${Settings.filestore.stores.user_files}-deleted`,
|
2020-03-16 12:09:56 -04:00
|
|
|
`${projectId}/${fileId}-${date.toISOString()}`,
|
2020-03-13 12:14:06 -04:00
|
|
|
content
|
2020-02-17 09:04:42 -05:00
|
|
|
)
|
2020-03-13 12:14:06 -04:00
|
|
|
})
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should remove the file from the original bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
|
|
|
app.persistor,
|
|
|
|
Settings.filestore.stores.user_files,
|
|
|
|
`${projectId}/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
)
|
2020-03-13 12:14:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
if (BackendSettings[backend].fallback) {
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a fallback', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let constantFileContent,
|
|
|
|
fileId,
|
|
|
|
fileKey,
|
|
|
|
fileUrl,
|
|
|
|
bucket,
|
|
|
|
fallbackBucket
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
constantFileContent = `This is yet more file content ${Math.random()}`
|
2020-03-10 13:54:09 -04:00
|
|
|
fileId = ObjectId().toString()
|
|
|
|
fileKey = `${projectId}/${fileId}`
|
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
|
|
|
|
bucket = Settings.filestore.stores.user_files
|
|
|
|
fallbackBucket = Settings.filestore.fallback.buckets[bucket]
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a file in the fallback bucket', function () {
|
|
|
|
beforeEach(async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not find file in the primary', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should find the file in the fallback', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is disabled', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = false
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should fetch the file', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const res = await rp.get(fileUrl)
|
|
|
|
expect(res.body).to.equal(constantFileContent)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not copy the file to the primary', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
await rp.get(fileUrl)
|
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is enabled', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = true
|
2020-02-17 09:04:42 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should fetch the file', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const res = await rp.get(fileUrl)
|
|
|
|
expect(res.body).to.equal(constantFileContent)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('copies the file to the primary', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
await rp.get(fileUrl)
|
|
|
|
// wait for the file to copy in the background
|
2020-04-02 06:58:43 -04:00
|
|
|
await msleep(1000)
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copying a file', function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
let newFileId, newFileUrl, newFileKey, opts
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2020-03-10 13:54:09 -04:00
|
|
|
const newProjectID = ObjectId().toString()
|
|
|
|
newFileId = ObjectId().toString()
|
|
|
|
newFileUrl = `${filestoreUrl}/project/${newProjectID}/file/${newFileId}`
|
|
|
|
newFileKey = `${newProjectID}/${newFileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
|
|
|
|
opts = {
|
|
|
|
method: 'put',
|
|
|
|
uri: newFileUrl,
|
|
|
|
json: {
|
|
|
|
source: {
|
|
|
|
project_id: projectId,
|
2021-07-13 07:04:46 -04:00
|
|
|
file_id: fileId,
|
|
|
|
},
|
|
|
|
},
|
2020-02-17 09:04:42 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is false', function () {
|
|
|
|
beforeEach(async function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = false
|
2020-02-17 09:04:42 -05:00
|
|
|
|
|
|
|
const response = await rp(opts)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should leave the old file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not create a new file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
newFileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should create a new file in the new bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
newFileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not copy the old file to the primary with the old key', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
// wait for the file to copy in the background
|
2020-04-02 06:58:43 -04:00
|
|
|
await msleep(1000)
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when copyOnMiss is true', function () {
|
|
|
|
beforeEach(async function () {
|
2020-07-07 08:49:54 -04:00
|
|
|
app.persistor.settings.copyOnMiss = true
|
2020-02-17 09:04:42 -05:00
|
|
|
|
|
|
|
const response = await rp(opts)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should leave the old file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not create a new file in the old bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
newFileKey
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should create a new file in the new bucket', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
newFileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should copy the old file to the primary with the old key', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
// wait for the file to copy in the background
|
2020-04-02 06:58:43 -04:00
|
|
|
await msleep(1000)
|
2020-02-17 09:04:42 -05:00
|
|
|
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when sending a file', function () {
|
|
|
|
beforeEach(async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const writeStream = request.post(fileUrl)
|
2021-07-13 07:04:46 -04:00
|
|
|
const readStream =
|
|
|
|
streamifier.createReadStream(constantFileContent)
|
2020-02-17 09:04:42 -05:00
|
|
|
// hack to consume the result to ensure the http request has been fully processed
|
|
|
|
const resultStream = fs.createWriteStream('/dev/null')
|
|
|
|
await pipeline(readStream, writeStream, resultStream)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should store the file on the primary', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not store the file on the fallback', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.expectPersistorNotToHaveFile(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
2020-03-10 13:54:09 -04:00
|
|
|
`${projectId}/${fileId}`
|
2020-02-17 09:04:42 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when deleting a file', function () {
|
|
|
|
describe('when the file exists on the primary', function () {
|
|
|
|
beforeEach(async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should delete the file', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const response = await rp.del(fileUrl)
|
|
|
|
expect(response.statusCode).to.equal(204)
|
|
|
|
await expect(
|
|
|
|
rp.get(fileUrl)
|
|
|
|
).to.eventually.be.rejected.and.have.property('statusCode', 404)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when the file exists on the fallback', function () {
|
|
|
|
beforeEach(async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should delete the file', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const response = await rp.del(fileUrl)
|
|
|
|
expect(response.statusCode).to.equal(204)
|
|
|
|
await expect(
|
|
|
|
rp.get(fileUrl)
|
|
|
|
).to.eventually.be.rejected.and.have.property('statusCode', 404)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when the file exists on both the primary and the fallback', function () {
|
|
|
|
beforeEach(async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.primaryPersistor,
|
|
|
|
bucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
2020-03-13 12:14:06 -04:00
|
|
|
await TestHelper.uploadStringToPersistor(
|
2020-02-17 09:04:42 -05:00
|
|
|
app.persistor.fallbackPersistor,
|
|
|
|
fallbackBucket,
|
|
|
|
fileKey,
|
|
|
|
constantFileContent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should delete the files', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
const response = await rp.del(fileUrl)
|
|
|
|
expect(response.statusCode).to.equal(204)
|
|
|
|
await expect(
|
|
|
|
rp.get(fileUrl)
|
|
|
|
).to.eventually.be.rejected.and.have.property('statusCode', 404)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when the file does not exist', function () {
|
|
|
|
it('should return return 204', async function () {
|
2020-02-17 09:04:42 -05:00
|
|
|
// S3 doesn't give us a 404 when the object doesn't exist, so to stay
|
|
|
|
// consistent we merrily return 204 ourselves here as well
|
|
|
|
const response = await rp.del(fileUrl)
|
|
|
|
expect(response.statusCode).to.equal(204)
|
|
|
|
})
|
|
|
|
})
|
2020-01-07 10:05:51 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a pdf file', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
let fileId, fileUrl, localFileSize
|
|
|
|
const localFileReadPath = Path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'../../fixtures/test.pdf'
|
|
|
|
)
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-10 13:54:09 -04:00
|
|
|
fileId = ObjectId().toString()
|
|
|
|
fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
|
2019-12-23 08:36:12 -05:00
|
|
|
const stat = await fsStat(localFileReadPath)
|
|
|
|
localFileSize = stat.size
|
|
|
|
const writeStream = request.post(fileUrl)
|
|
|
|
const endStream = fs.createWriteStream('/dev/null')
|
|
|
|
const readStream = fs.createReadStream(localFileReadPath)
|
|
|
|
await pipeline(readStream, writeStream, endStream)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should be able get the file back', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const response = await rp.get(fileUrl)
|
|
|
|
expect(response.body.substring(0, 8)).to.equal('%PDF-1.5')
|
|
|
|
})
|
|
|
|
|
2020-02-12 06:01:51 -05:00
|
|
|
if (['S3Persistor', 'GcsPersistor'].includes(backend)) {
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should record an egress metric for the upload', async function () {
|
2020-03-13 12:14:06 -04:00
|
|
|
const metric = await TestHelper.getMetric(
|
2020-02-12 06:01:51 -05:00
|
|
|
filestoreUrl,
|
|
|
|
`${metricPrefix}_egress`
|
|
|
|
)
|
2019-12-23 08:36:12 -05:00
|
|
|
expect(metric - previousEgress).to.equal(localFileSize)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('getting the preview image', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
this.timeout(1000 * 20)
|
|
|
|
let previewFileUrl
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
previewFileUrl = `${fileUrl}?style=preview`
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not time out', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const response = await rp.get(previewFileUrl)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should respond with image data', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
// note: this test relies of the imagemagick conversion working
|
|
|
|
const response = await rp.get(previewFileUrl)
|
|
|
|
expect(response.body.length).to.be.greaterThan(400)
|
|
|
|
expect(response.body.substr(1, 3)).to.equal('PNG')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('warming the cache', function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
this.timeout(1000 * 20)
|
|
|
|
let previewFileUrl
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
previewFileUrl = `${fileUrl}?style=preview&cacheWarm=true`
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not time out', async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
const response = await rp.get(previewFileUrl)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it("should respond with only an 'OK'", async function () {
|
2019-12-23 08:36:12 -05:00
|
|
|
// note: this test relies of the imagemagick conversion working
|
|
|
|
const response = await rp.get(previewFileUrl)
|
|
|
|
expect(response.body).to.equal('OK')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|