Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

strongloop / node-foreman / 230

8 May 2016 - 12:54 coverage: 88.64%. First build
230

Pull #117

travis-ci

8bd1dd86bbf8705a5a702b86a2f3a390?size=18&default=identiconrmg
proxy: add tests for multi-proxy behaviour

The test setup is based on the behaviour described in #93, but with the
required modifications to support testing without using any fixed ports.
Pull Request #117: WIP: Upgrade all outdated dependencies

43 of 51 new or added lines in 6 files covered. (84.31%)

554 of 625 relevant lines covered (88.64%)

92.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.24
/lib/proc.js
1
// Copyright IBM Corp. 2012,2016. All Rights Reserved.
2
// Node module: foreman
3
// This file is licensed under the MIT License.
4
// License text available at https://opensource.org/licenses/MIT
5

6
var prog       = require('child_process');
75×
7

8
var cons       = require('./console').Console;
75×
9

10
var _colors    = require('./colors');
75×
11
var colors_max = _colors.colors_max;
75×
12
var colors     = _colors.colors;
75×
13

14
var os         = require('os');
75×
15
var platform   = os.platform();
75×
16

17
// Run a Specific Process
18
// - Key is a Process Name and Number
19
// - Proc is an object with the launch properties
20
//
21
// i.e. web=2 becomes the web.2 key
22
function run(key, proc, emitter) {
75×
23
  var file, args;
40×
24
  if (platform === 'win32') {
40×
25
    file = process.env.comspec || 'cmd.exe';
!
26
    args = ['/s', '/c', proc.command];
!
27
  } else {
28
    file = '/bin/sh';
40×
29
    args = ['-c', proc.command];
40×
30
  }
31
  var child = prog.spawn(file, args, { env: proc.env });
40×
32

33
  child.stdout.on('data', function(data) {
40×
34
    cons.log(key, proc, data.toString());
30×
35
  });
36

37
  child.stderr.on('data', function(data) {
40×
38
    cons.log(key, proc, data.toString());
41×
39
  });
40

41
  child.on('close', function(code) {
40×
42
    if(code === 0) {
40×
43
      cons.info(key, proc, "Exited Successfully");
13×
44
    } else {
45
      cons.error(key, proc, "Exited with exit code " + code);
27×
46
    }
47
  });
48

49
  child.on('exit', function(code, signal) {
40×
50
    emitter.emit('killall', signal);
40×
51
  });
52

53
  emitter.on('killall', function(signal) {
40×
54
    try {
100×
55
      child.kill(signal);
100×
56
    } catch(_e) {
NEW
57
      cons.Warn('Proxy server already dead');
!
58
    }
59
  });
60

61
}
62

63
// Run a Specific Process Once using the ENV variables
64
// from the .env file
65
function once(input, envs, callback) {
75×
66
  var file, args;
10×
67
  var proc = {
10×
68
    command : input,
69
    env     : merge(merge({}, process.env), envs)
70
  };
71

72
  if (platform === 'win32') {
10×
73
    file = process.env.comspec || 'cmd.exe';
!
74
    args = ['/s', '/c', proc.command];
!
75
  } else {
76
    file = '/bin/sh';
10×
77
    args = ['-c', proc.command];
10×
78
  }
79

80
  var child = prog.spawn(file, args, { env: proc.env, stdio: 'inherit' });
10×
81

82
  child.on('close', function(code) {
10×
83
    callback(code);
10×
84
  });
85
}
86

87
// Figure Out What to Start Based on Procfile Processes
88
// And Requirements Passed as Command Line Arguments
89
//
90
// e.g. web=2,api=3 are requirements
91
function start(procs, requirements, envs, portarg, emitter){
75×
92

93
  var j = 0;
20×
94
  var k = 0;
20×
95
  var port = parseInt(portarg);
20×
96

97
  if(port < 1024) {
20×
98
    return cons.Error('Only Proxies Can Bind to Privileged Ports - '+
!
99
                      'Try \'sudo nf start -x %s\'', port);
100
  }
101

102

103
  for(var key in requirements) {
20×
104
    var n = parseInt(requirements[key]);
40×
105

106
    for(var i = 0; i < n; i++) {
40×
107

108
      var color_val = (j + k) % colors_max;
40×
109

110
      if (!procs[key]) {
40×
111
        cons.Warn("Required Key '%s' Does Not Exist in Procfile Definition", key);
!
112
        continue;
!
113
      }
114

115
      var p = {
40×
116
        command : procs[key],
117
        color   : colors[color_val],
118
        env     : merge(merge({}, process.env), envs)
119
      };
120

121
      p.env.PORT = port + j + k * 100;
40×
122
      p.env.FOREMAN_WORKER_NAME = p.env.FOREMAN_WORKER_NAME || key + "." + (i + 1);
40×
123

124
      run(key + "." + (i + 1), p, emitter);
40×
125

126
      j++;
40×
127

128
    }
129
    j = 0;
40×
130
    k++;
40×
131
  }
132
}
133

134
// Merge object b into object a
135
function merge(a, b) {
75×
136
  if (a && b) {
100×
137
    for (var key in b) {
95×
138
      a[key] = b[key];
10,585×
139
    }
140
  }
141
  return a;
100×
142
}
143

144
module.exports.start = start;
75×
145
module.exports.run   = run;
75×
146
module.exports.once  = once;
75×
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc