Skip to content

Commit b24f7b7

Browse files
committed
Add upower_power_profiles_daemon template
This works like the existing power_profiles_daemon template, but for the new D-Bus name/object path introduced in https://gitlab.freedesktop.org/upower/power-profiles-daemon/-/releases/0.20 Enable integration test on Ubuntu, which has version 0.13 in 22.04 LTS.
1 parent 9e46a1f commit b24f7b7

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

dbusmock/templates/power_profiles_daemon.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
"""power-profiles-daemon mock template
1+
"""power-profiles-daemon < 0.20 mock template
22
33
This creates the expected methods and properties of the main
44
net.hadess.PowerProfiles object.
55
66
This provides only the non-deprecated D-Bus API as of version 0.9.
7+
Note that this template is deprecated: Version 0.20 listens on a different
8+
bus name/object path, it is provided in upower_power_profiles_daemon.py
79
"""
810

911
# This program is free software; you can redistribute it and/or modify it under
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""power-profiles-daemon >= 0.20 mock template
2+
3+
This creates the expected methods and properties of the main
4+
org.freedesktop.UPower.PowerProfiles object.
5+
6+
This provides the D-Bus API as of version 0.20.
7+
"""
8+
9+
# This program is free software; you can redistribute it and/or modify it under
10+
# the terms of the GNU Lesser General Public License as published by the Free
11+
# Software Foundation; either version 3 of the License, or (at your option) any
12+
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
13+
# of the license.
14+
15+
__author__ = "Bastien Nocera"
16+
__copyright__ = """
17+
(c) 2021, Red Hat Inc.
18+
(c) 2017 - 2024 Martin Pitt <[email protected]>
19+
"""
20+
21+
import dbus
22+
23+
BUS_NAME = "org.freedesktop.UPower.PowerProfiles"
24+
MAIN_OBJ = "/org/freedesktop/UPower/PowerProfiles"
25+
MAIN_IFACE = "org.freedesktop.UPower.PowerProfiles"
26+
SYSTEM_BUS = True
27+
28+
29+
def hold_profile(self, profile, reason, application_id):
30+
self.cookie += 1
31+
element = {"Profile": profile, "Reason": reason, "ApplicationId": application_id}
32+
self.holds[self.cookie] = element
33+
self.props[MAIN_IFACE]["ActiveProfileHolds"] = []
34+
for value in self.holds.values():
35+
self.props[MAIN_IFACE]["ActiveProfileHolds"].append(value)
36+
return self.cookie
37+
38+
39+
def release_profile(self, cookie):
40+
self.holds.pop(cookie)
41+
self.props[MAIN_IFACE]["ActiveProfileHolds"] = []
42+
for value in self.holds.values():
43+
self.props[MAIN_IFACE]["ActiveProfileHolds"].append(value)
44+
if len(self.props[MAIN_IFACE]["ActiveProfileHolds"]) == 0:
45+
self.props[MAIN_IFACE]["ActiveProfileHolds"] = dbus.Array([], signature="(aa{sv})")
46+
47+
48+
def load(mock, parameters):
49+
# Loaded!
50+
mock.loaded = True
51+
mock.cookie = 0
52+
mock.hold_profile = hold_profile
53+
mock.release_profile = release_profile
54+
mock.holds = {}
55+
56+
props = {
57+
"ActiveProfile": parameters.get("ActiveProfile", "balanced"),
58+
"PerformanceDegraded": parameters.get("PerformanceDegraded", ""),
59+
"Profiles": [
60+
dbus.Dictionary({"Profile": "power-saver", "Driver": "dbusmock"}, signature="sv"),
61+
dbus.Dictionary({"Profile": "balanced", "Driver": "dbusmock"}, signature="sv"),
62+
dbus.Dictionary({"Profile": "performance", "Driver": "dbusmock"}, signature="sv"),
63+
],
64+
"Actions": dbus.Array([], signature="s"),
65+
"ActiveProfileHolds": dbus.Array([], signature="(aa{sv})"),
66+
}
67+
mock.AddProperties(MAIN_IFACE, dbus.Dictionary(props, signature="sv"))
68+
69+
mock.AddMethods(
70+
MAIN_IFACE,
71+
[
72+
("HoldProfile", "sss", "u", "ret = self.hold_profile(self, args[0], args[1], args[2])"),
73+
("ReleaseProfile", "u", "", "self.release_profile(self, args[0])"),
74+
],
75+
)

tests/run-debian

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ eatmydata apt-get -y --purge dist-upgrade
1414
eatmydata apt-get install --no-install-recommends -y git \
1515
python3-all python3-setuptools python3-setuptools-scm python3-build python3-venv \
1616
python3-dbus python3-pytest python3-gi gir1.2-glib-2.0 \
17-
dbus libnotify-bin upower network-manager bluez ofono ofono-scripts
18-
19-
# power-profiles-daemon 0.20 did not yet land in Ubuntu 24.04
20-
. /etc/os-release
21-
if [ "$ID" = "debian" ]; then
22-
eatmydata apt-get install -y power-profiles-daemon
23-
fi
17+
dbus libnotify-bin upower network-manager bluez ofono ofono-scripts power-profiles-daemon
2418

2519
# systemd's tools otherwise fail on "not been booted with systemd"
2620
mkdir -p /run/systemd/system

tests/test_power_profiles_daemon.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@ def setUpClass(cls):
3838
cls.dbus_con = cls.get_dbus(True)
3939

4040
def setUp(self):
41-
(self.p_mock, self.obj_ppd) = self.spawn_server_template("power_profiles_daemon", {}, stdout=subprocess.PIPE)
41+
# depending on the installed client version, we need to pick the right template
42+
try:
43+
version = subprocess.run(
44+
["powerprofilesctl", "version"], capture_output=True, text=True, check=True
45+
).stdout
46+
version = ".".join(version.strip().split(".")[:2])
47+
template = "power_profiles_daemon" if float(version) < 0.2 else "upower_power_profiles_daemon"
48+
except subprocess.CalledProcessError as e:
49+
# 0.20 crashes without daemon: https://gitlab.freedesktop.org/upower/power-profiles-daemon/-/issues/139
50+
print("Failed to get powerprofilesctl version, assuming >= 0.20:", e, file=sys.stderr)
51+
template = "upower_power_profiles_daemon"
52+
53+
(self.p_mock, self.obj_ppd) = self.spawn_server_template(template, {}, stdout=subprocess.PIPE)
4254
# set log to nonblocking
4355
flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
4456
fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)

0 commit comments

Comments
 (0)