traffic tests: use TestCaseInTempdir.mktemp()
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Thu, 25 Oct 2018 00:37:02 +0000 (13:37 +1300)
committerDouglas Bagnall <dbagnall@samba.org>
Tue, 8 Jan 2019 22:55:34 +0000 (23:55 +0100)
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/blackbox/traffic_learner.py
python/samba/tests/blackbox/traffic_replay.py
python/samba/tests/blackbox/traffic_summary.py

index e6adce7a568eb95bae40524aa8875305e7948756..bc4e7a6bda2a607af2825a34825d5c1e1b338616 100644 (file)
@@ -17,7 +17,6 @@
 
 """Blackbox tests for traffic_leaner"""
 
-from contextlib import contextmanager
 import os
 import tempfile
 from samba.emulate import traffic
@@ -28,18 +27,6 @@ LEARNER  = "script/traffic_learner"
 DATA_DIR = "python/samba/tests/blackbox/testdata"
 
 
-@contextmanager
-def temp_file(temp_dir):
-    try:
-        tf   = tempfile.NamedTemporaryFile(dir=temp_dir)
-        name = tf.name
-        tf.close()
-        yield name
-    finally:
-        if os.path.exists(name):
-            os.remove(name)
-
-
 class TrafficLearnerTests(BlackboxTestCase):
 
     def test_no_output_file(self):
@@ -50,9 +37,9 @@ class TrafficLearnerTests(BlackboxTestCase):
         """Ensure a model is generated from a summary file and it is
            correct"""
 
-        with temp_file(self.tempdir) as output:
-            summary  = os.path.join(DATA_DIR, "traffic-sample-very-short.txt")
-            command  = "%s %s --out %s" % (LEARNER, summary, output)
+        with self.mktemp() as output:
+            summary = os.path.join(DATA_DIR, "traffic-sample-very-short.txt")
+            command = "%s %s --out %s" % (LEARNER, summary, output)
             self.check_run(command)
 
             expected_fn = os.path.join(DATA_DIR, "traffic_learner.expected")
index a84d1a423e4e6c220c1e2e737b504f220e451855..835c248f0d9f1e1954ed1ae338930568292afb22 100644 (file)
@@ -17,7 +17,6 @@
 
 """Blackbox tests for traffic_replay"""
 
-from contextlib import contextmanager
 import os
 import tempfile
 
@@ -34,18 +33,6 @@ MODEL         = os.path.join(DATA_DIR, "traffic-sample-very-short.model")
 EXPECTED_OUTPUT = os.path.join(DATA_DIR, "traffic_replay-%s.expected")
 
 
-@contextmanager
-def temp_file(temp_dir):
-    try:
-        tf   = tempfile.NamedTemporaryFile(dir=temp_dir)
-        name = tf.name
-        tf.close()
-        yield name
-    finally:
-        if os.path.exists(name):
-            os.remove(name)
-
-
 class TrafficLearnerTests(BlackboxTestCase):
 
     def tearDown(self):
@@ -76,7 +63,7 @@ class TrafficLearnerTests(BlackboxTestCase):
                                    "--old-scale",
                                    "--conversation-persistence=0.95"],
                                   )):
-            with temp_file(self.tempdir) as output:
+            with self.mktemp() as output:
                 command = ([SCRIPT, MODEL,
                             "--traffic-summary", output,
                             "-D1", "-S0.1"] +
index 99fe6b84e946882dedc0584c0991c59eb2a471c6..c0be4c082e62ae03436aa5926877cde7e635f1d7 100644 (file)
@@ -17,7 +17,6 @@
 
 """Blackbox tests for traffic_summary"""
 
-from contextlib import contextmanager
 import os
 import subprocess
 import tempfile
@@ -30,18 +29,6 @@ INPUT       = os.path.join(DATA_DIR, "traffic_summary.pdml")
 EXPECTED_FN = os.path.join(DATA_DIR, "traffic_summary.expected")
 
 
-@contextmanager
-def temp_file(temp_dir):
-    try:
-        tf   = tempfile.NamedTemporaryFile(dir=temp_dir)
-        name = tf.name
-        tf.close()
-        yield name
-    finally:
-        if os.path.exists(name):
-            os.remove(name)
-
-
 class TrafficSummaryTests(BlackboxTestCase):
 
     def check_twig(self):
@@ -57,8 +44,8 @@ class TrafficSummaryTests(BlackboxTestCase):
         if not self.check_twig():
             self.skipTest("Perl module XML::Twig is not installed")
 
-        with temp_file(self.tempdir) as output:
-            command  = "%s %s >%s" % (SCRIPT, INPUT, output)
+        with self.mktemp() as output:
+            command = "%s %s >%s" % (SCRIPT, INPUT, output)
             print(command)
             self.check_run(command)
             expected = open(EXPECTED_FN).readlines()