Skip to content

Commit

Permalink
infamy: Fix default transport selection
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
wkz committed Dec 26, 2024
1 parent 655bf1f commit d65fee6
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions test/infamy/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@ 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__()

self.add_argument("-d", "--debug", default=False, action="store_true")
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")


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d65fee6

Please sign in to comment.