diff --git a/providers/base/bin/check_power_mode.py b/providers/base/bin/check_power_mode.py index a0683d2c99..c31a407c8c 100755 --- a/providers/base/bin/check_power_mode.py +++ b/providers/base/bin/check_power_mode.py @@ -2,7 +2,8 @@ # # This file is part of Checkbox. # -# Copyright 2023 Canonical Ltd. +# Copyright 2024 Canonical Ltd. +# Authors: Bin Li # # Checkbox is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3, @@ -28,10 +29,11 @@ def main(): sysfs_root = Path("/sys/firmware/acpi/") profile_path = sysfs_root / "platform_profile" - profile = get_sysfs_content(profile_path).split() - print(profile[0]) - # uncomment the following line to change another mode - # os.system(f"powerprofilesctl set power-saver") + profile = get_sysfs_content(profile_path) + print(profile) + # uncomment the following lines to set another mode for testing + # from switch_power_mode import set_power_profile + # set_power_profile("power-saver") if __name__ == "__main__": diff --git a/providers/base/tests/test_check_power_mode.py b/providers/base/tests/test_check_power_mode.py new file mode 100644 index 0000000000..033f2b45b2 --- /dev/null +++ b/providers/base/tests/test_check_power_mode.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# Copyright 2024 Canonical Ltd. +# Written by: +# Bin Li +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# pylint: disable=import-error + +""" A unittest module for the check_power_mode module. """ +import unittest +from unittest.mock import patch +import io + +from check_power_mode import main + + +class TestCheckPowerMode(unittest.TestCase): + """Tests for the check_power_mode module.""" + + @patch("sys.stdout", new_callable=io.StringIO) + @patch("check_power_mode.get_sysfs_content") + def test_main_success(self, mock_get_sysfs_content, mock_stdout): + """ + Tests successful execution of the main function. + """ + mock_get_sysfs_content.return_value = "balanced" + + # Call the main function + main() + + self.assertEqual(mock_stdout.getvalue(), "balanced\n") + + @patch("sys.stdout", new_callable=io.StringIO) + @patch("check_power_mode.get_sysfs_content") + def test_main_failure(self, mock_get_sysfs_content, mock_stdout): + """ + Tests failed execution of the main function. + """ + mock_get_sysfs_content.return_value = "" + + main() + + self.assertNotEqual(mock_stdout.getvalue(), "balanced\n") + + +if __name__ == "__main__": + unittest.main()