Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / fileSyncAppender-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 , EOL = require('os').EOL || '\n';
9
10 log4js.clearAppenders();
11
12 function remove(filename) {
13   try {
14     fs.unlinkSync(filename);
15   } catch (e) {
16     //doesn't really matter if it failed
17   }
18 }
19
20 vows.describe('log4js fileSyncAppender').addBatch({
21   'with default fileSyncAppender settings': {
22     topic: function() {
23       var that = this
24       , testFile = path.join(__dirname, '/fa-default-sync-test.log')
25       , logger = log4js.getLogger('default-settings');
26       remove(testFile);
27
28       log4js.clearAppenders();
29       log4js.addAppender(
30         require('../lib/appenders/fileSync').appender(testFile),
31         'default-settings'
32       );
33
34       logger.info("This should be in the file.");
35
36       fs.readFile(testFile, "utf8", that.callback);
37     },
38     'should write log messages to the file': function (err, fileContents) {
39       assert.include(fileContents, "This should be in the file." + EOL);
40     },
41     'log messages should be in the basic layout format': function(err, fileContents) {
42       assert.match(
43         fileContents,
44           /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
45       );
46     }
47   },
48   'with a max file size and no backups': {
49     topic: function() {
50       var testFile = path.join(__dirname, '/fa-maxFileSize-sync-test.log')
51       , logger = log4js.getLogger('max-file-size')
52       , that = this;
53       remove(testFile);
54       remove(testFile + '.1');
55       //log file of 100 bytes maximum, no backups
56       log4js.clearAppenders();
57       log4js.addAppender(
58         require('../lib/appenders/fileSync').appender(testFile, log4js.layouts.basicLayout, 100, 0),
59         'max-file-size'
60       );
61       logger.info("This is the first log message.");
62       logger.info("This is an intermediate log message.");
63       logger.info("This is the second log message.");
64
65       fs.readFile(testFile, "utf8", that.callback);
66     },
67     'log file should only contain the second message': function (err, fileContents) {
68       assert.include(fileContents, "This is the second log message." + EOL);
69       assert.equal(fileContents.indexOf("This is the first log message."), -1);
70     },
71     'the number of files': {
72       topic: function() {
73         fs.readdir(__dirname, this.callback);
74       },
75       'starting with the test file name should be two': function(err, files) {
76         //there will always be one backup if you've specified a max log size
77         var logFiles = files.filter(
78           function(file) { return file.indexOf('fa-maxFileSize-sync-test.log') > -1; }
79         );
80         assert.equal(logFiles.length, 2);
81       }
82     }
83   },
84   'with a max file size and 2 backups': {
85     topic: function() {
86       var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-sync-test.log')
87       , logger = log4js.getLogger('max-file-size-backups');
88       remove(testFile);
89       remove(testFile+'.1');
90       remove(testFile+'.2');
91
92       //log file of 50 bytes maximum, 2 backups
93       log4js.clearAppenders();
94       log4js.addAppender(
95         require('../lib/appenders/fileSync').appender(testFile, log4js.layouts.basicLayout, 50, 2),
96         'max-file-size-backups'
97       );
98       logger.info("This is the first log message.");
99       logger.info("This is the second log message.");
100       logger.info("This is the third log message.");
101       logger.info("This is the fourth log message.");
102       var that = this;
103
104       fs.readdir(__dirname, function(err, files) {
105         if (files) {
106           that.callback(null, files.sort());
107         } else {
108           that.callback(err, files);
109         }
110       });
111     },
112     'the log files': {
113       topic: function(files) {
114         var logFiles = files.filter(
115           function(file) { return file.indexOf('fa-maxFileSize-with-backups-sync-test.log') > -1; }
116         );
117         return logFiles;
118       },
119       'should be 3': function (files) {
120         assert.equal(files.length, 3);
121       },
122       'should be named in sequence': function (files) {
123         assert.deepEqual(files, [
124           'fa-maxFileSize-with-backups-sync-test.log',
125           'fa-maxFileSize-with-backups-sync-test.log.1',
126           'fa-maxFileSize-with-backups-sync-test.log.2'
127         ]);
128       },
129       'and the contents of the first file': {
130         topic: function(logFiles) {
131           fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
132         },
133         'should be the last log message': function(contents) {
134           assert.include(contents, 'This is the fourth log message.');
135         }
136       },
137       'and the contents of the second file': {
138         topic: function(logFiles) {
139           fs.readFile(path.join(__dirname, logFiles[1]), "utf8", this.callback);
140         },
141         'should be the third log message': function(contents) {
142           assert.include(contents, 'This is the third log message.');
143         }
144       },
145       'and the contents of the third file': {
146         topic: function(logFiles) {
147           fs.readFile(path.join(__dirname, logFiles[2]), "utf8", this.callback);
148         },
149         'should be the second log message': function(contents) {
150           assert.include(contents, 'This is the second log message.');
151         }
152       }
153     }
154   }
155 }).addBatch({
156   'configure' : {
157     'with fileSyncAppender': {
158       topic: function() {
159         var log4js = require('../lib/log4js')
160         , logger;
161         //this config defines one file appender (to ./tmp-sync-tests.log)
162         //and sets the log level for "tests" to WARN
163         log4js.configure({
164             appenders: [{
165                 category: "tests",
166                 type: "file",
167                 filename: "tmp-sync-tests.log",
168                 layout: { type: "messagePassThrough" }
169             }],
170
171             levels: { tests:  "WARN" }
172         });
173         logger = log4js.getLogger('tests');
174         logger.info('this should not be written to the file');
175         logger.warn('this should be written to the file');
176
177         fs.readFile('tmp-sync-tests.log', 'utf8', this.callback);
178       },
179       'should load appender configuration from a json file': function(err, contents) {
180         assert.include(contents, 'this should be written to the file' + EOL);
181         assert.equal(contents.indexOf('this should not be written to the file'), -1);
182       }
183     }
184   }
185 }).export(module);