From d65fee6e9ff38b3606697b294c77fd40d37b3847 Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Fri, 20 Dec 2024 23:05:07 +0100 Subject: [PATCH] infamy: Fix default transport selection Using `sys.argv[0]` does not quite achieve the effect we want, namely that any (test-case, $PYTHONHASHSEED) tuple should always default to the same transport. For example, the following two examples should always use the same transport: $ make PYTHONHASHSEED=1337 test-sh infamy0:test$ ./case/ietf_system/hostname/test.py $ make PYTHONHASHSEED=1337 test-sh infamy0:test$ cd ./case/ietf_system/hostname infamy0:hostname$ ./test.py But, since their argv[0]'s are different, they don't. Therefore use the _last two components_ of argv[0]'s full path instead. This should: - Spread the allocation over an entire suite run, since the penultimate component is usually the test name. - Keep the allocation stable when test code is modified (which is common during debugging of failing tests) - Avoid instabilities stemming from the local storage location (i.e., home directory names etc.) --- test/infamy/env.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/test/infamy/env.py b/test/infamy/env.py index c7cb071f5..06f84216c 100644 --- a/test/infamy/env.py +++ b/test/infamy/env.py @@ -18,6 +18,20 @@ def attr(self, _, default=None): class ArgumentParser(argparse.ArgumentParser): + def DefaultTransport(): + """Pick pseudo-random transport + + If the user does not specify a particular transport, make sure + that any (test, $PYTHONHASHSEED) tuple will always map to the + same transport. + + """ + name = "/".join(os.path.realpath(sys.argv[0]).split(os.sep)[-2:]) + seed = os.environ.get('PYTHONHASHSEED', 0) + random.seed(f"{name}-{seed}") + + return random.choice(["netconf", "restconf"]) + def __init__(self, top): super().__init__() @@ -25,7 +39,7 @@ def __init__(self, top): self.add_argument("-l", "--logical-topology", dest="ltop", default=top) self.add_argument("-p", "--package", default=None) self.add_argument("-y", "--yangdir", default=None) - self.add_argument("-t", "--transport", default=None) + self.add_argument("-t", "--transport", default=ArgumentParser.DefaultTransport()) self.add_argument("ptop", nargs=1, metavar="topology") @@ -84,15 +98,12 @@ def attach(self, node, port="mgmt", protocol=None, test_reset=True, username = N else: mapping = None - # Test protocol always highest prio, followed by command line, - # then environment (detected from defconfig), lastly random. + # Precedence: + # 1. Caller specifies `protocol` + # 2. User specifies `-t` when executing test + # 3. One is pseudo-randomly picked based on $PYTHONHASHSEED if protocol is None: - if self.args.transport is not None: - protocol = self.args.transport - else: - hseed = os.environ.get('PYTHONHASHSEED', 0) - random.seed(f"{sys.argv[0]}-{hseed}") - protocol = random.choice(["netconf", "restconf"]) + protocol = self.args.transport if password is None: password = self.get_password(node)