test framework: vpp_papi_provider.py - further cleanup
[vpp.git] / test / test_dhcp.py
index 179221d..2efa9a7 100644 (file)
@@ -8,7 +8,6 @@ from framework import VppTestCase, VppTestRunner, running_extended_tests
 from vpp_neighbor import VppNeighbor
 from vpp_ip_route import find_route, VppIpTable
 from util import mk_ll_addr
-
 from scapy.layers.l2 import Ether, getmacbyip, ARP
 from scapy.layers.inet import IP, UDP, ICMP
 from scapy.layers.inet6 import IPv6, in6_getnsmac
@@ -20,7 +19,7 @@ from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
 from socket import AF_INET, AF_INET6
 from scapy.utils import inet_pton, inet_ntop
 from scapy.utils6 import in6_ptop
-from util import mactobinary
+from vpp_papi import mac_pton
 
 DHCP4_CLIENT_PORT = 68
 DHCP4_SERVER_PORT = 67
@@ -361,6 +360,16 @@ class TestDHCP(VppTestCase):
         # not sure why this is not decoding
         # adv = pkt[DHCP6_Advertise]
 
+    def wait_for_no_route(self, address, length,
+                          n_tries=50, s_time=1):
+        while (n_tries):
+            if not find_route(self, address, length):
+                return True
+            n_tries = n_tries - 1
+            self.sleep(s_time)
+
+        return False
+
     def test_dhcp_proxy(self):
         """ DHCPv4 Proxy """
 
@@ -1053,8 +1062,7 @@ class TestDHCP(VppTestCase):
         nd_entry = VppNeighbor(self,
                                self.pg1.sw_if_index,
                                self.pg1.remote_hosts[1].mac,
-                               self.pg1.remote_hosts[1].ip6,
-                               af=AF_INET6)
+                               self.pg1.remote_hosts[1].ip6)
         nd_entry.add_vpp_config()
 
         #
@@ -1205,7 +1213,7 @@ class TestDHCP(VppTestCase):
         #
         # Configure DHCP client on PG3 and capture the discover sent
         #
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname)
 
         rx = self.pg3.get_capture(1)
 
@@ -1219,7 +1227,7 @@ class TestDHCP(VppTestCase):
                    UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
                    BOOTP(op=1,
                          yiaddr=self.pg3.local_ip4,
-                         chaddr=mactobinary(self.pg3.local_mac)) /
+                         chaddr=mac_pton(self.pg3.local_mac)) /
                    DHCP(options=[('message-type', 'offer'),
                                  ('server_id', self.pg3.remote_ip4),
                                  'end']))
@@ -1239,7 +1247,7 @@ class TestDHCP(VppTestCase):
                  IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
-                       chaddr=mactobinary(self.pg3.local_mac)) /
+                       chaddr=mac_pton(self.pg3.local_mac)) /
                  DHCP(options=[('message-type', 'ack'),
                                ('subnet_mask', "255.255.255.0"),
                                ('router', self.pg3.remote_ip4),
@@ -1268,13 +1276,14 @@ class TestDHCP(VppTestCase):
 
         # remove the left over ARP entry
         self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
-                                      mactobinary(self.pg3.remote_mac),
+                                      self.pg3.remote_mac,
                                       self.pg3.remote_ip4,
                                       is_add=0)
+
         #
         # remove the DHCP config
         #
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
 
         #
         # and now the route should be gone
@@ -1288,8 +1297,8 @@ class TestDHCP(VppTestCase):
         self.pg3.admin_down()
         self.sleep(1)
         self.pg3.admin_up()
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname,
-                              client_id=self.pg3.local_mac)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname,
+                                     client_id=self.pg3.local_mac)
 
         rx = self.pg3.get_capture(1)
 
@@ -1316,7 +1325,7 @@ class TestDHCP(VppTestCase):
                  IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
-                       chaddr=mactobinary(self.pg3.local_mac)) /
+                       chaddr=mac_pton(self.pg3.local_mac)) /
                  DHCP(options=[('message-type', 'ack'),
                                ('subnet_mask', "255.255.255.0"),
                                ('router', self.pg3.remote_ip4),
@@ -1346,7 +1355,7 @@ class TestDHCP(VppTestCase):
         #
         # remove the DHCP config
         #
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
 
         self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
         self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
@@ -1358,8 +1367,8 @@ class TestDHCP(VppTestCase):
         #
         # Configure DHCP client on PG3 and capture the discover sent
         #
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname,
-                              set_broadcast_flag=0)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname,
+                                     set_broadcast_flag=0)
 
         rx = self.pg3.get_capture(1)
 
@@ -1374,7 +1383,7 @@ class TestDHCP(VppTestCase):
                    IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
                    UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
                    BOOTP(op=1, yiaddr=self.pg3.local_ip4,
-                         chaddr=mactobinary(self.pg3.local_mac)) /
+                         chaddr=mac_pton(self.pg3.local_mac)) /
                    DHCP(options=[('message-type', 'offer'),
                                  ('server_id', self.pg3.remote_ip4),
                                  'end']))
@@ -1395,7 +1404,7 @@ class TestDHCP(VppTestCase):
                  IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
-                       chaddr=mactobinary(self.pg3.local_mac)) /
+                       chaddr=mac_pton(self.pg3.local_mac)) /
                  DHCP(options=[('message-type', 'ack'),
                                ('subnet_mask', "255.255.255.0"),
                                ('router', self.pg3.remote_ip4),
@@ -1424,7 +1433,7 @@ class TestDHCP(VppTestCase):
 
         # remove the left over ARP entry
         self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
-                                      mactobinary(self.pg3.remote_mac),
+                                      self.pg3.remote_mac,
                                       self.pg3.remote_ip4,
                                       is_add=0)
 
@@ -1453,7 +1462,7 @@ class TestDHCP(VppTestCase):
         #
         # remove the DHCP config
         #
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
 
         #
         # and now the route should be gone
@@ -1467,7 +1476,7 @@ class TestDHCP(VppTestCase):
         self.pg3.admin_down()
         self.sleep(1)
         self.pg3.admin_up()
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname)
 
         rx = self.pg3.get_capture(1)
 
@@ -1482,7 +1491,7 @@ class TestDHCP(VppTestCase):
                    UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
                    BOOTP(op=1,
                          yiaddr=self.pg3.local_ip4,
-                         chaddr=mactobinary(self.pg3.local_mac)) /
+                         chaddr=mac_pton(self.pg3.local_mac)) /
                    DHCP(options=[('message-type', 'offer'),
                                  ('server_id', self.pg3.remote_ip4),
                                  ('lease_time', lease_time),
@@ -1503,7 +1512,7 @@ class TestDHCP(VppTestCase):
                  IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
                  UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
                  BOOTP(op=1, yiaddr=self.pg3.local_ip4,
-                       chaddr=mactobinary(self.pg3.local_mac)) /
+                       chaddr=mac_pton(self.pg3.local_mac)) /
                  DHCP(options=[('message-type', 'ack'),
                                ('subnet_mask', '255.255.255.0'),
                                ('router', self.pg3.remote_ip4),
@@ -1531,25 +1540,20 @@ class TestDHCP(VppTestCase):
 
         # remove the left over ARP entry
         self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
-                                      mactobinary(self.pg3.remote_mac),
+                                      self.pg3.remote_mac,
                                       self.pg3.remote_ip4,
                                       is_add=0)
 
         #
-        # Sleep for the lease time
+        # the route should be gone after the lease expires
         #
-        self.sleep(lease_time+1)
-
-        #
-        # And now the route should be gone
-        #
-        self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
-        self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
+        self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
+        self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
 
         #
         # remove the DHCP config
         #
-        self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0)
+        self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0)
 
 
 if __name__ == '__main__':