Commit 11bb5b72 authored by zhifan huang's avatar zhifan huang

test: add new module to create network

add new module to easily create network. Only need to define points and
edges, and than, each node's ip address and route are auto-configed.

add a network typo, where the send and receive of packets use different
routes.

add 3 test, 2 is ping test. 1 is new speed test.
speed test: send 1Mb message, and test the cost of time.
parent 09ea6992
table inet limit_demo {
limit lim_1mbps { rate 256 kbytes/second}
chain limit_chain {
type filter hook postrouting priority 0; policy drop;
limit name "lim_1mbps" accept
}
}
This diff is collapsed.
#!/bin/python3
import argparse
import socket
HOST = "127.0.0.1"
PORT = 50000
REPEAT = 1 << 8
TESTDATA = b"miku" * 1024
parser = argparse.ArgumentParser()
parser.add_argument("host", type=str, metavar="ip address", default=HOST)
parser.add_argument("port", default=PORT, nargs="?")
parser.add_argument("-6", "--ipv6", action="store_true")
args = parser.parse_args()
if args.ipv6:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((args.host, PORT))
for i in range(REPEAT):
sock.send(TESTDATA)
sock.close()
import argparse
import logging
import socket
import time
logger = logging.getLogger(__name__)
HOST_4 = "0.0.0.0"
HOST_6 = "::"
PORT = 50000
BUFFER = 4096
parser = argparse.ArgumentParser()
parser.add_argument("-6", "--ipv6", action="store_true")
args = parser.parse_args()
if args.ipv6:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.bind((HOST_6, PORT))
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST_4, PORT))
sock.listen()
client, addr = sock.accept()
start = time.perf_counter()
count = 0
while True:
data = client.recv(BUFFER)
if data:
count += len(data)
continue
client.close()
end = time.perf_counter()
delta = end - start
logger.info("time use %.2fs, speed %.2fMB/s"
, delta, count / delta / (1 << 20))
break
print(delta)
"""contain ping-test for re6set net"""
import os
import unittest
import time
import psutil
import logging
import os
import random
import time
import unittest
from pathlib2 import Path
from subprocess import PIPE
import psutil
from re6st.tests.test_network import my_net, network_build, re6st_wrap
PING_PATH = str(Path(__file__).parent.resolve() / "ping.py")
SPEED_TEST_CLIENT = str(Path(__file__).parent.resolve() / "speed_test_client.py")
SPEED_TEST_SERVER = str(Path(__file__).parent.resolve() / "speed_test_server.py")
def deploy_re6st(nm, recreate=False):
net = nm.registries
......@@ -30,6 +35,34 @@ def deploy_re6st(nm, recreate=False):
nodes.append(node)
return nodes, registries
def find_node(nodes, name):
for node in nodes:
if node.node.name == name:
return node
def speed_test(client, server, protocol=6):
"""client sent 1MB message to server
return:
the time cost of server to receive all packet.
"""
server_cmd = ['python3', SPEED_TEST_SERVER]
client_cmd = ['python3', SPEED_TEST_CLIENT]
if protocol == 6:
ip_addr = server.ip6 + '1'
server_cmd.append('-6')
client_cmd.append('-6')
else:
ip_addr = server.ip
client_cmd.append(ip_addr)
proc = server.node.Popen(server_cmd, stdout=PIPE)
time.sleep(3)
client.node.run(client_cmd)
out, _ = proc.communicate()
return float(out)
def wait_stable(nodes, timeout=240):
"""try use ping6 from each node to the other until ping success to all the
other nodes
......@@ -89,6 +122,49 @@ class TestNet(unittest.TestCase):
# except:
# pass
def test_switch_route(self):
"""A network containing 3 routes from n4 to n1, the default limit
bandwidth at 256kb/s. The others don't have a limit. test speed from n4
to n1
This test is not sufficient. It proves re6stnet can choose a different
route than the default. It doesn't guarantee re6stnet will choose an
optimized route."""
nm = my_net.diff_route()
nodes, _ = deploy_re6st(nm)
wait_stable(nodes, 40)
n1 = find_node(nodes, 'n1')
n2 = find_node(nodes, 'n2')
n4 = find_node(nodes, 'n4')
self.assertTrue(speed_test(n2, n1) > 1,
"route config fault, limit on bandwidth not work.")
self.assertTrue(speed_test(n4, n1) < 1,
"use the default route, not a optimized route")
time.sleep(5)
# speed)
def test_ping_complete_net(self):
"""ping test a complete network"""
nm = my_net.complete_net(8)
nodes, _ = deploy_re6st(nm, True)
wait_stable(nodes, 50)
time.sleep(10)
self.assertTrue(wait_stable(nodes, 40), " ping test failed")
def test_ping_ring_net(self):
"""ping test a ring net"""
nm = my_net.ring_net(8)
nodes, _ = deploy_re6st(nm, True)
wait_stable(nodes, 50)
time.sleep(10)
self.assertTrue(wait_stable(nodes, 40), " ping test failed")
def test_ping_router(self):
"""create a network in a net segment, test the connectivity by ping
"""
......
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