fix storage test
This commit is contained in:
@@ -17,7 +17,7 @@ const BoxError = require('../boxerror.js'),
|
||||
os = require('os'),
|
||||
path = require('path'),
|
||||
rimraf = require('rimraf'),
|
||||
recursive_readdir = require('recursive-readdir'),
|
||||
recursiveReaddir = require('recursive-readdir'),
|
||||
s3 = require('../storage/s3.js'),
|
||||
safe = require('safetydance'),
|
||||
settings = require('../settings.js'),
|
||||
@@ -150,25 +150,19 @@ describe('Storage', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove file', function (done) {
|
||||
it('can remove file', async function () {
|
||||
const sourceFile = gTmpFolder + '/uploadtest/test-hardlink.txt';
|
||||
|
||||
filesystem.remove(gBackupConfig, sourceFile, function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(sourceFile)).to.be(false);
|
||||
done();
|
||||
});
|
||||
await filesystem.remove(gBackupConfig, sourceFile);
|
||||
expect(fs.existsSync(sourceFile)).to.be(false);
|
||||
});
|
||||
|
||||
it('can remove empty dir', function (done) {
|
||||
it('can remove empty dir', async function () {
|
||||
const sourceDir = gTmpFolder + '/emptydir';
|
||||
fs.mkdirSync(sourceDir);
|
||||
|
||||
filesystem.remove(gBackupConfig, sourceDir, function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(sourceDir)).to.be(false);
|
||||
done();
|
||||
});
|
||||
await filesystem.remove(gBackupConfig, sourceDir, () => {});
|
||||
expect(fs.existsSync(sourceDir)).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -206,18 +200,12 @@ describe('Storage', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove file', function (done) {
|
||||
noop.remove(gBackupConfig, 'sourceFile', function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
it('can remove file', async function () {
|
||||
await noop.remove(gBackupConfig, 'sourceFile');
|
||||
});
|
||||
|
||||
it('can remove empty dir', function (done) {
|
||||
noop.remove(gBackupConfig, 'sourceDir', function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
it('can remove empty dir', async function () {
|
||||
await noop.remove(gBackupConfig, 'sourceDir', () => {});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -297,19 +285,13 @@ describe('Storage', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove file', function (done) {
|
||||
s3.remove(gBackupConfig, 'uploadtest-copy/test.txt', function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(path.join(gS3Folder, 'uploadtest-copy/test.txt'))).to.be(false);
|
||||
done();
|
||||
});
|
||||
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);
|
||||
});
|
||||
|
||||
it('can remove non-existent dir', function (done) {
|
||||
noop.remove(gBackupConfig, 'blah', function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
it('can remove non-existent dir', async function () {
|
||||
await noop.remove(gBackupConfig, 'blah', () => {});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -328,70 +310,71 @@ describe('Storage', function () {
|
||||
const GCSMockBasePath = path.join(os.tmpdir(), 'gcs-backup-test-buckets/');
|
||||
|
||||
before(function () {
|
||||
const mockGCS = function(){
|
||||
return {bucket: function(){
|
||||
const file = function(filename){
|
||||
const mockGCS = function() {
|
||||
return {
|
||||
bucket: function() {
|
||||
const file = function (filename) {
|
||||
function ensurePathWritable(filename) {
|
||||
filename = GCSMockBasePath + filename;
|
||||
fs.mkdirSync(path.dirname(filename), { recursive: true });
|
||||
return filename;
|
||||
}
|
||||
|
||||
const ensurePathWritable = function (filename) {
|
||||
filename = GCSMockBasePath + filename;
|
||||
fs.mkdirSync(path.dirname(filename), { recursive: true });
|
||||
return filename;
|
||||
return {
|
||||
name: filename,
|
||||
createReadStream: function() {
|
||||
return fs.createReadStream(ensurePathWritable(filename))
|
||||
.on('error', function(e){
|
||||
console.log('error createReadStream: '+filename);
|
||||
if (e.code == 'ENOENT') { e.code = 404; }
|
||||
this.emit('error', e);
|
||||
})
|
||||
;
|
||||
},
|
||||
createWriteStream: function() {
|
||||
return fs.createWriteStream(ensurePathWritable(filename));
|
||||
},
|
||||
delete: async function() {
|
||||
await fs.promises.unlink(ensurePathWritable(filename));
|
||||
},
|
||||
copy: function(dst, cb) {
|
||||
function notFoundHandler(e) {
|
||||
if (e && e.code == 'ENOENT') { e.code = 404; return cb(e); }
|
||||
cb();
|
||||
}
|
||||
|
||||
return fs.createReadStream(ensurePathWritable(filename))
|
||||
.on('end', cb)
|
||||
.on('error', notFoundHandler)
|
||||
.pipe(fs.createWriteStream(ensurePathWritable(dst)))
|
||||
.on('end', cb)
|
||||
.on('error', notFoundHandler)
|
||||
;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
name: filename,
|
||||
createReadStream: function(){
|
||||
return fs.createReadStream(ensurePathWritable(filename))
|
||||
.on('error', function(e){
|
||||
console.log('error createReadStream: '+filename);
|
||||
if (e.code == 'ENOENT') { e.code = 404; }
|
||||
this.emit('error', e);
|
||||
})
|
||||
;
|
||||
},
|
||||
createWriteStream: function(){
|
||||
return fs.createWriteStream(ensurePathWritable(filename));
|
||||
},
|
||||
delete: function(cb){
|
||||
fs.unlink(ensurePathWritable(filename), cb);
|
||||
},
|
||||
copy: function(dst, cb){
|
||||
const notFoundHandler = function(e){
|
||||
if (e && e.code == 'ENOENT') { e.code = 404; return cb(e);}
|
||||
cb();
|
||||
};
|
||||
return fs.createReadStream(ensurePathWritable(filename))
|
||||
.on('end', cb)
|
||||
.on('error', notFoundHandler)
|
||||
.pipe(fs.createWriteStream(ensurePathWritable(dst)))
|
||||
.on('end', cb)
|
||||
.on('error', notFoundHandler)
|
||||
;
|
||||
file,
|
||||
|
||||
getFiles: function(q, cb) {
|
||||
const target = GCSMockBasePath + q.prefix;
|
||||
recursiveReaddir(target, function(e, files) {
|
||||
const pageToken = q.pageToken || 0;
|
||||
|
||||
const chunkedFiles = chunk(files, q.maxResults);
|
||||
if (q.pageToken >= chunkedFiles.length) return cb(null, []);
|
||||
|
||||
const gFiles = chunkedFiles[pageToken].map(function(f) {
|
||||
return file(path.relative(GCSMockBasePath, f)); //convert to gcs
|
||||
});
|
||||
|
||||
q.pageToken = pageToken + 1;
|
||||
cb(null, gFiles, q.pageToken < chunkedFiles.length ? q : null);
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
file: file,
|
||||
getFiles: function(q, cb){
|
||||
const target = GCSMockBasePath + q.prefix;
|
||||
recursive_readdir(target, function(e, files){
|
||||
|
||||
const pageToken = q.pageToken || 0;
|
||||
|
||||
const chunkedFiles = chunk(files, q.maxResults);
|
||||
if (q.pageToken >= chunkedFiles.length) return cb(null, []);
|
||||
|
||||
const gFiles = chunkedFiles[pageToken].map(function(f){
|
||||
return file(path.relative(GCSMockBasePath, f)); //convert to google
|
||||
});
|
||||
|
||||
q.pageToken = pageToken + 1;
|
||||
cb(null, gFiles, q.pageToken < chunkedFiles.length ? q : null);
|
||||
});
|
||||
}
|
||||
};
|
||||
}};
|
||||
}};
|
||||
};
|
||||
gcs._mockInject(mockGCS);
|
||||
});
|
||||
@@ -451,20 +434,13 @@ describe('Storage', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove file', function (done) {
|
||||
gcs.remove(gBackupConfig, 'uploadtest-copy/test.txt', function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(fs.existsSync(path.join(GCSMockBasePath, 'uploadtest-copy/test.txt'))).to.be(false);
|
||||
done();
|
||||
});
|
||||
it('can remove file', async function () {
|
||||
await gcs.remove(gBackupConfig, 'uploadtest-copy/test.txt');
|
||||
expect(fs.existsSync(path.join(GCSMockBasePath, 'uploadtest-copy/test.txt'))).to.be(false);
|
||||
});
|
||||
|
||||
it('can remove non-existent dir', function (done) {
|
||||
gcs.remove(gBackupConfig, 'blah', function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
it('can remove non-existent dir', async function () {
|
||||
await gcs.remove(gBackupConfig, 'blah', () => {});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user