Commit 6b4aec14 authored by zhifan huang's avatar zhifan huang

change output

parent 61149ff9
import subprocess import subprocess
import time from subprocess import PIPE
import random
import weakref import weakref
import sys import sys
import logging
class NetManager(object): class NetManager(object):
"""contain all the nemu object created, so they can live more time""" """contain all the nemu object created, so they can live more time"""
...@@ -10,6 +10,7 @@ class NetManager(object): ...@@ -10,6 +10,7 @@ class NetManager(object):
self.object = [] self.object = []
self.registrys = {} self.registrys = {}
class Device(object): class Device(object):
_id = 0 _id = 0
...@@ -54,7 +55,6 @@ class Bridge(Device): ...@@ -54,7 +55,6 @@ class Bridge(Device):
self.up = True self.up = True
class Netns(object): class Netns(object):
"""a network namespace""" """a network namespace"""
@property @property
...@@ -67,33 +67,32 @@ class Netns(object): ...@@ -67,33 +67,32 @@ class Netns(object):
def __init__(self): def __init__(self):
self.devices = [] self.devices = []
self.app = subprocess.Popen(['unshare', '-n'], stdin=subprocess.PIPE) self.app = subprocess.Popen(['unshare', '-n'], stdin=PIPE)
self.pid = self.app.pid self.pid = self.app.pid
lo = Device("lo", name="lo") lo = Device("lo", name="lo")
self.add_device(lo) self.add_device(lo)
lo.up = True lo.up = True
self.run(['sysctl', '-w', 'net.ipv4.ip_forward=1']) self.run(['sysctl', '-w', 'net.ipv4.ip_forward=1'], stdout=PIPE)
self.run(['sysctl', '-w', 'net.ipv6.conf.default.forwarding=1'], stderr=sys.stderr) self.run(['sysctl', '-w', 'net.ipv6.conf.default.forwarding=1'], stdout=PIPE)
def Popen(self, cmd, stdout=sys.stdout, stderr=sys.stderr,**kw): def Popen(self, cmd, stdout=sys.stdout, stderr=sys.stderr,**kw):
""" a wrapper for subprocess.Popen""" """ wrapper for subprocess.Popen"""
return subprocess.Popen(['nsenter', '-t', str(self.pid), '-n'] + cmd, stdout=stdout, stderr=stderr, **kw) return subprocess.Popen(['nsenter', '-t', str(self.pid), '-n'] + cmd, stdout=stdout, stderr=stderr, **kw)
def run(self, cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,**kw): def run(self, cmd, stdout=sys.stdout, stderr=sys.stderr,**kw):
""" wrapper for subprocess.checkout""" """ wrapper for subprocess.checkout"""
subprocess.check_call(['nsenter', '-t', str(self.pid), '-n'] + cmd, stdout=stdout, stderr=stderr, **kw) subprocess.check_call(['nsenter', '-t', str(self.pid), '-n'] + cmd, stdout=stdout, stderr=stderr, **kw)
def add_device(self, dev): def add_device(self, dev):
self.devices.append(dev) self.devices.append(dev)
dev.net = weakref.proxy(self) dev.net = weakref.proxy(self)
@staticmethod @staticmethod
def connect_direct(node1, node2): def connect_direct(node1, node2):
"""connect 2 node by veth""" """connect 2 node by veth"""
...@@ -120,7 +119,6 @@ class Netns(object): ...@@ -120,7 +119,6 @@ class Netns(object):
return dev1, dev2 return dev1, dev2
def main(): def main():
app1 = Netns() app1 = Netns()
app2 = Netns() app2 = Netns()
...@@ -132,22 +130,27 @@ def main(): ...@@ -132,22 +130,27 @@ def main():
subprocess.check_call(['nsenter', '-t', str(app2.pid), '-n'] + ["ping", "10.1.1.1"]) subprocess.check_call(['nsenter', '-t', str(app2.pid), '-n'] + ["ping", "10.1.1.1"])
def connectible_test(nm):
"""test each node can ping to their registry
nm: NetManger
"""
for reg in nm.registrys:
for node in nm.registrys[reg]:
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=subprocess.PIPE)
ret = app0.wait()
assert ret == 0, "network construct failed"
logging.debug("each node can ping to their registry")
def net_simple(): def net_simple():
nm = NetManager() nm = NetManager()
node1 = Netns() node1 = Netns()
node2 = Netns() node2 = Netns()
Netns.connect_direct(node1, node2) Netns.connect_direct(node1, node2)
# subprocess.check_call(['nsenter', '-t', str(node2.pid), '-n'] + ["ping", '-c', '1', "10.1.1.1"])
nm.registrys[node1] = [node2] nm.registrys[node1] = [node2]
connectible_test(nm)
for reg in nm.registrys:
for node in nm.registrys[reg]:
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=subprocess.PIPE)
ret = app0.wait()
assert ret == 0, "network construct failed"
return nm return nm
...@@ -173,12 +176,7 @@ def net_route(): ...@@ -173,12 +176,7 @@ def net_route():
nm.object.append(router) nm.object.append(router)
nm.registrys[registry] = [node1, node2] nm.registrys[registry] = [node1, node2]
for reg in nm.registrys: connectible_test(nm)
for node in nm.registrys[reg]:
app0 = node.Popen(["ping", "-c", "1", reg.ip], stdout=subprocess.PIPE)
ret = app0.wait()
assert ret == 0, "network construct failed"
return nm return nm
......
import os
import sys
import json import json
import shutil import shutil
import sqlite3 import sqlite3
...@@ -8,10 +6,10 @@ import ipaddress ...@@ -8,10 +6,10 @@ import ipaddress
import time import time
import re import re
import tempfile import tempfile
import logging
from subprocess import PIPE, call from subprocess import PIPE, call
from pathlib2 import Path from pathlib2 import Path
import re6st.tests.tools as tools import re6st.tests.tools as tools
WORK_DIR = Path(__file__).parent.resolve() / "temp_net_test" WORK_DIR = Path(__file__).parent.resolve() / "temp_net_test"
...@@ -27,7 +25,8 @@ def initial(): ...@@ -27,7 +25,8 @@ def initial():
if not WORK_DIR.exists(): if not WORK_DIR.exists():
WORK_DIR.mkdir() WORK_DIR.mkdir()
if not DH_FILE.exists(): if not DH_FILE.exists():
call("openssl dhparam -out %s 2048" % str(DH_FILE), shell=True) logging.info("create dh file")
call(['openssl', 'dhparam', '-out', str(DH_FILE), '2048'], stderr=PIPE)
def ip_to_serial(ip6): def ip_to_serial(ip6):
...@@ -64,10 +63,9 @@ class Re6stRegistry(object): ...@@ -64,10 +63,9 @@ class Re6stRegistry(object):
# use hash to identify the registry # use hash to identify the registry
with self.ca_key.open() as f: with self.ca_key.open() as f:
text = f.readlines() text = f.read()
text = ''.join(text)
self.ident = hash(text) self.ident = hash(text)
self.run() self.run()
# wait the servcice started # wait the servcice started
...@@ -81,6 +79,7 @@ class Re6stRegistry(object): ...@@ -81,6 +79,7 @@ class Re6stRegistry(object):
except socket.error: except socket.error:
time.sleep(.1) time.sleep(.1)
"""]).wait() """]).wait()
logging.debug("re6st service started")
@classmethod @classmethod
def generate_name(cls): def generate_name(cls):
...@@ -160,7 +159,7 @@ class Re6stNode(object): ...@@ -160,7 +159,7 @@ class Re6stNode(object):
if not self.path.exists(): if not self.path.exists():
self.path.mkdir() self.path.mkdir()
self.create_node() self.create_node()
sys.stderr.write("{}'s subnet is {}\n".format(self.name, self.ip6)) logging.info("{}'s subnet is {}".format(self.name, self.ip6))
@classmethod @classmethod
...@@ -171,14 +170,14 @@ class Re6stNode(object): ...@@ -171,14 +170,14 @@ class Re6stNode(object):
def create_node(self): def create_node(self):
"""create necessary file for node""" """create necessary file for node"""
sys.stderr.write("---create file for node {}---\n".format(self.name)) logging.info("create dir of node {}".format(self.name))
cmd = "{script} --registry {registry_url} --email {email}" cmd = "{script} --registry {registry_url} --email {email}"
cmd = cmd.format(script=RE6ST_CONF, registry_url=self.registry.url, cmd = cmd.format(script=RE6ST_CONF, registry_url=self.registry.url,
email=self.email).split() email=self.email).split()
p = self.node.Popen(cmd, stdin=PIPE, stdout=PIPE, p = self.node.Popen(cmd, stdin=PIPE, stdout=PIPE, cwd=str(self.path))
cwd=str(self.path))
db = sqlite3.connect(str(self.registry.db), isolation_level=None)
# read token
db = sqlite3.connect(str(self.registry.db), isolation_level=None)
count = 0 count = 0
token = None token = None
while not token: while not token:
...@@ -189,10 +188,11 @@ class Re6stNode(object): ...@@ -189,10 +188,11 @@ class Re6stNode(object):
if count > 100: if count > 100:
p.terminate() p.terminate()
raise Exception("can't connect to the Register") raise Exception("can't connect to the Register")
out, _ = p.communicate(str(token[0])) out, _ = p.communicate(str(token[0]))
print "output: {}".format(out) 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) self.ip6 = re.search('(?<=subnet: )[0-9:a-z]+', out).group(0)
data = {'ip6': self.ip6, 'hash': self.registry.ident} data = {'ip6': self.ip6, 'hash': self.registry.ident}
with open(str(self.data_file), 'w') as f: with open(str(self.data_file), 'w') as f:
json.dump(data, f) json.dump(data, f)
......
"""contain ping-test for re6set net""" """contain ping-test for re6set net"""
import unittest import unittest
import time import time
import re6st_wrap
import subprocess import subprocess
import sys import sys
import os import os
import logging
from pathlib2 import Path from pathlib2 import Path
import re6st_wrap
import my_net import my_net
PING_PATH = str(Path(__file__).parent.resolve() / "ping.py") PING_PATH = str(Path(__file__).parent.resolve() / "ping.py")
...@@ -16,12 +17,11 @@ def deploy_re6st(nm): ...@@ -16,12 +17,11 @@ def deploy_re6st(nm):
nodes = [] nodes = []
registrys = [] registrys = []
for registry in net: for registry in net:
reg = re6st_wrap.Re6stRegistry(registry, "2001:db8:42::", recreate=True) reg = re6st_wrap.Re6stRegistry(registry, "2001:db8:42::", recreate=False)
reg_node = re6st_wrap.Re6stNode(registry, reg, name=reg.name) reg_node = re6st_wrap.Re6stNode(registry, reg, name=reg.name)
registrys.append(reg) registrys.append(reg)
reg_node.run("--gateway", "--disable-proto", "none", "--ip", registry.ip) reg_node.run("--gateway", "--disable-proto", "none", "--ip", registry.ip)
nodes.append(reg_node) nodes.append(reg_node)
print("add nodes")
for m in net[registry]: for m in net[registry]:
node = re6st_wrap.Re6stNode(m, reg) node = re6st_wrap.Re6stNode(m, reg)
node.run("-i" + m.out.name) node.run("-i" + m.out.name)
...@@ -29,7 +29,7 @@ def deploy_re6st(nm): ...@@ -29,7 +29,7 @@ def deploy_re6st(nm):
return nodes, registrys return nodes, registrys
def wait_stable(nodes, timeout=180): def wait_stable(nodes, timeout=180):
sys.stderr.write("wait stalbe\n") logging.info("wait all node stable")
now = time.time() now = time.time()
# wait all the nodes can connect to each other # wait all the nodes can connect to each other
ips = {node.ip6:node.name for node in nodes} ips = {node.ip6:node.name for node in nodes}
...@@ -43,10 +43,10 @@ def wait_stable(nodes, timeout=180): ...@@ -43,10 +43,10 @@ def wait_stable(nodes, timeout=180):
for i in range(len(unfinished)-1,-1,-1): for i in range(len(unfinished)-1,-1,-1):
node = unfinished[i] node = unfinished[i]
if node.ping_proc.poll() is not None: if node.ping_proc.poll() is not None:
sys.stderr.write("{}'s network is stable\n".format(node.name)) logging.info("{}'s network is stable".format(node.name))
unfinished.pop(i) unfinished.pop(i)
node.ping_proc.wait()
time.sleep(0.5) time.sleep(0.5)
if time.time() - now > timeout: if time.time() - now > timeout:
for node in unfinished: for node in unfinished:
node.ping_proc.terminate() node.ping_proc.terminate()
...@@ -55,7 +55,7 @@ def wait_stable(nodes, timeout=180): ...@@ -55,7 +55,7 @@ def wait_stable(nodes, timeout=180):
def ping_test(nodes): def ping_test(nodes):
"""test Re6st node connecty """ """test Re6st node connecty """
ips = {node.ip6:node.name for node in nodes} ips = {node.ip6:node.name for node in nodes}
sys.stderr.write("start ping test\n") logging.info("start ping test")
failed = False failed = False
for node in nodes: for node in nodes:
sub_ips = set(ips) - {node.ip6} sub_ips = set(ips) - {node.ip6}
...@@ -64,9 +64,9 @@ def ping_test(nodes): ...@@ -64,9 +64,9 @@ def ping_test(nodes):
out, _ = node.ping_proc.communicate() out, _ = node.ping_proc.communicate()
unreached = [ ips[addr] for addr in out.split()] unreached = [ ips[addr] for addr in out.split()]
if unreached: if unreached:
sys.stderr.write("{} can't ping to {}\n".format(node.name, ",".join(unreached))) logging.warning("{} can't ping to {}".format(node.name, ",".join(unreached)))
failed = True failed = True
sys.stderr.write("ping test finish\n") logging.info("ping test finish")
return failed return failed
...@@ -76,21 +76,13 @@ class TestPing(unittest.TestCase): ...@@ -76,21 +76,13 @@ class TestPing(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
"""set up for all case""" """create work dir"""
# re6st_wrap.WORK_DIR = xxx # chage the work dir logging.basicConfig(level=logging.INFO)
re6st_wrap.initial() re6st_wrap.initial()
def tearDown(self): def tearDown(self):
# terminate all the sub process # terminate all the sub process
return return
p = subprocess.Popen("ps -aux | grep re6st | grep -v grep", shell=True, stdout =subprocess.PIPE)
out, _ = p.communicate()
out = str(out)
out = out.split('\n')
out.pop()
for proc in out:
proc = proc.split()[1]
subprocess.call(["kill", "-15", proc])
def test_sample(self): def test_sample(self):
...@@ -120,7 +112,7 @@ class TestPing(unittest.TestCase): ...@@ -120,7 +112,7 @@ class TestPing(unittest.TestCase):
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main(verbosity=3)
......
from crypt import crypt
import sys import sys
import os import os
import time import time
import subprocess import subprocess
from OpenSSL import crypto
from OpenSSL import crypto
from re6st import registry from re6st import registry
......
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