From be5386ab36acc96c963b39cbce162ebd2b5b841e Mon Sep 17 00:00:00 2001
From: Victor Stinner <victor.stinner@enovance.com>
Date: Wed, 18 Dec 2013 18:10:29 +0100
Subject: [PATCH] Fix test_notifier_logger for Python 3

 * Get the current line at runtime, don't hardcode the line number
 * Only strip last character of the filename if it ends with ".pyc"
   or ".pyo".  On Python 3, import automatically replaces .pyc and
   .pyo with .py.
 * Use threading.current_thread().ident to get the identifier of the
   current thread. The get_ident() function is available in thread
   module in Python 2, but in threading module in Python 3. And on
   Python 3, logging.thread symbol does not exist anymore.

Change-Id: I3f248bb860caafaf38eefcf440d36b9bebc8f05e
---
 tests/test_notifier_logger.py | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/tests/test_notifier_logger.py b/tests/test_notifier_logger.py
index 67c7983ca..d6af9c305 100644
--- a/tests/test_notifier_logger.py
+++ b/tests/test_notifier_logger.py
@@ -15,6 +15,11 @@
 import logging
 import logging.config
 import os
+import sys
+try:
+    import threading
+except ImportError:
+    threading = None
 
 import mock
 import testscenarios
@@ -33,6 +38,13 @@ logging.AUDIT = logging.INFO + 1
 logging.addLevelName(logging.AUDIT, 'AUDIT')
 
 
+def get_thread_ident():
+    if threading is not None:
+        return threading.current_thread().ident
+    else:
+        return None
+
+
 class TestLogNotifier(test_utils.BaseTestCase):
 
     scenarios = [
@@ -81,7 +93,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
             {'process': os.getpid(),
              'funcName': None,
              'name': 'foo',
-             'thread': logging.thread.get_ident() if logging.thread else None,
+             'thread': get_thread_ident(),
              'levelno': levelno,
              'processName': 'MainProcess',
              'pathname': '/foo/bar',
@@ -118,6 +130,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
         levelno = getattr(logging, self.priority.upper())
 
         logger = logging.getLogger('default')
+        lineno = sys._getframe().f_lineno + 1
         logger.log(levelno, 'foobar')
 
         n = messaging.notify._impl_test.NOTIFICATIONS[0][1]
@@ -126,16 +139,19 @@ class TestLogNotifier(test_utils.BaseTestCase):
         self.assertEqual(n['event_type'], 'logrecord')
         self.assertEqual(n['timestamp'], str(timeutils.utcnow.override_time))
         self.assertEqual(n['publisher_id'], None)
+        pathname = __file__
+        if pathname.endswith(('.pyc', '.pyo')):
+            pathname = pathname[:-1]
         self.assertDictEqual(
             n['payload'],
             {'process': os.getpid(),
              'funcName': 'test_logging_conf',
              'name': 'default',
-             'thread': logging.thread.get_ident() if logging.thread else None,
+             'thread': get_thread_ident(),
              'levelno': levelno,
              'processName': 'MainProcess',
-             'pathname': __file__[:-1],  # Remove the 'c' of .pyc
-             'lineno': 121,
+             'pathname': pathname,
+             'lineno': lineno,
              'msg': 'foobar',
              'exc_info': None,
              'levelname': logging.getLevelName(levelno),