Commit e600e173 authored by zhifan huang's avatar zhifan huang

fix: fix problem when merge_requests

remove useless test, correct typo and eek problem, refactor verbose
command

see nexedi/re6stnet!38
parent 3140768d
......@@ -25,9 +25,9 @@ class Node(nemu.Node):
@property
def ip(self):
if hasattr(self, "_ip"):
try:
return str(self._ip)
except AttributeError:
# return 1 ipv4 address of the one interface, reverse mode
for iface in self.get_interfaces()[::-1]:
for addr in iface.get_addresses():
......@@ -49,7 +49,7 @@ class NetManager(object):
"""contain all the nemu object created, so they can live more time"""
def __init__(self):
self.object = []
self.registrys = {}
self.registries = {}
def connectible_test(nm):
......@@ -61,11 +61,13 @@ def connectible_test(nm):
Raise:
AssertionError
"""
for reg in nm.registrys:
for node in nm.registrys[reg]:
for reg in nm.registries:
for node in nm.registries[reg]:
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=PIPE)
ret = app0.wait()
assert ret == 0, "network construct failed {} to {}".format(node.ip, reg.ip)
if ret:
raise ConnectionError(
"network construct failed {} to {}".format(node.ip, reg.ip))
logging.debug("each node can ping to their registry")
......@@ -89,7 +91,7 @@ def net_route():
m2_if_0 = machine2.connect_switch(switch1, "192.168.1.3")
nm.object.append(switch1)
nm.registrys[registry] = [machine1, machine2]
nm.registries[registry] = [machine1, machine2]
connectible_test(nm)
return nm
......@@ -116,7 +118,7 @@ def net_demo():
nm = NetManager()
nm.object = [internet, switch3, switch1, switch2, gateway1, gateway2]
nm.registrys = {registry: [m1, m2, m3, m4, m5, m6, m7, m8]}
nm.registries = {registry: [m1, m2, m3, m4, m5, m6, m7, m8]}
# for node in [g1, m3, m4, m5]:
# print "pid: {}".format(node.pid)
......@@ -183,7 +185,7 @@ def network_direct():
registry = Node()
m0 = Node()
nm = NetManager()
nm.registrys = {registry: [m0]}
nm.registries = {registry: [m0]}
re_if_0, m_if_0 = nemu.P2PInterface.create_pair(registry, m0)
......@@ -200,5 +202,5 @@ def network_direct():
return nm
if __name__ == "__main__":
nm = network_demo()
nm = net_demo()
time.sleep(1000000)
......@@ -29,9 +29,7 @@ class MultiPing(MultiPing):
except socket.timeout:
pass
except socket.error as e:
if e.errno == errno.EWOULDBLOCK:
pass
else:
if e.errno != errno.EWOULDBLOCK:
raise
return pkts
......@@ -43,17 +41,14 @@ def main():
args = parser.parse_args()
addrs = args.a
retry = args.retry
no_responses = ""
while True:
while retry and no_responses:
mp = MultiPing(addrs)
mp.send()
_, no_responses = mp.receive(PING_TIMEOUT)
if retry and no_responses:
continue
else:
sys.stdout.write(" ".join(no_responses))
return
if __name__ == '__main__':
......
......@@ -10,6 +10,7 @@ import time
import re
import tempfile
import logging
import errno
from subprocess import PIPE, call
from pathlib2 import Path
......@@ -117,21 +118,27 @@ class Re6stRegistry(object):
serial=ip_to_serial(self.ip6))
def run(self):
cmd = ("{script} --ca {ca} --key {key} --dh {dh} --ipv4 10.42.0.0/16 8 "
" --logfile {log} --db {db} --run {run} --hello 4 --mailhost s "
"-v4 --client-count {nb}")
cmd = cmd.format(script=RE6ST_REGISTRY, ca=self.ca_crt,
key=self.ca_key, dh=DH_FILE, log=self.log, db=self.db,
run=self.run_path, nb=(self.client_number+1)//2).split()
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]
#convert PosixPath to str, can be remove in python3
cmd = map(str, cmd)
cmd = RE6ST_REGISTRY.split() + cmd
logging.debug("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)
def clean(self):
"""remove the file created last time"""
for f in [self.log]:
if f.exists():
f.unlink()
try:
self.log.unlink()
except OSError as e:
if e.errno != errno.ENOENT:
raise
def __del__(self):
try:
......@@ -206,9 +213,8 @@ class Re6stNode(object):
def create_node(self):
"""create necessary file for node"""
logging.info("create dir of node %s", self.name)
cmd = "{script} --registry {registry_url} --email {email}"
cmd = cmd.format(script=RE6ST_CONF, registry_url=self.registry.url,
email=self.email).split()
cmd = ["--registry", self.registry.url, '--email', self.email]
cmd = RE6ST_CONF.split() + cmd
p = self.node.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE,
cwd=str(self.path))
# read token
......@@ -235,14 +241,13 @@ class Re6stNode(object):
def run(self, *args):
"""execute re6stnet"""
cmd = ("{script} --log {log} --run {run} --state {state}"
" --dh {dh} --ca {ca} --cert {cert} --key {key} -v4"
" --registry {registry} --console {console}"
)
cmd = cmd.format(script=RE6STNET, log=self.path, run=self.run_path,
state=self.path, dh=DH_FILE, ca=self.registry.ca_crt,
cert=self.crt, key=self.key, registry=self.registry.url,
console=self.console).split()
cmd = ['--log', self.path, '--run', self.run_path, '--state', self.path,
'--dh', DH_FILE, '--ca', self.registry.ca_crt, '--cert', self.crt,
'--key', self.key, '-v4', '--registry', self.registry.url,
'--console', self.console]
cmd = map(str, cmd)
cmd = RE6STNET.split() + cmd
cmd += args
logging.debug("run node %s at ns: %s with cmd: %s",
self.name, self.node.pid, " ".join(cmd))
......@@ -252,8 +257,11 @@ class Re6stNode(object):
"""remove the file created last time"""
for name in ["re6stnet.log", "babeld.state", "cache.db", "babeld.log"]:
f = self.path / name
if f.exists():
try:
f.unlink()
except OSError as e:
if e.errno != errno.ENOENT:
raise
def stop(self):
"""stop running re6stnet process"""
......
......@@ -15,23 +15,23 @@ PING_PATH = str(Path(__file__).parent.resolve() / "ping.py")
BABEL_HMAC = 'babel_hmac0', 'babel_hmac1', 'babel_hmac2'
def deploy_re6st(nm, recreate=False):
net = nm.registrys
net = nm.registries
nodes = []
registrys = []
registries = []
re6st_wrap.Re6stRegistry.registry_seq = 0
re6st_wrap.Re6stNode.node_seq = 0
for registry in net:
reg = re6st_wrap.Re6stRegistry(registry, "2001:db8:42::", len(net[registry]),
recreate=recreate)
reg_node = re6st_wrap.Re6stNode(registry, reg, name=reg.name)
registrys.append(reg)
registries.append(reg)
reg_node.run("--gateway", "--disable-proto", "none", "--ip", registry.ip)
nodes.append(reg_node)
for m in net[registry]:
node = re6st_wrap.Re6stNode(m, reg)
node.run("-i" + m.iface.name)
nodes.append(node)
return nodes, registrys
return nodes, registries
def wait_stable(nodes, timeout=240):
"""try use ping6 from each node to the other until ping success to all the
......
"""Re6st unittest module
"""
\ No newline at end of file
# contatin the test case
__all__ = ["test_registry",
"test_registry_client",
"test_conf",
"test_tunnel"]
......@@ -90,6 +90,7 @@ class TestRegistryServer(unittest.TestCase):
"0000000000000\0" # ERROR, IndexError: msg is null
]
try:
res1 = self.server.recv(4)
res2 = self.server.recv(4)
res3 = self.server.recv(4)
......@@ -99,7 +100,9 @@ class TestRegistryServer(unittest.TestCase):
self.assertEqual(res2, (None, None)) # binary to digital failed
self.assertEqual(res3, (None, None)) # code don't match
self.assertEqual(res4, ("0001001001001", "a_msg"))
except:
pass
finally:
del self.server.sock.recv
def test_onTimeout(self):
......
__all__ = ["test_multi_gateway_manager", "test_base_tunnel_manager"]
......@@ -39,75 +39,7 @@ class testBaseTunnelManager(unittest.TestCase):
self.tunnel.close()
del self.tunnel
@patch("re6st.tunnel.BaseTunnelManager._babel_dump_one", create=True)
@patch("re6st.tunnel.BaseTunnelManager._babel_dump_two", create=True)
def test_babel_dump(self, two, one):
""" case two func in requesting_dump"""
self.tunnel._BaseTunnelManager__requesting_dump = set(['one', 'two'])
self.tunnel.babel_dump()
# assert is empty
self.assertFalse(self.tunnel._BaseTunnelManager__requesting_dump)
one.assert_called_once()
two.assert_called_once()
@patch("re6st.ctl.Babel.request_dump")
def test_request_dump_empty(self, request_dump):
"""case when self.__requesting_dump is None or empty"""
reason = "rina"
self.tunnel._BaseTunnelManager__request_dump(reason)
self.assertEqual(self.tunnel._BaseTunnelManager__requesting_dump, set([reason]))
request_dump.assert_called_once()
@patch("re6st.ctl.Babel.request_dump")
def test___request_dump_not_empty(self, request_dump):
"""case when self.__requesting_dump is not empty"""
self.tunnel._BaseTunnelManager__requesting_dump = set(["rina"])
reason = "reason"
self.tunnel._BaseTunnelManager__request_dump(reason)
self.assertEqual(self.tunnel._BaseTunnelManager__requesting_dump, set([reason, "rina"]))
request_dump.assert_not_called()
def test_selectTimeout_add_callback(self):
"""case add new callback"""
self.tunnel._timeouts = [(1, self.tunnel.close)]
callback = self.tunnel.babel_dump
self.tunnel.selectTimeout(10, callback)
self.assertIn((10, callback), self.tunnel._timeouts)
def test_selectTimeout_removing(self):
"""case remove a callback"""
removed = self.tunnel.babel_dump
self.tunnel._timeouts = [(1, self.tunnel.close), (10, removed)]
self.tunnel.selectTimeout(None, removed)
self.assertEqual(self.tunnel._timeouts, [(1, self.tunnel.close)])
def test_selectTimeout_update(self):
"""case update a callback"""
updated = self.tunnel.babel_dump
self.tunnel._timeouts = [(1, self.tunnel.close), (10, updated)]
self.tunnel.selectTimeout(100, updated)
self.assertEqual(self.tunnel._timeouts, [(1, self.tunnel.close), (100, updated)])
#TODO selectTimeout in contain callback, removing, update
@patch("re6st.tunnel.BaseTunnelManager.selectTimeout")
def test_invalidatePeers(self, selectTimeout):
......@@ -173,17 +105,6 @@ class testBaseTunnelManager(unittest.TestCase):
self.assertEqual(res, "1,1,1;0,0,0;2,2,2")
@patch("re6st.tunnel.BaseTunnelManager.selectTimeout")
def test_processPacket_version(self, selectTimeout):
"""code is 0, for network version, peer is none"""
c = chr(0)
self.tunnel._processPacket(c)
self.assertEqual(selectTimeout.call_args[0][1], self.tunnel.newVersion)
@patch("re6st.x509.Cert.verifyVersion", Mock(return_value=True))
@patch("re6st.tunnel.BaseTunnelManager.selectTimeout")
def test_processPacket_version(self, selectTimeout):
......@@ -194,13 +115,13 @@ class testBaseTunnelManager(unittest.TestCase):
peer = x509.Peer("000001")
version1 = "00003"
version2 = "00007"
self.tunnel._version = "00005"
self.tunnel._version = version3 = "00005"
self.tunnel._peers.append(peer)
res = self.tunnel._processPacket(c + version1, peer)
self.tunnel._processPacket(c + version2, peer)
self.assertEqual(res, "00005")
self.assertEqual(res, version3)
self.assertEqual(self.tunnel._version, version2)
self.assertEqual(peer.version, version2)
self.assertEqual(selectTimeout.call_args[0][1], self.tunnel.newVersion)
......
......@@ -51,10 +51,7 @@ def generate_cert(ca, ca_key, csr, prefix, serial, not_after=None):
cert.set_pubkey(req.get_pubkey())
cert.set_serial_number(serial)
cert.sign(ca_key, 'sha512')
cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
return cert
return crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
def create_cert_file(pkey_file, cert_file, ca, ca_key, prefix, serial):
pkey, csr = generate_csr()
......
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