Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / fileAppender-test.js
1 "use strict";
2 var vows = require('vows')
3 , fs = require('fs')
4 , path = require('path')
5 , sandbox = require('sandboxed-module')
6 , log4js = require('../lib/log4js')
7 , assert = require('assert')
8 , zlib = require('zlib')
9 , EOL = require('os').EOL || '\n';
10
11 log4js.clearAppenders();
12
13 function remove(filename) {
14   try {
15     fs.unlinkSync(filename);
16   } catch (e) {
17     //doesn't really matter if it failed
18   }
19 }
20
21 vows.describe('log4js fileAppender').addBatch({
22   'adding multiple fileAppenders': {
23     topic: function () {
24       var listenersCount = process.listeners('exit').length
25       , logger = log4js.getLogger('default-settings')
26       , count = 5, logfile;
27
28       while (count--) {
29         logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
30         log4js.addAppender(require('../lib/appenders/file').appender(logfile), 'default-settings');
31       }
32
33       return listenersCount;
34     },
35
36     'does not add more than one `exit` listeners': function (initialCount) {
37       assert.ok(process.listeners('exit').length <= initialCount + 1);
38     }
39   },
40
41   'exit listener': {
42     topic: function() {
43       var exitListener
44       , openedFiles = []
45       , fileAppender = sandbox.require(
46         '../lib/appenders/file',
47         {
48           globals: {
49             process: {
50               on: function(evt, listener) {
51                 exitListener = listener;
52               }
53             }
54           },
55           requires: {
56             '../streams': {
57               RollingFileStream: function(filename) {
58                 openedFiles.push(filename);
59
60                 this.end = function() {
61                   openedFiles.shift();
62                 };
63
64                 this.on = function() {};
65               }
66             }
67           }
68         }
69       );
70       for (var i=0; i < 5; i += 1) {
71         fileAppender.appender('test' + i, null, 100);
72       }
73       assert.isNotEmpty(openedFiles);
74       exitListener();
75       return openedFiles;
76     },
77     'should close all open files': function(openedFiles) {
78       assert.isEmpty(openedFiles);
79     }
80   },
81
82   'with default fileAppender settings': {
83     topic: function() {
84       var that = this
85       , testFile = path.join(__dirname, '/fa-default-test.log')
86       , logger = log4js.getLogger('default-settings');
87       remove(testFile);
88
89       log4js.clearAppenders();
90       log4js.addAppender(require('../lib/appenders/file').appender(testFile), 'default-settings');
91
92       logger.info("This should be in the file.");
93
94       setTimeout(function() {
95         fs.readFile(testFile, "utf8", that.callback);
96       }, 100);
97     },
98     'should write log messages to the file': function (err, fileContents) {
99       assert.include(fileContents, "This should be in the file." + EOL);
100     },
101     'log messages should be in the basic layout format': function(err, fileContents) {
102       assert.match(
103         fileContents,
104           /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
105       );
106     }
107   },
108   'fileAppender subcategories': {
109     topic: function() {
110       var that = this;
111
112       log4js.clearAppenders();
113
114       function addAppender(cat) {
115         var testFile = path.join(
116           __dirname,
117           '/fa-subcategories-test-'+cat.join('-').replace(/\./g, "_")+'.log'
118         );
119         remove(testFile);
120         log4js.addAppender(require('../lib/appenders/file').appender(testFile), cat);
121         return testFile;
122       }
123
124       var file_sub1 = addAppender([ 'sub1']);
125
126       var file_sub1_sub12$sub1_sub13 = addAppender([ 'sub1.sub12', 'sub1.sub13' ]);
127
128       var file_sub1_sub12 = addAppender([ 'sub1.sub12' ]);
129
130
131       var logger_sub1_sub12_sub123 = log4js.getLogger('sub1.sub12.sub123');
132
133       var logger_sub1_sub13_sub133 = log4js.getLogger('sub1.sub13.sub133');
134
135       var logger_sub1_sub14 = log4js.getLogger('sub1.sub14');
136
137       var logger_sub2 = log4js.getLogger('sub2');
138
139
140       logger_sub1_sub12_sub123.info('sub1_sub12_sub123');
141
142       logger_sub1_sub13_sub133.info('sub1_sub13_sub133');
143
144       logger_sub1_sub14.info('sub1_sub14');
145
146       logger_sub2.info('sub2');
147
148
149       setTimeout(function() {
150         that.callback(null, {
151           file_sub1: fs.readFileSync(file_sub1).toString(),
152           file_sub1_sub12$sub1_sub13: fs.readFileSync(file_sub1_sub12$sub1_sub13).toString(),
153           file_sub1_sub12: fs.readFileSync(file_sub1_sub12).toString()
154         });
155       }, 3000);
156     },
157     'check file contents': function (err, fileContents) {
158
159       // everything but category 'sub2'
160       assert.match(
161         fileContents.file_sub1,
162         /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] (sub1.sub12.sub123 - sub1_sub12_sub123|sub1.sub13.sub133 - sub1_sub13_sub133|sub1.sub14 - sub1_sub14)[\s\S]){3}$/ // jshint ignore:line
163       );
164       assert.ok(
165         fileContents.file_sub1.match(/sub123/) &&
166         fileContents.file_sub1.match(/sub133/) &&
167         fileContents.file_sub1.match(/sub14/)
168       );
169       assert.ok(!fileContents.file_sub1.match(/sub2/));
170
171       // only catgories starting with 'sub1.sub12' and 'sub1.sub13'
172       assert.match(
173         fileContents.file_sub1_sub12$sub1_sub13,
174         /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] (sub1.sub12.sub123 - sub1_sub12_sub123|sub1.sub13.sub133 - sub1_sub13_sub133)[\s\S]){2}$/ //jshint ignore:line
175       );
176       assert.ok(
177         fileContents.file_sub1_sub12$sub1_sub13.match(/sub123/) &&
178         fileContents.file_sub1_sub12$sub1_sub13.match(/sub133/)
179       );
180       assert.ok(!fileContents.file_sub1_sub12$sub1_sub13.match(/sub14|sub2/));
181
182       // only catgories starting with 'sub1.sub12'
183       assert.match(
184         fileContents.file_sub1_sub12,
185         /^(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] (sub1.sub12.sub123 - sub1_sub12_sub123)[\s\S]){1}$/ //jshint ignore:line
186       );
187       assert.ok(!fileContents.file_sub1_sub12.match(/sub14|sub2|sub13/));
188
189     }
190   },
191   'with a max file size and no backups': {
192     topic: function() {
193       var testFile = path.join(__dirname, '/fa-maxFileSize-test.log')
194       , logger = log4js.getLogger('max-file-size')
195       , that = this;
196       remove(testFile);
197       remove(testFile + '.1');
198       //log file of 100 bytes maximum, no backups
199       log4js.clearAppenders();
200       log4js.addAppender(
201         require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 100, 0),
202         'max-file-size'
203       );
204       logger.info("This is the first log message.");
205       logger.info("This is an intermediate log message.");
206       logger.info("This is the second log message.");
207       //wait for the file system to catch up
208       setTimeout(function() {
209         fs.readFile(testFile, "utf8", that.callback);
210       }, 100);
211     },
212     'log file should only contain the second message': function(err, fileContents) {
213       assert.include(fileContents, "This is the second log message.");
214       assert.equal(fileContents.indexOf("This is the first log message."), -1);
215     },
216     'the number of files': {
217       topic: function() {
218         fs.readdir(__dirname, this.callback);
219       },
220       'starting with the test file name should be two': function(err, files) {
221         //there will always be one backup if you've specified a max log size
222         var logFiles = files.filter(
223           function(file) { return file.indexOf('fa-maxFileSize-test.log') > -1; }
224         );
225         assert.equal(logFiles.length, 2);
226       }
227     }
228   },
229   'with a max file size and 2 backups': {
230     topic: function() {
231       var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-test.log')
232       , logger = log4js.getLogger('max-file-size-backups');
233       remove(testFile);
234       remove(testFile+'.1');
235       remove(testFile+'.2');
236
237       //log file of 50 bytes maximum, 2 backups
238       log4js.clearAppenders();
239       log4js.addAppender(
240         require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 50, 2),
241         'max-file-size-backups'
242       );
243       logger.info("This is the first log message.");
244       logger.info("This is the second log message.");
245       logger.info("This is the third log message.");
246       logger.info("This is the fourth log message.");
247       var that = this;
248       //give the system a chance to open the stream
249       setTimeout(function() {
250         fs.readdir(__dirname, function(err, files) {
251           if (files) {
252             that.callback(null, files.sort());
253           } else {
254             that.callback(err, files);
255           }
256         });
257       }, 200);
258     },
259     'the log files': {
260       topic: function(files) {
261         var logFiles = files.filter(
262           function(file) { return file.indexOf('fa-maxFileSize-with-backups-test.log') > -1; }
263         );
264         return logFiles;
265       },
266       'should be 3': function (files) {
267         assert.equal(files.length, 3);
268       },
269       'should be named in sequence': function (files) {
270         assert.deepEqual(files, [
271           'fa-maxFileSize-with-backups-test.log',
272           'fa-maxFileSize-with-backups-test.log.1',
273           'fa-maxFileSize-with-backups-test.log.2'
274         ]);
275       },
276       'and the contents of the first file': {
277         topic: function(logFiles) {
278           fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
279         },
280         'should be the last log message': function(contents) {
281           assert.include(contents, 'This is the fourth log message.');
282         }
283       },
284       'and the contents of the second file': {
285         topic: function(logFiles) {
286           fs.readFile(path.join(__dirname, logFiles[1]), "utf8", this.callback);
287         },
288         'should be the third log message': function(contents) {
289           assert.include(contents, 'This is the third log message.');
290         }
291       },
292       'and the contents of the third file': {
293         topic: function(logFiles) {
294           fs.readFile(path.join(__dirname, logFiles[2]), "utf8", this.callback);
295         },
296         'should be the second log message': function(contents) {
297           assert.include(contents, 'This is the second log message.');
298         }
299       }
300     }
301   },
302   'with a max file size and 2 compressed backups': {
303     topic: function() {
304       var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-compressed-test.log')
305       , logger = log4js.getLogger('max-file-size-backups');
306       remove(testFile);
307       remove(testFile+'.1.gz');
308       remove(testFile+'.2.gz');
309
310       //log file of 50 bytes maximum, 2 backups
311       log4js.clearAppenders();
312       log4js.addAppender(
313         require('../lib/appenders/file').appender(
314           testFile, log4js.layouts.basicLayout, 50, 2, true
315         ),
316         'max-file-size-backups'
317       );
318       logger.info("This is the first log message.");
319       logger.info("This is the second log message.");
320       logger.info("This is the third log message.");
321       logger.info("This is the fourth log message.");
322       var that = this;
323       //give the system a chance to open the stream
324       setTimeout(function() {
325         fs.readdir(__dirname, function(err, files) {
326           if (files) {
327             that.callback(null, files.sort());
328           } else {
329             that.callback(err, files);
330           }
331         });
332       }, 1000);
333     },
334     'the log files': {
335       topic: function(files) {
336         var logFiles = files.filter(
337           function(file) {
338             return file.indexOf('fa-maxFileSize-with-backups-compressed-test.log') > -1;
339           }
340         );
341         return logFiles;
342       },
343       'should be 3': function (files) {
344         assert.equal(files.length, 3);
345       },
346       'should be named in sequence': function (files) {
347         assert.deepEqual(files, [
348           'fa-maxFileSize-with-backups-compressed-test.log',
349           'fa-maxFileSize-with-backups-compressed-test.log.1.gz',
350           'fa-maxFileSize-with-backups-compressed-test.log.2.gz'
351         ]);
352       },
353       'and the contents of the first file': {
354         topic: function(logFiles) {
355           fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
356         },
357         'should be the last log message': function(contents) {
358           assert.include(contents, 'This is the fourth log message.');
359         }
360       },
361       'and the contents of the second file': {
362         topic: function(logFiles) {
363           zlib.gunzip(fs.readFileSync(path.join(__dirname, logFiles[1])), this.callback);
364         },
365         'should be the third log message': function(contents) {
366           assert.include(contents.toString('utf8'), 'This is the third log message.');
367         }
368       },
369       'and the contents of the third file': {
370         topic: function(logFiles) {
371           zlib.gunzip(fs.readFileSync(path.join(__dirname, logFiles[2])), this.callback);
372         },
373         'should be the second log message': function(contents) {
374           assert.include(contents.toString('utf8'), 'This is the second log message.');
375         }
376       }
377     }
378   }
379 }).addBatch({
380   'configure' : {
381     'with fileAppender': {
382       topic: function() {
383         var log4js = require('../lib/log4js')
384         , logger;
385         //this config file defines one file appender (to ./tmp-tests.log)
386         //and sets the log level for "tests" to WARN
387         log4js.configure('./test/log4js.json');
388         logger = log4js.getLogger('tests');
389         logger.info('this should not be written to the file');
390         logger.warn('this should be written to the file');
391
392         fs.readFile('tmp-tests.log', 'utf8', this.callback);
393       },
394       'should load appender configuration from a json file': function (err, contents) {
395         assert.include(contents, 'this should be written to the file' + EOL);
396         assert.equal(contents.indexOf('this should not be written to the file'), -1);
397       }
398     }
399   }
400 }).addBatch({
401   'when underlying stream errors': {
402     topic: function() {
403       var consoleArgs
404       , errorHandler
405       , fileAppender = sandbox.require(
406         '../lib/appenders/file',
407         {
408           globals: {
409             console: {
410               error: function() {
411                 consoleArgs = Array.prototype.slice.call(arguments);
412               }
413             }
414           },
415           requires: {
416             '../streams': {
417               RollingFileStream: function(filename) {
418
419                 this.end = function() {};
420                 this.on = function(evt, cb) {
421                   if (evt === 'error') {
422                     errorHandler = cb;
423                   }
424                 };
425               }
426             }
427           }
428         }
429       );
430       fileAppender.appender('test1.log', null, 100);
431       errorHandler({ error: 'aargh' });
432       return consoleArgs;
433     },
434     'should log the error to console.error': function(consoleArgs) {
435       assert.isNotEmpty(consoleArgs);
436       assert.equal(consoleArgs[0], 'log4js.fileAppender - Writing to file %s, error happened ');
437       assert.equal(consoleArgs[1], 'test1.log');
438       assert.equal(consoleArgs[2].error, 'aargh');
439     }
440   }
441
442 }).export(module);