Merge branch 'master' into ut

Conflicts:
	js/utils.js
	test/server.js
This commit is contained in:
Arthur Britto
2012-09-28 12:34:27 -07:00
51 changed files with 1358 additions and 647 deletions

View File

@@ -9,15 +9,17 @@ exports.newcoind = path.join(process.cwd(), "newcoind");
// Configuration for servers.
exports.servers = {
// A local test server.
alpha : {
'trusted' : true,
// "peer_ip" : "0.0.0.0",
// "peer_port" : 51235,
"rpc_ip" : "0.0.0.0",
"rpc_port" : 5005,
"websocket_ip" : "127.0.0.1",
"websocket_port" : 6005,
"validation_seed" : "shhDFVsmS2GSu5vUyZSPXYfj1r79h",
"validators" : "n9L8LZZCwsdXzKUN9zoVxs4YznYXZ9hEhsQZY7aVpxtFaSceiyDZ beta"
'rpc_ip' : "0.0.0.0",
'rpc_port' : 5005,
'websocket_ip' : "127.0.0.1",
'websocket_port' : 6005,
'validation_seed' : "shhDFVsmS2GSu5vUyZSPXYfj1r79h",
'validators' : "n9L8LZZCwsdXzKUN9zoVxs4YznYXZ9hEhsQZY7aVpxtFaSceiyDZ beta"
}
};
// vim:ts=4

View File

@@ -1,4 +1,7 @@
// Manage test servers
//
// YYY Would be nice to be able to hide server output.
//
// Provide servers
//
@@ -6,7 +9,7 @@
//
var config = require("./config.js");
var utils = require("./utils.js");
var utils = require("../js/utils.js");
var fs = require("fs");
var path = require("path");
@@ -16,107 +19,137 @@ var WebSocket = require("ws");
var servers = {};
var serverPath = function(name) {
return "tmp/server/" + name;
// Create a server object
var Server = function(name) {
this.name = name;
};
// Return a server's newcoind.cfg as string.
var configContent = function(name) {
var cfg = config.servers[name];
Server.method('configContent', function() {
var cfg = config.servers[this.name];
return Object.keys(cfg).map(function (o) {
return Object.keys(cfg).map(function(o) {
return util.format("[%s]\n%s\n", o, cfg[o]);
}).join("");
};
});
var configPath = function(name) {
return path.join(serverPath(name), "newcoind.cfg");
};
Server.method('serverPath', function() {
return "tmp/server/" + this.name;
});
Server.method('configPath', function() {
return path.join(this.serverPath(), "newcoind.cfg");
});
// Write a server's newcoind.cfg.
var writeConfig = function(name, done) {
fs.writeFile(configPath(name), configContent(name), 'utf8', done);
};
Server.method('writeConfig', function(done) {
fs.writeFile(this.configPath(), this.configContent(), 'utf8', done);
});
var serverSpawnSync = function(name) {
// Spawn the server.
Server.method('serverSpawnSync', function() {
// Spawn in standalone mode for now.
var server = child.spawn(
this.child = child.spawn(
config.newcoind,
[
"-a",
"--conf=newcoind.cfg"
],
{
cwd: serverPath(name),
cwd: this.serverPath(),
env: process.env,
stdio: 'inherit'
});
servers[name] = server;
console.log("server: %s: %s -a --conf=%s", server.pid, config.newcoind, configPath(name));
console.log("sever: start: servers = %s", Object.keys(servers).toString());
console.log("server: start %s: %s -a --conf=%s", this.child.pid, config.newcoind, this.configPath());
server.on('exit', function (code, signal) {
// By default, just log exits.
this.child.on('exit', function(code, signal) {
// If could not exec: code=127, signal=null
// If regular exit: code=0, signal=null
console.log("sever: spawn: server exited code=%s: signal=%s", code, signal);
delete servers[name];
console.log("server: spawn: server exited code=%s: signal=%s", code, signal);
});
};
});
var makeBase = function(name, done) {
var path = serverPath(name);
// Prepare server's working directory.
Server.method('makeBase', function(done) {
var path = this.serverPath();
var self = this;
// Reset the server directory, build it if needed.
utils.resetPath(path, '0777', function (e) {
utils.resetPath(path, '0777', function(e) {
if (e) {
throw e;
}
else {
writeConfig(name, done);
self.writeConfig(done);
}
});
};
var wsOpen = function(done) {
var socket = new WebSocket(util.format("ws:://%s:%s", server.websocket_ip, server.websocket_port));
socket.on('open') {
done();
});
};
});
// Create a standalone server.
// Prepare the working directory and spawn the server.
exports.start = function(name, done) {
makeBase(name, function (e) {
Server.method('start', function(done) {
var self = this;
this.makeBase(function(e) {
if (e) {
throw e;
}
else {
serverSpawnSync(name);
wsOpen(done);
self.serverSpawnSync();
done();
}
});
};
});
exports.stop = function(name, done) {
console.log("sever: stop: servers = %s", Object.keys(servers).toString());
var server = servers[name];
if (server) {
server.on('exit', function (code, signal) {
console.log("sever: stop: server exited");
delete servers[name];
// Stop a standalone server.
Server.method('stop', function(done) {
if (this.child) {
// Update the on exit to invoke done.
this.child.on('exit', function(code, signal) {
console.log("server: stop: server exited");
done();
});
server.kill();
this.child.kill();
}
else
{
console.log("sever: stop: no such server");
console.log("server: stop: no such server");
done();
}
});
// Start the named server.
exports.start = function(name, done) {
if (servers[name])
{
console.log("server: start: server already started.");
}
else
{
var server = new Server(name);
servers[name] = server;
console.log("server: start: %s", JSON.stringify(server));
server.start(done);
}
};
// Delete the named server.
exports.stop = function(name, done) {
console.log("server: stop: %s of %s", name, Object.keys(servers).toString());
var server = servers[name];
if (server) {
server.stop(done);
delete servers[name];
}
};
exports.Server = Server;
// vim:ts=4

View File

@@ -1,16 +1,21 @@
// console.log("standalone-test.js>");
var fs = require("fs");
var buster = require("buster");
var server = require("./server.js");
var remote = require("../js/remote.js");
var config = require("./config.js");
buster.testCase("Check standalone server startup", {
"server start and stop": function (done) {
// How long to wait for server to start.
var serverDelay = 1500;
buster.testRunner.timeout = 5000;
buster.testCase("Standalone server startup", {
"server start and stop": function(done) {
server.start("alpha",
function (e) {
function(e) {
buster.refute(e);
server.stop("alpha", function (e) {
server.stop("alpha", function(e) {
buster.refute(e);
done();
});
@@ -18,5 +23,166 @@ buster.testCase("Check standalone server startup", {
}
});
// console.log("standalone-test.js<");
buster.testCase("WebSocket connection", {
'setUp' :
function(done) {
server.start("alpha",
function(e) {
buster.refute(e);
done();
}
);
},
'tearDown' :
function(done) {
server.stop("alpha", function(e) {
buster.refute(e);
done();
});
},
"websocket connect and disconnect" :
function(done) {
var alpha = remote.remoteConfig(config, "alpha");
alpha.connect(function(stat) {
buster.assert(1 == stat); // OPEN
alpha.disconnect(function(stat) {
buster.assert(3 == stat); // CLOSED
done();
});
}, serverDelay);
},
});
buster.testCase("Websocket commands", {
'setUp' :
function(done) {
server.start("alpha",
function(e) {
buster.refute(e);
alpha = remote.remoteConfig(config, "alpha");
alpha.connect(function(stat) {
buster.assert(1 == stat); // OPEN
done();
}, serverDelay);
});
},
'tearDown' :
function(done) {
alpha.disconnect(function(stat) {
buster.assert(3 == stat); // CLOSED
server.stop("alpha", function(e) {
buster.refute(e);
done();
});
});
},
'ledger_current' :
function(done) {
alpha.ledger_current(function (r) {
console.log(r);
buster.assert.equals(r.ledger_index, 3);
done();
});
},
'// ledger_closed' :
function(done) {
alpha.ledger_closed(function (r) {
console.log("result: %s", JSON.stringify(r));
buster.assert.equals(r.ledger_index, 2);
done();
});
},
'account_root success' :
function(done) {
alpha.ledger_closed(function (r) {
// console.log("result: %s", JSON.stringify(r));
buster.refute('error' in r);
alpha.ledger_entry({
'ledger_index' : r.ledger_index,
'account_root' : 'iHb9CJAWyB4ij91VRWn96DkukG4bwdtyTh'
} , function (r) {
// console.log("account_root: %s", JSON.stringify(r));
buster.assert('node' in r);
done();
});
});
},
'account_root malformedAddress' :
function(done) {
alpha.ledger_closed(function (r) {
// console.log("result: %s", JSON.stringify(r));
buster.refute('error' in r);
alpha.ledger_entry({
'ledger_index' : r.ledger_index,
'account_root' : 'foobar'
} , function (r) {
// console.log("account_root: %s", JSON.stringify(r));
buster.assert.equals(r.error, 'malformedAddress');
done();
});
});
},
'account_root entryNotFound' :
function(done) {
alpha.ledger_closed(function (r) {
// console.log("result: %s", JSON.stringify(r));
buster.refute('error' in r);
alpha.ledger_entry({
'ledger_index' : r.ledger_index,
'account_root' : 'iG1QQv2nh2gi7RCZ1P8YYcBUKCCN633jCn'
} , function (r) {
// console.log("account_root: %s", JSON.stringify(r));
buster.assert.equals(r.error, 'entryNotFound');
done();
});
});
},
'ledger_entry index' :
function(done) {
alpha.ledger_closed(function (r) {
// console.log("result: %s", JSON.stringify(r));
buster.refute('error' in r);
alpha.ledger_entry({
'ledger_index' : r.ledger_index,
'index' : "2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8",
} , function (r) {
console.log("node: %s", JSON.stringify(r));
buster.assert('node_binary' in r);
done();
});
});
},
});
// vim:ts=4

View File

@@ -1,130 +0,0 @@
var fs = require("fs");
var path = require("path");
var filterErr = function(code, done) {
return function (e) {
done(e && e.code === code ? undefined : e);
};
};
var throwErr = function(done) {
return function (e) {
if (e)
throw e;
done();
};
};
// apply function to elements of array. Return first true value to done or undefined.
var mapOr = function(func, array, done) {
if (array.length) {
func(array[array.length-1], function (v) {
if (v) {
done(v);
}
else {
array.length -= 1;
mapOr(func, array, done);
}
});
}
else {
done();
}
};
// Make a directory and sub-directories.
var mkPath = function(dirPath, mode, done) {
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function (e) {
if (!e || e.code === "EEXIST") {
// Created or already exists, done.
done();
}
else if (e.code === "ENOENT") {
// Missing sub dir.
mkPath(path.dirname(dirPath), mode, function(e) {
if (e) {
throw e;
}
else {
mkPath(dirPath, mode, done);
}
});
}
else {
throw e;
}
});
};
// Empty a directory.
var emptyPath = function(dirPath, done) {
fs.readdir(dirPath, function (err, files) {
if (err) {
done(err);
}
else {
mapOr(rmPath, files.map(function(f) { return path.join(dirPath, f); }), done);
}
});
};
// Remove path recursively.
var rmPath = function(dirPath, done) {
// console.log("rmPath: %s", dirPath);
fs.lstat(dirPath, function (err, stats) {
if (err && err.code == "ENOENT") {
done();
}
if (err) {
done(err);
}
else if (stats.isDirectory()) {
emptyPath(dirPath, function (e) {
if (e) {
done(e);
}
else {
// console.log("rmdir: %s", dirPath); done();
fs.rmdir(dirPath, done);
}
});
}
else {
// console.log("unlink: %s", dirPath); done();
fs.unlink(dirPath, done);
}
});
};
// Create directory if needed and empty if needed.
var resetPath = function(dirPath, mode, done) {
mkPath(dirPath, mode, function (e) {
if (e) {
done(e);
}
else {
emptyPath(dirPath, done);
}
});
};
var trace = function(comment, func) {
return function() {
console.log("%s: %s", trace, arguments.toString);
func(arguments);
};
};
exports.emptyPath = emptyPath;
exports.mapOr = mapOr;
exports.mkPath = mkPath;
exports.resetPath = resetPath;
exports.rmPath = rmPath;
exports.trace = trace;
// vim:ts=4