def test_sys_tools(self): SysTools.touch('/tmp/test_utils') SysTools.REBOOT_SKIP_FILES = ('/tmp/test_utils') self.assertFalse(SysTools.is_system_openwrt()) self.assertEqual(SysTools.get_mac_address('if_test'), "00:00:00:00:00:00") self.assertEqual(SysTools.get_sys_mac_address(), "00:00:00:00:00:00") with open('/tmp/test_mac', 'w') as f: f.write('01:02:03:04:05:06') f.close() SysTools.SYS_MAC_FILE = '/tmp/test_mac' self.assertEqual(SysTools.get_sys_mac_address(), "01:02:03:04:05:06") SysTools.SYS_MAC_FILE = '/etc/gshadow' self.assertEqual(SysTools.get_sys_mac_address(), "00:00:00:00:00:00") print(SysTools.get_host_name()) print(SysTools.get_ip_address('if_test')) if not SysTools.reboot(): SysTools.sys_failure_reboot('skip files exist') SysTools.external_reboot(('test', 'test')) if os.path.exists('/tmp/test_utils'): os.remove('/tmp/test_utils') # d = Dispatcher() # timestamp = time.time() + 1000 # SysTools.set_system_time(d, timestamp) # SysTools.touch('/etc/openwrt_release') # SysTools.set_system_time(d, timestamp) # time.sleep(2) # self.assertTrue(timestamp > time.time()) #should no permission to change system time # del d # if os.path.exists('/etc/openwrt_release'): # os.remove('/etc/openwrt_release') self.assertIsNotNone(SysTools.sys_up_time()) ret = SysTools.if_indextoname(1) print "ifname of index 1:", ret self.assertIsNotNone(ret) ret = SysTools.if_indextoname("test") self.assertIsNone(ret) ret = SysTools.if_indextoname(600) self.assertIsNone(ret) ret = SysTools.if_nametoindex("lo") print "ifindex of lo:", ret self.assertIsNotNone(ret) ret = SysTools.if_nametoindex(5) self.assertIsNone(ret) ret = SysTools.if_nametoindex("dafsd") self.assertIsNone(ret) ret = SysTools.is_if_oper_up('eth0') self.assertTrue(ret)
def filter_proto_interface(self, proto, interface_list): """Filter the needed interface with specified proto. :param proto: proto needed :param interface_list: list of interface """ if not SysTools.is_system_openwrt(): return interface_list uci_interfaces = set() try: remove_interface = [] output = check_output(['uci', 'show', 'network']) network_list = output.strip().split('\n') for config in network_list: cfg, option = config.split('=') net_prex = cfg.split(".") if net_prex[-1] == "proto": ifname = '.'.join(net_prex[:-1]) + '.ifname' interface = check_output(['uci', 'get', ifname]).split('\n')[0] uci_interfaces.add(interface) if net_prex[-1] == "proto" and str(option) != proto: ifname = '.'.join(net_prex[:-1]) + '.ifname' interface = check_output(['uci', 'get', ifname]).split('\n')[0] if interface in interface_list: remove_interface.append(interface) if net_prex[-1] == "proto" and str(option).find("dhcpv6") >= 0: ifname = '.'.join(net_prex[:-1]) + '.ifname' interface = check_output(['uci', 'get', ifname]).split('\n')[0] if interface in interface_list and interface in remove_interface: remove_interface.remove(interface) # remove proto mismatched interface # only return the interface which is configured with # proto provision or dhcpv6 and status must be up for interface in remove_interface: if interface in self.interfaces: continue interface_list.remove(interface) except Exception as error: InterfaceStatus.logger.error("Got exception: %s", str(error)) self.logger.debug("scan interfaces: up interface list %s, uci interface list %s", interface_list, uci_interfaces) interface_list = uci_interfaces.intersection(set(interface_list)) return list(interface_list)
# limitations under the License. # import unittest import os import time from subprocess import Popen from signal import (signal, SIGUSR1, SIGUSR2, SIGTERM, SIGINT, SIGQUIT) from rpd.common.utils import SysTools from rpd.gpb.dhcp_pb2 import t_DhcpMessage, t_DhcpData from rpd.gpb.tpc_pb2 import t_TpcMessage # These imports are intended for the RPD, so we will enclose it by the # if statement if SysTools.is_system_openwrt(): from rpd.manager import Manager, ProcessInfo from glibc import (SIG_BLOCK, SIG_UNBLOCK, sigset_t, signalfd_siginfo, sigemptyset, sigaddset, sigprocmask) @unittest.skipUnless(SysTools.is_system_openwrt(), "Don't change system time on local machine") class TestManager(unittest.TestCase): @classmethod def setUpClass(cls): # Save original value for REBOOT_HOLD variable and block it for testing cls.reboot_hold_backup = os.getenv("PC_REBOOT_HOLD", 'false') os.environ['PC_REBOOT_HOLD'] = 'true' @classmethod
def __init__(self): if SysTools.is_system_openwrt(): self.path_bin = '/usr/lib/python2.7/site-packages/rpd/' else: # self.path_bin = './' self.path_bin = os.path.split(os.path.realpath(__file__))[0]