apache_error_log_metrics: Refactor unit test
Pull out the function to shell out to the script.
TEST=unit tests pass
BUG=None
Change-Id: Ife3ff1b9490082b35ef29a39267b47aa1fde40a6
Reviewed-on: https://chromium-review.googlesource.com/709950
Commit-Ready: Paul Hobbs <phobbs@google.com>
Tested-by: Paul Hobbs <phobbs@google.com>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
diff --git a/site_utils/stats/apache_error_log_metrics_unittest.py b/site_utils/stats/apache_error_log_metrics_unittest.py
index eb9df9c..2ef3b01 100644
--- a/site_utils/stats/apache_error_log_metrics_unittest.py
+++ b/site_utils/stats/apache_error_log_metrics_unittest.py
@@ -74,45 +74,46 @@
self.assertEqual('error', matched[4].group('log_level'))
self.assertEqual(None, matched[4].group('mod_wsgi'))
+
+ def _ShellOutToScript(self, lines):
+ """Shells out to the script.
+
+ @param lines: Lines to feed to stdin."""
+ with tempfile.NamedTemporaryFile() as temp_file:
+ p = subprocess.Popen([SCRIPT_PATH,
+ '--debug-metrics-file', temp_file.name],
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+ p.communicate('\n'.join(lines))
+
+ with open(temp_file.name) as fh:
+ return fh.read()
+
+
def testApacheErrorLogScriptWithNonMatchingLine(self):
"""Try shelling out the the script with --debug-file.
Sending it a non-matching line should result in no output from
ERROR_LOG_METRIC.
"""
- with tempfile.NamedTemporaryFile() as temp_file:
- p = subprocess.Popen([SCRIPT_PATH,
- '--debug-metrics-file', temp_file.name],
- stdin=subprocess.PIPE, stdout=subprocess.PIPE)
- p.communicate('an-example-log-line')
+ contents = self._ShellOutToScript(['an example log line'])
- with open(temp_file.name) as fh:
- contents = fh.read()
-
- # We have to use re.search here with a word border character ('\b')
- # because the ERROR_LOG_LINE_METRIC contains ERROR_LOG_METRIC as a
- # substring.
- self.assertTrue(re.search(
- apache_error_log_metrics.ERROR_LOG_LINE_METRIC[1:] + r'\b',
- contents))
- self.assertFalse(re.search(
- apache_error_log_metrics.ERROR_LOG_METRIC[1:] + r'\b',
- contents))
+ # We have to use re.search here with a word border character ('\b')
+ # because the ERROR_LOG_LINE_METRIC contains ERROR_LOG_METRIC as a
+ # substring.
+ self.assertTrue(re.search(
+ apache_error_log_metrics.ERROR_LOG_LINE_METRIC[1:] + r'\b',
+ contents))
+ self.assertFalse(re.search(
+ apache_error_log_metrics.ERROR_LOG_METRIC[1:] + r'\b',
+ contents))
def testApachErrorLogScriptWithMatchingLine(self):
- """Try shelling out the the script with --debug-file.
+ """Try shelling out the the script with a matching line.
Sending it a line which matches the first-line regex should result in
output from ERROR_LOG_METRIC.
"""
- with tempfile.NamedTemporaryFile() as temp_file:
- p = subprocess.Popen([SCRIPT_PATH,
- '--debug-metrics-file', temp_file.name],
- stdin=subprocess.PIPE, stdout=subprocess.PIPE)
- p.communicate('[foo] [:bar] [pid 123] WARNING')
-
- with open(temp_file.name) as fh:
- contents = fh.read()
+ contents = self._ShellOutToScript(['[foo] [:bar] [pid 123] WARNING'])
self.assertTrue(re.search(
apache_error_log_metrics.ERROR_LOG_LINE_METRIC[1:] + r'\b',
@@ -121,17 +122,9 @@
apache_error_log_metrics.ERROR_LOG_METRIC[1:] + r'\b',
contents))
-
def testApachErrorLogScriptWithSpecialLines(self):
- """Sending lines with specific messages ."""
- with tempfile.NamedTemporaryFile() as temp_file:
- p = subprocess.Popen([SCRIPT_PATH,
- '--debug-metrics-file', temp_file.name],
- stdin=subprocess.PIPE, stdout=subprocess.PIPE)
- p.communicate('[foo] [:bar] [pid 123] WARNING Segmentation fault')
-
- with open(temp_file.name) as fh:
- contents = fh.read()
+ """Sending lines with specific messages."""
+ contents = self._ShellOutToScript(['[foo] [:bar] [pid 123] WARNING Segmentation fault'])
self.assertTrue(re.search(
apache_error_log_metrics.SEGFAULT_METRIC[1:] + r'\b',