diff --git a/providers/base/bin/check_power_mode.py b/providers/base/bin/check_power_mode.py new file mode 100755 index 0000000000..c31a407c8c --- /dev/null +++ b/providers/base/bin/check_power_mode.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# +# This file is part of Checkbox. +# +# 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, +# as published by the Free Software Foundation. + +# +# Checkbox 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 Checkbox. If not, see . +# + +"""Check current power mode.""" +from pathlib import Path +from switch_power_mode import get_sysfs_content + + +def main(): + """main function to read the power mode.""" + sysfs_root = Path("/sys/firmware/acpi/") + profile_path = sysfs_root / "platform_profile" + + 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__": + 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() diff --git a/providers/base/units/suspend/suspend.pxu b/providers/base/units/suspend/suspend.pxu index 305af613cf..50282bcb12 100644 --- a/providers/base/units/suspend/suspend.pxu +++ b/providers/base/units/suspend/suspend.pxu @@ -56,6 +56,17 @@ estimated_duration: 1.2 _summary: Dumps memory info to a file for comparison after suspend command: meminfo_resource.py > "$PLAINBOX_SESSION_SHARE"/meminfo_before_suspend +plugin: shell +category_id: com.canonical.plainbox::suspend +id: suspend/power_mode_before_suspend +estimated_duration: 1.2 +requires: + module.name == 'platform_profile' + package.name == 'power-profiles-daemon' + platform_profile.supported == 'True' +_summary: Dumps power_mode info to a file for comparison after suspend +command: check_power_mode.py > "$PLAINBOX_SESSION_SHARE"/power_mode_before_suspend + unit: template template-resource: device template-filter: device.category == 'NETWORK' @@ -616,6 +627,19 @@ _purpose: command: meminfo_resource.py | diff "$PLAINBOX_SESSION_SHARE"/meminfo_before_suspend - _summary: Ensure all memory is accessible after waking from suspend mode. +plugin: shell +category_id: com.canonical.plainbox::suspend +id: suspend/power_mode_after_suspend +estimated_duration: 1.2 +requires: + module.name == 'platform_profile' + package.name == 'power-profiles-daemon' + platform_profile.supported == 'True' +depends: suspend/suspend_advanced_auto suspend/power_mode_before_suspend +_description: + Verify that power mode is changed after resuming from suspend. +command: check_power_mode.py | diff "$PLAINBOX_SESSION_SHARE"/power_mode_before_suspend - + plugin: manual category_id: com.canonical.plainbox::suspend id: suspend/display_after_suspend