Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check power mode before and after suspend (New) #1145

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions providers/base/bin/check_power_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
#
# This file is part of Checkbox.
#
# Copyright 2024 Canonical Ltd.
# Authors: Bin Li <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.
#

"""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()
58 changes: 58 additions & 0 deletions providers/base/tests/test_check_power_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# Written by:
# Bin Li <[email protected]>
#
# 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 <http://www.gnu.org/licenses/>.

# 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()
24 changes: 24 additions & 0 deletions providers/base/units/suspend/suspend.pxu
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
Loading