Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / reloadConfiguration-test.js
1 "use strict";
2 var vows = require('vows')
3 , assert = require('assert')
4 , sandbox = require('sandboxed-module');
5
6 function setupConsoleTest() {
7   var fakeConsole = {}
8   , logEvents = []
9   , log4js;
10   
11   ['trace','debug','log','info','warn','error'].forEach(function(fn) {
12     fakeConsole[fn] = function() {
13       throw new Error("this should not be called.");
14     };
15   });
16
17   log4js = sandbox.require(
18     '../lib/log4js', 
19     {
20       globals: {
21         console: fakeConsole
22       }
23     }
24   );
25
26   log4js.clearAppenders();
27   log4js.addAppender(function(evt) {
28     logEvents.push(evt);
29   });
30
31   return { log4js: log4js, logEvents: logEvents, fakeConsole: fakeConsole };
32 }
33
34 vows.describe('reload configuration').addBatch({
35   'with config file changing' : {
36     topic: function() {
37       var pathsChecked = [],
38       logEvents = [],
39       logger,
40       modulePath = 'path/to/log4js.json',
41       fakeFS = {
42         lastMtime: Date.now(),
43         config: { 
44           appenders: [ 
45             { type: 'console', layout: { type: 'messagePassThrough' } } 
46           ],
47           levels: { 'a-test' : 'INFO' } 
48         },
49         readFileSync: function (file, encoding) {
50           assert.equal(file, modulePath);
51           assert.equal(encoding, 'utf8');
52           return JSON.stringify(fakeFS.config);
53         },
54         statSync: function (path) {
55           pathsChecked.push(path);
56           if (path === modulePath) {
57             fakeFS.lastMtime += 1;
58             return { mtime: new Date(fakeFS.lastMtime) };
59           } else {
60             throw new Error("no such file");
61           }
62         }
63       },
64       fakeConsole = {
65         'name': 'console',
66         'appender': function () {
67           return function(evt) { logEvents.push(evt); };
68         },
69         'configure': function (config) {
70           return fakeConsole.appender();
71         }
72       },
73       setIntervalCallback,
74       fakeSetInterval = function(cb, timeout) {
75         setIntervalCallback = cb;
76       },
77       log4js = sandbox.require(
78         '../lib/log4js',
79         {
80           requires: {
81             'fs': fakeFS,
82             './appenders/console': fakeConsole
83           },
84           globals: {
85             'console': fakeConsole,
86             'setInterval' : fakeSetInterval,
87           }
88         }
89       );
90       
91       log4js.configure('path/to/log4js.json', { reloadSecs: 30 });
92       logger = log4js.getLogger('a-test');
93       logger.info("info1");
94       logger.debug("debug2 - should be ignored");
95       fakeFS.config.levels['a-test'] = "DEBUG";
96       setIntervalCallback();
97       logger.info("info3");
98       logger.debug("debug4");
99       
100       return logEvents;
101     },
102     'should configure log4js from first log4js.json found': function(logEvents) {
103       assert.equal(logEvents[0].data[0], 'info1');
104       assert.equal(logEvents[1].data[0], 'info3');
105       assert.equal(logEvents[2].data[0], 'debug4');
106       assert.equal(logEvents.length, 3);
107     }
108   },
109   
110   'with config file staying the same' : {
111     topic: function() {
112       var pathsChecked = [],
113       fileRead = 0,
114       logEvents = [],
115       logger,
116       modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
117       mtime = new Date(),
118       fakeFS = {
119         config: { 
120           appenders: [ 
121             { type: 'console', layout: { type: 'messagePassThrough' } } 
122           ],
123           levels: { 'a-test' : 'INFO' } 
124         },
125         readFileSync: function (file, encoding) {
126           fileRead += 1;
127           assert.isString(file);
128           assert.equal(file, modulePath);
129           assert.equal(encoding, 'utf8');
130           return JSON.stringify(fakeFS.config);
131         },
132         statSync: function (path) {
133           pathsChecked.push(path);
134           if (path === modulePath) {
135             return { mtime: mtime };
136           } else {
137             throw new Error("no such file");
138           }
139         }
140       },
141       fakeConsole = {
142         'name': 'console',
143         'appender': function () {
144           return function(evt) { logEvents.push(evt); };
145         },
146         'configure': function (config) {
147           return fakeConsole.appender();
148         }
149       },
150       setIntervalCallback,
151       fakeSetInterval = function(cb, timeout) {
152         setIntervalCallback = cb;
153       },
154       log4js = sandbox.require(
155         '../lib/log4js',
156         {
157           requires: {
158             'fs': fakeFS,
159             './appenders/console': fakeConsole
160           },
161           globals: {
162             'console': fakeConsole,
163             'setInterval' : fakeSetInterval,
164           }
165         }
166       );
167       
168       log4js.configure(modulePath, { reloadSecs: 3 });
169       logger = log4js.getLogger('a-test');
170       logger.info("info1");
171       logger.debug("debug2 - should be ignored");
172       setIntervalCallback();
173       logger.info("info3");
174       logger.debug("debug4");
175       
176       return [ pathsChecked, logEvents, modulePath, fileRead ];
177     },
178     'should only read the configuration file once': function(args) {
179       var fileRead = args[3];
180       assert.equal(fileRead, 1);
181     },
182     'should configure log4js from first log4js.json found': function(args) {
183       var logEvents = args[1];
184       assert.equal(logEvents.length, 2);
185       assert.equal(logEvents[0].data[0], 'info1');
186       assert.equal(logEvents[1].data[0], 'info3');
187     }
188   },
189
190   'when config file is removed': {
191     topic: function() {
192       var pathsChecked = [],
193       fileRead = 0,
194       logEvents = [],
195       logger,
196       modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
197       mtime = new Date(),
198       fakeFS = {
199         config: { 
200           appenders: [ 
201             { type: 'console', layout: { type: 'messagePassThrough' } } 
202           ],
203           levels: { 'a-test' : 'INFO' } 
204         },
205         readFileSync: function (file, encoding) {
206           fileRead += 1;
207           assert.isString(file);
208           assert.equal(file, modulePath);
209           assert.equal(encoding, 'utf8');
210           return JSON.stringify(fakeFS.config);
211         },
212         statSync: function (path) {
213           this.statSync = function() {
214             throw new Error("no such file");
215           };
216           return { mtime: new Date() };
217         }
218       },
219       fakeConsole = {
220         'name': 'console',
221         'appender': function () {
222           return function(evt) { logEvents.push(evt); };
223         },
224         'configure': function (config) {
225           return fakeConsole.appender();
226         }
227       },
228       setIntervalCallback,
229       fakeSetInterval = function(cb, timeout) {
230         setIntervalCallback = cb;
231       },
232       log4js = sandbox.require(
233         '../lib/log4js',
234         {
235           requires: {
236             'fs': fakeFS,
237             './appenders/console': fakeConsole
238           },
239           globals: {
240             'console': fakeConsole,
241             'setInterval' : fakeSetInterval,
242           }
243         }
244       );
245       
246       log4js.configure(modulePath, { reloadSecs: 3 });
247       logger = log4js.getLogger('a-test');
248       logger.info("info1");
249       logger.debug("debug2 - should be ignored");
250       setIntervalCallback();
251       logger.info("info3");
252       logger.debug("debug4");
253       
254       return [ pathsChecked, logEvents, modulePath, fileRead ];
255     },
256     'should only read the configuration file once': function(args) {
257       var fileRead = args[3];
258       assert.equal(fileRead, 1);
259     },
260     'should not clear configuration when config file not found': function(args) {
261       var logEvents = args[1];
262       assert.equal(logEvents.length, 3);
263       assert.equal(logEvents[0].data[0], 'info1');
264       assert.equal(logEvents[1].level.toString(), 'WARN');
265       assert.include(logEvents[1].data[0], 'Failed to load configuration file');
266       assert.equal(logEvents[2].data[0], 'info3');
267     }
268   },
269
270   'when passed an object': {
271     topic: function() {
272       var test = setupConsoleTest();
273       test.log4js.configure({}, { reloadSecs: 30 });
274       return test.logEvents;
275     },
276     'should log a warning': function(events) {
277       assert.equal(events[0].level.toString(), 'WARN');
278       assert.equal(
279         events[0].data[0], 
280         'Ignoring configuration reload parameter for "object" configuration.'
281       );
282     }
283   },
284
285   'when called twice with reload options': {
286     topic: function() {
287       var modulePath = require('path').normalize(__dirname + '/../lib/log4js.json'),
288       fakeFS = {
289         readFileSync: function (file, encoding) {
290           return JSON.stringify({});
291         },
292         statSync: function (path) {
293           return { mtime: new Date() };
294         }
295       },
296       fakeConsole = {
297         'name': 'console',
298         'appender': function () {
299           return function(evt) { };
300         },
301         'configure': function (config) {
302           return fakeConsole.appender();
303         }
304       },
305       setIntervalCallback,
306       intervalCleared = false,
307       clearedId,
308       fakeSetInterval = function(cb, timeout) {
309         setIntervalCallback = cb;
310         return 1234;
311       },
312       log4js = sandbox.require(
313         '../lib/log4js',
314         {
315           requires: {
316             'fs': fakeFS,
317             './appenders/console': fakeConsole
318           },
319           globals: {
320             'console': fakeConsole,
321             'setInterval' : fakeSetInterval,
322             'clearInterval': function(interval) {
323               intervalCleared = true;
324               clearedId = interval;
325             }
326           }
327         }
328       );
329       
330       log4js.configure(modulePath, { reloadSecs: 3 });
331       log4js.configure(modulePath, { reloadSecs: 15 });
332       
333       return { cleared: intervalCleared, id: clearedId };
334     },
335     'should clear the previous interval': function(result) {
336       assert.isTrue(result.cleared);
337       assert.equal(result.id, 1234);
338     }
339   }
340 }).exportTo(module);