From 6d8d39a0920598c01914f7828a1871314e9ee5d8 Mon Sep 17 00:00:00 2001 From: Piotr Bartman Date: Mon, 21 Nov 2022 18:58:22 +0100 Subject: [PATCH] tests: take into account new features take into account in misc tests last-update and last-updates-check features add Actual and Expected message to show the differences on CI for easier debugging create DatetimeMock to determine values of new features --- qubes/tests/api_misc.py | 111 ++++++++++++++++++++++++++++++---------- 1 file changed, 85 insertions(+), 26 deletions(-) diff --git a/qubes/tests/api_misc.py b/qubes/tests/api_misc.py index cdcbe54eb..841af27f0 100644 --- a/qubes/tests/api_misc.py +++ b/qubes/tests/api_misc.py @@ -18,6 +18,7 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, see . import asyncio +from datetime import datetime from unittest import mock import qubes.tests import qubes.api.misc @@ -205,22 +206,43 @@ def test_016_notify_tools_invalid_value_gui(self): def test_020_notify_updates_standalone(self): del self.src.template - response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n') - self.assertIsNone(response) - self.assertEqual(self.src.mock_calls, [ - mock.call.updateable.__bool__(), - mock.call.features.__setitem__('updates-available', True), - ]) + with DatetimeMock() as mock_date: + response = self.call_mgmt_func( + b'qubes.NotifyUpdates', payload=b'1\n') + self.assertIsNone(response) + expected = [ + mock.call.updateable.__bool__(), + mock.call.features.__getitem__('updates-available'), + mock.call.features.__getitem__().__bool__(), + mock.call.features.__setitem__('updates-available', True), + mock.call.features.__setitem__( + 'last-updates-check', mock_date.str + ), + ] + self.assertEqual(self.src.mock_calls, expected, + "\nActual:\n" + str(self.src.mock_calls) + + "\nExpected\n" + str(expected)) self.assertEqual(self.app.mock_calls, [mock.call.save()]) def test_021_notify_updates_standalone2(self): del self.src.template - response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'0\n') - self.assertIsNone(response) - self.assertEqual(self.src.mock_calls, [ - mock.call.updateable.__bool__(), - mock.call.features.__setitem__('updates-available', False), - ]) + with DatetimeMock() as mock_date: + response = self.call_mgmt_func( + b'qubes.NotifyUpdates', payload=b'0\n') + self.assertIsNone(response) + expected = [ + mock.call.updateable.__bool__(), + mock.call.features.__getitem__('updates-available'), + mock.call.features.__getitem__().__bool__(), + mock.call.features.__setitem__('last-update', mock_date.str), + mock.call.features.__setitem__('updates-available', False), + mock.call.features.__setitem__( + 'last-updates-check', mock_date.str + ), + ] + self.assertEqual(self.src.mock_calls, expected, + "\nActual:\n" + str(self.src.mock_calls) + + "\nExpected\n" + str(expected)) self.assertEqual(self.app.mock_calls, [ mock.call.save() ]) @@ -258,14 +280,25 @@ def test_025_notify_updates_template_based(self): self.src.updateable = False self.tpl.is_running.return_value = False self.src.storage.outdated_volumes = [] - response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n') - self.assertIsNone(response) + with DatetimeMock() as mock_date: + response = self.call_mgmt_func( + b'qubes.NotifyUpdates', payload=b'1\n') + self.assertIsNone(response) + expected = [ + mock.call.updateable.__bool__(), + mock.call.is_running(), + mock.call.features.__getitem__('updates-available'), + mock.call.features.__getitem__().__bool__(), + mock.call.features.__setitem__('updates-available', True), + mock.call.features.__setitem__( + 'last-updates-check', mock_date.str + ), + ] + self.assertEqual(self.src.mock_calls, []) - self.assertEqual(self.tpl.mock_calls, [ - mock.call.updateable.__bool__(), - mock.call.is_running(), - mock.call.features.__setitem__('updates-available', True), - ]) + self.assertEqual(self.tpl.mock_calls, expected, + "\nActual:\n" + str(self.tpl.mock_calls) + + "\nExpected\n" + str(expected)) self.assertEqual(self.app.mock_calls, [ mock.call.save() ]) @@ -305,16 +338,42 @@ def test_028_notify_updates_template_based_dispvm(self): self.src.updateable = False self.src.template.is_running.return_value = False self.src.storage.outdated_volumes = [] - response = self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'1\n') - self.assertIsNone(response) + with DatetimeMock() as mock_date: + response = self.call_mgmt_func( + b'qubes.NotifyUpdates', payload=b'1\n') + self.assertIsNone(response) + expected = [ + mock.call.updateable.__bool__(), + mock.call.is_running(), + mock.call.features.__getitem__('updates-available'), + mock.call.features.__getitem__().__bool__(), + mock.call.features.__setitem__('updates-available', True), + mock.call.features.__setitem__( + 'last-updates-check', mock_date.str + ), + ] self.assertEqual(self.src.mock_calls, []) + self.assertEqual(self.tpl.mock_calls, expected, + "\nActual:\n" + str(self.tpl.mock_calls) + + "\nExpected\n" + str(expected)) self.assertEqual(self.dvm.mock_calls, []) - self.assertEqual(self.tpl.mock_calls, [ - mock.call.updateable.__bool__(), - mock.call.is_running(), - mock.call.features.__setitem__('updates-available', True), - ]) self.assertIsInstance(self.src.updates_available, mock.Mock) self.assertEqual(self.app.mock_calls, [ mock.call.save() ]) + + +class DatetimeMock: + def __init__(self, year=2038, month=1, day=19, hour=3, minute=14, second=7): + self.mock_date = None + self.datetime = datetime(year, month, day, hour, minute, second) + + def __enter__(self): + self.mock_date = mock.patch('qubes.api.misc.datetime').start() + self.mock_date.today.return_value = self.datetime + self.mock_date.side_effect = lambda *args, **kw: datetime(*args, **kw) + self.mock_date.str = self.datetime.strftime('%Y-%m-%d %H:%M:%S') + return self.mock_date + + def __exit__(self, exc_type, exc_val, exc_tb): + self.mock_date.stop()