From eb658a1034948c89944a005cc732c0fccfbf235b Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Mon, 20 May 2013 22:29:52 -0400 Subject: [PATCH] Add unit tests to ensure TZ variable remains set See review https://review.openstack.org/29836. Change-Id: I8ec80202789707f723abfe93ccc9cf1e677e4dc6 Signed-off-by: Peter Portante --- swift/common/daemon.py | 7 +++++++ swift/common/wsgi.py | 12 ++++++------ test/unit/common/test_daemon.py | 8 ++++++-- test/unit/common/test_wsgi.py | 10 ++++++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/swift/common/daemon.py b/swift/common/daemon.py index 1955b1326e..0b3effb542 100644 --- a/swift/common/daemon.py +++ b/swift/common/daemon.py @@ -15,6 +15,7 @@ import os import sys +import time import signal from re import sub @@ -99,6 +100,12 @@ def run_daemon(klass, conf_file, section_name='', once=False, **kwargs): eventlet_debug = utils.config_true_value(conf.get('eventlet_debug', 'no')) eventlet.debug.hub_exceptions(eventlet_debug) + # Ensure TZ environment variable exists to avoid stat('/etc/localtime') on + # some platforms. This locks in reported times to the timezone in which + # the server first starts running in locations that periodically change + # timezones. + os.environ['TZ'] = time.strftime("%z", time.gmtime()) + try: klass(conf).run(once=once, **kwargs) except KeyboardInterrupt: diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py index 39bae45de5..02b932f1a7 100644 --- a/swift/common/wsgi.py +++ b/swift/common/wsgi.py @@ -190,6 +190,12 @@ class RestrictedGreenPool(GreenPool): def run_server(conf, logger, sock): + # Ensure TZ environment variable exists to avoid stat('/etc/localtime') on + # some platforms. This locks in reported times to the timezone in which + # the server first starts running in locations that periodically change + # timezones. + os.environ['TZ'] = time.strftime("%z", time.gmtime()) + wsgi.HttpProtocol.default_request_version = "HTTP/1.0" # Turn off logging requests by the underlying WSGI software. wsgi.HttpProtocol.log_request = lambda *a: None @@ -245,12 +251,6 @@ def run_wsgi(conf_path, app_section, *args, **kwargs): # redirect errors to logger and close stdio capture_stdio(logger) - # Ensure TZ environment variable exists to avoid stat('/etc/localtime') on - # some platforms. This locks in reported times to the timezone in which - # the server first starts running in locations that periodically change - # timezones. - os.environ['TZ'] = time.strftime("%z", time.gmtime()) - worker_count = int(conf.get('workers', '1')) # Useful for profiling [no forks]. if worker_count == 0: diff --git a/test/unit/common/test_daemon.py b/test/unit/common/test_daemon.py index 267d2cecad..d6a5819628 100644 --- a/test/unit/common/test_daemon.py +++ b/test/unit/common/test_daemon.py @@ -15,11 +15,13 @@ # TODO: Test kill_children signal handlers +import os import unittest from getpass import getuser import logging from StringIO import StringIO from test.unit import tmpfile +from mock import patch from swift.common import daemon, utils @@ -85,8 +87,10 @@ class TestRunDaemon(unittest.TestCase): user = %s """ % getuser() with tmpfile(sample_conf) as conf_file: - daemon.run_daemon(MyDaemon, conf_file) - self.assertEquals(MyDaemon.forever_called, True) + with patch.dict('os.environ', {'TZ': ''}): + daemon.run_daemon(MyDaemon, conf_file) + self.assertEquals(MyDaemon.forever_called, True) + self.assert_(os.environ['TZ'] is not '') daemon.run_daemon(MyDaemon, conf_file, once=True) self.assertEquals(MyDaemon.once_called, True) diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index 3c96f16470..a1c628ad0e 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -360,10 +360,12 @@ class TestWSGI(unittest.TestCase): _fake_rings(conf_root) with patch('swift.common.wsgi.wsgi') as _wsgi: with patch('swift.common.wsgi.eventlet') as _eventlet: - conf = wsgi.appconfig(conf_dir) - logger = logging.getLogger('test') - sock = listen(('localhost', 0)) - wsgi.run_server(conf, logger, sock) + with patch.dict('os.environ', {'TZ': ''}): + conf = wsgi.appconfig(conf_dir) + logger = logging.getLogger('test') + sock = listen(('localhost', 0)) + wsgi.run_server(conf, logger, sock) + self.assert_(os.environ['TZ'] is not '') self.assertEquals('HTTP/1.0', _wsgi.HttpProtocol.default_request_version)