Skip to content
Merged
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
20 changes: 17 additions & 3 deletions providers/base/bin/switch_power_mode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python3
# !/usr/bin/env python3
#
# This file is part of Checkbox.
#
Expand Down Expand Up @@ -48,9 +48,23 @@ def set_power_profile(profile):
Raises:
SystemExit: If the power profile could not be set.
"""
# In sys file the modes are low-power, balanced, or performance
# In sys file the modes are quiet, low-power, cool, balanced or performance
# but powerprofilesctl only accepts power-saver, balanced or performance
profile = "power-saver" if profile == "low-power" else profile
profile_mappings = {
"low-power": "power-saver",
"power-saver": "power-saver",
"quiet": "power-saver",
"balanced": "balanced",
"balanced_performance": "balanced",
"cool": "balanced",
"performance": "performance",
}

if profile not in profile_mappings:
raise SystemExit("Unhandled ACPI platform profile: {}".format(profile))

profile = profile_mappings[profile]

try:
subprocess.check_call(["powerprofilesctl", "set", profile])
except subprocess.CalledProcessError as e:
Expand Down
17 changes: 17 additions & 0 deletions providers/base/tests/test_switch_power_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ def test_set_power_profile_failure(self, mock_check_call):
["powerprofilesctl", "set", "performance"]
)

@patch("subprocess.check_call") # Mock the subprocess.check_call function
def test_set_power_profile_unhandled(self, mock_check_call):
"""
Tests handling of a unhandled power profile setting.
"""
mock_check_call.side_effect = subprocess.CalledProcessError(
1, "powerprofilesctl"
)

with self.assertRaises(SystemExit) as cm:
set_power_profile("unexpected-profile")

self.assertEqual(
str(cm.exception),
"Unhandled ACPI platform profile: unexpected-profile",
)

@patch("sys.stdout", new_callable=io.StringIO)
def test_main_success(self, mock_stdout):
"""
Expand Down
Loading