Commit e99fcb5b authored by zhifan huang's avatar zhifan huang

dada

parent 8b3dfc3d
"""thie moudle use net namespace to create different net node"""
import subprocess import subprocess
from subprocess import PIPE from subprocess import PIPE
import weakref import weakref
import sys import sys
import os
import logging import logging
import time import time
import ipaddress import ipaddress
...@@ -53,13 +54,6 @@ class Device(object): ...@@ -53,13 +54,6 @@ class Device(object):
class Netns(object): class Netns(object):
"""a network namespace""" """a network namespace"""
@property
def ip(self):
return self.out.ips[-1]
@property
def out(self):
return self.devices[-1]
def __init__(self): def __init__(self):
self.devices = [] self.devices = []
...@@ -140,6 +134,23 @@ class Netns(object): ...@@ -140,6 +134,23 @@ class Netns(object):
except: except:
pass pass
class Host(Netns):
"""node used to run a application, not for connecting
use the first create veth, and it's ip to avoid multi device/ip situation
"""
def __init__(self):
super(Host, self).__init__()
@property
def ip(self):
return self.out.ips[0]
@property
def out(self):
return self.devices[1]
def connectible_test(nm): def connectible_test(nm):
"""test each node can ping to their registry """test each node can ping to their registry
nm: NetManger nm: NetManger
...@@ -152,14 +163,13 @@ def connectible_test(nm): ...@@ -152,14 +163,13 @@ def connectible_test(nm):
logging.debug("each node can ping to their registry") logging.debug("each node can ping to their registry")
def net_simple(): def net_simple():
""" registry .1 ------ .2 node """ registry .1 ------ .2 node
10.1.1 10.1.1
""" """
nm = NetManager() nm = NetManager()
node1 = Netns() node1 = Host()
node2 = Netns() node2 = Host()
dev1, dev2 = node1.connect_direct(node2) dev1, dev2 = node1.connect_direct(node2)
dev1.add_ip4("10.1.1.1", prefix=24) dev1.add_ip4("10.1.1.1", prefix=24)
dev2.add_ip4("10.1.1.2", prefix=24) dev2.add_ip4("10.1.1.2", prefix=24)
...@@ -177,9 +187,9 @@ def net_route(): ...@@ -177,9 +187,9 @@ def net_route():
router = Netns() router = Netns()
router.add_device_bridge() router.add_device_bridge()
registry = Netns() registry = Host()
node1 = Netns() node1 = Host()
node2 = Netns() node2 = Host()
veth_r, _ = registry.connect_router(router) veth_r, _ = registry.connect_router(router)
veth_n1, _ = node1.connect_router(router) veth_n1, _ = node1.connect_router(router)
...@@ -196,43 +206,41 @@ def net_route(): ...@@ -196,43 +206,41 @@ def net_route():
return nm return nm
def net_demo(): def net_demo():
# Underlying network: """
# Underlying network:
# registry .2------ ------.2 registry2
# | | registry .2------
# 10.0.0| |10.3.0 |
# .1 | |.1 10.0.0|
# ---------------Internet---------------- .1 |
# |.1 |.1 |.1 ---------------Internet----------------
# |10.1.0 |10.2.0 | |.1 |.1 |.1
# |.2 |.2 | |10.1.0 |10.2.0 |
# gateway1 gateway2 s3:10.0.1 |.2 |.2 |
# |.1 |.1 |.2 |.3 |.4 gateway1 gateway2 s3:10.0.1
# s1:10.1.1 --s2:10.2.1-- m6 m7 m8 |.1 |.1 |.2 |.3 |.4
# |.2 |.3 |.2 |.3 |.4 |.5 s1:10.1.1 --s2:10.2.1-- m6 m7 m8
# m1 m2 m3 m4 m5 m10 |.2 |.3 |.2 |.3 |.4
# m1 m2 m3 m4 m5
"""
nm = NetManager() nm = NetManager()
internet = Netns() internet = Netns()
gateway1 = Netns() gateway1 = Netns()
router1 = Netns() router1 = Netns()
gateway2 = Netns() gateway2 = Netns()
router2 = Netns() router2 = Netns()
router3 = Netns() router3 = Netns()
registry = Netns() registry = Host()
node1 = Netns() node1 = Host()
node2 = Netns() node2 = Host()
node3 = Netns() node3 = Host()
node4 = Netns() node4 = Host()
node5 = Netns() node5 = Host()
node6 = Netns() node6 = Host()
node7 = Netns() node7 = Host()
node8 = Netns() node8 = Host()
router1.add_device_bridge() router1.add_device_bridge()
router2.add_device_bridge() router2.add_device_bridge()
...@@ -251,6 +259,7 @@ def net_demo(): ...@@ -251,6 +259,7 @@ def net_demo():
veth_g1_1.add_ip4("10.1.0.2", 24) veth_g1_1.add_ip4("10.1.0.2", 24)
gateway1.add_route("10.0.0.0/8", 'via', "10.1.0.1") gateway1.add_route("10.0.0.0/8", 'via', "10.1.0.1")
# sign ip for node
ip = ipaddress.ip_address(u"10.1.1.1") ip = ipaddress.ip_address(u"10.1.1.1")
for node in [gateway1, node1, node2]: for node in [gateway1, node1, node2]:
dev, _ = node.connect_router(router1) dev, _ = node.connect_router(router1)
...@@ -288,14 +297,14 @@ def net_demo(): ...@@ -288,14 +297,14 @@ def net_demo():
internet.add_route("10.2.0.0/16", 'via', "10.2.0.2") internet.add_route("10.2.0.0/16", 'via', "10.2.0.2")
# time.sleep(100000) gateway1.proc = gateway1.Popen(['miniupnpd', '-d', '-f', 'miniupnpd.conf', '-P', 'miniupnpd.pid',
'-a', gateway1.devices[-1].name, '-i', gateway1.devices[-1].name], stdout=PIPE, stderr=PIPE)
nm.object += [internet, gateway1, gateway2, router1, router2, router3] nm.object += [internet, gateway1, gateway2, router1, router2, router3]
nm.registrys[registry] = [node1, node2, node3, node4, node5, node6, node7, node8] nm.registrys[registry] = [node1, node2, node3, node4, node5, node6, node7, node8]
connectible_test(nm) connectible_test(nm)
gateway1.proc = gateway1.Popen(['miniupnpd', '-d', '-f', 'miniupnpd.conf', '-P', 'miniupnpd.pid',
'-a', gateway1.devices[-1].name, '-i', gateway1.devices[-1].name], stdout=PIPE, stderr=PIPE)
return nm return nm
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -131,7 +131,7 @@ class TestNet(unittest.TestCase): ...@@ -131,7 +131,7 @@ class TestNet(unittest.TestCase):
nm = my_net.net_route() nm = my_net.net_route()
nodes, registrys = deploy_re6st(nm) nodes, registrys = deploy_re6st(nm)
wait_stable(nodes, 20) wait_stable(nodes, 40)
time.sleep(10) time.sleep(10)
self.assertTrue(wait_stable(nodes, 20), " ping test failed") self.assertTrue(wait_stable(nodes, 20), " ping test failed")
......
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