Tests: simple refactor in vpp_pg_interface. 60/19860/5
authorPaul Vinciguerra <pvinci@vinciconsulting.com>
Tue, 28 May 2019 19:40:47 +0000 (15:40 -0400)
committerDave Barach <openvpp@barachs.net>
Tue, 4 Jun 2019 16:05:57 +0000 (16:05 +0000)
Pull common code into a single method.

Type: refactor

Change-Id: Ic540d23eebbd17f838ed7a1a9dee80815a27847c
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
test/vpp_pg_interface.py

index 7c31cfc..380c1fa 100755 (executable)
@@ -109,24 +109,32 @@ class VppPGInterface(VppInterface):
                 self.in_path, self.pg_index, self.cap_name)
         self._nb_replays = None
 
-    def enable_capture(self):
-        """ Enable capture on this packet-generator interface
-            of at most n packets.
-            If n < 0, this is no limit
-        """
+    def _rename_previous_capture_file(self, path, counter, file):
+        # if a file from a previous capture exists, rename it.
         try:
-            if os.path.isfile(self.out_path):
+            if os.path.isfile(path):
                 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
                     (self.test.tempdir,
                      time.time(),
                      self.name,
-                     self.out_history_counter,
-                     self._out_file)
+                     counter,
+                     file)
                 self.test.logger.debug("Renaming %s->%s" %
-                                       (self.out_path, name))
-                os.rename(self.out_path, name)
-        except:
-            pass
+                                       (path, name))
+                os.rename(path, name)
+        except OSError:
+            self.test.logger.debug("OSError: Could not rename %s %s" %
+                                   (path, file))
+
+    def enable_capture(self):
+        """ Enable capture on this packet-generator interface
+            of at most n packets.
+            If n < 0, this is no limit
+        """
+
+        self._rename_previous_capture_file(self.out_path,
+                                           self.out_history_counter,
+                                           self._out_file)
         # FIXME this should be an API, but no such exists atm
         self.test.vapi.cli(self.capture_cli)
         self._pcap_reader = None
@@ -142,19 +150,9 @@ class VppPGInterface(VppInterface):
 
         """
         self._nb_replays = nb_replays
-        try:
-            if os.path.isfile(self.in_path):
-                name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %\
-                    (self.test.tempdir,
-                     time.time(),
-                     self.name,
-                     self.in_history_counter,
-                     self._in_file)
-                self.test.logger.debug("Renaming %s->%s" %
-                                       (self.in_path, name))
-                os.rename(self.in_path, name)
-        except:
-            pass
+        self._rename_previous_capture_file(self.in_path,
+                                           self.in_history_counter,
+                                           self._in_file)
         wrpcap(self.in_path, pkts)
         self.test.register_capture(self.cap_name)
         # FIXME this should be an API, but no such exists atm