Reset RNG seed with current time and pid for each test started

This will hopefully fix fullstack failures where different process
fixtures running in parallel test processes and relying on the same
random.choice() generator seeded by the same initial value could pick up
the same value as a service free port, and spawn their respective
resources using the same port.

Which made one of those unlucky services to fail.

Change-Id: I13cfa9392fd138c5e1b1b7d397b9ea91b2a47ed2
Closes-Bug: #1551288
This commit is contained in:
Ihar Hrachyshka 2016-03-14 14:35:31 +01:00
parent 53c03f5ed3
commit bb567e9b32
3 changed files with 19 additions and 0 deletions

View File

@ -154,6 +154,8 @@ class DietTestCase(base.BaseTestCase):
self.addOnException(self.check_for_systemexit)
self.orig_pid = os.getpid()
tools.reset_random_seed()
def addOnException(self, handler):
def safe_handler(*args, **kwargs):

View File

@ -18,6 +18,7 @@ from oslo_db.sqlalchemy import test_base
from neutron.db.migration import cli as migration
from neutron.tests.common import base
from neutron.tests.fullstack.resources import client as client_resource
from neutron.tests import tools
class BaseFullStackTestCase(base.MySQLTestCase):
@ -25,6 +26,10 @@ class BaseFullStackTestCase(base.MySQLTestCase):
def setUp(self, environment):
super(BaseFullStackTestCase, self).setUp()
# NOTE(ihrachys): seed should be reset before environment fixture below
# since the latter starts services that may rely on generated port
# numbers
tools.reset_random_seed()
self.create_db_tables()
self.environment = environment
self.environment.test_name = self.get_name()

View File

@ -13,9 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import platform
import random
import string
import time
import warnings
import fixtures
@ -204,3 +206,13 @@ def is_bsd():
if 'bsd' in system.lower():
return True
return False
def reset_random_seed():
# reset random seed to make sure other processes extracting values from RNG
# don't get the same results (useful especially when you then use the
# random values to allocate system resources from global pool, like ports
# to listen). Use both current time and pid to make sure no tests started
# at the same time get the same values from RNG
seed = time.time() + os.getpid()
random.seed(seed)