Skip to content

Commit

Permalink
Refactor randomization of transport protocol
Browse files Browse the repository at this point in the history
Simplify the logic and add a parameter to the command line parser
instead of environment variable.
  • Loading branch information
mattiaswal committed Jun 27, 2024
1 parent e3cd981 commit d27153e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
5 changes: 4 additions & 1 deletion test/env
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ if [ "$containerize" ]; then
--cap-add=NET_ADMIN \
--device=/dev/net/tun \
--env PYTHONHASHSEED=${PYTHONHASHSEED:-$(shuf -i 0-$(((1 << 32) - 1)) -n 1)} \
--env FORCE_PROTOCOL=${FORCE_PROTOCOL} \
--env VIRTUAL_ENV_DISABLE_PROMPT=yes \
--env INFAMY_EXTRA_ARGS="$INFAMY_EXTRA_ARGS" \
--env PROMPT_DIRTRIM="$ixdir" \
--env PS1="$(build_ps1)" \
--expose 9001-9010 --publish-all \
Expand Down Expand Up @@ -220,5 +220,8 @@ INFAMY_ARGS="$INFAMY_ARGS -y $envdir/yangdir"
start_topology "$qenethdir" "$files"
[ "$topology" ] && INFAMY_ARGS="$INFAMY_ARGS $topology"
export INFAMY_ARGS
if [ -n "$INFAMY_EXTRA_ARGS" ]; then
exec "$@" -o="$INFAMY_EXTRA_ARGS"
fi

exec "$@"
25 changes: 7 additions & 18 deletions test/infamy/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import shlex
import sys
import random
import hashlib
import struct

from . import neigh, netconf, restconf, ssh, tap, topology

Expand All @@ -24,6 +22,7 @@ def __init__(self, ltop):
self.add_argument("-l", "--logical-topology", dest="ltop", default=ltop)
self.add_argument("-p", "--package", default=None)
self.add_argument("-y", "--yangdir", default=None)
self.add_argument("-t", "--transport", default=None)
self.add_argument("ptop", nargs=1, metavar="topology")


Expand Down Expand Up @@ -64,30 +63,20 @@ def attr(self, name, default=None):
def get_password(self, node):
return self.ptop.get_password(node)

def attach(self, node, port, protocol = None, factory_default=True):
def attach(self, node, port, protocol=None, factory_default=True):
if self.ltop:
mapping = self.ltop.mapping[node]
node, port = self.ltop.xlate(node, port)
else:
mapping = None

if protocol is None:
if "FORCE_PROTOCOL" in os.environ and os.environ["FORCE_PROTOCOL"] != "":
protocol=os.environ["FORCE_PROTOCOL"]
elif "PYTHONHASHSEED" in os.environ:
file_name = f"{sys.argv[0]}-{os.environ['PYTHONHASHSEED']}"
hash_object = hashlib.md5(file_name.encode())
hash_digest = hash_object.digest()

seed = struct.unpack('i', hash_digest[:4])[0]
random.seed(seed)
protocols=["restconf", "netconf"]
random.shuffle(protocols)
protocol=protocols[0]
if not self.args.transport is None:
protocol=self.args.transport
else:
protocols=["restconf", "netconf"]
random.shuffle(protocols)
protocol=protocols[0]
random.seed(f"{sys.argv[0]}-{os.environ.get('PYTHONHASHSEED',0)}")
protocol = random.choice(["netconf", "restconf"])

password=self.get_password(node)
ctrl = self.ptop.get_ctrl()
cport, _ = self.ptop.get_mgmt_link(ctrl, node)
Expand Down
2 changes: 1 addition & 1 deletion test/infamy/netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def get_iface(self, iface):
if interface is None:
return None

# This really not required, it is due a bug in rousette.
# Restconf (rousette) address by id and netconf (netopper2) address by name
return interface[iface]

def delete_xpath(self, xpath):
Expand Down
2 changes: 1 addition & 1 deletion test/infamy/restconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def get_iface(self, iface):
if interface is None:
return None

# This is a bug in rousette, should be able to use the same code as NETCONF
# Restconf (rousette) address by id and netconf (netopper2) address by name
return interface[0]

def delete_xpath(self, xpath):
Expand Down

0 comments on commit d27153e

Please sign in to comment.