master-thesis/p4app/test.py

116 lines
3.3 KiB
Python
Raw Permalink Normal View History

2019-03-14 16:32:51 +00:00
from __future__ import unicode_literals
import struct
import sys
import re
import logging
import argparse
import subprocess
import ipaddress
logging.basicConfig()
log = logging.getLogger("main")
class TestStuff(object):
2019-03-14 19:19:50 +00:00
def __init__(self):
pass
2019-03-14 16:32:51 +00:00
2019-03-19 23:04:21 +00:00
def test_ping6_switch(self):
2019-03-19 23:02:52 +00:00
host = "h1"
dst_ipv6 = ipaddress.ip_address("2001:db8::42")
log.info("Trying to reach {} from {}".format(dst_ipv6, host))
2019-04-02 15:05:53 +00:00
cmd = "mx {} ping6 -c1 {}".format(host, dst_ipv6).split(" ")
2019-03-19 23:02:52 +00:00
return cmd
2019-03-31 13:54:39 +00:00
def test_ping4_switch(self):
host = "h3"
dst = ipaddress.ip_address("10.0.0.66")
log.info("Trying to reach {} from {}".format(dst, host))
2019-04-02 15:05:53 +00:00
cmd = "mx {} ping -c1 {}".format(host, dst).split(" ")
2019-03-31 13:54:39 +00:00
return cmd
2019-03-14 19:19:50 +00:00
def test_v6_static_mapping(self):
host = "h1"
2019-03-25 11:18:15 +00:00
nat64_prefix = ipaddress.ip_network("2001:db8:1::/96")
2019-03-14 19:19:50 +00:00
dst_ipv4 = ipaddress.ip_address("10.0.0.1")
translated_ipv4 = nat64_prefix[int(dst_ipv4)] # '2001:db8:1::a00:1'
2019-03-14 16:32:51 +00:00
2019-03-14 19:19:50 +00:00
log.info("Trying to reach {} ({}) from {}".format(dst_ipv4, translated_ipv4, host))
2019-04-02 15:05:53 +00:00
cmd = "mx {} ping6 -c1 {}".format(host, translated_ipv4).split(" ")
2019-03-14 16:32:51 +00:00
2019-03-19 23:02:52 +00:00
return cmd
2019-04-03 10:50:19 +00:00
def test_v4_static_mapping(self):
host = "h3"
dst_ipv4 = ipaddress.ip_address("10.1.1.1")
log.info("Trying to reach {} from {}".format(dst_ipv4, host))
cmd = "mx {} ping -c1 {}".format(host, dst_ipv4).split(" ")
return cmd
def test_v6_udp_to_v4(self):
print('mx h3 "echo V4-OK | socat - UDP-LISTEN:2342"')
print('mx h1 "echo V6-OK | socat - UDP6:[2001:db8:1::a00:1]:2342"')
return
def test_v6_tcp_to_v4(self):
print('mx h3 "echo V4-OK | socat - TCP-LISTEN:2342"')
print('mx h1 "echo V6-OK | socat - TCP6:[2001:db8:1::a00:1]:2342"')
return
def test_v4_tcp_to_v6(self):
print('mx h3 "echo V4-OK | socat - TCP:10.1.1.1:2342"')
print('mx h1 "echo V6-OK | socat - TCP-LISTEN:2342"')
return
def test_v4_udp_to_v6(self):
print('mx h3 "echo V4-OK | socat - UDP:10.1.1.1:2342"')
print('mx h1 "echo V6-OK | socat - UDP-LISTEN:2342"')
return
2019-03-14 16:32:51 +00:00
def commandline(self):
parser = argparse.ArgumentParser(description='controller++')
2019-03-14 19:19:50 +00:00
methods_dir = dir(self)
methods = [m for m in methods_dir if re.match("^test", m)]
methods = [re.sub("^test_(.*)", r"\1", m) for m in methods]
2019-03-14 19:21:31 +00:00
parser.add_argument('-m',
'--method', help="which method?", choices=methods, required=True)
2019-03-14 16:32:51 +00:00
parser.add_argument('--debug', help='Enable debug logging', action='store_true')
parser.add_argument('--verbose', help='Enable verbose logging', action='store_true')
parser.add_argument('--multicast-to-controller', help='Send debug multicast to controller', action='store_true')
self.args = parser.parse_args()
2019-03-14 19:19:50 +00:00
if self.args.debug:
log.setLevel(logging.DEBUG)
elif self.args.verbose:
log.setLevel(logging.INFO)
else:
log.setLevel(logging.WARNING)
f = getattr(self, "test_{}".format(self.args.method))
2019-03-19 23:02:52 +00:00
subprocess.call(f())
2019-03-14 16:32:51 +00:00
if __name__ == "__main__":
import sys
import os
log.info("Booting...")
log.debug("Debug enabled.")
2019-03-14 19:19:50 +00:00
t = TestStuff()
t.commandline()