Skip to content

Commit

Permalink
test: netns: Clean up lingering namespaces on test exit
Browse files Browse the repository at this point in the history
This gets rid of annoying "Address already in use" errors when running
tests repeatedly from a test-sh.
  • Loading branch information
wkz committed Nov 4, 2024
1 parent c69dce5 commit a2953d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/infamy/netns.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class IsolatedMacVlans:
"""

Instances = []
def Cleanup():
for ns in list(IsolatedMacVlans.Instances):
ns.stop()

def __init__(self, ifmap, lo=True):
self.sleeper = None
self.ifmap, self.lo = ifmap, lo
Expand Down Expand Up @@ -76,11 +81,13 @@ def start(self):
self.__exit__(None, None, None)
raise e

self.Instances.append(self)
return self

def stop(self):
self.sleeper.kill()
self.sleeper.wait()
self.Instances.remove(self)
time.sleep(0.5)

def __enter__(self):
Expand Down
4 changes: 4 additions & 0 deletions test/infamy/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import traceback

import infamy.netns

class Test:
def __init__(self, output=sys.stdout):
self.out = output
Expand All @@ -24,6 +26,8 @@ def __exit__(self, _, e, __):
self.out.write(f"# Exiting ({now})\n")
self.out.flush()

infamy.netns.IsolatedMacVlans.Cleanup()

if not e:
self._not_ok("Missing explicit test result\n")
else:
Expand Down

0 comments on commit a2953d8

Please sign in to comment.