Commit 8527bff8 authored by zhifan huang's avatar zhifan huang

update net_test to 3

parent d41daa7d
"""thie moudle use net namespace to create different net node"""
import subprocess
from subprocess import PIPE
from subprocess import PIPE, DEVNULL
import weakref
import logging
import time
......@@ -73,6 +73,20 @@ class Netns(object):
def run(self, cmd, **kw):
""" wrapper for subprocess.checkout"""
subprocess.check_call(['nsenter', '-t', str(self.pid), '-n'] + cmd, **kw)
self.run(['sysctl', '-w', 'net.ipv4.ip_forward=1'], stdout=DEVNULL)
self.run(['sysctl', '-w', 'net.ipv6.conf.default.forwarding=1'], stdout=DEVNULL)
# self.run(['sysctl', '-q', 'net.ipv4.icmp_echo_ignore_broadcasts=0'], stdout=PIPE)
def Popen(self, cmd, **kw) ->subprocess.Popen:
""" wrapper for subprocess.Popen"""
return subprocess.Popen(['nsenter', '-t', str(self.pid), '-n'] + cmd, **kw)
def run(self, cmd, **kw):
""" wrapper for subprocess.run"""
subprocess.run(['nsenter', '-t', str(self.pid), '-n'] + cmd, **kw)
def add_device(self, dev):
self.devices.append(dev)
......@@ -105,7 +119,7 @@ class Netns(object):
dev2 = Device("veth")
node1.add_device(dev1)
node2.add_device(dev2)
subprocess.check_call(['ip', 'link', 'add', dev1.name, 'netns', str(node1.pid), 'type', 'veth', 'peer', dev2.name, 'netns', str(node2.pid)])
subprocess.run(['ip', 'link', 'add', dev1.name, 'netns', str(node1.pid) ,'type', 'veth', 'peer', dev2.name, 'netns', str(node2.pid)])
dev1.up = dev2.up = True
return dev1, dev2
......@@ -131,8 +145,7 @@ class Netns(object):
return dev1, dev2
def __del__(self):
self.app.terminate()
self.app.wait()
self.app.communicate(b"exit")
if hasattr(self, "proc"):
self.proc.terminate()
......@@ -164,7 +177,7 @@ def connectible_test(nm):
"""
for reg in nm.registrys:
for node in nm.registrys[reg]:
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=PIPE)
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=DEVNULL)
ret = app0.wait()
assert ret == 0, "network construct failed {} to {}".format(node.ip, reg.ip)
......
......@@ -10,17 +10,21 @@ import time
import re
import tempfile
import logging
from subprocess import PIPE, call
from pathlib2 import Path
from subprocess import PIPE, DEVNULL, run
from pathlib import Path
import re6st.tests.tools as tools
import my_net
WORK_DIR = Path(__file__).parent / "temp_net_test"
DH_FILE = WORK_DIR / "dh2048.pem"
RE6STNET = "re6stnet"
RE6ST_REGISTRY = "re6st-registry"
#RE6ST_REGISTRY = "python -m re6st.cli.registry"
RE6ST_REGISTRY = "python3 -m re6st.cli.registry"
RE6ST_CONF = "re6st-conf"
def initial():
......@@ -29,7 +33,7 @@ def initial():
WORK_DIR.mkdir()
if not DH_FILE.exists():
logging.info("create dh file")
call(['openssl', 'dhparam', '-out', str(DH_FILE), '2048'], stderr=PIPE)
run(['openssl', 'dhparam', '-out', str(DH_FILE), '2048'], stderr=DEVNULL)
def ip_to_serial(ip6):
"""convert ipv6 address to serial"""
......@@ -50,7 +54,7 @@ class Re6stRegistry(object):
"""class run a re6st-registry service on a namespace"""
registry_seq = 0
def __init__(self, node, ip6, recreate=False):
def __init__(self, node:my_net.Host, ip6, recreate=False):
self.node = node
# TODO need set once
self.ip = node.ip
......@@ -96,7 +100,8 @@ class Re6stRegistry(object):
time.sleep(.1)
"""])
try:
wait_ps(p, 5)
p.wait(5)
# wait_ps(p, 5)
except Exception as e:
logging.error("%s: %s", self.name, e)
raise e
......@@ -125,7 +130,8 @@ class Re6stRegistry(object):
run=self.run_path).split()
logging.info("run registry %s at ns: %s with cmd: %s",
self.name, self.node.pid, " ".join(cmd))
self.proc = self.node.Popen(cmd, stdout=PIPE, stderr=PIPE)
# self.proc = self.node.Popen(cmd, stdout=DEVNULL, stderr=DEVNULL)
self.proc = self.node.Popen(cmd, stdout=DEVNULL)
def clean(self):
"""remove the file created last time"""
......@@ -146,7 +152,7 @@ class Re6stNode(object):
"""class run a re6stnet service on a namespace"""
node_seq = 0
def __init__(self, node, registry, name=None, recreate=False):
def __init__(self, node:my_net.Host , registry:Re6stRegistry, name=None, recreate=False):
"""
node: nemu node
name: name for res6st node
......@@ -210,7 +216,7 @@ class Re6stNode(object):
cmd = "{script} --registry {registry_url} --email {email}"
cmd = cmd.format(script=RE6ST_CONF, registry_url=self.registry.url,
email=self.email).split()
p = self.node.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE,
p = self.node.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=DEVNULL,
cwd=str(self.path))
# read token
db = sqlite3.connect(str(self.registry.db), isolation_level=None)
......@@ -224,8 +230,9 @@ class Re6stNode(object):
if count > 100:
p.terminate()
raise Exception("can't connect to the Register")
out, _ = p.communicate(str(token[0]))
out, _ = p.communicate(token[0].encode())
out = out.decode()
# logging.debug("re6st-conf output: {}".format(out))
# find the ipv6 subnet of node
self.ip6 = re.search('(?<=subnet: )[0-9:a-z]+', out).group(0)
......@@ -247,7 +254,7 @@ class Re6stNode(object):
cmd += args
logging.info("run node %s at ns: %s with cmd: %s",
self.name, self.node.pid, " ".join(cmd))
self.proc = self.node.Popen(cmd, stdout=PIPE, stderr=PIPE)
self.proc = self.node.Popen(cmd, stdout=DEVNULL, stderr=DEVNULL)
def clean(self):
"""remove the file created last time"""
......@@ -260,8 +267,10 @@ class Re6stNode(object):
"""stop running re6stnet process"""
logging.debug("%s teminate process %s", self.name, self.proc.pid)
self.proc.terminate()
self.proc.wait(2)
# timeout only in python3. deadlock maybe
wait_ps(self.proc, 2)
# wait_ps(self.proc, 2)
def __del__(self):
"""teminate process and rm temp dir"""
......
......@@ -7,9 +7,11 @@ import logging
import sqlite3
import random
from binascii import b2a_hex
from pathlib2 import Path
from pathlib import Path
import re6st_wrap
import my_net
import tracemalloc
PING_PATH = str(Path(__file__).parent.resolve() / "ping.py")
BABEL_HMAC = 'babel_hmac0', 'babel_hmac1', 'babel_hmac2'
......@@ -138,6 +140,17 @@ class TestNet(unittest.TestCase):
self.assertTrue(wait_stable(nodes, 30), " ping test failed")
def test_ping_simple(self):
"""create a network in a net segment, test the connectivity by ping
"""
nm = my_net.net_simple()
nodes, registrys = deploy_re6st(nm)
wait_stable(nodes, 40)
time.sleep(10)
self.assertTrue(wait_stable(nodes, 30), " ping test failed")
def test_ping_demo(self):
"""create a network demo, test the connectivity by ping
wait at most 50 seconds, and test each node ping to other by ipv6 addr
......@@ -221,4 +234,5 @@ if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, filename='test.log', filemode='w',
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%I:%M:%S')
tracemalloc.start()
unittest.main(verbosity=3)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment