|
| 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 | + |
| 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