Add unit tests to ensure TZ variable remains set

See review https://review.openstack.org/29836.

Change-Id: I8ec80202789707f723abfe93ccc9cf1e677e4dc6
Signed-off-by: Peter Portante <peter.portante@redhat.com>
This commit is contained in:
Peter Portante 2013-05-20 22:29:52 -04:00
parent 7bc9b0cb15
commit eb658a1034
4 changed files with 25 additions and 12 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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)

View File

@ -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)