Commit 8b3dfc3d authored by zhifan huang's avatar zhifan huang

ad reboot test

parent 29496295
from binascii import b2a_hex
import psutil
import logging
BABEL_HMAC = 'babel_hmac0', 'babel_hmac1', 'babel_hmac2'
def getConfig(db, name):
r = db.execute("SELECT value FROM config WHERE name=?", (name,)).fetchone()
if r:
return b2a_hex(*r)
def checkHMAC(db, machines):
hmac = [getConfig(db, k) for k in BABEL_HMAC]
rc = True
for x in psutil.Process().children(True):
if x.name() == 'babeld':
sign = accept = None
args = x.cmdline()
for x in args:
if x.endswith('/babeld.log'):
if x[:-11].split('/')[-1] not in machines:
break
elif x.startswith('key '):
x = x.split()
if 'sign' in x:
sign = x[-1]
elif 'accept' in x:
accept = x[-1]
else:
i = 0 if hmac[0] else 1
if hmac[i] != sign or hmac[i+1] != accept:
logging.warn('HMAC config wrong for in %s' % args)
logging.warn("HMAC sign: {}, accept: {}".format(sign, accept))
rc = False
if rc:
logging.info('All nodes use Babel with the correct HMAC configuration')
else:
logging.warn('Expected config: %s' % dict(zip(BABEL_HMAC, hmac)))
return rc
from sys import prefix import time
import nemu import nemu
import weakref import weakref
import ipaddress import ipaddress
...@@ -102,8 +102,8 @@ def network_demo(): ...@@ -102,8 +102,8 @@ def network_demo():
nm.registrys = {registry: [m0, m1, m2, m3, m4, m5]} nm.registrys = {registry: [m0, m1, m2, m3, m4, m5]}
# for node in [registry, m0, m2, m3, m4, m5]: for node in [registry, internet, m0, m2, m3, m4, m5]:
# print "pid: {}".format(node.pid) print "pid: {}".format(node.pid)
g0_if_0, _ = g1.connect_network(internet) g0_if_0, _ = g1.connect_network(internet)
m0.connect_network(internet) m0.connect_network(internet)
...@@ -145,12 +145,13 @@ def network_demo(): ...@@ -145,12 +145,13 @@ def network_demo():
node.add_route(nexthop="10.1.10.1") node.add_route(nexthop="10.1.10.1")
for node in [m0, m1, m2, m3, m4, m5]: for node in [m0, m1, m2, m3, m4, m5]:
app0 = node.Popen(["ping", "-c", "1", registry.ip], stdout=PIPE) app0 = node.Popen(["ping", "-c", "1", registry.ip], stdout=PIPE)
ret = app0.wait() ret = app0.wait()
assert ret == 0, "network construct failed" assert ret == 0, "network construct failed"
time.sleep(1000000)
return nm return nm
...@@ -176,4 +177,4 @@ def network_direct(): ...@@ -176,4 +177,4 @@ def network_direct():
return nm return nm
if __name__ == "__main__": if __name__ == "__main__":
network_direct() network_demo()
...@@ -264,14 +264,18 @@ class Re6stNode(object): ...@@ -264,14 +264,18 @@ class Re6stNode(object):
if f.exists(): if f.exists():
f.unlink() f.unlink()
def stop(self):
"""stop running re6stnet process"""
logging.debug("{} teminate process {}".format(self.name, self.proc.pid))
self.proc.terminate()
# timeout only in python3. deadlock maybe
wait_ps(self.proc, 2)
def __del__(self): def __del__(self):
"""teminate process and rm temp dir""" """teminate process and rm temp dir"""
try: try:
logging.debug("{} teminate process {}".format(self.name, self.proc.pid)) self.stop()
self.proc.terminate()
# timeout only in python3. deadlock maybe
wait_ps(self.proc)
except Exception as e: except Exception as e:
logging.warn("{}: {}".format(self.name, e)) logging.warn("{}: {}".format(self.name, e))
......
"""contain ping-test for re6set net""" """contain ping-test for re6set net"""
import os
import unittest import unittest
import time import time
import subprocess
import sys
import psutil import psutil
import os
import logging import logging
import sqlite3 import sqlite3
import random
from binascii import b2a_hex
from pathlib2 import Path from pathlib2 import Path
import re6st_wrap import re6st_wrap
import my_net import my_net
import hmac
import random
PING_PATH = str(Path(__file__).parent.resolve() / "ping.py") PING_PATH = str(Path(__file__).parent.resolve() / "ping.py")
BABEL_HMAC = 'babel_hmac0', 'babel_hmac1', 'babel_hmac2'
def deploy_re6st(nm): def deploy_re6st(nm, recreate=False):
net = nm.registrys net = nm.registrys
nodes = [] nodes = []
registrys = [] registrys = []
re6st_wrap.Re6stRegistry.registry_seq = 0 re6st_wrap.Re6stRegistry.registry_seq = 0
re6st_wrap.Re6stNode.node_seq = 0 re6st_wrap.Re6stNode.node_seq = 0
for registry in net: for registry in net:
reg = re6st_wrap.Re6stRegistry(registry, "2001:db8:42::", recreate=False) reg = re6st_wrap.Re6stRegistry(registry, "2001:db8:42::", recreate=recreate)
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)
...@@ -67,22 +65,41 @@ def wait_stable(nodes, timeout=240): ...@@ -67,22 +65,41 @@ def wait_stable(nodes, timeout=240):
logging.info("wait time cost: {}".format(time.time() - now)) logging.info("wait time cost: {}".format(time.time() - now))
return True return True
def ping_test(nodes):
"""test Re6st node connecty """
ips = {node.ip6:node.name for node in nodes} def getConfig(db, name):
logging.info("start ping test") r = db.execute("SELECT value FROM config WHERE name=?", (name,)).fetchone()
failed = False if r:
for node in nodes: return b2a_hex(*r)
sub_ips = set(ips) - {node.ip6}
node.ping_proc = node.node.Popen( def checkHMAC(db, machines):
["python", PING_PATH, '-a'] + list(sub_ips), stdout=subprocess.PIPE) hmac = [getConfig(db, k) for k in BABEL_HMAC]
out, _ = node.ping_proc.communicate() rc = True
unreached = [ ips[addr] for addr in out.split()] for x in psutil.Process().children(True):
if unreached: if x.name() == 'babeld':
logging.warning("{} can't ping to {}".format(node.name, ",".join(unreached))) sign = accept = None
failed = True args = x.cmdline()
logging.info("ping test finish") for x in args:
return failed if x.endswith('/babeld.log'):
if x[:-11].split('/')[-1] not in machines:
break
elif x.startswith('key '):
x = x.split()
if 'sign' in x:
sign = x[-1]
elif 'accept' in x:
accept = x[-1]
else:
i = 0 if hmac[0] else 1
if hmac[i] != sign or hmac[i+1] != accept:
logging.warn('HMAC config wrong for in %s' % args)
logging.warn("HMAC sign: {}, accept: {}".format(sign, accept))
rc = False
if rc:
logging.info('All nodes use Babel with the correct HMAC configuration')
else:
logging.warn('Expected config: %s' % dict(zip(BABEL_HMAC, hmac)))
return rc
@unittest.skipIf(os.geteuid() != 0, "require root or create user namespace plz") @unittest.skipIf(os.geteuid() != 0, "require root or create user namespace plz")
...@@ -96,16 +113,30 @@ class TestNet(unittest.TestCase): ...@@ -96,16 +113,30 @@ class TestNet(unittest.TestCase):
re6st_wrap.initial() re6st_wrap.initial()
@classmethod @classmethod
def tearDown(cls): def tearDownClass(cls):
# logging.basicConfig(level=logging.WARNING)
# clean all the running process # clean all the running process
for p in psutil.Process().children(): for p in psutil.Process().children():
logging.debug("unterminate subprocess, name: {}, pid: {}, status: {}, cmd: {}".format(p.name(), p.pid, p.status(), p.cmdline())) logging.debug("unterminate subprocess, name: {}, pid: {}, status: {}, cmd: {}".format(p.name(), p.pid, p.status(), p.cmdline()))
p.terminate()
logging.basicConfig(level=logging.WARNING)
# try: # try:
# p.kill() # p.kill()
# except: # except:
# pass # pass
def test_ping_router(self):
"""create a network in a net segment, test the connectivity by ping
"""
nm = my_net.net_route()
nodes, registrys = deploy_re6st(nm)
wait_stable(nodes, 20)
time.sleep(10)
self.assertTrue(wait_stable(nodes, 20), " ping test failed")
def test_ping_demo(self): def test_ping_demo(self):
"""create a network demo, test the connectivity by ping """create a network demo, test the connectivity by ping
wait at most 60 seconds, and test each node ping to other by ipv6 addr wait at most 60 seconds, and test each node ping to other by ipv6 addr
...@@ -113,39 +144,39 @@ class TestNet(unittest.TestCase): ...@@ -113,39 +144,39 @@ class TestNet(unittest.TestCase):
nm = my_net.net_demo() nm = my_net.net_demo()
nodes, registrys = deploy_re6st(nm) nodes, registrys = deploy_re6st(nm)
# wait 60, if the re6stnet stable quit wait # wait 60, if the re6stnet stable quit wait
wait_stable(nodes, 50)
time.sleep(20)
self.assertTrue(wait_stable(nodes, 80), "ping test failed") self.assertTrue(wait_stable(nodes, 20), "ping test failed")
# stop on machine randomly and reboot it, test if network recover
# index = int(random.random() * 7) + 1
# machine = nodes[index]
# machine.proc.terminate()
# machine.proc.wait()
# time.sleep(5)
# machine.run("-i" + machine.node.out.name)
# self.assertTrue(wait_stable(nodes, 200), "second ping test failed")
def test_ping_router(self): def test_reboot_one_machine(self):
"""create a network in a net segment, test the connectivity by ping """create a network demo, wait the net stable, reboot on machine,
then test if network recover, this test seems always failed
""" """
nm = my_net.net_route() nm = my_net.net_demo()
nodes, registrys = deploy_re6st(nm) nodes, registrys = deploy_re6st(nm)
wait_stable(nodes, 50) wait_stable(nodes, 100)
time.sleep(10) # stop on machine randomly
index = int(random.random() * 7) + 1
machine = nodes[index]
machine.proc.terminate()
machine.proc.wait()
time.sleep(5)
machine.run("-i" + machine.node.out.name)
self.assertTrue(wait_stable(nodes, 40), " ping test failed") self.assertTrue(wait_stable(nodes, 100), "network can't recover")
def test_hmac(self): def test_hmac(self):
"""create a network demo, and run hmac test, this test check hmac 3 times """create a network demo, and run hmac test, this test check hmac 3 times
the third part always failed, unless deploy_re6st in no recreate mode
""" """
nm = my_net.net_demo() nm = my_net.net_demo()
nodes, registrys = deploy_re6st(nm) nodes, registrys = deploy_re6st(nm, False)
updateHMAC = ['python', '-c', "import urllib, sys; sys.exit(" updateHMAC = ['python', '-c', "import urllib, sys; sys.exit("
"204 != urllib.urlopen('http://127.0.0.1/updateHMAC').code)"] "204 != urllib.urlopen('http://127.0.0.1/updateHMAC').code)"]
...@@ -161,26 +192,24 @@ class TestNet(unittest.TestCase): ...@@ -161,26 +192,24 @@ class TestNet(unittest.TestCase):
wait_stable(nodes, 100) wait_stable(nodes, 100)
logging.info('Check that the initial HMAC config is deployed on network 1') logging.info('Check that the initial HMAC config is deployed on network 1')
self.assertTrue(hmac.checkHMAC(reg1_db, m_net1), "first hmac check failed") self.assertTrue(checkHMAC(reg1_db, m_net1), "first hmac check failed")
logging.info('Test that a HMAC update works with nodes that are up') logging.info('Test that a HMAC update works with nodes that are up')
registry.node.run(updateHMAC) registry.node.run(updateHMAC)
time.sleep(60) time.sleep(60)
# Checking HMAC on machines connected to registry 1... # Checking HMAC on machines connected to registry 1...
self.assertTrue(hmac.checkHMAC(reg1_db, m_net1), "second hmac check failed: HMAC update don't work") self.assertTrue(checkHMAC(reg1_db, m_net1), "second hmac check failed: HMAC update don't work")
# # check if one machine restarted # # check if one machine restarted
logging.info('Test that machines can update upon reboot ' logging.info('Test that machines can update upon reboot '
'when they were off during a HMAC update.') 'when they were off during a HMAC update.')
logging.info('Re6st on {} is stopped'.format(machine1.name)) machine1.stop()
machine1.proc.terminate()
machine1.proc.wait()
time.sleep(5) time.sleep(5)
registry.node.run(updateHMAC) registry.node.run(updateHMAC)
time.sleep(60) time.sleep(60)
machine1.run("-i" + machine1.node.out.name) machine1.run("-i" + machine1.node.out.name)
wait_stable(nodes, 100) wait_stable(nodes, 100)
self.assertTrue(hmac.checkHMAC(reg1_db, m_net1), "third hmac check failed: machine restart failed") self.assertTrue(checkHMAC(reg1_db, m_net1), "third hmac check failed: machine restart failed")
logging.info( 'Testing of HMAC done!') logging.info( 'Testing of HMAC done!')
reg1_db.close() reg1_db.close()
......
...@@ -403,6 +403,11 @@ class TestRegistrtServer(unittest.TestCase): ...@@ -403,6 +403,11 @@ class TestRegistrtServer(unittest.TestCase):
self.assertEqual(get_hmac(), [None, key_2, key_1]) self.assertEqual(get_hmac(), [None, key_2, key_1])
#setp 5
self.server.updateHMAC()
self.assertEqual(get_hmac(), [key_2, None, None])
def test_getNodePrefix(self): def test_getNodePrefix(self):
# prefix in short format # prefix in short format
......
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