Commit 388566e8 authored by zhifan huang's avatar zhifan huang Committed by Xiaowu Zhang

fix: fix problem in merge request

modify fixnemu device name comment, remove extra line, space, remove dup
definition, eek
parent fea1061c
......@@ -75,16 +75,15 @@ get_addr_data.func_code = _get_addr_data.func_code
@staticmethod
def _gen_if_name():
n = Interface._gen_next_id()
# The maximum size of the interface name is 15(char [IFNAMSIZ(16)])
# len(NETNSbr-) = 8, So we need to limit the part "%.4x%.3x" % (os.getpid(), n)
# to size 7. That means PID < 0xffff, n < 0xfff
# PID often exceeds 0xffff, so use mod 0xffff to limit it length.
# Max 15 chars
# XXX: We truncate pid to not exceed IFNAMSIZ on systems with 32-bits pids
# but we should find something better to avoid possible collision.
return "NETNSif-%.4x%.3x" % (os.getpid() % 0xffff, n)
Interface._gen_if_name = _gen_if_name
@staticmethod
def _gen_br_name():
n = Switch._gen_next_id()
# Max 15 chars
# XXX: same as for _gen_if_name
return "NETNSbr-%.4x%.3x" % (os.getpid() % 0xffff, n)
Switch._gen_br_name = _gen_br_name
import json
import os
import unittest
import time
import sqlite3
from pathlib2 import Path
import subprocess
import tempfile
import json
import time
import unittest
import zlib
from re6st import registry, x509
......@@ -15,7 +15,7 @@ from re6st.tests import DEMO_PATH
DH_FILE = DEMO_PATH / "dh2048.pem"
class dummyNode():
class DummyNode(object):
"""fake node to reuse Re6stRegistry
error: node.Popen has destory method which not in subprocess.Popen
......@@ -37,7 +37,7 @@ class TestRegistryClentInteract(unittest.TestCase):
def setUp(self):
self.port = 18080
self.url = "http://localhost:{}/".format(self.port)
# not inportant, used in network_config check
# not important, used in network_config check
self.max_clients = 10
def tearDown(self):
......@@ -46,7 +46,7 @@ class TestRegistryClentInteract(unittest.TestCase):
def test_1_main(self):
""" a client interact a server, no re6stnet node test basic function"""
try:
self.server = re6st_wrap.Re6stRegistry(dummyNode(), "2001:db8:42::",
self.server = re6st_wrap.Re6stRegistry(DummyNode(), "2001:db8:42::",
self.max_clients, port=self.port,
recreate=True)
except:
......@@ -60,14 +60,14 @@ class TestRegistryClentInteract(unittest.TestCase):
client.requestToken(email)
# read token from db
db = sqlite3.connect(str(self.server.db), isolation_level=None)
count = 0
token = None
while not token:
for _ in xrange(100):
time.sleep(.1)
token = db.execute("SELECT token FROM token WHERE email=?",
(email,)).fetchone()
count += 1
if count > 100:
if token:
break
else:
raise Exception("Request token failed, no token in database")
# token: tuple[unicode,]
token = str(token[0])
......
import time
import nemu
import weakref
import ipaddress
import logging
import nemu
import time
import weakref
from subprocess import PIPE
from pathlib2 import Path
......@@ -13,6 +13,9 @@ fix_file = DEMO_PATH / "fixnemu.py"
exec(open(str(fix_file)).read())
IPTABLES = 'iptables-nft'
class ConnectableError(Exception):
pass
class Node(nemu.Node):
"""simple nemu.Node used for registry and nodes"""
def __init__(self):
......@@ -57,12 +60,12 @@ class NetManager(object):
Raise:
AssertionError
"""
for reg, nodes in self.registries.items():
for reg, nodes in self.registries.iteritems():
for node in nodes:
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=PIPE)
ret = app0.wait()
if ret:
raise ConnectionError(
raise ConnectableError(
"network construct failed {} to {}".format(node.ip, reg.ip))
logging.debug("each node can ping to their registry")
......@@ -83,9 +86,9 @@ def net_route():
machine1 = Node()
machine2 = Node()
r1_if_0 = registry.connect_switch(switch1, "192.168.1.1")
m1_if_0 = machine1.connect_switch(switch1, "192.168.1.2")
m2_if_0 = machine2.connect_switch(switch1, "192.168.1.3")
registry.connect_switch(switch1, "192.168.1.1")
machine1.connect_switch(switch1, "192.168.1.2")
machine2.connect_switch(switch1, "192.168.1.3")
nm.object.append(switch1)
nm.registries[registry] = [machine1, machine2]
......@@ -120,7 +123,7 @@ def net_demo():
# for node in [g1, m3, m4, m5]:
# print "pid: {}".format(node.pid)
re_if_0, in_if_0 = nemu.P2PInterface.create_pair(registry,internet)
re_if_0, in_if_0 = nemu.P2PInterface.create_pair(registry, internet)
g1_if_0, in_if_1 = nemu.P2PInterface.create_pair(gateway1, internet)
g2_if_0, in_if_2 = nemu.P2PInterface.create_pair(gateway2, internet)
......@@ -169,11 +172,11 @@ def net_demo():
MINIUPnP_CONF = Path(__file__).parent / 'miniupnpd.conf'
gateway1.proc = gateway1.Popen(['miniupnpd', '-d', '-f', MINIUPnP_CONF,
'-P', 'miniupnpd.pid','-a', gateway1.if_s.name,
'-P', 'miniupnpd.pid', '-a', gateway1.if_s.name,
'-i', g1_if_0.name],
stdout=PIPE, stderr=PIPE)
switch1.up = switch2.up = switch3.up =True
switch1.up = switch2.up = switch3.up = True
nm.connectable_test()
return nm
......@@ -192,10 +195,7 @@ def network_direct():
m_if_0.add_v4_address(u"10.1.2.2", prefix_len=24)
re_if_0.up = m_if_0.up = True
for node in [m0]:
app0 = node.Popen(["ping", "-c", "1", "10.1.2.1"], stdout=PIPE)
ret = app0.wait()
assert ret == 0, "network construct failed"
nm.connectable_test()
return nm
if __name__ == "__main__":
......
"""wrap the deploy of re6st node, ease the creation of cert
file and run of the node
"""
import errno
import ipaddress
import json
import logging
import re
import shutil
import sqlite3
import weakref
import ipaddress
import time
import re
import tempfile
import logging
import errno
import time
import weakref
from subprocess import PIPE
from pathlib2 import Path
......@@ -20,11 +20,8 @@ from re6st.tests import DEMO_PATH
WORK_DIR = Path(__file__).parent / "temp_net_test"
DH_FILE = DEMO_PATH / "dh2048.pem"
RE6STNET = "re6stnet"
RE6STNET = "python -m re6st.cli.node"
RE6ST_REGISTRY = "re6st-registry"
RE6ST_REGISTRY = "python -m re6st.cli.registry"
RE6ST_CONF = "re6st-conf"
RE6ST_CONF = "python -m re6st.cli.conf"
def initial():
......@@ -112,15 +109,15 @@ class Re6stRegistry(object):
serial=ip_to_serial(self.ip6))
def run(self):
cmd =['--ca', self.ca_crt, '--key', self.ca_key, '--dh', DH_FILE,
cmd = ['--ca', self.ca_crt, '--key', self.ca_key, '--dh', DH_FILE,
'--ipv4', '10.42.0.0/16', '8', '--logfile', self.log, '--db', self.db,
'--run', self.run_path, '--hello', '4', '--mailhost', 's', '-v4',
'--client-count', (self.client_number+1)//2, '--port', self.port]
#convert PosixPath to str, can be remove in python3
#PY3: convert PosixPath to str, can be remove in Python 3
cmd = map(str, cmd)
cmd = RE6ST_REGISTRY.split() + cmd
cmd[:0] = RE6ST_REGISTRY.split()
logging.debug("run registry %s at ns: %s with cmd: %s",
self.name, self.node.pid, " ".join(cmd))
......@@ -151,7 +148,7 @@ class Re6stNode(object):
node: nemu node
name: name for res6st node
"""
self.name = name if name else self.generate_name()
self.name = name or self.generate_name()
self.node = node
self.registry = weakref.proxy(registry)
......@@ -180,7 +177,7 @@ class Re6stNode(object):
with self.data_file.open() as f:
data = json.load(f)
self.ip6 = data.get("ip6")
recreate = not data.get('hash') == self.registry.ident
recreate = data.get('hash') != self.registry.ident
else:
recreate = True
......@@ -195,7 +192,6 @@ class Re6stNode(object):
self.clean()
def __repr__(self):
return self.name
......@@ -208,19 +204,19 @@ class Re6stNode(object):
"""create necessary file for node"""
logging.info("create dir of node %s", self.name)
cmd = ["--registry", self.registry.url, '--email', self.email]
cmd = RE6ST_CONF.split() + cmd
cmd[:0] = RE6ST_CONF.split()
p = self.node.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE,
cwd=str(self.path))
# read token
db = sqlite3.connect(str(self.registry.db), isolation_level=None)
count = 0
token = None
while not token:
for _ in xrange(100):
time.sleep(.1)
token = db.execute("SELECT token FROM token WHERE email=?",
(self.email,)).fetchone()
count += 1
if count > 100:
if token:
break
else:
p.destroy()
raise Exception("can't connect to the Register")
......@@ -239,8 +235,9 @@ class Re6stNode(object):
'--dh', DH_FILE, '--ca', self.registry.ca_crt, '--cert', self.crt,
'--key', self.key, '-v4', '--registry', self.registry.url,
'--console', self.console]
#PY3: same as for Re6stRegistry.run
cmd = map(str, cmd)
cmd = RE6STNET.split() + cmd
cmd[:0] = RE6STNET.split()
cmd += args
logging.debug("run node %s at ns: %s with cmd: %s",
......@@ -265,15 +262,14 @@ class Re6stNode(object):
logging.debug("%s teminate process %s", self.name, self.proc.pid)
self.proc.destroy()
def __del__(self):
"""teminate process and rm temp dir"""
try:
self.stop()
except Exception as e:
logging.warn("%s: %s", self.name, e)
logging.warning("%s: %s", self.name, e)
# python2 seems auto clean the tempdir
# re6stnet seems auto clean the tempdir
# try:
# shutil.rmtree(self.run_path)
# except Exception as e:
......
......@@ -4,12 +4,11 @@ import unittest
import time
import psutil
import logging
import sqlite3
import random
from binascii import b2a_hex
from pathlib2 import Path
import re6st_wrap
import network_build
import re6st_wrap
PING_PATH = str(Path(__file__).parent.resolve() / "ping.py")
......@@ -53,7 +52,7 @@ def wait_stable(nodes, timeout=240):
# check all the node network can ping each other, in order reverse
unfinished = list(nodes)
while unfinished:
for i in range(len(unfinished)-1, -1, -1):
for i in xrange(len(unfinished)-1, -1, -1):
node = unfinished[i]
if node.ping_proc.poll() is not None:
logging.debug("%s 's network is stable", node.name)
......@@ -63,12 +62,12 @@ def wait_stable(nodes, timeout=240):
if time.time() - now > timeout:
for node in unfinished:
node.ping_proc.destroy()
logging.warn("%s can't ping to all the nodes", unfinished)
logging.warning("%s can't ping to all the nodes", unfinished)
return False
logging.info("wait time cost: %s", time.time() - now)
return True
@unittest.skipIf(os.geteuid() != 0, "require root or create user namespace plz")
@unittest.skipIf(os.geteuid(), "Using root or creating a user namespace")
class TestNet(unittest.TestCase):
""" network test case"""
......
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