tests: fix storage test
This commit is contained in:
@@ -20,7 +20,8 @@ const backups = require('../backups.js'),
|
||||
path = require('path'),
|
||||
readdirp = require('readdirp'),
|
||||
s3 = require('../storage/s3.js'),
|
||||
safe = require('safetydance');
|
||||
safe = require('safetydance'),
|
||||
stream = require('stream/promises');
|
||||
|
||||
const chunk = s3._chunk;
|
||||
|
||||
@@ -64,42 +65,39 @@ describe('Storage', function () {
|
||||
expect(fs.existsSync(path.join(gBackupConfig.backupFolder, 'snapshot'))).to.be(true); // auto-created
|
||||
});
|
||||
|
||||
it('can upload', function (done) {
|
||||
it('can upload', async function () {
|
||||
const sourceFile = path.join(__dirname, 'storage/data/test.txt');
|
||||
const sourceStream = fs.createReadStream(sourceFile);
|
||||
const destFile = gTmpFolder + '/uploadtest/test.txt';
|
||||
filesystem.upload(gBackupConfig, destFile, sourceStream, function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(destFile));
|
||||
expect(fs.statSync(sourceFile).size).to.be(fs.statSync(destFile).size);
|
||||
done();
|
||||
});
|
||||
const uploader = await filesystem.upload(gBackupConfig, destFile);
|
||||
await stream.pipeline(sourceStream, uploader.stream);
|
||||
await uploader.finish();
|
||||
expect(fs.existsSync(destFile));
|
||||
expect(fs.statSync(sourceFile).size).to.be(fs.statSync(destFile).size);
|
||||
});
|
||||
|
||||
it('upload waits for empty file to be created', function (done) {
|
||||
it('upload waits for empty file to be created', async function () {
|
||||
const sourceFile = path.join(__dirname, 'storage/data/empty');
|
||||
const sourceStream = fs.createReadStream(sourceFile);
|
||||
const destFile = gTmpFolder + '/uploadtest/empty';
|
||||
filesystem.upload(gBackupConfig, destFile, sourceStream, function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(destFile));
|
||||
expect(fs.statSync(sourceFile).size).to.be(fs.statSync(destFile).size);
|
||||
done();
|
||||
});
|
||||
const uploader = await filesystem.upload(gBackupConfig, destFile);
|
||||
await stream.pipeline(sourceStream, uploader.stream);
|
||||
await uploader.finish();
|
||||
expect(fs.existsSync(destFile));
|
||||
expect(fs.statSync(sourceFile).size).to.be(fs.statSync(destFile).size);
|
||||
});
|
||||
|
||||
it('upload unlinks old file', function (done) {
|
||||
it('upload unlinks old file', async function () {
|
||||
const sourceFile = path.join(__dirname, 'storage/data/test.txt');
|
||||
const sourceStream = fs.createReadStream(sourceFile);
|
||||
const destFile = gTmpFolder + '/uploadtest/test.txt';
|
||||
const oldStat = fs.statSync(destFile);
|
||||
filesystem.upload(gBackupConfig, destFile, sourceStream, function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(destFile)).to.be(true);
|
||||
expect(fs.statSync(sourceFile).size).to.be(fs.statSync(destFile).size);
|
||||
expect(oldStat.inode).to.not.be(fs.statSync(destFile).size);
|
||||
done();
|
||||
});
|
||||
const uploader = await filesystem.upload(gBackupConfig, destFile);
|
||||
await stream.pipeline(sourceStream, uploader.stream);
|
||||
await uploader.finish();
|
||||
expect(fs.existsSync(destFile)).to.be(true);
|
||||
expect(fs.statSync(sourceFile).size).to.be(fs.statSync(destFile).size);
|
||||
expect(oldStat.inode).to.not.be(fs.statSync(destFile).size);
|
||||
});
|
||||
|
||||
it('can download file', async function () {
|
||||
@@ -219,16 +217,15 @@ describe('Storage', function () {
|
||||
fs.rmSync(MockS3.config.basePath, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('can upload', function (done) {
|
||||
it('can upload', async function () {
|
||||
const sourceFile = path.join(__dirname, 'storage/data/test.txt');
|
||||
const sourceStream = fs.createReadStream(sourceFile);
|
||||
const destKey = 'uploadtest/test.txt';
|
||||
s3.upload(gBackupConfig, destKey, sourceStream, function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(path.join(gS3Folder, destKey))).to.be(true);
|
||||
expect(fs.statSync(path.join(gS3Folder, destKey)).size).to.be(fs.statSync(sourceFile).size);
|
||||
done();
|
||||
});
|
||||
const uploader = await s3.upload(gBackupConfig, destKey);
|
||||
await stream.pipeline(sourceStream, uploader.stream);
|
||||
await uploader.finish();
|
||||
expect(fs.existsSync(path.join(gS3Folder, destKey))).to.be(true);
|
||||
expect(fs.statSync(path.join(gS3Folder, destKey)).size).to.be(fs.statSync(sourceFile).size);
|
||||
});
|
||||
|
||||
it('can download file', async function () {
|
||||
@@ -261,12 +258,12 @@ describe('Storage', function () {
|
||||
expect(fs.statSync(path.join(gS3Folder, 'uploadtest-copy/C++.gitignore')).size).to.be(7);
|
||||
});
|
||||
|
||||
xit('can remove file', async function () {
|
||||
it('can remove file', async function () {
|
||||
await s3.remove(gBackupConfig, 'uploadtest-copy/test.txt');
|
||||
expect(fs.existsSync(path.join(gS3Folder, 'uploadtest-copy/test.txt'))).to.be(false);
|
||||
});
|
||||
|
||||
xit('can remove non-existent dir', async function () {
|
||||
it('can remove non-existent dir', async function () {
|
||||
await noop.remove(gBackupConfig, 'blah', () => {});
|
||||
});
|
||||
});
|
||||
@@ -304,8 +301,7 @@ describe('Storage', function () {
|
||||
console.log('error createReadStream: '+filename);
|
||||
if (e.code == 'ENOENT') { e.code = 404; }
|
||||
this.emit('error', e);
|
||||
})
|
||||
;
|
||||
});
|
||||
},
|
||||
createWriteStream: function() {
|
||||
return fs.createWriteStream(ensurePathWritable(filename));
|
||||
@@ -324,8 +320,7 @@ describe('Storage', function () {
|
||||
.on('error', notFoundHandler)
|
||||
.pipe(fs.createWriteStream(ensurePathWritable(dst)))
|
||||
.on('end', cb)
|
||||
.on('error', notFoundHandler)
|
||||
;
|
||||
.on('error', notFoundHandler);
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -360,15 +355,13 @@ describe('Storage', function () {
|
||||
done();
|
||||
});
|
||||
|
||||
it('can backup', function (done) {
|
||||
it('can backup', async function () {
|
||||
const sourceFile = path.join(__dirname, 'storage/data/test.txt');
|
||||
const sourceStream = fs.createReadStream(sourceFile);
|
||||
const destKey = 'uploadtest/test.txt';
|
||||
gcs.upload(gBackupConfig, destKey, sourceStream, function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
done();
|
||||
});
|
||||
const uploader = await gcs.upload(gBackupConfig, destKey);
|
||||
await stream.pipeline(sourceStream, uploader.stream);
|
||||
await uploader.finish();
|
||||
});
|
||||
|
||||
it('can download file', async function () {
|
||||
|
||||
Reference in New Issue
Block a user