def realSetup(self): self.router = Router.load(self.config) # Start the service in a separate process self.service = Service( dict(helper=dict(save=os.path.join(self.temp, "save.pickle"))), self.router) time.sleep(0.2) # Safety
def setUp(self): r = Router.load({'clients': 'eth0'}) # Start the service self.service = Service({}, r) time.sleep(0.2) # Safety # Configure app configure(app)
def setUp(self): r = Router.load({'clients': 'eth0'}) # Start the service self.service = Service({}, r) time.sleep(0.2) # Safety # Configure app configure(app)
def setUp(self): r = Router.load(yaml.load(""" clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos1 - qos2 eth2: name: WAN description: "My second interface" password: 1234 qos: - qos1 qos: qos1: name: 100M description: "My first QoS" bandwidth: 100mbps netem: delay 100ms 10ms distribution experimental qos2: name: 10M description: "My second QoS" bandwidth: 10mbps netem: delay 200ms 10ms """)) self.service = Service({}, r) # helper configure(app, dict(web=dict(expire=2))) self.app = app.test_client() # web time.sleep(0.1)
def run(cls, args=sys.argv[1:], binder=None): """Start the helper service. This method should be used to instantiate :class:`Service`. The configuration file must be provided as an argument. :param args: list of command line arguments :type args: list of strings :param binder: binder to register to the router """ from optparse import OptionParser usage = "usage: %prog [options] config.yaml" parser = OptionParser(usage=usage) parser.add_option("-l", "--log", type="string", dest="log", help="log to file", metavar="FILE") parser.add_option("-s", "--syslog", action="store_true", dest="syslog", help="log to syslog") parser.add_option("-d", "--debug", action="count", dest="debug", help="enable debugging") (options, args) = parser.parse_args(args) if len(args) != 1: parser.error("incorrect number of arguments") # Logger configuration l = logging.root if options.debug == 1: l.setLevel(logging.INFO) if options.debug > 1: l.setLevel(logging.DEBUG) if options.log: fh = logging.FileHandler(options.log) fh.setFormatter(logging.Formatter("%(asctime)s %(name)s[%(process)d] " "%(levelname)s: %(message)s")) l.addHandler(fh) if options.syslog: sh = logging.handlers.SysLogHandler(address="/dev/log", facility="daemon") sh.setFormatter(logging.Formatter("kitero[%(process)d]: " "%(levelname)s %(message)s")) l.addHandler(sh) if not options.syslog and not options.log: # Log to stderr eh = logging.StreamHandler() eh.setFormatter(logging.Formatter("%(asctime)s %(name)s[%(process)d] " "%(levelname)s: %(message)s")) l.addHandler(eh) # Reading configuration file logger.info("read configuration file %r" % args[0]) try: config = yaml.safe_load(file(args[0])) config = kitero.config.merge(config) # Create the router router = Router.load(config["router"]) # Add the regular binder to it if binder is not None: router.register(binder) # Start service s = cls(config, router) s.wait() except Exception as e: logger.exception("unhandled error received") sys.exit(1) sys.exit(0)
def test_load_minimal(self): """Load router with minimal information""" doc = """ clients: eth0 """ r = Router.load(yaml.load(doc)) self.assertEqual(r.interfaces, {}) self.assertEqual(r.incoming, ["eth0"]) self.assertEqual(r.clients, {})
def test_load_with_missing(self): """Load router with missing information""" doc = """ interfaces: eth1: description: "My first interface" """ with self.assertRaises(ValueError): r = Router.load(yaml.load(doc))
def test_load(self): """Load router from YAML representation""" doc = """ clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos1 - qos2 eth2: name: WAN description: "My second interface" qos: - qos1 - qos3 password: 1234 qos: qos1: name: "100M" description: "My first QoS" bandwidth: 100mbps netem: delay 100ms 10ms distribution experimental qos2: name: "10M" description: "My second QoS" bandwidth: 10mbps netem: delay 200ms 10ms qos3: name: "1M" description: "My third QoS" bandwidth: 1mbps netem: delay 500ms 30ms """ r = Router.load(yaml.load(doc)) self.assertEqual(r.incoming, ["eth0"]) self.assertEqual(r.clients, {}) self.assertEqual(r.interfaces["eth1"], Interface("LAN", "My first interface", qos={'qos1': QoS("100M", "My first QoS", {"bandwidth": "100mbps", "netem": "delay 100ms 10ms distribution experimental"}), 'qos2': QoS("10M", "My second QoS", {"bandwidth": "10mbps", "netem": "delay 200ms 10ms"})})) self.assertEqual(r.interfaces["eth2"], Interface("WAN", "My second interface", qos={'qos1': QoS("100M", "My first QoS", {"bandwidth": "100mbps", "netem": "delay 100ms 10ms distribution experimental"}), 'qos3': QoS("1M", "My third QoS", {"bandwidth": "1mbps", "netem": "delay 500ms 30ms"})})) self.assertEqual(len(r.interfaces), 2) self.assertEqual(r.interfaces["eth2"].check_password("1234"), True)
def test_load_unknown_qos(self): """Load router from YAML with unknown QoS""" doc = """ clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos3 """ with self.assertRaises(KeyError): r = Router.load(yaml.load(doc))
def test_several_incoming(self): """Load router with several incoming interfaces""" doc = """ clients: - eth0 - eth2 interfaces: eth1: name: LAN description: "My first interface" """ r = Router.load(yaml.load(doc)) self.assertEqual(r.incoming, ["eth0", "eth2"])
def test_load_almost_minimal(self): """Load router with almost minimal information""" doc = """ clients: eth0 interfaces: eth1: name: LAN description: "My first interface" """ r = Router.load(yaml.load(doc)) self.assertEqual(r.interfaces, {'eth1': Interface("LAN", "My first interface")}) self.assertEqual(r.incoming, ["eth0"]) self.assertEqual(r.clients, {})
def setUp(self): r = Router.load( yaml.load(""" clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos1 - qos2 eth2: name: WAN description: "My second interface" qos: - qos1 - qos3 qos: qos1: name: 100M description: "My first QoS" bandwidth: 100mbps netem: delay 100ms 10ms distribution experimental qos2: name: 10M description: "My second QoS" bandwidth: 10mbps netem: delay 200ms 10ms qos3: name: 1M description: "My third QoS" bandwidth: 1mbps netem: delay 500ms 30ms """)) # Start the service in a separate process self.service = Service({}, r) time.sleep(0.2) # Safety
def setUp(self): r = Router.load(yaml.load(""" clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos1 - qos2 eth2: name: WAN description: "My second interface" qos: - qos1 - qos3 qos: qos1: name: 100M description: "My first QoS" bandwidth: 100mbps netem: delay 100ms 10ms distribution experimental qos2: name: 10M description: "My second QoS" bandwidth: 10mbps netem: delay 200ms 10ms qos3: name: 1M description: "My third QoS" bandwidth: 1mbps netem: delay 500ms 30ms """)) # Start the service in a separate process self.service = Service({}, r) time.sleep(0.2) # Safety
def setUp(self): self.binder = self.BINDER() doc = """ clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos1 - qos2 eth2: name: WAN description: "My second interface" qos: - qos1 - qos3 - qos4 qos: qos1: name: "100M" description: "My first QoS" bandwidth: down: 100mbps buffer 10Mbit latency 1s up: 50mbps buffer 10Mbit latency 1s netem: delay 100ms 10ms distribution experimental qos2: name: "10M" description: "My second QoS" bandwidth: 10mbps buffer 10Mbit latency 1s netem: delay 200ms 10ms qos3: name: "1M" description: "My third QoS" netem: down: delay 500ms 30ms up: delay 10ms 2ms loss 0.01% qos4: name: "unlimited" description: "My fourth QoS" """ self.router = Router.load(yaml.load(doc)) self.router.register(self.binder) # Provide fake binaries for `ip`, `iptables`, `tc` self.temp = tempfile.mkdtemp() biny = os.path.join(self.temp, "bin") os.mkdir(biny) self.oldpath = os.environ['PATH'] os.environ['PATH'] = "%s:%s" % (biny, os.environ['PATH']) f = file(os.path.join(biny, "fake"), "w") f.write("""#!/bin/sh echo $(basename $0) "$@" >> "%s" case "$(basename $0) $@" in "iptables -t mangle -v -S kitero-ACCOUNTING") cat <<EOF -N kitero-ACCOUNTING -A kitero-ACCOUNTING -o eth2 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "up-eth2-172.29.7.14" -c 39219 2079628 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "down-eth2-172.29.7.14" -c 72867 108647983 -A kitero-ACCOUNTING -o eth2 -m connmark --mark 0x41000000/0xffe00000 -m comment --comment "up-eth2-172.29.7.15" -c 247 20796 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x41000000/0xffe00000 -m comment --comment "down-eth2-172.29.7.15" -c 867 2015775 -A kitero-ACCOUNTING -o eth1 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "up-eth1-172.29.7.19" -c 8888 99999 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "down-eth1-172.29.7.19" -c 8888 11111 EOF ;; "ip6tables -t mangle -v -S kitero-ACCOUNTING") cat <<EOF -N kitero-ACCOUNTING -A kitero-ACCOUNTING -o eth2 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "up-eth2-2001:db8::1" -c 3219 209628 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "down-eth2-2001:db8::1" -c 7287 18647983 -A kitero-ACCOUNTING -o eth1 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "up-eth1-2001:db8::2" -c 8885 97999 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "down-eth1-2001:db8::2" -c 8885 12111 EOF ;; esac exit 0 """ % os.path.join(self.temp, "output.txt")) f.close() os.chmod(os.path.join(biny, "fake"), stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) for ex in ['iptables', 'ip6tables', 'tc', 'ip']: os.symlink("fake", os.path.join(biny, ex))
def run(cls, args=sys.argv[1:], binder=None): """Start the helper service. This method should be used to instantiate :class:`Service`. The configuration file must be provided as an argument. :param args: list of command line arguments :type args: list of strings :param binder: binder to register to the router """ from optparse import OptionParser usage = "usage: %prog [options] config.yaml" parser = OptionParser(usage=usage) parser.add_option("-l", "--log", type="string", dest="log", help="log to file", metavar="FILE") parser.add_option("-s", "--syslog", action="store_true", dest="syslog", help="log to syslog") parser.add_option("-d", "--debug", action="count", dest="debug", help="enable debugging") (options, args) = parser.parse_args(args) if len(args) != 1: parser.error("incorrect number of arguments") # Logger configuration l = logging.root if options.debug == 1: l.setLevel(logging.INFO) if options.debug > 1: l.setLevel(logging.DEBUG) if options.log: fh = logging.FileHandler(options.log) fh.setFormatter( logging.Formatter('%(asctime)s %(name)s[%(process)d] ' '%(levelname)s: %(message)s')) l.addHandler(fh) if options.syslog: sh = logging.handlers.SysLogHandler(address='/dev/log', facility="daemon") sh.setFormatter( logging.Formatter('kitero[%(process)d]: ' '%(levelname)s %(message)s')) l.addHandler(sh) if not options.syslog and not options.log: # Log to stderr eh = logging.StreamHandler() eh.setFormatter( logging.Formatter('%(asctime)s %(name)s[%(process)d] ' '%(levelname)s: %(message)s')) l.addHandler(eh) # Reading configuration file logger.info("read configuration file %r" % args[0]) try: config = yaml.safe_load(file(args[0])) config = kitero.config.merge(config) # Create the router router = Router.load(config['router']) # Add the regular binder to it if binder is not None: router.register(binder) # Start service s = cls(config, router) s.wait() except Exception as e: logger.exception("unhandled error received") sys.exit(1) sys.exit(0)
def setUp(self): self.binder = self.BINDER() doc = """ clients: eth0 interfaces: eth1: name: LAN description: "My first interface" qos: - qos1 - qos2 eth2: name: WAN description: "My second interface" qos: - qos1 - qos3 - qos4 qos: qos1: name: "100M" description: "My first QoS" bandwidth: down: 100mbps buffer 10Mbit latency 1s up: 50mbps buffer 10Mbit latency 1s netem: delay 100ms 10ms distribution experimental qos2: name: "10M" description: "My second QoS" bandwidth: 10mbps buffer 10Mbit latency 1s netem: delay 200ms 10ms qos3: name: "1M" description: "My third QoS" netem: down: delay 500ms 30ms up: delay 10ms 2ms loss 0.01% qos4: name: "unlimited" description: "My fourth QoS" """ self.router = Router.load(yaml.load(doc)) self.router.register(self.binder) # Provide fake binaries for `ip`, `iptables`, `tc` self.temp = tempfile.mkdtemp() biny = os.path.join(self.temp, "bin") os.mkdir(biny) self.oldpath = os.environ['PATH'] os.environ['PATH'] = "%s:%s" % (biny, os.environ['PATH']) f = file(os.path.join(biny, "fake"), "w") f.write("""#!/bin/sh echo $(basename $0) "$@" >> "%s" case "$(basename $0) $@" in "iptables -t mangle -v -S kitero-ACCOUNTING") cat <<EOF -N kitero-ACCOUNTING -A kitero-ACCOUNTING -o eth2 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "up-eth2-172.29.7.14" -c 39219 2079628 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "down-eth2-172.29.7.14" -c 72867 108647983 -A kitero-ACCOUNTING -o eth2 -m connmark --mark 0x41000000/0xffe00000 -m comment --comment "up-eth2-172.29.7.15" -c 247 20796 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x41000000/0xffe00000 -m comment --comment "down-eth2-172.29.7.15" -c 867 2015775 -A kitero-ACCOUNTING -o eth1 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "up-eth1-172.29.7.19" -c 8888 99999 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "down-eth1-172.29.7.19" -c 8888 11111 EOF ;; "ip6tables -t mangle -v -S kitero-ACCOUNTING") cat <<EOF -N kitero-ACCOUNTING -A kitero-ACCOUNTING -o eth2 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "up-eth2-2001:db8::1" -c 3219 209628 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x40000000/0xffe00000 -m comment --comment "down-eth2-2001:db8::1" -c 7287 18647983 -A kitero-ACCOUNTING -o eth1 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "up-eth1-2001:db8::2" -c 8885 97999 -A kitero-ACCOUNTING -o eth0 -m connmark --mark 0x20000000/0xffe00000 -m comment --comment "down-eth1-2001:db8::2" -c 8885 12111 EOF ;; esac exit 0 """ % os.path.join(self.temp, "output.txt")) f.close() os.chmod( os.path.join(biny, "fake"), stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) for ex in ['iptables', 'ip6tables', 'tc', 'ip']: os.symlink("fake", os.path.join(biny, ex))
def realSetup(self): self.router = Router.load(self.config) # Start the service in a separate process self.service = Service(dict(helper=dict(save=os.path.join(self.temp, "save.pickle"))), self.router) time.sleep(0.2) # Safety