Fixed #24717 -- Fixed model_regress test failure on RHEL6 SCL

The test failed on RHEL6 systems running python 2.7 from a RedHat
Software Collection (SCL) because this test runs an external python
script with a stripped system environment. RedHat SCLs work by setting
a number of system environment variables when these are stripped out by
this test the python 2.7 interpreter is no longer able to function
properly because it can not find the system libraries needed.

Now we use use mock to modify the system environment directly.
This commit is contained in:
David D. Riddle 2015-04-28 11:59:05 -05:00 committed by Tim Graham
parent 683ece0ec8
commit 31e6c9c8e3
1 changed files with 14 additions and 16 deletions

View File

@ -7,7 +7,8 @@ import warnings
from django.core.files.temp import NamedTemporaryFile from django.core.files.temp import NamedTemporaryFile
from django.db import DJANGO_VERSION_PICKLE_KEY, models from django.db import DJANGO_VERSION_PICKLE_KEY, models
from django.test import TestCase from django.test import TestCase, mock
from django.utils._os import upath
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.version import get_version from django.utils.version import get_version
@ -84,19 +85,16 @@ print(article.headline)"""
with NamedTemporaryFile(mode='w+', suffix=".py") as script: with NamedTemporaryFile(mode='w+', suffix=".py") as script:
script.write(script_template % pickle.dumps(a)) script.write(script_template % pickle.dumps(a))
script.flush() script.flush()
pythonpath = [os.path.dirname(script.name)] + sys.path # A path to model_regress must be set in PYTHONPATH
env = { model_regress_dir = os.path.dirname(upath(__file__))
# Needed to run test outside of tests directory model_regress_path = os.path.abspath(model_regress_dir)
str('PYTHONPATH'): os.pathsep.join(pythonpath), tests_path = os.path.split(model_regress_path)[0]
# Needed on Windows because http://bugs.python.org/issue8557 pythonpath = os.environ.get('PYTHONPATH', '')
str('PATH'): os.environ['PATH'], pythonpath = os.pathsep.join([tests_path, pythonpath])
str('TMPDIR'): os.environ['TMPDIR'],
str('LANG'): os.environ.get('LANG', ''), with mock.patch.dict('os.environ', {'PYTHONPATH': pythonpath}):
} try:
if 'SYSTEMROOT' in os.environ: # Windows http://bugs.python.org/issue20614 result = subprocess.check_output([sys.executable, script.name])
env[str('SYSTEMROOT')] = os.environ['SYSTEMROOT'] except subprocess.CalledProcessError:
try: self.fail("Unable to reload model pickled data")
result = subprocess.check_output([sys.executable, script.name], env=env)
except subprocess.CalledProcessError:
self.fail("Unable to reload model pickled data")
self.assertEqual(result.strip().decode(), "Some object") self.assertEqual(result.strip().decode(), "Some object")