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

change output

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