From cd68cd250118217a471e34cfbcfc18c7cf9347f3 Mon Sep 17 00:00:00 2001 From: Ruben Rodriguez Buchillon Date: Mon, 16 Jul 2018 19:21:42 +0800 Subject: stats_manager: prepare StatsManager to be a utility used in hdctools This is the first CL in a series of CLs to start using StatsManager in servo/hdctools (package depends on ec-devutils, as in this package). This CL: - beefs up StatsManager to handle unavailable units more gracefully - adds a few more tests to stats_manager_unittest.py - adds some minor unit testing for powerlog's file retrieval logic BRANCH=None BUG=chromium:760267 TEST=manual testing, unit tests still pass, powerlog still works Change-Id: Ifcdfcc482008484fbc21326c6f087ebf466c3e74 Signed-off-by: Ruben Rodriguez Buchillon Reviewed-on: https://chromium-review.googlesource.com/1140025 Reviewed-by: Mengqi Guo --- extra/usb_power/powerlog_unittest.py | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 extra/usb_power/powerlog_unittest.py (limited to 'extra/usb_power/powerlog_unittest.py') diff --git a/extra/usb_power/powerlog_unittest.py b/extra/usb_power/powerlog_unittest.py new file mode 100644 index 0000000000..7058c57aa7 --- /dev/null +++ b/extra/usb_power/powerlog_unittest.py @@ -0,0 +1,49 @@ +# Copyright 2018 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +"""Unit tests for powerlog.""" + +import os +import shutil +import tempfile +import unittest + +import powerlog + +class TestPowerlog(unittest.TestCase): + """Test to verify powerlog util methods work as expected.""" + + def setUp(self): + """Set up data and create a temporary directory to save data and stats.""" + self.tempdir = tempfile.mkdtemp() + self.filename = 'testfile' + self.filepath = os.path.join(self.tempdir, self.filename) + with open(self.filepath, 'w') as f: + f.write('') + + def tearDown(self): + """Delete the temporary directory and its content.""" + shutil.rmtree(self.tempdir) + + def test_ProcessFilenameAbsoluteFilePath(self): + """Absolute file path is returned unchanged.""" + processed_fname = powerlog.process_filename(self.filepath) + self.assertEqual(self.filepath, processed_fname) + + def test_ProcessFilenameRelativeFilePath(self): + """Finds relative file path inside a known config location.""" + original = powerlog.CONFIG_LOCATIONS + powerlog.CONFIG_LOCATIONS = [self.tempdir] + processed_fname = powerlog.process_filename(self.filename) + try: + self.assertEqual(self.filepath, processed_fname) + finally: + powerlog.CONFIG_LOCATIONS = original + + def test_ProcessFilenameInvalid(self): + """IOError is raised when file cannot be found by any of the four ways.""" + with self.assertRaises(IOError): + powerlog.process_filename(self.filename) + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.1