Exemple #1
0
class DefGwTest(unittest.TestCase):
    """
    Validate default router setting.
    """
    def setUp(self):
        self.gw_store = defaultdict(str)
        self.dhcp_gw_info = UplinkGatewayInfo(self.gw_store)

    def test_gw_ip_for_DHCP(self):
        self.assertEqual(self.dhcp_gw_info.getIP(), None)
        self.assertEqual(self.dhcp_gw_info.getMac(), None)

    def test_gw_ip_for_Ip_pool(self):
        self.dhcp_gw_info.read_default_gw()

        def_gw_cmd = "ip route show |grep default| awk '{print $3}'"
        p = subprocess.Popen([def_gw_cmd], stdout=subprocess.PIPE, shell=True)
        def_ip = p.stdout.read().decode("utf-8").strip()
        self.assertEqual(self.dhcp_gw_info.getIP(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.getMac(), None)
    def set_deafult_gw(self, args):
        """
        Set GW for given uplink network
        Args:
            args: IP address of GW.

        Returns:
        """

        gw_ip = ip_address(args.ip)
        gw_info = UplinkGatewayInfo()
        gw_info.update_ip(str(gw_ip))
        print("set Default gw IP to %s" % gw_info.getIP())
    def list_all_record(self, args):
        """
                Print list DHCP and GW records.
        Args:
            args: None

        Returns: None
        """

        for k, v in self.dhcp_client_state.items():
            print("mac: %s DHCP state: %s" % (k.replace('_', ':'), str(v)))

        gw_info = UplinkGatewayInfo(store.GatewayInfoMap())
        print("Default GW: %s" % gw_info.getIP())
        print("GW Mac address: %s" % gw_info.getMac())
Exemple #4
0
    def test_ip_alloc(self):
        sid1 = "IMSI02917"
        ip1 = self._dhcp_allocator.alloc_ip_address(sid1)
        threading.Event().wait(2)
        gw_map = store.GatewayInfoMap()
        dhcp_gw_info = UplinkGatewayInfo(gw_map)
        dhcp_store = store.MacToIP()  # mac => DHCP_State

        self.assertEqual(str(dhcp_gw_info.getIP()), "192.168.128.211")
        self._dhcp_allocator.release_ip_address(sid1, ip1)

        # wait for DHCP release
        threading.Event().wait(7)
        mac1 = create_mac_from_sid(sid1)
        dhcp_state1 = dhcp_store.get(mac1.as_redis_key())

        self.assertEqual(dhcp_state1.state, DHCPState.RELEASE)

        ip1_1 = self._dhcp_allocator.alloc_ip_address(sid1)
        threading.Event().wait(2)
        self.assertEqual(str(ip1), str(ip1_1))

        self._dhcp_allocator.release_ip_address(sid1, ip1_1)
        threading.Event().wait(5)
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(), [])

        ip1 = self._dhcp_allocator.alloc_ip_address("IMSI02918")
        self.assertEqual(str(ip1), "192.168.128.146")
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(),
                         [ip_network('192.168.128.0/24')])

        ip2 = self._dhcp_allocator.alloc_ip_address("IMSI029192")
        self.assertNotEqual(ip1, ip2)

        ip3 = self._dhcp_allocator.alloc_ip_address("IMSI0432")
        self.assertNotEqual(ip1, ip3)
        self.assertNotEqual(ip2, ip3)
        # release unallocated IP of SID
        self._dhcp_allocator.ip_allocator.release_ip("IMSI033", ip3,
                                                     ip_network("1.1.1.0/24"))
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(),
                         [ip_network('192.168.128.0/24')])

        sid4 = "IMSI54321"
        ip4 = self._dhcp_allocator.alloc_ip_address(sid4)
        threading.Event().wait(1)
        self._dhcp_allocator.release_ip_address(sid4, ip4)
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(),
                         [ip_network('192.168.128.0/24')])

        # wait for DHCP release
        threading.Event().wait(7)
        mac4 = create_mac_from_sid(sid4)
        dhcp_state = dhcp_store.get(mac4.as_redis_key())

        self.assertEqual(dhcp_state.state, DHCPState.RELEASE)
        ip4_2 = self._dhcp_allocator.alloc_ip_address(sid4)
        self.assertEqual(ip4, ip4_2)

        try:
            self._dhcp_allocator.release_ip_address(sid1, ip1)
            self.assertEqual("should not", "reach here")
        except MappingNotFoundError:
            pass