Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

tests for inittab

  • Loading branch information...
commit 0bae85ec35925c9af76f1938d0630c7e9392b50f 1 parent b607ffa
Wong Liang Zan authored
144 test/export_inittab.test.js
... ... @@ -0,0 +1,144 @@
  1 +var exporter = require('../lib/exporter')
  2 + , fs = require('fs')
  3 + , path = require('path')
  4 + , exec = require('child_process').exec
  5 + , should = require('should')
  6 + , async = require('async')
  7 + , _ = require('underscore');
  8 +
  9 +function defaultCmdOptions(opts) {
  10 + return _.extend({
  11 + format: 'inittab',
  12 + location: path.normalize(__dirname + '/../tmp/inittab'),
  13 + concurrency: null,
  14 + directory: null,
  15 + app: path.basename(path.dirname(process.cwd())),
  16 + user: process.env.USER,
  17 + template: null,
  18 + log: path.normalize(__dirname + '/../tmp/log'),
  19 + port: 5000
  20 + }, opts);
  21 +}
  22 +
  23 +function verifyMasterTemplate(contents, callback) {
  24 + var processFilePath = __dirname + '/../tmp/inittab';
  25 + path.existsSync(processFilePath).should.eql(true);
  26 + fs.readFileSync(processFilePath, 'utf8').should.eql(contents);
  27 + return callback(null);
  28 +}
  29 +
  30 +function removeWrittenTemplates(callback) {
  31 + var cmd = 'rm ' + __dirname + '/../tmp/inittab';
  32 + var rm = exec(cmd, function(err, stdout, stderr) {
  33 + callback(err);
  34 + });
  35 +}
  36 +
  37 +describe('exporter', function() {
  38 + describe('inittab', function() {
  39 +
  40 + describe('given no options', function() {
  41 + before(function(done) {
  42 + removeWrittenTemplates(function(err) {
  43 + if (err) { console.log(err); }
  44 + done();
  45 + });
  46 + });
  47 +
  48 + after(function(done) {
  49 + removeWrittenTemplates(function(err) {
  50 + if (err) { console.log(err); }
  51 + done();
  52 + });
  53 + });
  54 +
  55 + it('should build the template', function(done) {
  56 + var procs = {
  57 + server1: 'node scripts/server.js',
  58 + server2: 'node scripts/server.js'
  59 + };
  60 + var cmdOptions = defaultCmdOptions({});
  61 +
  62 + exporter.writeTemplates(procs, cmdOptions, function(err) {
  63 + var contents = "# ----- forewoman Projects processes -----\n"
  64 + + "SERVER10:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server1-0.log 2>&1'\n"
  65 + + "SERVER20:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server2-0.log 2>&1'\n"
  66 + + "# ----- end foreman Projects processes -----";
  67 + verifyMasterTemplate(contents, function(err) {
  68 + done();
  69 + });
  70 + });
  71 + });
  72 + });
  73 +
  74 + describe('given concurrent options', function() {
  75 + it('should build the template', function(done) {
  76 + var procs = {
  77 + server1: 'node scripts/server.js',
  78 + server2: 'node scripts/server.js'
  79 + };
  80 + var cmdOptions = defaultCmdOptions({
  81 + concurrency: 'server1=2'
  82 + });
  83 +
  84 + exporter.writeTemplates(procs, cmdOptions, function(err) {
  85 + var contents = "# ----- forewoman Projects processes -----\n"
  86 + + "SERVER10:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server1-0.log 2>&1'\n"
  87 + + "SERVER11:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server1-1.log 2>&1'\n"
  88 + + "SERVER20:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server2-0.log 2>&1'\n"
  89 + + "# ----- end foreman Projects processes -----";
  90 + verifyMasterTemplate(contents, function(err) {
  91 + done();
  92 + });
  93 + });
  94 + });
  95 + });
  96 +
  97 +
  98 + describe('given app options', function() {
  99 + it('should build the template', function(done) {
  100 + var appName = 'foo';
  101 + var procs = {
  102 + server1: 'node scripts/server.js',
  103 + server2: 'node scripts/server.js'
  104 + };
  105 + var cmdOptions = defaultCmdOptions({
  106 + app: appName
  107 + });
  108 +
  109 + exporter.writeTemplates(procs, cmdOptions, function(err) {
  110 + var contents = "# ----- forewoman " + appName + " processes -----\n"
  111 + + "SERVER10:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server1-0.log 2>&1'\n"
  112 + + "SERVER20:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server2-0.log 2>&1'\n"
  113 + + "# ----- end foreman " + appName + " processes -----";
  114 + verifyMasterTemplate(contents, function(err) {
  115 + done();
  116 + });
  117 + });
  118 + });
  119 + });
  120 +
  121 + describe('given template options', function() {
  122 + it('should build the template', function(done) {
  123 + var procs = {
  124 + server1: 'node scripts/server.js',
  125 + server2: 'node scripts/server.js'
  126 + };
  127 + var cmdOptions = defaultCmdOptions({
  128 + template: 'master=' + __dirname + '/fixtures/inittab.alternate'
  129 + });
  130 +
  131 + exporter.writeTemplates(procs, cmdOptions, function(err) {
  132 + var contents = "# ----- forewoman alternate Projects processes -----\n"
  133 + + "SERVER10:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server1-0.log 2>&1'\n"
  134 + + "SERVER20:4:respawn:/bin/su - " + process.env.USER + " -c 'PORT=5000 node scripts/server.js >> " + process.cwd() + "/tmp/log/server2-0.log 2>&1'\n"
  135 + + "# ----- end foreman alternate Projects processes -----";
  136 + verifyMasterTemplate(contents, function(err) {
  137 + done();
  138 + });
  139 + });
  140 + });
  141 + });
  142 +
  143 + });
  144 +});
5 test/fixtures/inittab.alternate
... ... @@ -0,0 +1,5 @@
  1 +# ----- forewoman alternate {{app}} processes -----
  2 +{{#processes}}
  3 +{{id}}:4:respawn:/bin/su - {{user}} -c 'PORT={{port}} {{processCommand}} >> {{logRoot}}/{{processName}}-{{num}}.log 2>&1'
  4 +{{/processes}}
  5 +# ----- end foreman alternate {{app}} processes -----

0 comments on commit 0bae85e

Please sign in to comment.
Something went wrong with that request. Please try again.