summaryrefslogtreecommitdiff
path: root/extra/usb_power/stats_manager_unittest.py
diff options
context:
space:
mode:
authorRuben Rodriguez Buchillon <coconutruben@chromium.org>2018-07-24 18:38:25 +0800
committerchrome-bot <chrome-bot@chromium.org>2018-08-01 00:05:03 -0700
commitaad29cce9475d04e3c92e5a6636ef04c335458ef (patch)
treebeb30f234141ddc688d3a9411ead806777939b2d /extra/usb_power/stats_manager_unittest.py
parentb3b220a8869479aad5dbfd0a11903ce5d28c6c4b (diff)
downloadchrome-ec-aad29cce9475d04e3c92e5a6636ef04c335458ef.tar.gz
stats_manager: Avoid losing data
In the case of multiple StatsManager, we might get data clobbered. That's particularly bad on long-running tests. This CL introduces two methods to make sure output files are unique in a directory. (1) on init, the user can supply a StatsManagerID (smid) that will be prepended to all output files. E.g. if the smid is 'ec' the summary will be stored at ec_summary.txt (2) should that fail because multiple StatsManagers are using the same smid supplied (or none were supplied) a simple file rotation is done where an integer suffix keeps getting incremented until the filename is unique. E.g. if summary.txt already exists, then summary1.txt will be created. This is not threadsafe at all so the user still have to use some caution to avoid clobbering their own data. BRANCH=None BUG=chromium:760267 TEST=unit tests still all pass Change-Id: I6cc083259362ee20e0242b94ac7cbb1228a06a7a Signed-off-by: Ruben Rodriguez Buchillon <coconutruben@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1140029 Reviewed-by: Mengqi Guo <mqg@chromium.org>
Diffstat (limited to 'extra/usb_power/stats_manager_unittest.py')
-rw-r--r--extra/usb_power/stats_manager_unittest.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/extra/usb_power/stats_manager_unittest.py b/extra/usb_power/stats_manager_unittest.py
index 2fb1080f4d..a669ab8e8f 100644
--- a/extra/usb_power/stats_manager_unittest.py
+++ b/extra/usb_power/stats_manager_unittest.py
@@ -143,6 +143,15 @@ class TestStatsManager(unittest.TestCase):
# Verify nothing gets appended to domain for filename if no unit exists.
self.assertIn('B.txt', files)
+ def test_SaveRawDataSMID(self):
+ """SaveRawData uses the smid when creating output filename."""
+ identifier = 'ec'
+ self.data = stats_manager.StatsManager(smid=identifier)
+ self._populate_dummy_stats()
+ files = self.data.SaveRawData(self.tempdir)
+ for fname in files:
+ self.assertTrue(os.path.basename(fname).startswith(identifier))
+
def test_SummaryToStringHideDomains(self):
"""Keys indicated in hide_domains are not printed in the summary."""
data = stats_manager.StatsManager(hide_domains=['A-domain'])
@@ -168,6 +177,14 @@ class TestStatsManager(unittest.TestCase):
summary_str = data.SummaryToString()
self.assertRegexpMatches(summary_str, d_b_a_c_regexp)
+ def test_MakeUniqueFName(self):
+ data = stats_manager.StatsManager()
+ testfile = os.path.join(self.tempdir, 'testfile.txt')
+ with open(testfile, 'w') as f:
+ f.write('')
+ expected_fname = os.path.join(self.tempdir, 'testfile0.txt')
+ self.assertEqual(expected_fname, data._MakeUniqueFName(testfile))
+
def test_SaveSummary(self):
"""SaveSummary properly dumps the summary into a file."""
self._populate_dummy_stats()
@@ -190,6 +207,14 @@ class TestStatsManager(unittest.TestCase):
'@@ B_mV 3 2.50 0.82 3.50 1.50\n',
f.readline())
+ def test_SaveSummarySMID(self):
+ """SaveSummary uses the smid when creating output filename."""
+ identifier = 'ec'
+ self.data = stats_manager.StatsManager(smid=identifier)
+ self._populate_dummy_stats()
+ fname = os.path.basename(self.data.SaveSummary(self.tempdir))
+ self.assertTrue(fname.startswith(identifier))
+
def test_SaveSummaryJSON(self):
"""SaveSummaryJSON saves the added data properly in JSON format."""
self._populate_dummy_stats()
@@ -208,6 +233,14 @@ class TestStatsManager(unittest.TestCase):
self.assertAlmostEqual(2.5, summary['B']['mean'])
self.assertEqual('millivolt', summary['B']['unit'])
+ def test_SaveSummaryJSONSMID(self):
+ """SaveSummaryJSON uses the smid when creating output filename."""
+ identifier = 'ec'
+ self.data = stats_manager.StatsManager(smid=identifier)
+ self._populate_dummy_stats()
+ fname = os.path.basename(self.data.SaveSummaryJSON(self.tempdir))
+ self.assertTrue(fname.startswith(identifier))
+
def test_SaveSummaryJSONNoUnit(self):
"""SaveSummaryJSON marks unknown units properly as N/A."""
self._populate_dummy_stats_no_unit()