make test: Loopback interface CRUD test 67/4467/8
authorMatej Klotton <mklotton@cisco.com>
Thu, 22 Dec 2016 10:06:56 +0000 (11:06 +0100)
committerDamjan Marion <dmarion.lists@gmail.com>
Mon, 9 Jan 2017 13:33:39 +0000 (13:33 +0000)
Change-Id: I0581da7a682bfe4dd6520ecf1b2ea6bd8c20b1b3
Signed-off-by: Matej Klotton <mklotton@cisco.com>
test/framework.py
test/test_interface_crud.py [new file with mode: 0644]
test/vpp_interface.py
test/vpp_lo_interface.py
test/vpp_papi_provider.py

index 324a64c..e364a8f 100644 (file)
@@ -355,9 +355,10 @@ class VppTestCase(unittest.TestCase):
     @classmethod
     def create_pg_interfaces(cls, interfaces):
         """
-        Create packet-generator interfaces
+        Create packet-generator interfaces.
 
-        :param interfaces: iterable indexes of the interfaces
+        :param interfaces: iterable indexes of the interfaces.
+        :returns: List of created interfaces.
 
         """
         result = []
@@ -371,10 +372,10 @@ class VppTestCase(unittest.TestCase):
     @classmethod
     def create_loopback_interfaces(cls, interfaces):
         """
-        Create loopback interfaces
-
-        :param interfaces: iterable indexes of the interfaces
+        Create loopback interfaces.
 
+        :param interfaces: iterable indexes of the interfaces.
+        :returns: List of created interfaces.
         """
         result = []
         for i in interfaces:
diff --git a/test/test_interface_crud.py b/test/test_interface_crud.py
new file mode 100644 (file)
index 0000000..6391704
--- /dev/null
@@ -0,0 +1,151 @@
+#!/usr/bin/env python
+"""CRUD tests of APIs (Create, Read, Update, Delete) HLD:
+
+- interface up/down/add/delete - interface type:
+    - pg (TBD)
+    - loopback
+    - vhostuser (TBD)
+    - af_packet (TBD)
+    - netmap (TBD)
+    - tuntap (root privileges needed)
+    - vxlan (TBD)
+"""
+
+import unittest
+
+from scapy.layers.inet import IP, ICMP
+from scapy.layers.l2 import Ether
+
+from framework import VppTestCase, VppTestRunner
+
+
+class TestLoopbackInterfaceCRUD(VppTestCase):
+    """CRUD Loopback
+
+    """
+
+    @classmethod
+    def setUpClass(cls):
+        super(TestLoopbackInterfaceCRUD, cls).setUpClass()
+        try:
+            cls.create_pg_interfaces(range(1))
+            for i in cls.pg_interfaces:
+                i.config_ip4()
+                i.resolve_arp()
+        except:
+            cls.tearDownClass()
+            raise
+
+    @staticmethod
+    def create_icmp_stream(src_if, dst_ifs):
+        """
+
+        :param VppInterface src_if: Packets are send to this interface,
+            using this interfaces remote host.
+        :param list dst_ifs: IPv4 ICMP requests are send to interfaces
+            addresses.
+        :return: List of generated packets.
+        """
+        pkts = []
+        for i in dst_ifs:
+            p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
+                 IP(src=src_if.remote_ip4, dst=i.local_ip4) /
+                 ICMP(id=i.sw_if_index, type='echo-request'))
+            pkts.append(p)
+        return pkts
+
+    def verify_icmp(self, capture, request_src_if, dst_ifs):
+        """
+
+        :param capture: Capture to verify.
+        :param VppInterface request_src_if: Interface where was send packets.
+        :param list dst_ifs: Interfaces where was generated IPv4 ICMP requests.
+        """
+        rcvd_icmp_pkts = []
+        for pkt in capture:
+            try:
+                ip = pkt[IP]
+                icmp = pkt[ICMP]
+            except IndexError:
+                pass
+            else:
+                info = (ip.src, ip.dst, icmp.type, icmp.id)
+                rcvd_icmp_pkts.append(info)
+
+        for i in dst_ifs:
+            # 0 - icmp echo response
+            info = (i.local_ip4, request_src_if.remote_ip4, 0, i.sw_if_index)
+            self.assertIn(info, rcvd_icmp_pkts)
+
+    def test_crud(self):
+        # create
+        loopbacks = self.create_loopback_interfaces(range(20))
+        for i in loopbacks:
+            i.local_ip4_prefix_len = 32
+            i.config_ip4()
+            i.admin_up()
+
+        # read (check sw if dump, ip4 fib, ip6 fib)
+        if_dump = self.vapi.sw_interface_dump()
+        fib4_dump = self.vapi.ip_fib_dump()
+        for i in loopbacks:
+            self.assertTrue(i.is_interface_config_in_dump(if_dump))
+            self.assertTrue(i.is_ip4_entry_in_fib_dump(fib4_dump))
+
+        # check ping
+        stream = self.create_icmp_stream(self.pg0, loopbacks)
+        self.pg0.add_stream(stream)
+        self.pg_enable_capture(self.pg_interfaces)
+        self.pg_start()
+        capture = self.pg0.get_capture(expected_count=len(stream))
+
+        self.verify_icmp(capture, self.pg0, loopbacks)
+
+        # delete
+        for i in loopbacks:
+            i.remove_vpp_config()
+
+        # read (check not in sw if dump, ip4 fib, ip6 fib)
+        if_dump = self.vapi.sw_interface_dump()
+        fib4_dump = self.vapi.ip_fib_dump()
+        for i in loopbacks:
+            self.assertFalse(i.is_interface_config_in_dump(if_dump))
+            self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))
+
+        #  check not ping
+        stream = self.create_icmp_stream(self.pg0, loopbacks)
+        self.pg0.add_stream(stream)
+        self.pg_enable_capture(self.pg_interfaces)
+        self.pg_start()
+        self.pg0.assert_nothing_captured()
+
+    def test_down(self):
+        # create
+        loopbacks = self.create_loopback_interfaces(range(20))
+        for i in loopbacks:
+            i.local_ip4_prefix_len = 32
+            i.config_ip4()
+            i.admin_up()
+
+        # disable
+        for i in loopbacks:
+            i.admin_down()
+            i.unconfig_ip4()
+
+        # read (check not in sw if dump, ip4 fib, ip6 fib)
+        if_dump = self.vapi.sw_interface_dump()
+        fib4_dump = self.vapi.ip_fib_dump()
+        for i in loopbacks:
+            self.assertTrue(i.is_interface_config_in_dump(if_dump))
+            self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))
+
+        #  check not ping
+        stream = self.create_icmp_stream(self.pg0, loopbacks)
+        self.pg0.add_stream(stream)
+        self.pg_enable_capture(self.pg_interfaces)
+        self.pg_start()
+        self.pg0.assert_nothing_captured()
+
+
+if __name__ == '__main__':
+    unittest.main(testRunner=VppTestRunner)
index 7886510..6ccb92b 100644 (file)
@@ -151,15 +151,21 @@ class VppInterface(object):
 
         self._local_ip4 = "172.16.%u.1" % self.sw_if_index
         self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
+        self.local_ip4_prefix_len = 24
+        self.has_ip4_config = False
+        self.ip4_table_id = 0
 
         self._local_ip6 = "fd01:%04x::1" % self.sw_if_index
         self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
+        self.local_ip6_prefix_len = 64
+        self.has_ip6_config = False
+        self.ip6_table_id = 0
 
         r = self.test.vapi.sw_interface_dump()
         for intf in r:
             if intf.sw_if_index == self.sw_if_index:
                 self._name = intf.interface_name.split(b'\0', 1)[0]
-                self._local_mac =\
+                self._local_mac = \
                     ':'.join(intf.l2_address.encode('hex')[i:i + 2]
                              for i in range(0, 12, 2))
                 self._dump = intf
@@ -172,20 +178,19 @@ class VppInterface(object):
 
     def config_ip4(self):
         """Configure IPv4 address on the VPP interface."""
-        addr = self.local_ip4n
-        addr_len = 24
         self.test.vapi.sw_interface_add_del_address(
-            self.sw_if_index, addr, addr_len)
+            self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
         self.has_ip4_config = True
 
     def unconfig_ip4(self):
-        """Remove IPv4 address on the VPP interface"""
+        """Remove IPv4 address on the VPP interface."""
         try:
-            if (self.has_ip4_config):
+            if self.has_ip4_config:
                 self.test.vapi.sw_interface_add_del_address(
                     self.sw_if_index,
                     self.local_ip4n,
-                    24, is_add=0)
+                    self.local_ip4_prefix_len,
+                    is_add=0)
         except AttributeError:
             self.has_ip4_config = False
         self.has_ip4_config = False
@@ -199,43 +204,46 @@ class VppInterface(object):
 
     def config_ip6(self):
         """Configure IPv6 address on the VPP interface."""
-        addr = self._local_ip6n
-        addr_len = 64
         self.test.vapi.sw_interface_add_del_address(
-            self.sw_if_index, addr, addr_len, is_ipv6=1)
+            self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
+            is_ipv6=1)
         self.has_ip6_config = True
 
     def unconfig_ip6(self):
-        """Remove IPv6 address on the VPP interface"""
+        """Remove IPv6 address on the VPP interface."""
         try:
-            if (self.has_ip6_config):
+            if self.has_ip6_config:
                 self.test.vapi.sw_interface_add_del_address(
                     self.sw_if_index,
                     self.local_ip6n,
-                    64, is_ipv6=1, is_add=0)
+                    self.local_ip6_prefix_len,
+                    is_ipv6=1, is_add=0)
         except AttributeError:
             self.has_ip6_config = False
         self.has_ip6_config = False
 
     def unconfig(self):
-        """Unconfigure IPv6 and IPv4 address on the VPP interface"""
+        """Unconfigure IPv6 and IPv4 address on the VPP interface."""
         self.unconfig_ip4()
         self.unconfig_ip6()
 
     def set_table_ip4(self, table_id):
         """Set the interface in a IPv4 Table.
 
-        .. note:: Must be called before configuring IP4 addresses."""
+        .. note:: Must be called before configuring IP4 addresses.
+        """
+        self.ip4_table_id = table_id
         self.test.vapi.sw_interface_set_table(
-            self.sw_if_index, 0, table_id)
+            self.sw_if_index, 0, self.ip4_table_id)
 
     def set_table_ip6(self, table_id):
         """Set the interface in a IPv6 Table.
 
         .. note:: Must be called before configuring IP6 addresses.
         """
+        self.ip6_table_id = table_id
         self.test.vapi.sw_interface_set_table(
-            self.sw_if_index, 1, table_id)
+            self.sw_if_index, 1, self.ip6_table_id)
 
     def disable_ipv6_ra(self):
         """Configure IPv6 RA suppress on the VPP interface."""
@@ -245,6 +253,10 @@ class VppInterface(object):
         """Put interface ADMIN-UP."""
         self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
 
+    def admin_down(self):
+        """Put interface ADMIN-down."""
+        self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=0)
+
     def add_sub_if(self, sub_if):
         """Register a sub-interface with this interface.
 
@@ -262,3 +274,11 @@ class VppInterface(object):
         """Enable MPLS on the VPP interface."""
         self.test.vapi.sw_interface_enable_disable_mpls(
             self.sw_if_index)
+
+    def is_ip4_entry_in_fib_dump(self, dump):
+        for i in dump:
+            if i.address == self.local_ip4n and \
+               i.address_length == self.local_ip4_prefix_len and \
+               i.table_id == self.ip4_table_id:
+                return True
+        return False
index 19ee152..9493a70 100644 (file)
@@ -1,13 +1,35 @@
-
+from vpp_object import VppObject
 from vpp_interface import VppInterface
 
 
-class VppLoInterface(VppInterface):
+class VppLoInterface(VppInterface, VppObject):
     """VPP loopback interface."""
 
     def __init__(self, test, lo_index):
         """ Create VPP loopback interface """
-        r = test.vapi.create_loopback()
-        self._sw_if_index = r.sw_if_index
+        self._test = test
+        self.add_vpp_config()
         super(VppLoInterface, self).__init__(test)
         self._lo_index = lo_index
+
+    def add_vpp_config(self):
+        r = self.test.vapi.create_loopback()
+        self._sw_if_index = r.sw_if_index
+
+    def remove_vpp_config(self):
+        self.test.vapi.delete_loopback(self.sw_if_index)
+
+    def query_vpp_config(self):
+        dump = self.vapi.sw_interface_dump()
+        return self.is_interface_config_in_dump(dump)
+
+    def is_interface_config_in_dump(self, dump):
+        for i in dump:
+            if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
+               i.sw_if_index == self.sw_if_index:
+                return True
+        else:
+            return False
+
+    def object_id(self):
+        return "loopback-%d" % self._sw_if_index
index bdbcc3d..db530d9 100644 (file)
@@ -465,6 +465,10 @@ class VppPapiProvider(object):
         return self.api(self.papi.create_loopback,
                         {'mac_address': mac})
 
+    def delete_loopback(self, sw_if_index):
+        return self.api(self.papi.delete_loopback,
+                        {'sw_if_index': sw_if_index, })
+
     def ip_add_del_route(
             self,
             dst_address,
@@ -904,8 +908,8 @@ class VppPapiProvider(object):
         """
         :param is_add:
         :param mask:
-        :param match_n_vectors (Default value = 1):
-        :param table_index (Default value = 0xFFFFFFFF)
+        :param match_n_vectors: (Default value = 1):
+        :param table_index: (Default value = 0xFFFFFFFF)
         :param nbuckets:  (Default value = 2)
         :param memory_size:  (Default value = 2097152)
         :param skip_n_vectors:  (Default value = 0)