Skip to content

Tests to merge #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/FileSystemBehaviour/Append.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var GFS = require('../../lib/things.js').addons.gfs('mongodb://localhost:27017/things-js-test-fs');

var args = process.argv.slice(2);
var identifier = args[0];
var fpath = args[1];

console.log('Process ' + identifier + ' starting');
GFS.appendFile(fpath, ' hello ' + identifier, function(err, res){
if(err){
console.log('Error appending on process ' + identifier);
}
process.exit();
});



57 changes: 57 additions & 0 deletions test/FileSystemBehaviour/Filesystem-forked-append.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var GFS = require('../../lib/things.js').addons.gfs('mongodb://localhost:27017/things-js-test-fs');
var spawn = require('child_process').spawn

var fname = '/appendtest.txt';
var spawnFile = './Append.js';
var numProcs = Number(process.argv.slice(2)[0]) || 3;

function init(){
return new Promise(function(resolve){
var procs = [];
GFS.writeFile(fname, '', function(err){
if(err){
console.log('Could not successfully create the test file. Aborting...');
process.exit();
}
for(var i = 0; i < numProcs; i++){
procs.push(spawnChild(i));
}
Promise.all(procs).then(resolve);
});
});
}

function spawnChild(id){
return new Promise(function(resolve){
var child = spawn('node', [spawnFile, id, fname]);
child.on('exit', function(){
resolve();
});
child.stdout.on('data', function(data){
console.log(data.toString());
});
})
}

function multiAppend(){
init().then(function(){
GFS.readFile(fname, function(err, data){
if(err){
console.log('Error occurred: ' + err);
}
else{
console.log('Here is the result of the file:\n' + data);
GFS.deleteFile(fname, function(err){
if(err){
console.log(err);
}
process.exit();
});
}
});
});
}

multiAppend();


47 changes: 47 additions & 0 deletions test/FileSystemBehaviour/Filesystem-multi-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var assert = require('assert');
var thingsjs = require('../lib/things.js');
var GFS = require('../lib/things.js').addons.gfs('mongodb://localhost:27017/things-js-test-fs');
var mqtt = require('mqtt');
var mosca = require('mosca');
var sinon = require('sinon');

var fname = '/test_read.txt';


function timeWrite(writeValue){
return new Promise(function(resolve){
var start = Date.now();
GFS.writeFile(fname, 'hello world', function(err, data){
var end = Date.now();
if(err){
console.log(err);
resolve([err, writeValue+1]);
}
else{
var elapsed = (end - start) / 1000;
resolve([elapsed, writeValue+1]);
}
});
});
}

function multiWrite(instances){
var reads = [];
for(var i = 0; i < instances; i++){
var promise = timeWrite(i).then(function(res){
var time = (res[0] instanceof Error) ? ('could not calculate due to error\n') : ( res[0] + ' seconds\n');
console.log('Time it took for write ' + res[1] + ' : ' + time);
});
reads.push(promise);
}

Promise.all(reads).then(function(){
GFS.deleteFile(fname, function(err){
console.log('Tests completed');
process.exit();
});
});
}

multiWrite(10);

211 changes: 211 additions & 0 deletions test/Filesystem-REST-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
var assert = require('assert');
var expect = require('chai').expect;
var should = require('chai').should();
var sinon = require('sinon');

var http = require('http');
var express = require('express');
var helmet = require('helmet');
var FSServer = require('../lib/things.js').addons.FSServer;
var mongoclient = require('mongodb').MongoClient;

describe('REST API', function(){
var self = this;
this.mongourl = 'mongodb://localhost:27017/things-js-test-fs';
this.port = 3030;
this.fsurl = 'localhost';
this.fspath = '/fs';
this.folders = ['folder1', 'folder2', 'folder3'];
this.files = {
'file1': 'hello world',
'file2': '12345'
}

// initialize the test server
before(function(done){
this.timeout(10000);
var app = express();
var router = express.Router();

app.use(express.json());
app.use(helmet());
app.use(express.urlencoded({ extended: true }));

var gfs = new FSServer(self.mongourl, router);
app.use(self.fspath, router);

self.server = http.createServer(app).listen(self.port, done);
});

function makehttp(method, path, body){
console.log(self.fsurl, self.port, path);
var options = {
method: method,
host: self.fsurl,
port: self.port,
path: self.fspath + path,
headers: {
'Content-Type': 'application/json'
}
}
return new Promise(function(resolve, reject){
var req = http.request(options, function(res){
var reply = '';
res.on('data', function(c){
reply += c;
});
res.on('end', function(){
resolve(JSON.parse(reply));
});
})
.on('error', function(){
throw new Error();
});
if(body){
req.write(JSON.stringify(body));
}
req.end();
});
}

it('Fetch the root directory', function(){
this.timeout(5000);

return makehttp('GET', '/').then(function(res){
expect(res.abs_path).to.eql('/');
});
});

it('Create a folder in the root directory', function(){
this.timeout(5000);

var body = {
type: 'directory',
name: self.folders[0]
}

return makehttp('POST', '/', body).then(function(res){
expect(res).to.exist;
});
});

it('Fetch the newly created folder', function(){
this.timeout(5000);

return makehttp('GET', '/'+self.folders[0]).then(function(res){
expect(res.name).to.eql(self.folders[0]);
expect(Object.keys(res.children).length).to.eql(0);
});
});

it('Create a file in the root directory', function(){
this.timeout(5000);

var fname = Object.keys(self.files)[0];
var body = {
type: 'file',
name: fname,
content: self.files[fname]
}

return makehttp('POST', '/', body).then(function(res){
expect(res).to.exist;
});
});

it('Fetch the newly created file', function(){
this.timeout(5000);
var fname = Object.keys(self.files)[0];

return makehttp('GET', '/'+fname).then(function(res){
expect(res.name).to.eql(fname);
expect(res.content).to.eql(self.files[fname]);
});
});

it('Create a file in a subdirectory', function(){
this.timeout(5000);

var fname = Object.keys(self.files)[1];
var body = {
type: 'file',
name: fname,
content: self.files[fname]
}

return makehttp('POST', '/'+self.folders[0], body).then(function(res){
expect(res).to.exist;
});
});

it('Fetch the file within the subdirectory', function(){
this.timeout(5000);
var fname = Object.keys(self.files)[1];
var fpath = '/' + self.folders[0] + '/' + fname;

return makehttp('GET', fpath).then(function(res){
expect(res.name).to.eql(fname);
expect(res.content).to.eql(self.files[fname]);
});
});

it('Check that the subdirectory has reference to the new file', function(){
this.timeout(5000);
var fname = Object.keys(self.files)[1];

return makehttp('GET', '/'+self.folders[0]).then(function(res){
expect(res.children[fname]).to.exist;
});
});

it('Delete a file', function(){
this.timeout(5000);
var fname = Object.keys(self.files)[0];
return makehttp('GET', '/'+fname).then(function(res){
return makehttp('DELETE', '/?ids='+res._id).then(function(data){
expect(data).to.exist;
});
});
});

it('Try accessing a deleted file', function(){
this.timeout(5000);
var fname = Object.keys(self.files)[0];

return makehttp('GET', '/'+fname).then(function(res){
expect(res.error).to.exist;
});
});

it('Delete a non-empty folder', function(){
this.timeout(5000);
return makehttp('GET', '/'+self.folders[0]).then(function(res){
return makehttp('DELETE', '/?ids='+res._id).then(function(data){
expect(data).to.exist;
});
});
});

it('Try to access a file from within the deleted folder', function(){
this.timeout(5000);
var fpath = '/' + self.folders[0] + Object.keys(self.files)[1];
return makehttp('GET', fpath).then(function(res){
expect(res.error).to.exist;
});
});


after(function(){
// drop the entire test db
mongoclient.connect(self.mongourl, { useNewParser: true }, function(err, db){
db.dropCollection('fsobjects', function(){
db.close();
});
});
self.server.close();
});

});



Loading