From b8f2b6c0dcafcbe342e8d16b945576bf786b8acc Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 15 Sep 2012 14:37:21 -0700 Subject: [PATCH 1/3] Initial check in of testing scripts. --- .gitignore | 3 +- test/buster.js | 9 +++ test/server.js | 35 +++++++++++ test/standalone-test.js | 17 ++++++ test/utils.js | 129 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 test/buster.js create mode 100644 test/server.js create mode 100644 test/standalone-test.js create mode 100644 test/utils.js diff --git a/.gitignore b/.gitignore index 2530f7eb82..ec0acfa4ff 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ *.o obj/* bin/newcoind - newcoind +# Ignore locally installed node_modules +node_modules diff --git a/test/buster.js b/test/buster.js new file mode 100644 index 0000000000..5dd49ec7cc --- /dev/null +++ b/test/buster.js @@ -0,0 +1,9 @@ +var config = module.exports; + +config["Newcoin tests"] = { + rootPath: "../", + environment: "node", + tests: [ + "test/*-test.js" + ] +} diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000000..c0e735b8af --- /dev/null +++ b/test/server.js @@ -0,0 +1,35 @@ +// Manage test servers + +// Provide servers +// +// Servers are created in tmp/server/$server +// + +console.log("server.js>"); + +var utils = require("./utils.js"); + +// var child = require("child"); + +var serverPath = function(name) { + return "tmp/server/" + name; +}; + +var makeBase = function(name, done) { + var path = serverPath(name); + + console.log("start> %s: %s", name, path); + + // Remove the existing dir. + utils.resetPath(path, parseInt('0777', 8), done); + + console.log("start< %s", name); +}; + +var start = function(name, done) { + makeBase(name, done); +}; + +exports.start = start; + +console.log("server.js<"); diff --git a/test/standalone-test.js b/test/standalone-test.js new file mode 100644 index 0000000000..3f6c1c8d58 --- /dev/null +++ b/test/standalone-test.js @@ -0,0 +1,17 @@ +// console.log("standalone-test.js>"); + +var fs = require("fs"); +var buster = require("buster"); + +var server = require("./server.js"); + +buster.testCase("Check standalone server startup", { + "Start": function (done) { + server.start("alpha", function(e) { + buster.refute(e); + done(); + }); + } +}); + +// console.log("standalone-test.js<"); diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000000..c755f02b38 --- /dev/null +++ b/test/utils.js @@ -0,0 +1,129 @@ + +var fs = require("fs"); +var path = require("path"); + +var filterErr = function(code, done) { + return function (e) { + done(e.code !== code ? e : undefined); + } +}; + +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, mode, function (e) { + if (e && e.code === "EEXIST") { + // 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; + From f45886cc6de27f1d3ab409eac2ccc310ef57d03c Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 15 Sep 2012 14:56:15 -0700 Subject: [PATCH 2/3] Cosmetic (tabbing). --- test/server.js | 1 + test/standalone-test.js | 11 ++-- test/utils.js | 123 ++++++++++++++++++++-------------------- 3 files changed, 69 insertions(+), 66 deletions(-) diff --git a/test/server.js b/test/server.js index c0e735b8af..3c7b54d398 100644 --- a/test/server.js +++ b/test/server.js @@ -33,3 +33,4 @@ var start = function(name, done) { exports.start = start; console.log("server.js<"); +// vim:ts=4 diff --git a/test/standalone-test.js b/test/standalone-test.js index 3f6c1c8d58..81221d7d19 100644 --- a/test/standalone-test.js +++ b/test/standalone-test.js @@ -7,11 +7,12 @@ var server = require("./server.js"); buster.testCase("Check standalone server startup", { "Start": function (done) { - server.start("alpha", function(e) { - buster.refute(e); - done(); - }); - } + server.start("alpha", function(e) { + buster.refute(e); + done(); + }); + } }); // console.log("standalone-test.js<"); +// vim:ts=4 diff --git a/test/utils.js b/test/utils.js index c755f02b38..c840431213 100644 --- a/test/utils.js +++ b/test/utils.js @@ -4,34 +4,34 @@ var path = require("path"); var filterErr = function(code, done) { return function (e) { - done(e.code !== code ? e : undefined); - } + done(e.code !== code ? e : undefined); + }; }; var throwErr = function(done) { return function (e) { - if (e) - throw e; - - done(); - }; + 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); - } - }); + func(array[array.length-1], function (v) { + if (v) { + done(v); + } + else { + array.length -= 1; + mapOr(func, array, done); + } + }); } else { - done(); + done(); } }; @@ -39,23 +39,23 @@ var mapOr = function(func, array, done) { var mkPath = function(dirPath, mode, done) { fs.mkdir(dirPath, mode, function (e) { if (e && e.code === "EEXIST") { - // Already exists, done. - done(); + // Already exists, done. + done(); } else if (e.code === "ENOENT") { - // Missing sub dir. + // Missing sub dir. - mkPath(path.dirname(dirPath), mode, function(e) { - if (e) { - throw e; - } - else { - mkPath(dirPath, mode, done); - } - }); + mkPath(path.dirname(dirPath), mode, function(e) { + if (e) { + throw e; + } + else { + mkPath(dirPath, mode, done); + } + }); } else { - throw e; + throw e; } }); }; @@ -74,50 +74,50 @@ var emptyPath = function(dirPath, done) { // Remove path recursively. var rmPath = function(dirPath, done) { -// console.log("rmPath: %s", dirPath); + 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); - } + 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); + } }); - } - 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); - } - }); + if (e) { + done(e); + } + else { + emptyPath(dirPath, done); + } + }); }; var trace = function(comment, func) { return function() { - console.log("%s: %s", trace, arguments.toString); - func(arguments); - }; + console.log("%s: %s", trace, arguments.toString); + func(arguments); + }; }; exports.emptyPath = emptyPath; @@ -127,3 +127,4 @@ exports.resetPath = resetPath; exports.rmPath = rmPath; exports.trace = trace; +// vim:ts=4 From 6865e575cb46d57798c45238057170029e50df84 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 15 Sep 2012 15:45:33 -0700 Subject: [PATCH 3/3] Build testing newcoind.cfg as needed. --- .gitignore | 3 +++ test/server.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ec0acfa4ff..92eb7bb785 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ newcoind # Ignore locally installed node_modules node_modules + +# Ignore tmp directory. +tmp diff --git a/test/server.js b/test/server.js index 3c7b54d398..404df4060c 100644 --- a/test/server.js +++ b/test/server.js @@ -7,21 +7,45 @@ console.log("server.js>"); +var config = require("./config.js"); var utils = require("./utils.js"); +var fs = require("fs"); +var path = require("path"); +var util = require("util"); // var child = require("child"); var serverPath = function(name) { return "tmp/server/" + name; }; +// Return a server's newcoind.cfg as string. +var serverConfig = function(name) { + var cfg = config.servers[name]; + + return Object.keys(cfg).map(function (o) { + return util.format("[%s]\n%s\n", o, cfg[o]); + }).join(""); +}; + +var writeConfig = function(name, done) { + fs.writeFile(path.join(serverPath(name), "newcoind.cfg"), serverConfig(name), 'utf8', done); +}; + var makeBase = function(name, done) { var path = serverPath(name); console.log("start> %s: %s", name, path); - // Remove the existing dir. - utils.resetPath(path, parseInt('0777', 8), done); + // Reset the server directory, build it if needed. + utils.resetPath(path, parseInt('0777', 8), function (e) { + if (e) { + throw e; + } + else { + writeConfig(name, done); + } + }); console.log("start< %s", name); };