aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/log4js/test/gelfAppender-test.js
blob: 76fb5ea3bc442f2cc9e8c92ad45b3eded4c079af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"use strict";
var vows = require('vows')
, assert = require('assert')
, sandbox = require('sandboxed-module')
, log4js = require('../lib/log4js')
, realLayouts = require('../lib/layouts')
, setupLogging = function(options, category, compressedLength) {
  var fakeDgram = {
    sent: false,
    socket: {
      packetLength: 0,
      closed: false,
      close: function() {
        this.closed = true;
      },
      send: function(pkt, offset, pktLength, port, host) {
        fakeDgram.sent = true;
        this.packet = pkt;
        this.offset = offset;
        this.packetLength = pktLength;
        this.port = port;
        this.host = host;
      }
    },
    createSocket: function(type) {
      this.type = type;
      return this.socket;
    }
  }
  , fakeZlib = {
    gzip: function(objectToCompress, callback) {
      fakeZlib.uncompressed = objectToCompress;
      if (this.shouldError) {
        callback({ stack: "oh noes" });
        return;
      }

      if (compressedLength) {
        callback(null, { length: compressedLength });
      } else {
        callback(null, "I've been compressed");
      }
    }
  }
  , exitHandler
  , fakeConsole = {
    error: function(message) {
      this.message = message;
    }
  }
  , fakeLayouts = {
    layout: function(type, options) {
      this.type = type;
      this.options = options;
      return realLayouts.messagePassThroughLayout;
    },
    messagePassThroughLayout: realLayouts.messagePassThroughLayout
  }
  , appender = sandbox.require('../lib/appenders/gelf', {
    requires: {
      dgram: fakeDgram,
      zlib: fakeZlib,
      '../layouts': fakeLayouts
    },
    globals: {
      process: {
        on: function(evt, handler) {
          if (evt === 'exit') {
            exitHandler = handler;
          }
        }
      },
      console: fakeConsole
    }
  });

  log4js.clearAppenders();
  log4js.addAppender(appender.configure(options || {}), category || "gelf-test");
  return {
    dgram: fakeDgram,
    compress: fakeZlib,
    exitHandler: exitHandler,
    console: fakeConsole,
    layouts: fakeLayouts,
    logger: log4js.getLogger(category || "gelf-test")
  };
};

vows.describe('log4js gelfAppender').addBatch({

  'with default gelfAppender settings': {
    topic: function() {
      var setup = setupLogging();
      setup.logger.info("This is a test");
      return setup;
    },
    'the dgram packet': {
      topic: function(setup) {
        return setup.dgram;
      },
      'should be sent via udp to the localhost gelf server': function(dgram) {
        assert.equal(dgram.type, "udp4");
        assert.equal(dgram.socket.host, "localhost");
        assert.equal(dgram.socket.port, 12201);
        assert.equal(dgram.socket.offset, 0);
        assert.ok(dgram.socket.packetLength > 0, "Received blank message");
      },
      'should be compressed': function(dgram) {
        assert.equal(dgram.socket.packet, "I've been compressed");
      }
    },
    'the uncompressed log message': {
      topic: function(setup) {
        var message = JSON.parse(setup.compress.uncompressed);
        return message;
      },
      'should be in the gelf format': function(message) {
        assert.equal(message.version, '1.1');
        assert.equal(message.host, require('os').hostname());
        assert.equal(message.level, 6); //INFO
        assert.equal(message.short_message, 'This is a test');
      }
    }
  },
  'with a message longer than 8k': {
    topic: function() {
      var setup = setupLogging(undefined, undefined, 10240);
      setup.logger.info("Blah.");
      return setup;
    },
    'the dgram packet': {
      topic: function(setup) {
        return setup.dgram;
      },
      'should not be sent': function(dgram) {
        assert.equal(dgram.sent, false);
      }
    }
  },
  'with non-default options': {
    topic: function() {
      var setup = setupLogging({
        host: 'somewhere',
        port: 12345,
        hostname: 'cheese',
        facility: 'nonsense'
      });
      setup.logger.debug("Just testing.");
      return setup;
    },
    'the dgram packet': {
      topic: function(setup) {
        return setup.dgram;
      },
      'should pick up the options': function(dgram) {
        assert.equal(dgram.socket.host, 'somewhere');
        assert.equal(dgram.socket.port, 12345);
      }
    },
    'the uncompressed packet': {
      topic: function(setup) {
        var message = JSON.parse(setup.compress.uncompressed);
        return message;
      },
      'should pick up the options': function(message) {
        assert.equal(message.host, 'cheese');
        assert.equal(message._facility, 'nonsense');
      }
    }
  },

  'on process.exit': {
    topic: function() {
      var setup = setupLogging();
      setup.exitHandler();
      return setup;
    },
    'should close open sockets': function(setup) {
      assert.isTrue(setup.dgram.socket.closed);
    }
  },

  'on zlib error': {
    topic: function() {
      var setup = setupLogging();
      setup.compress.shouldError = true;
      setup.logger.info('whatever');
      return setup;
    },
    'should output to console.error': function(setup) {
      assert.equal(setup.console.message, 'oh noes');
    }
  },

  'with layout in configuration': {
    topic: function() {
      var setup = setupLogging({
        layout: {
          type: 'madeuplayout',
          earlgrey: 'yes, please'
        }
      });
      return setup;
    },
    'should pass options to layout': function(setup) {
      assert.equal(setup.layouts.type, 'madeuplayout');
      assert.equal(setup.layouts.options.earlgrey, 'yes, please');
    }
  },

  'with custom fields options': {
    topic: function() {
      var setup = setupLogging({
        host: 'somewhere',
        port: 12345,
        hostname: 'cheese',
        facility: 'nonsense',
        customFields: {
          _every1: 'Hello every one',
          _every2: 'Hello every two'
        }
      });
      var myFields = {
        GELF: true,
        _every2: 'Overwritten!',
        _myField: 'This is my field!'
      };
      setup.logger.debug(myFields, "Just testing.");
      return setup;
    },
    'the dgram packet': {
      topic: function(setup) {
        return setup.dgram;
      },
      'should pick up the options': function(dgram) {
        assert.equal(dgram.socket.host, 'somewhere');
        assert.equal(dgram.socket.port, 12345);
      }
    },
    'the uncompressed packet': {
      topic: function(setup) {
        var message = JSON.parse(setup.compress.uncompressed);
        return message;
      },
      'should pick up the options': function(message) {
        assert.equal(message.host, 'cheese');
        assert.isUndefined(message.GELF); // make sure flag was removed
        assert.equal(message._facility, 'nonsense');
        assert.equal(message._every1, 'Hello every one'); // the default value
        assert.equal(message._every2, 'Overwritten!'); // the overwritten value
        assert.equal(message._myField, 'This is my field!'); // the value for this message only
        assert.equal(message.short_message, 'Just testing.'); // skip the field object
      }
    }
  }

}).export(module);