Skip to content

Commit 5033ba3

Browse files
hadessmartinpitt
authored andcommitted
Add mock server for low-memory-monitor (#54)
API version 2.0 https://gitlab.freedesktop.org/hadess/low-memory-monitor/
1 parent 776b131 commit 5033ba3

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''low-memory-monitor mock template
2+
3+
This creates the expected methods and properties of the main
4+
org.freedesktop.LowMemoryMonitor object.
5+
6+
This provides only the 2.0 D-Bus API of low-memory-monitor.
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+
__email__ = '[email protected]'
17+
__copyright__ = '(c) 2019, Red Hat Inc.'
18+
__license__ = 'LGPL 3+'
19+
20+
import dbus
21+
22+
from dbusmock import MOCK_IFACE
23+
24+
BUS_NAME = 'org.freedesktop.LowMemoryMonitor'
25+
MAIN_OBJ = '/org/freedesktop/LowMemoryMonitor'
26+
MAIN_IFACE = 'org.freedesktop.LowMemoryMonitor'
27+
SYSTEM_BUS = True
28+
29+
30+
def load(mock, parameters):
31+
# Loaded!
32+
mock.loaded = True
33+
34+
35+
@dbus.service.method(MOCK_IFACE,
36+
in_signature='y', out_signature='')
37+
def EmitWarning(self, state):
38+
self.EmitSignal(MAIN_IFACE, 'LowMemoryWarning', 'y', [dbus.Byte(state)])

tests/test_low_memory_monitor.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/python3
2+
3+
# This program is free software; you can redistribute it and/or modify it under
4+
# the terms of the GNU Lesser General Public License as published by the Free
5+
# Software Foundation; either version 3 of the License, or (at your option) any
6+
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
7+
# of the license.
8+
9+
__author__ = 'Bastien Nocera'
10+
__email__ = '[email protected]'
11+
__copyright__ = '(c) 2019 Red Hat Inc.'
12+
__license__ = 'LGPL 3+'
13+
14+
import unittest
15+
import sys
16+
import subprocess
17+
import dbus
18+
import dbus.mainloop.glib
19+
import dbusmock
20+
import fcntl
21+
import os
22+
23+
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
24+
25+
26+
class TestLowMemoryMonitor(dbusmock.DBusTestCase):
27+
'''Test mocking low-memory-monitor'''
28+
29+
@classmethod
30+
def setUpClass(klass):
31+
klass.start_system_bus()
32+
klass.dbus_con = klass.get_dbus(True)
33+
34+
def setUp(self):
35+
(self.p_mock, self.obj_lmm) = self.spawn_server_template(
36+
'low_memory_monitor', {}, stdout=subprocess.PIPE)
37+
# set log to nonblocking
38+
flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
39+
fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
40+
self.last_warning = -1
41+
self.dbusmock = dbus.Interface(self.obj_lmm, dbusmock.MOCK_IFACE)
42+
43+
def tearDown(self):
44+
self.p_mock.terminate()
45+
self.p_mock.wait()
46+
47+
def test_low_memory_warning_signal(self):
48+
'''LowMemoryWarning signal'''
49+
50+
self.dbusmock.EmitWarning(100)
51+
log = self.p_mock.stdout.read()
52+
self.assertRegex(log, b'[0-9.]+ emit .*LowMemoryWarning 100\n')
53+
54+
self.dbusmock.EmitWarning(255)
55+
log = self.p_mock.stdout.read()
56+
self.assertRegex(log, b'[0-9.]+ emit .*LowMemoryWarning 255\n')
57+
58+
59+
if __name__ == '__main__':
60+
# avoid writing to stderr
61+
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))

0 commit comments

Comments
 (0)