def test_false_command(self): """Test a command that return non 0 result""" self.assertEqual(Commands.run_noerr("false"), "")
def test_error_command_withoutput(self): """Test a command with non-0 result and output""" result = Commands.run_noerr("grep stuff /i_do_not_exist") self.assertEqual(result[:6], "grep: ")
def test_inexistant_command(self): """Test an inexistant command with run_noerr""" with self.assertRaises(CommandError) as ce: Commands.run_noerr("i_do_not_exist") self.assertEqual(ce.exception.command, "i_do_not_exist") self.assertEqual(ce.exception.retcode, errno.ENOENT)
def setup(self): """Setup the binder for the first time. Cleaning is also handled here since the binder has no way to clean on exit. """ self.interfaces = self.router.interfaces.keys() # Ordered interface list self.interfaces.sort() self.mark = Mark(len(self.interfaces), # Netfilter mark producer self.config['max_users']) self.slots = SlotsProvider(self.config['max_users']) # Slot producer self.tickets = TicketsProvider() # Ticket producer # Netfilter for chain in [ "prerouting", "accounting", "postrouting" ]: subs = dict(chain = self.config[chain], chain_upper = chain.upper()) if chain == "accounting": subs['chain_upper'] = "POSTROUTING" logger.info("setup %(chain)s chain" % subs) # Cleanup old iptables rules for iptables in self.iptables: Commands.run_noerr("%(iptables)s -t mangle -D %(chain_upper)s -j %(chain)s", "%(iptables)s -t mangle -F %(chain)s", "%(iptables)s -t mangle -X %(chain)s", iptables=iptables, **subs) # Setup the new chains Commands.run("%(iptables)s -t mangle -N %(chain)s", "%(iptables)s -t mangle -I %(chain_upper)s -j %(chain)s", iptables=iptables, **subs) # Setup QoS for interface in self.interfaces + self.router.incoming: logger.info("setup QoS for interface %s" % interface) Commands.run_noerr("tc qdisc del dev %(interface)s root", interface=interface) Commands.run( # Flush QoS "tc qdisc add dev %(interface)s root handle 1: drr", # Default class "tc class add dev %(interface)s parent 1: classid 1:2 drr", "tc qdisc add dev %(interface)s parent 1:2 handle 12: sfq", # Use default class for unmatched traffic "tc filter add dev %(interface)s protocol arp parent 1:0" " prio 1 u32 match u32 0 0 flowid 1:2", # ARP interface=interface, **self.config) for iptables in self.iptables: Commands.run( "%(iptables)s -t mangle -A %(postrouting)s" " -o %(interface)s -j CLASSIFY --set-class 1:2", # IP iptables=iptables, interface=interface, **self.config) # Setup routing rules for interface in self.interfaces: logger.info("setup ip rules for interface %s" % interface) for ip in self.ipcmd: Commands.run_noerr("%(ip)s rule del fwmark %(mark)s table %(interface)s", mark="%s/%s" % self.mark(self.interfaces.index(interface)), ip = ip, interface=interface) Commands.run("%(ip)s rule add fwmark %(mark)s table %(interface)s", ip = ip, mark="%s/%s" % self.mark(self.interfaces.index(interface)), interface=interface)