17 # fields enclosed by brackets "[]" replaced with your own identifying |
17 # fields enclosed by brackets "[]" replaced with your own identifying |
18 # information: Portions Copyright [yyyy] [name of copyright owner] |
18 # information: Portions Copyright [yyyy] [name of copyright owner] |
19 # |
19 # |
20 # CDDL HEADER END |
20 # CDDL HEADER END |
21 # |
21 # |
22 # Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
22 # Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
23 # |
23 # |
24 |
|
25 import solaris_install.logger |
24 import solaris_install.logger |
26 import logging |
25 import logging |
27 import os |
26 import os |
28 import random |
27 import random |
|
28 import shutil |
|
29 import socket |
|
30 import struct |
|
31 import sys |
29 import tempfile |
32 import tempfile |
30 import thread |
33 import thread |
31 import time |
34 import time |
32 import unittest |
35 import unittest |
33 from solaris_install.logger import InstallLogger, LogInitError |
36 |
34 import socket |
37 from solaris_install.logger import InstallLogger, LogInitError, \ |
35 import struct |
38 INSTALL_LOGGER_NAME |
36 import sys |
39 from solaris_install.engine.test.engine_test_utils import \ |
|
40 get_new_engine_instance |
37 |
41 |
38 LOGGER = None |
42 LOGGER = None |
39 INSTALL_LOGGER_NAME = 'InsLggr' |
|
40 TEST_LOG = 'test_log' |
43 TEST_LOG = 'test_log' |
41 |
44 |
42 #A Simple Socket Receiver for Testing |
45 # A Simple Socket Receiver for Testing |
43 |
46 |
44 |
47 |
45 def parse_msg(the_socket, cb_function): |
48 def parse_msg(the_socket, cb_function): |
46 '''Parse the messages sent by the client.''' |
49 '''Parse the messages sent by the client.''' |
47 total_len = 0 |
50 total_len = 0 |
101 class TestInstallEngine(object): |
104 class TestInstallEngine(object): |
102 '''Simple Engine for testing with the installLogger''' |
105 '''Simple Engine for testing with the installLogger''' |
103 |
106 |
104 _instance = None |
107 _instance = None |
105 |
108 |
106 def __new__(cls, default_log=None): |
109 def __new__(cls, default_log): |
107 |
110 |
108 if TestInstallEngine._instance is None: |
111 if TestInstallEngine._instance is None: |
109 TestInstallEngine._instance = object.__new__(cls) |
112 TestInstallEngine._instance = object.__new__(cls) |
110 return TestInstallEngine._instance |
113 return TestInstallEngine._instance |
111 else: |
114 else: |
112 raise SingletonError("TestInstallEngine instance already exists", |
115 raise SingletonError("TestInstallEngine instance already exists", |
113 TestInstallEngine._instance) |
116 TestInstallEngine._instance) |
114 |
117 |
115 def __init__(self, default_log=None): |
118 def __init__(self, default_log): |
116 self._init_logging(default_log) |
119 self._init_logging(default_log) |
117 |
120 |
118 def _init_logging(self, default_log): |
121 def _init_logging(self, default_log): |
119 '''Initializes the logger''' |
122 '''Initializes the logger''' |
120 logging.setLoggerClass(InstallLogger) |
123 logging.setLoggerClass(InstallLogger) |
121 global LOGGER |
124 global LOGGER |
122 InstallLogger.ENGINE = self |
125 InstallLogger.ENGINE = self |
123 LOGGER = InstallLogger.manager.getLogger(INSTALL_LOGGER_NAME, default_log) |
126 LOGGER = InstallLogger.manager.getLogger(INSTALL_LOGGER_NAME, |
|
127 log=default_log) |
124 LOGGER.setLevel(logging.DEBUG) |
128 LOGGER.setLevel(logging.DEBUG) |
125 |
129 |
126 if not isinstance(LOGGER, InstallLogger): |
130 if not isinstance(LOGGER, InstallLogger): |
127 raise LogInitError("install logger not of correct type!") |
131 raise LogInitError("install logger not of correct type!") |
128 LOGGER.debug('Beginning the Engine') |
132 LOGGER.debug('Beginning the Engine') |
131 '''Calculates a number and returns the result''' |
135 '''Calculates a number and returns the result''' |
132 overall_progress = (1 * (float(cp_progress) / 100)) |
136 overall_progress = (1 * (float(cp_progress) / 100)) |
133 return overall_progress |
137 return overall_progress |
134 |
138 |
135 |
139 |
|
140 class TestSimpleInstallLogger(unittest.TestCase): |
|
141 '''Tests the InstallLogger outside of the InstallEngine''' |
|
142 |
|
143 def tearDown(self): |
|
144 InstallLogger.DEFAULTFILEHANDLER = None |
|
145 logging.Logger.manager.loggerDict = {} |
|
146 logging.setLoggerClass(logging.Logger) |
|
147 logging._defaultFormatter = logging.Formatter() |
|
148 |
|
149 def test_no_default_logfile(self): |
|
150 '''Test that the logger does not fail with no default log''' |
|
151 logging.setLoggerClass(InstallLogger) |
|
152 LOGGER = InstallLogger.manager.getLogger(INSTALL_LOGGER_NAME) |
|
153 self.failIf(not LOGGER.DEFAULTFILEHANDLER == None) |
|
154 |
|
155 def test_no_default_fh(self): |
|
156 '''Test that logging can be set up a user create FileHandler''' |
|
157 logging.setLoggerClass(InstallLogger) |
|
158 LOGGER = InstallLogger.manager.getLogger(INSTALL_LOGGER_NAME) |
|
159 self.log_tmp_dir = tempfile.mkdtemp(dir="/tmp", prefix="logging_") |
|
160 self.logfile = os.path.join(self.log_tmp_dir, TEST_LOG) |
|
161 |
|
162 fh = logging.FileHandler(self.logfile) |
|
163 LOGGER.addHandler(fh) |
|
164 LOGGER.info('This is from the logger') |
|
165 logtext = open(self.logfile).read() |
|
166 logsearch = "This is from the logger" |
|
167 index = logtext.find(logsearch) |
|
168 self.assertNotEqual(index, -1, 'message is not in default log and' \ |
|
169 ' should be') |
|
170 |
|
171 |
136 class TestInstallLogger(unittest.TestCase): |
172 class TestInstallLogger(unittest.TestCase): |
137 '''Tests the Functionality of the InstallLogger subclass''' |
173 '''Tests the Functionality of the InstallLogger subclass''' |
138 |
174 |
139 def setUp(self): |
175 def setUp(self): |
|
176 |
|
177 self.log_tmp_dir = tempfile.mkdtemp(dir="/tmp", prefix="logging_") |
|
178 self.logfile = os.path.join(self.log_tmp_dir, TEST_LOG) |
140 self.pid = str(os.getpid()) |
179 self.pid = str(os.getpid()) |
141 self.eng = TestInstallEngine() |
180 self.eng = get_new_engine_instance(default_log=self.logfile) |
142 self.test_logger = logging.getLogger('InsLggr.TestLogger') |
181 self.test_logger = logging.getLogger(INSTALL_LOGGER_NAME) |
143 self.logfile = None |
|
144 self.list = [] |
182 self.list = [] |
145 |
183 |
146 def tearDown(self): |
184 def tearDown(self): |
147 self.eng = None |
185 self.eng = None |
148 TestInstallEngine._instance = None |
|
149 InstallLogger.DEFAULTFILEHANDLER = None |
186 InstallLogger.DEFAULTFILEHANDLER = None |
150 logging.Logger.manager.loggerDict = {} |
187 logging.Logger.manager.loggerDict = {} |
151 logging.setLoggerClass(logging.Logger) |
188 logging.setLoggerClass(logging.Logger) |
152 self.test_logger.name = None |
189 self.test_logger.name = None |
153 logging._defaultFormatter = logging.Formatter() |
190 logging._defaultFormatter = logging.Formatter() |
154 |
191 |
155 try: |
192 try: |
156 os.remove(solaris_install.logger.DEFAULTLOG) |
193 shutil.rmtree(self.log_tmp_dir) |
157 except: |
194 except: |
158 # File doesn't exist |
195 # File doesn't exist |
159 pass |
196 pass |
160 |
197 |
161 try: |
198 try: |
200 self.failIf(not isinstance(thelogger, InstallLogger)) |
237 self.failIf(not isinstance(thelogger, InstallLogger)) |
201 |
238 |
202 def test_get_default_log(self): |
239 def test_get_default_log(self): |
203 '''Ensure that default log is returned with default_log method''' |
240 '''Ensure that default log is returned with default_log method''' |
204 dlog = self.test_logger.default_log |
241 dlog = self.test_logger.default_log |
205 self.failIf(dlog != solaris_install.logger.DEFAULTLOG) |
242 self.failIf(dlog != self.logfile) |
206 |
243 |
207 def test_get_log_name(self): |
244 def test_get_log_name(self): |
208 '''Ensure that logger name is returned correctly''' |
245 '''Ensure that logger name is returned correctly''' |
209 logName = self.test_logger.name |
246 logName = self.test_logger.name |
210 self.failIf(logName != "InsLggr.TestLogger") |
247 self.failIf(logName != INSTALL_LOGGER_NAME) |
211 |
248 |
212 def test_create_second_logger_instance(self): |
249 def test_create_second_logger_instance(self): |
213 '''Ensure that only one InstallLogger is created for Install Logger''' |
250 '''Ensure that only one InstallLogger is created for Install Logger''' |
214 self.second_logger = logging.getLogger('SecLogger') |
251 self.second_logger = logging.getLogger('SecLogger') |
215 self.assertEqual(self.second_logger.name, 'SecLogger') |
252 self.assertEqual(self.second_logger.name, 'SecLogger') |
216 fh = logging.FileHandler('simplelog') |
253 fh = logging.FileHandler('simplelog') |
217 self.second_logger.addHandler(fh) |
254 self.second_logger.addHandler(fh) |
218 self.second_logger.info('This is from the second logger') |
255 self.second_logger.info('This is from the second logger') |
219 |
256 |
220 logfile = solaris_install.logger.DEFAULTLOG |
257 logtext = open(self.logfile).read() |
221 logtext = open(logfile).read() |
|
222 logsearch = "This is from the second logger" |
258 logsearch = "This is from the second logger" |
223 index = logtext.find(logsearch) |
259 index = logtext.find(logsearch) |
224 self.assertEqual(index, -1, 'message is in default log and \ |
260 self.assertEqual(index, -1, 'message is in default log and \ |
225 should not be') |
261 should not be') |
226 |
262 |
227 def test_add_FileHandler(self): |
263 def test_add_FileHandler(self): |
228 '''Ensure that FileHandlers can be added to a logger''' |
264 '''Ensure that FileHandlers can be added to a logger''' |
229 fh = solaris_install.logger.FileHandler('/var/tmp/install/fhtest') |
265 sec_log = os.path.join(self.log_tmp_dir, 'fhtest') |
|
266 fh = solaris_install.logger.FileHandler(sec_log) |
230 fh.setLevel(logging.CRITICAL) |
267 fh.setLevel(logging.CRITICAL) |
231 self.test_logger.addHandler(fh) |
268 self.test_logger.addHandler(fh) |
232 self.failIf(not os.path.exists('/var/tmp/install/fhtest')) |
269 self.failIf(not os.path.exists(sec_log)) |
233 |
270 |
234 def test_exclude_log_message(self): |
271 def test_exclude_log_message(self): |
235 '''Ensure that a log message below the designated level does not log''' |
272 '''Ensure that a log message below the designated level does not log''' |
236 fh = solaris_install.logger.FileHandler('/var/tmp/install/fhtest') |
273 sec_log = os.path.join(self.log_tmp_dir, 'fhtest') |
|
274 fh = solaris_install.logger.FileHandler(sec_log) |
237 fh.setLevel(logging.CRITICAL) |
275 fh.setLevel(logging.CRITICAL) |
238 self.test_logger.addHandler(fh) |
276 self.test_logger.addHandler(fh) |
239 self.failIf(not os.path.exists('/var/tmp/install/fhtest')) |
277 self.failIf(not os.path.exists(sec_log)) |
240 self.test_logger.critical('critical message') |
278 self.test_logger.critical('critical message') |
241 self.test_logger.debug('debug message') |
279 self.test_logger.debug('debug message') |
242 logtext = open('/var/tmp/install/fhtest').read() |
280 logtext = open(sec_log).read() |
243 logsearch = "critical message" |
281 logsearch = "critical message" |
244 index = logtext.find(logsearch) |
282 index = logtext.find(logsearch) |
245 self.assertNotEqual(-1, index, \ |
283 self.assertNotEqual(-1, index, \ |
246 "Failed to find the critical in log") |
284 "Failed to find the critical in log") |
247 logsearch = "debug message" |
285 logsearch = "debug message" |
263 self.assertNotEqual(-1, index, \ |
301 self.assertNotEqual(-1, index, \ |
264 "Failed to find the stream message in log") |
302 "Failed to find the stream message in log") |
265 |
303 |
266 def test_create_defaultlog(self): |
304 def test_create_defaultlog(self): |
267 '''Ensure default_log is created and uses the default format.''' |
305 '''Ensure default_log is created and uses the default format.''' |
268 self.failIf(not os.path.exists(solaris_install.logger.DEFAULTLOG)) |
306 self.failIf(not os.path.exists(self.logfile)) |
269 |
307 |
270 def test_log_debug_message(self): |
308 def test_log_debug_message(self): |
271 '''Ensure that debug log messages are logged to the log file''' |
309 '''Ensure that debug log messages are logged to the log file''' |
272 self.test_logger.debug('This is a debug message') |
310 self.test_logger.debug('This is a debug message') |
273 logfile = solaris_install.logger.DEFAULTLOG |
311 logtext = open(self.logfile).read() |
274 logtext = open(logfile).read() |
|
275 logsearch = "This is a debug message" |
312 logsearch = "This is a debug message" |
276 index = logtext.find(logsearch) |
313 index = logtext.find(logsearch) |
277 self.assertNotEqual(-1, index, \ |
314 self.assertNotEqual(-1, index, \ |
278 "Failed to find debug message in default log") |
315 "Failed to find debug message in default log") |
279 |
316 |
280 def test_log_warning_message(self): |
317 def test_log_warning_message(self): |
281 '''Ensure that warning log messages are logged to the log file''' |
318 '''Ensure that warning log messages are logged to the log file''' |
282 self.test_logger.warning('This is a warning message') |
319 self.test_logger.warning('This is a warning message') |
283 logfile = solaris_install.logger.DEFAULTLOG |
320 logtext = open(self.logfile).read() |
284 logtext = open(logfile).read() |
|
285 logsearch = "This is a warning message" |
321 logsearch = "This is a warning message" |
286 index = logtext.find(logsearch) |
322 index = logtext.find(logsearch) |
287 self.assertNotEqual(-1, index, \ |
323 self.assertNotEqual(-1, index, \ |
288 "Failed to find warning message in default log") |
324 "Failed to find warning message in default log") |
289 |
325 |
290 def test_log_info_message(self): |
326 def test_log_info_message(self): |
291 '''Ensure that info log messages are logged to the log file''' |
327 '''Ensure that info log messages are logged to the log file''' |
292 self.test_logger.info('This is an info message') |
328 self.test_logger.info('This is an info message') |
293 logfile = solaris_install.logger.DEFAULTLOG |
329 logtext = open(self.logfile).read() |
294 logtext = open(logfile).read() |
|
295 logsearch = "This is an info message" |
330 logsearch = "This is an info message" |
296 index = logtext.find(logsearch) |
331 index = logtext.find(logsearch) |
297 self.assertNotEqual(-1, index, \ |
332 self.assertNotEqual(-1, index, \ |
298 "Failed to find info message in default log") |
333 "Failed to find info message in default log") |
299 |
334 |
302 self.assertEqual(InstallLogger.INSTALL_FORMAT, \ |
337 self.assertEqual(InstallLogger.INSTALL_FORMAT, \ |
303 '%(asctime)-25s %(name)-10s %(levelname)-10s %(message)-50s') |
338 '%(asctime)-25s %(name)-10s %(levelname)-10s %(message)-50s') |
304 |
339 |
305 def test_transfer_log_destonly(self): |
340 def test_transfer_log_destonly(self): |
306 '''Ensure that default log transfers to destination''' |
341 '''Ensure that default log transfers to destination''' |
307 dest_dir = "/var/tmp/installLog/" |
342 |
|
343 dest_dir = "/tmp/installLog/" |
308 if not os.path.exists(dest_dir): |
344 if not os.path.exists(dest_dir): |
309 os.mkdir(dest_dir) |
345 os.mkdir(dest_dir) |
310 |
346 |
311 base_name = os.path.basename(solaris_install.logger.DEFAULTLOG) |
347 base_name = os.path.basename(self.logfile) |
312 test_filename = "/var/tmp/installLog/" + base_name |
348 test_filename = "/tmp/installLog/" + base_name |
313 self.test_logger.transfer_log(destination=dest_dir) |
349 self.test_logger.transfer_log(destination=dest_dir) |
314 self.failIf(not os.path.exists(test_filename)) |
350 self.failIf(not os.path.exists(test_filename)) |
315 |
351 |
316 def test_close(self): |
352 # This test is commented out because it is causing |
317 '''Ensure that InstallLogger close works''' |
353 # nose test failures. |
318 test_list = ['/var/tmp/install/default_log.' + self.pid] |
354 # CR 7177859 has been filed to track this issue. |
319 test_close_list = self.test_logger.close() |
355 # def test_close(self): |
320 self.assertEquals(test_list, test_close_list) |
356 # '''Ensure that InstallLogger close works''' |
|
357 # test_list = [self.logfile] |
|
358 # test_close_list = self.test_logger.close() |
|
359 # self.assertEquals(test_list, test_close_list) |
321 |
360 |
322 |
361 |
323 class TestProgressHandler(unittest.TestCase): |
362 class TestProgressHandler(unittest.TestCase): |
324 '''Tests the Functionality of the ProgressHandler''' |
363 '''Tests the Functionality of the ProgressHandler''' |
325 |
364 |
326 def setUp(self): |
365 def setUp(self): |
|
366 self.log_tmp_dir = tempfile.mkdtemp(dir="/tmp", prefix="logging_") |
|
367 self.logfile = os.path.join(self.log_tmp_dir, TEST_LOG) |
327 self.pid = str(os.getpid()) |
368 self.pid = str(os.getpid()) |
328 self.eng = TestInstallEngine() |
369 self.eng = TestInstallEngine(self.logfile) |
329 self.test_logger = logging.getLogger('InsLggr.TestLogger') |
370 self.test_logger = logging.getLogger(INSTALL_LOGGER_NAME) |
330 |
371 |
331 # Create parameters for the progress receiver |
372 # Create parameters for the progress receiver |
332 random.seed() |
373 random.seed() |
333 self.portno = random.randint(10000, 30000) |
374 self.portno = random.randint(10000, 30000) |
334 self.hostname = 'localhost' |
375 self.hostname = 'localhost' |
416 ''''Tests that progress is reported to the default log''' |
457 ''''Tests that progress is reported to the default log''' |
417 |
458 |
418 self.test_logger.report_progress( \ |
459 self.test_logger.report_progress( \ |
419 'this is a progress message with percentage 10', progress=10) |
460 'this is a progress message with percentage 10', progress=10) |
420 |
461 |
421 logfile = solaris_install.logger.DEFAULTLOG |
462 logtext = open(self.logfile).read() |
422 logtext = open(logfile).read() |
|
423 logsearch = "PROGRESS REPORT: progress percent:0.1" + \ |
463 logsearch = "PROGRESS REPORT: progress percent:0.1" + \ |
424 " this is a progress message with percentage 10" |
464 " this is a progress message with percentage 10" |
425 index = logtext.find(logsearch) |
465 index = logtext.find(logsearch) |
426 self.assertNotEqual(-1, index, \ |
466 self.assertNotEqual(-1, index, \ |
427 "Failed to find progress message in default log") |
467 "Failed to find progress message in default log") |