Initialize repo when volume is created.
Get started with the volume tests while we are at it.
This commit is contained in:
+14
-1
@@ -5,7 +5,8 @@ var exec = require('child_process').exec,
|
||||
path = require('path'),
|
||||
mkdirp = require('mkdirp'),
|
||||
fs = require('fs'),
|
||||
assert = require('assert');
|
||||
assert = require('assert'),
|
||||
crypto = require('crypto');
|
||||
|
||||
exports = module.exports = Repo;
|
||||
|
||||
@@ -127,10 +128,22 @@ Repo.prototype._createCommit = function (message, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
function createTempFileSync(dir, contents) {
|
||||
// dir is required because renames won't work across file systems
|
||||
var filename = path.join(dir, '___addFile___' + crypto.randomBytes(4).readUInt32LE(0));
|
||||
fs.writeFileSync(filename, contents);
|
||||
return filename;
|
||||
}
|
||||
|
||||
// FIXME: make stream API
|
||||
Repo.prototype._writeFileAndCommit = function (file, options, callback) {
|
||||
var that = this;
|
||||
var absoluteFilePath = path.join(this.checkoutDir, file);
|
||||
|
||||
if (options.contents) {
|
||||
options.file = createTempFileSync(this.checkoutDir, options.contents);
|
||||
}
|
||||
|
||||
fs.rename(options.file, absoluteFilePath, function (err) {
|
||||
if (err) return callback(err);
|
||||
that.git('add ' + file, function (err) {
|
||||
|
||||
+20
-7
@@ -19,10 +19,11 @@ exports = module.exports = {
|
||||
attachRepo: attachRepo
|
||||
};
|
||||
|
||||
var config;
|
||||
var config, repos = { };
|
||||
|
||||
function initialize(cfg) {
|
||||
config = cfg;
|
||||
// TODO: populate repos with existing repos
|
||||
}
|
||||
|
||||
function resolveVolumeRootPath(volume) {
|
||||
@@ -63,6 +64,7 @@ function deleteVolume(req, res, next) {
|
||||
console.log('Failed to delete volume mount point.', error);
|
||||
}
|
||||
|
||||
delete repos[req.params[0]];
|
||||
// TODO how to handle any errors in folder deletion?
|
||||
res.send(200);
|
||||
});
|
||||
@@ -87,27 +89,38 @@ function listVolumes(req, res, next) {
|
||||
ret.push(tmp);
|
||||
});
|
||||
|
||||
res.send(JSON.stringify(ret));
|
||||
res.send(200, ret);
|
||||
});
|
||||
}
|
||||
|
||||
function createVolume(req, res, next) {
|
||||
// TODO check for existing volumes
|
||||
|
||||
if (!req.body.name) {
|
||||
return next(new HttpError(400, 'volume name not specified'));
|
||||
}
|
||||
|
||||
if (req.body.name in repos) {
|
||||
return next(new HttpError(409, 'volume already exists'));
|
||||
}
|
||||
|
||||
var volumeRoot = resolveVolumeRootPath(req.body.name);
|
||||
var volumeMountPoint = resolveVolumeMountPoint(req.body.name);
|
||||
|
||||
encfs.create(volumeRoot, volumeMountPoint, 'foobar1337', function (error, result) {
|
||||
if (error) {
|
||||
console.log('Creating volume failed:', error);
|
||||
return next(new HttpError(400, 'volume creation failed: ' + error));
|
||||
return next(new HttpError(500, 'volume creation failed: ' + error));
|
||||
}
|
||||
|
||||
res.send(200);
|
||||
// ## move this to repo
|
||||
var repo = new Repo({ root: volumeMountPoint });
|
||||
repo.create({ name: 'nobody', email: 'somebody@like.me' }, function (error) {
|
||||
if (error) return next(new HttpError(500, 'Error creating repo in volume'));
|
||||
repo.addFile('README', { contents: 'Say something useful here' }, function (error, commit) {
|
||||
if (error) return next(new HttpError(500, 'Error adding README: ' + error));
|
||||
repos[req.body.name] = repo;
|
||||
res.send(201);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -150,7 +163,7 @@ function listFiles(req, res, next) {
|
||||
ret.push(tmp);
|
||||
});
|
||||
|
||||
res.send(JSON.stringify(ret));
|
||||
res.send(200, ret);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+14
-5
@@ -12,7 +12,9 @@ var optimist = require('optimist'),
|
||||
db = require('./database'),
|
||||
routes = require('./routes'),
|
||||
Repo = require('./repo'),
|
||||
debug = require('debug');
|
||||
debug = require('debug'),
|
||||
crypto = require('crypto'),
|
||||
os = require('os');
|
||||
|
||||
var app = express();
|
||||
|
||||
@@ -26,24 +28,31 @@ function getUserHomeDir() {
|
||||
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
|
||||
}
|
||||
|
||||
var homeDir = path.join(getUserHomeDir(), '.yellowtent');
|
||||
var baseDir = path.join(getUserHomeDir(), '.yellowtent');
|
||||
|
||||
app.configure('testing', function () {
|
||||
// to make sure tests can run repeatedly, set the basedir to something random
|
||||
var tmpdirname = 'repo-test-' + crypto.randomBytes(4).readUInt32LE(0);
|
||||
baseDir = path.join(os.tmpdir(), tmpdirname);
|
||||
});
|
||||
|
||||
|
||||
var argv = optimist.usage('Usage: $0 --dataRoot <directory>')
|
||||
.alias('h', 'help')
|
||||
.describe('h', 'Show this help.')
|
||||
|
||||
.alias('d', 'dataRoot')
|
||||
.default('d', path.join(homeDir, 'data'))
|
||||
.default('d', path.join(baseDir, 'data'))
|
||||
.describe('d', 'Volume data storage directory.')
|
||||
.string('d')
|
||||
|
||||
.alias('m', 'mountRoot')
|
||||
.default('m', path.join(homeDir, 'mount'))
|
||||
.default('m', path.join(baseDir, 'mount'))
|
||||
.describe('m', 'Volume mount point directory.')
|
||||
.string('m')
|
||||
|
||||
.alias('c', 'configRoot')
|
||||
.default('c', path.join(homeDir, 'config'))
|
||||
.default('c', path.join(baseDir, 'config'))
|
||||
.describe('c', 'Server config root directory for storing user db and meta data.')
|
||||
.string('c')
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
|
||||
process.env.NODE_ENV = 'testing'; // ugly
|
||||
|
||||
var server = require('../server'),
|
||||
request = require('superagent'),
|
||||
expect = require('expect.js'),
|
||||
@@ -6,6 +9,7 @@ var server = require('../server'),
|
||||
var SERVER_URL;
|
||||
var USERNAME = 'admin', PASSWORD = 'admin';
|
||||
var AUTH = new Buffer(USERNAME + ':' + PASSWORD).toString('base64');
|
||||
var TESTVOLUME = 'testvolume';
|
||||
|
||||
before(function (done) {
|
||||
server.start(function () {
|
||||
@@ -14,14 +18,16 @@ before(function (done) {
|
||||
});
|
||||
});
|
||||
|
||||
describe('api', function () {
|
||||
describe('bad requests', function () {
|
||||
it('random', function (done) {
|
||||
request.get(SERVER_URL + '/random', function (err, res) {
|
||||
expect(res.statusCode == 401).to.be.ok();
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('version', function () {
|
||||
it('version', function (done) {
|
||||
request.get(SERVER_URL + '/api/v1/version', function (err, res) {
|
||||
expect(res.statusCode == 200).to.be.ok();
|
||||
@@ -29,7 +35,9 @@ describe('api', function () {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('user', function () {
|
||||
it('admin', function (done) {
|
||||
request.post(SERVER_URL + '/api/v1/createadmin')
|
||||
.send({ username: USERNAME, password: PASSWORD, email: 'silly@me.com' })
|
||||
@@ -49,3 +57,40 @@ describe('api', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('volume', function () {
|
||||
it('create', function (done) {
|
||||
request.post(SERVER_URL + '/api/v1/volume/create')
|
||||
.set('Authorization', AUTH)
|
||||
.send({ name: TESTVOLUME })
|
||||
.end(function (err, res) {
|
||||
expect(res.statusCode == 201).to.be.ok();
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('list', function (done) {
|
||||
request.get(SERVER_URL + '/api/v1/volume/list')
|
||||
.set('Authorization', AUTH)
|
||||
.end(function (err, res) {
|
||||
expect(res.body.length == 1).to.be.ok();
|
||||
expect(res.body[0].name == TESTVOLUME).to.be.ok();
|
||||
expect(res.statusCode == 200).to.be.ok();
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('listFiles', function (done) {
|
||||
request.get(SERVER_URL + '/api/v1/volume/' + TESTVOLUME + '/list/')
|
||||
.set('Authorization', AUTH)
|
||||
.end(function (err, res) {
|
||||
var foundReadme = false;
|
||||
res.body.forEach(function (entry) {
|
||||
if (entry.filename == 'README') foundReadme = true;
|
||||
});
|
||||
expect(foundReadme == true).to.be.ok();
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user