Parcourir la source

[3517] Corrected test for interface detection.

The original version of this test incorrectly matched the names of the
interfaces detected by the IfaceMgr and those detected by the unit test.
As a result, the interfaces with similar names (e.g. virbr0 and virbr0-nic)
where compared for the equality of indexes, addresses and flags.
Marcin Siodelski il y a 10 ans
Parent
commit
25bae983a9
1 fichiers modifiés avec 30 ajouts et 28 suppressions
  1. 30 28
      src/lib/dhcp/tests/iface_mgr_unittest.cc

+ 30 - 28
src/lib/dhcp/tests/iface_mgr_unittest.cc

@@ -2642,46 +2642,48 @@ checkIfAddrs(const Iface & iface, struct ifaddrs *& ifptr) {
 }
 
 TEST_F(IfaceMgrTest, detectIfaces) {
-
-    NakedIfaceMgr* ifacemgr = new NakedIfaceMgr();
-    IfaceMgr::IfaceCollection& detectedIfaces = ifacemgr->getIfacesLst();
+    NakedIfaceMgr ifacemgr;
 
     // We are using struct ifaddrs as it is the only good portable one
     // ifreq and ioctls are far from portabe. For BSD ifreq::ifa_flags field
     // is only a short which, nowadays, can be negative
-    struct ifaddrs * iflist = 0, * ifptr = 0;
-
-    if(getifaddrs(& iflist) != 0) {
-        isc_throw(Unexpected, "Cannot detect interfaces");
-    }
+    struct ifaddrs *iflist = 0, *ifptr = 0;
+    ASSERT_EQ(0, getifaddrs(&iflist))
+        << "Unit test failed to detect interfaces.";
 
+    // Go over all interfaces detected by the unit test and see if they
+    // match with the interfaces detected by IfaceMgr.
     for (ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) {
-        for (IfaceMgr::IfaceCollection::const_iterator i = detectedIfaces.begin();
-            i != detectedIfaces.end(); ++ i) {
-            if (!strncmp(ifptr->ifa_name, (*i).getName().c_str(),
-                         (*i).getName().size())) {
-
-                // Typically unit-tests try to be silent. But interface detection
-                // is tricky enough to warrant additional prints.
-                std::cout << "Checking interface " << i->getName() << std::endl;
+        Iface* i = ifacemgr.getIface(std::string(ifptr->ifa_name));
+
+        // If the given interface was also detected by the IfaceMgr,
+        // check that its properties are correct.
+        if (i != NULL) {
+            // Check if interface index is reported properly
+            EXPECT_TRUE(checkIfIndex(*i, ifptr))
+                << "Non-matching index of the detected interface "
+                << i->getName();
+
+            // Check if flags are reported properly
+            EXPECT_TRUE(checkIfFlags(*i, ifptr))
+                << "Non-matching flags of the detected interface "
+                << i->getName();
+
+            // Check if addresses are reported properly
+            EXPECT_TRUE(checkIfAddrs(*i, ifptr))
+                << "Non-matching addresses on the detected interface "
+                << i->getName();
 
-                // Check if interface index is reported properly
-                EXPECT_TRUE(checkIfIndex(*i, ifptr));
-
-                // Check if flags are reported properly
-                EXPECT_TRUE(checkIfFlags(*i, ifptr));
-
-                // Check if addresses are reported properly
-                EXPECT_TRUE(checkIfAddrs(*i, ifptr));
-
-            }
+        } else {
+            // The interface detected here seems to be missing in the
+            // IfaceMgr.
+            ADD_FAILURE() << "Interface " << ifptr->ifa_name
+                          << " not detected by the Interface Manager";
         }
     }
 
     freeifaddrs(iflist);
     iflist = 0;
-
-    delete ifacemgr;
 }
 
 volatile bool callback_ok;