Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / gelfAppender-test.js
1 "use strict";
2 var vows = require('vows')
3 , assert = require('assert')
4 , sandbox = require('sandboxed-module')
5 , log4js = require('../lib/log4js')
6 , realLayouts = require('../lib/layouts')
7 , setupLogging = function(options, category, compressedLength) {
8   var fakeDgram = {
9     sent: false,
10     socket: {
11       packetLength: 0,
12       closed: false,
13       close: function() {
14         this.closed = true;
15       },
16       send: function(pkt, offset, pktLength, port, host) {
17         fakeDgram.sent = true;
18         this.packet = pkt;
19         this.offset = offset;
20         this.packetLength = pktLength;
21         this.port = port;
22         this.host = host;
23       }
24     },
25     createSocket: function(type) {
26       this.type = type;
27       return this.socket;
28     }
29   }
30   , fakeZlib = {
31     gzip: function(objectToCompress, callback) {
32       fakeZlib.uncompressed = objectToCompress;
33       if (this.shouldError) {
34         callback({ stack: "oh noes" });
35         return;
36       }
37
38       if (compressedLength) {
39         callback(null, { length: compressedLength });
40       } else {
41         callback(null, "I've been compressed");
42       }
43     }
44   }
45   , exitHandler
46   , fakeConsole = {
47     error: function(message) {
48       this.message = message;
49     }
50   }
51   , fakeLayouts = {
52     layout: function(type, options) {
53       this.type = type;
54       this.options = options;
55       return realLayouts.messagePassThroughLayout;
56     },
57     messagePassThroughLayout: realLayouts.messagePassThroughLayout
58   }
59   , appender = sandbox.require('../lib/appenders/gelf', {
60     requires: {
61       dgram: fakeDgram,
62       zlib: fakeZlib,
63       '../layouts': fakeLayouts
64     },
65     globals: {
66       process: {
67         on: function(evt, handler) {
68           if (evt === 'exit') {
69             exitHandler = handler;
70           }
71         }
72       },
73       console: fakeConsole
74     }
75   });
76
77   log4js.clearAppenders();
78   log4js.addAppender(appender.configure(options || {}), category || "gelf-test");
79   return {
80     dgram: fakeDgram,
81     compress: fakeZlib,
82     exitHandler: exitHandler,
83     console: fakeConsole,
84     layouts: fakeLayouts,
85     logger: log4js.getLogger(category || "gelf-test")
86   };
87 };
88
89 vows.describe('log4js gelfAppender').addBatch({
90
91   'with default gelfAppender settings': {
92     topic: function() {
93       var setup = setupLogging();
94       setup.logger.info("This is a test");
95       return setup;
96     },
97     'the dgram packet': {
98       topic: function(setup) {
99         return setup.dgram;
100       },
101       'should be sent via udp to the localhost gelf server': function(dgram) {
102         assert.equal(dgram.type, "udp4");
103         assert.equal(dgram.socket.host, "localhost");
104         assert.equal(dgram.socket.port, 12201);
105         assert.equal(dgram.socket.offset, 0);
106         assert.ok(dgram.socket.packetLength > 0, "Received blank message");
107       },
108       'should be compressed': function(dgram) {
109         assert.equal(dgram.socket.packet, "I've been compressed");
110       }
111     },
112     'the uncompressed log message': {
113       topic: function(setup) {
114         var message = JSON.parse(setup.compress.uncompressed);
115         return message;
116       },
117       'should be in the gelf format': function(message) {
118         assert.equal(message.version, '1.1');
119         assert.equal(message.host, require('os').hostname());
120         assert.equal(message.level, 6); //INFO
121         assert.equal(message.short_message, 'This is a test');
122       }
123     }
124   },
125   'with a message longer than 8k': {
126     topic: function() {
127       var setup = setupLogging(undefined, undefined, 10240);
128       setup.logger.info("Blah.");
129       return setup;
130     },
131     'the dgram packet': {
132       topic: function(setup) {
133         return setup.dgram;
134       },
135       'should not be sent': function(dgram) {
136         assert.equal(dgram.sent, false);
137       }
138     }
139   },
140   'with non-default options': {
141     topic: function() {
142       var setup = setupLogging({
143         host: 'somewhere',
144         port: 12345,
145         hostname: 'cheese',
146         facility: 'nonsense'
147       });
148       setup.logger.debug("Just testing.");
149       return setup;
150     },
151     'the dgram packet': {
152       topic: function(setup) {
153         return setup.dgram;
154       },
155       'should pick up the options': function(dgram) {
156         assert.equal(dgram.socket.host, 'somewhere');
157         assert.equal(dgram.socket.port, 12345);
158       }
159     },
160     'the uncompressed packet': {
161       topic: function(setup) {
162         var message = JSON.parse(setup.compress.uncompressed);
163         return message;
164       },
165       'should pick up the options': function(message) {
166         assert.equal(message.host, 'cheese');
167         assert.equal(message._facility, 'nonsense');
168       }
169     }
170   },
171
172   'on process.exit': {
173     topic: function() {
174       var setup = setupLogging();
175       setup.exitHandler();
176       return setup;
177     },
178     'should close open sockets': function(setup) {
179       assert.isTrue(setup.dgram.socket.closed);
180     }
181   },
182
183   'on zlib error': {
184     topic: function() {
185       var setup = setupLogging();
186       setup.compress.shouldError = true;
187       setup.logger.info('whatever');
188       return setup;
189     },
190     'should output to console.error': function(setup) {
191       assert.equal(setup.console.message, 'oh noes');
192     }
193   },
194
195   'with layout in configuration': {
196     topic: function() {
197       var setup = setupLogging({
198         layout: {
199           type: 'madeuplayout',
200           earlgrey: 'yes, please'
201         }
202       });
203       return setup;
204     },
205     'should pass options to layout': function(setup) {
206       assert.equal(setup.layouts.type, 'madeuplayout');
207       assert.equal(setup.layouts.options.earlgrey, 'yes, please');
208     }
209   },
210
211   'with custom fields options': {
212     topic: function() {
213       var setup = setupLogging({
214         host: 'somewhere',
215         port: 12345,
216         hostname: 'cheese',
217         facility: 'nonsense',
218         customFields: {
219           _every1: 'Hello every one',
220           _every2: 'Hello every two'
221         }
222       });
223       var myFields = {
224         GELF: true,
225         _every2: 'Overwritten!',
226         _myField: 'This is my field!'
227       };
228       setup.logger.debug(myFields, "Just testing.");
229       return setup;
230     },
231     'the dgram packet': {
232       topic: function(setup) {
233         return setup.dgram;
234       },
235       'should pick up the options': function(dgram) {
236         assert.equal(dgram.socket.host, 'somewhere');
237         assert.equal(dgram.socket.port, 12345);
238       }
239     },
240     'the uncompressed packet': {
241       topic: function(setup) {
242         var message = JSON.parse(setup.compress.uncompressed);
243         return message;
244       },
245       'should pick up the options': function(message) {
246         assert.equal(message.host, 'cheese');
247         assert.isUndefined(message.GELF); // make sure flag was removed
248         assert.equal(message._facility, 'nonsense');
249         assert.equal(message._every1, 'Hello every one'); // the default value
250         assert.equal(message._every2, 'Overwritten!'); // the overwritten value
251         assert.equal(message._myField, 'This is my field!'); // the value for this message only
252         assert.equal(message.short_message, 'Just testing.'); // skip the field object
253       }
254     }
255   }
256
257 }).export(module);