From 1525e85805e22f3434b7f9e9e87e15c1e8acbac0 Mon Sep 17 00:00:00 2001 From: Robert Baldyga Date: Thu, 3 Oct 2019 14:43:21 +0200 Subject: [PATCH] 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 --- tests/functional/Makefile | 7 +++++-- tests/functional/config/random.cfg | 2 ++ tests/functional/tests/utils/random.py | 8 ++++++-- tests/functional/utils/configure_random.py | 13 +++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 tests/functional/config/random.cfg create mode 100755 tests/functional/utils/configure_random.py diff --git a/tests/functional/Makefile b/tests/functional/Makefile index b8963b2..71a77dc 100755 --- a/tests/functional/Makefile +++ b/tests/functional/Makefile @@ -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 diff --git a/tests/functional/config/random.cfg b/tests/functional/config/random.cfg new file mode 100644 index 0000000..f7ab212 --- /dev/null +++ b/tests/functional/config/random.cfg @@ -0,0 +1,2 @@ +# This file content will be generated by utils/configure_random.py +# triggered from the Makefile diff --git a/tests/functional/tests/utils/random.py b/tests/functional/tests/utils/random.py index 924e018..2773570 100644 --- a/tests/functional/tests/utils/random.py +++ b/tests/functional/tests/utils/random.py @@ -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): diff --git a/tests/functional/utils/configure_random.py b/tests/functional/utils/configure_random.py new file mode 100755 index 0000000..71a0440 --- /dev/null +++ b/tests/functional/utils/configure_random.py @@ -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)))