Skip to content

Commit

Permalink
tests: take into account new features
Browse files Browse the repository at this point in the history
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
  • Loading branch information
piotrbartman committed Nov 22, 2022
1 parent 2c87455 commit 6d8d39a
Showing 1 changed file with 85 additions and 26 deletions.
111 changes: 85 additions & 26 deletions qubes/tests/api_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
import asyncio
from datetime import datetime
from unittest import mock
import qubes.tests
import qubes.api.misc
Expand Down Expand Up @@ -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()
])
Expand Down Expand Up @@ -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()
])
Expand Down Expand Up @@ -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()

0 comments on commit 6d8d39a

Please sign in to comment.