2016-05-03 23:18:18 +00:00
|
|
|
// Copyright IBM Corp. 2013,2014. All Rights Reserved.
|
|
|
|
// Node module: loopback-component-storage
|
|
|
|
// This file is licensed under the Artistic License 2.0.
|
|
|
|
// License text available at https://opensource.org/licenses/Artistic-2.0
|
2016-10-18 22:48:59 +00:00
|
|
|
'use strict';
|
2016-05-03 23:18:18 +00:00
|
|
|
|
2013-07-24 19:07:46 +00:00
|
|
|
var FileSystemProvider = require('../lib/providers/filesystem/index.js').Client;
|
2013-06-24 21:07:12 +00:00
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var path = require('path');
|
|
|
|
|
2014-03-29 05:07:46 +00:00
|
|
|
function verifyMetadata(fileOrContainer, name) {
|
2014-03-31 23:30:35 +00:00
|
|
|
assert(fileOrContainer.getMetadata());
|
|
|
|
assert.equal(fileOrContainer.getMetadata().name, name);
|
|
|
|
assert(fileOrContainer.getMetadata().uid === undefined);
|
|
|
|
assert(fileOrContainer.getMetadata().gid === undefined);
|
|
|
|
assert(fileOrContainer.getMetadata().atime);
|
|
|
|
assert(fileOrContainer.getMetadata().ctime);
|
|
|
|
assert(fileOrContainer.getMetadata().mtime);
|
|
|
|
assert.equal(typeof fileOrContainer.getMetadata().size, 'number');
|
2014-03-29 05:07:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
describe('FileSystem based storage provider', function() {
|
|
|
|
describe('container apis', function() {
|
2014-01-24 17:44:58 +00:00
|
|
|
var client = null;
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should require an existing directory as the root', function(done) {
|
2014-01-24 17:44:58 +00:00
|
|
|
client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
|
|
|
|
process.nextTick(done);
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should complain if the root directory doesn\'t exist', function(done) {
|
2014-01-24 17:44:58 +00:00
|
|
|
try {
|
|
|
|
client = new FileSystemProvider({root: path.join(__dirname, '_storage')});
|
|
|
|
process.nextTick(done.bind(null, 'Error'));
|
|
|
|
} catch (err) {
|
|
|
|
// Should be here
|
|
|
|
process.nextTick(done);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should return an empty list of containers', function(done) {
|
|
|
|
client.getContainers(function(err, containers) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.equal(0, containers.length);
|
|
|
|
done(err, containers);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should create a new container', function(done) {
|
|
|
|
client.createContainer({name: 'c1'}, function(err, container) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
2014-03-29 05:07:46 +00:00
|
|
|
verifyMetadata(container, 'c1');
|
2014-01-24 17:44:58 +00:00
|
|
|
done(err, container);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should get a container c1', function(done) {
|
|
|
|
client.getContainer('c1', function(err, container) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
2014-03-29 05:07:46 +00:00
|
|
|
verifyMetadata(container, 'c1');
|
2014-01-24 17:44:58 +00:00
|
|
|
done(err, container);
|
|
|
|
});
|
|
|
|
});
|
2013-06-24 22:53:14 +00:00
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should not get a container c2', function(done) {
|
|
|
|
client.getContainer('c2', function(err, container) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(err);
|
|
|
|
done(null, container);
|
|
|
|
});
|
2013-06-24 21:07:12 +00:00
|
|
|
});
|
2014-01-24 17:44:58 +00:00
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should return one container', function(done) {
|
|
|
|
client.getContainers(function(err, containers) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.equal(1, containers.length);
|
|
|
|
done(err, containers);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should destroy a container c1', function(done) {
|
|
|
|
client.destroyContainer('c1', function(err, container) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
done(err, container);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should not get a container c1 after destroy', function(done) {
|
|
|
|
client.getContainer('c1', function(err, container) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(err);
|
|
|
|
done(null, container);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
describe('file apis', function() {
|
2014-01-24 17:44:58 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should create a new container', function(done) {
|
|
|
|
client.createContainer({name: 'c1'}, function(err, container) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
done(err, container);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should upload a file', function(done) {
|
2014-01-24 17:44:58 +00:00
|
|
|
var writer = client.upload({container: 'c1', remote: 'f1.txt'});
|
|
|
|
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
|
|
|
|
writer.on('finish', done);
|
|
|
|
writer.on('error', done);
|
|
|
|
});
|
|
|
|
|
2016-12-19 00:49:43 +00:00
|
|
|
it('should fail to upload a file with invalid characters', function(done) {
|
|
|
|
var writer = client.upload({container: 'c1', remote: 'a/f1.txt'});
|
|
|
|
fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer);
|
|
|
|
var cb = done;
|
|
|
|
var clearCb = function() {};
|
|
|
|
writer.on('error', function() {
|
|
|
|
cb();
|
|
|
|
cb = clearCb;
|
|
|
|
});
|
|
|
|
writer.on('finish', function() {
|
|
|
|
cb(new Error('Should have finished with error callback'));
|
|
|
|
cb = clearCb;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should download a file', function(done) {
|
2014-01-24 17:44:58 +00:00
|
|
|
var reader = client.download({
|
|
|
|
container: 'c1',
|
2016-10-18 22:06:55 +00:00
|
|
|
remote: 'f1.txt',
|
2014-01-24 17:44:58 +00:00
|
|
|
});
|
|
|
|
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
|
|
|
|
reader.on('end', done);
|
|
|
|
reader.on('error', done);
|
|
|
|
});
|
|
|
|
|
2016-12-19 00:49:43 +00:00
|
|
|
it('should fail to download a file with invalid characters', function(done) {
|
|
|
|
var reader = client.download({container: 'c1', remote: 'a/f1.txt'});
|
|
|
|
reader.pipe(fs.createWriteStream(path.join(__dirname, 'files/a-f1_downloaded.txt')));
|
|
|
|
var cb = done;
|
|
|
|
var clearCb = function() {};
|
|
|
|
reader.on('error', function() {
|
|
|
|
cb();
|
|
|
|
cb = clearCb;
|
|
|
|
});
|
|
|
|
reader.on('end', function() {
|
|
|
|
cb(new Error('Expected error: Invalid name'));
|
|
|
|
cb = clearCb;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should get files for a container', function(done) {
|
|
|
|
client.getFiles('c1', function(err, files) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.equal(1, files.length);
|
|
|
|
done(err, files);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should get a file', function(done) {
|
|
|
|
client.getFile('c1', 'f1.txt', function(err, f) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.ok(f);
|
2014-03-29 05:07:46 +00:00
|
|
|
verifyMetadata(f, 'f1.txt');
|
2014-01-24 17:44:58 +00:00
|
|
|
done(err, f);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should remove a file', function(done) {
|
|
|
|
client.removeFile('c1', 'f1.txt', function(err) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should get no files from a container', function(done) {
|
|
|
|
client.getFiles('c1', function(err, files) {
|
2014-01-24 17:44:58 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.equal(0, files.length);
|
|
|
|
done(err, files);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-10-18 22:06:55 +00:00
|
|
|
it('should destroy a container c1', function(done) {
|
|
|
|
client.destroyContainer('c1', function(err, container) {
|
2014-01-24 17:44:58 +00:00
|
|
|
// console.error(err);
|
|
|
|
assert(!err);
|
|
|
|
done(err, container);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-24 21:07:12 +00:00
|
|
|
});
|