Add global random seed config

This is needed to make fuzzy tests work with xdist as each
xdist gateway expects to receive the same set of parameter
values, which for random generators may be achieved only by
providing globally shared seed.

Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
Robert Baldyga 2019-10-03 14:43:21 +02:00
parent b9b39fdb46
commit 1525e85805
4 changed files with 26 additions and 4 deletions

View File

@ -18,7 +18,7 @@ SRC=$(shell find $(SRCDIR) $(WRAPDIR) -name \*.c)
OBJS=$(patsubst %.c, %.o, $(SRC))
OCFLIB=$(ADAPTERDIR)/libocf.so
all: | sync
all: | sync config_random
$(MAKE) $(OCFLIB)
$(OCFLIB): $(OBJS)
@ -36,6 +36,9 @@ sync:
@$(MAKE) -C $(OCFDIR) src O=$(ADAPTERDIR)/ocf
@$(MAKE) -C $(OCFDIR) env O=$(ADAPTERDIR)/ocf OCF_ENV=posix
config_random:
@python3 utils/configure_random.py
clean:
@rm -rf $(OCFLIB) $(OBJS)
@echo " CLEAN "
@ -46,4 +49,4 @@ distclean: clean
@rm -rf $(INCDIR)/ocf
@echo " DISTCLEAN "
.PHONY: all clean sync distclean
.PHONY: all clean sync config_random distclean

View File

@ -0,0 +1,2 @@
# This file content will be generated by utils/configure_random.py
# triggered from the Makefile

View File

@ -36,6 +36,8 @@ class DefaultRanges(Range, enum.Enum):
class RandomGenerator:
def __init__(self, base_range=DefaultRanges.INT, count=1000):
with open("config/random.cfg") as f:
self.random = random.Random(int(f.read()))
self.exclude = []
self.range = base_range
self.count = count
@ -53,7 +55,7 @@ class RandomGenerator:
raise StopIteration()
self.n += 1
while True:
val = random.randint(self.range.min, self.range.max)
val = self.random.randint(self.range.min, self.range.max)
if self.exclude:
excl_map = map(lambda e: e.is_within(val), self.exclude)
is_excluded = reduce(lambda a, b: a or b, excl_map)
@ -64,6 +66,8 @@ class RandomGenerator:
class RandomStringGenerator:
def __init__(self, len_range=Range(0, 20), count=700):
with open("config/random.cfg") as f:
self.random = random.Random(int(f.read()))
self.generator = self.__string_generator(len_range)
self.count = count
self.n = 0
@ -78,7 +82,7 @@ class RandomStringGenerator:
string.punctuation,
string.hexdigits]:
yield ''.join(random.choice(t) for _ in range(
random.randint(len_range.min, len_range.max)
self.random.randint(len_range.min, len_range.max)
))
def __iter__(self):

View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
#
# Copyright(c) 2012-2018 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause-Clear
#
import sys
import random
with open("config/random.cfg", "w") as f:
f.write(str(random.randint(0, sys.maxsize)))