-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit tests and CI #124
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Run Pytest | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
workflow_dispatch: | ||
jobs: | ||
unit-test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.11' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install pytest psutil | ||
- name: Run Pytest | ||
run: | | ||
pytest -v | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
import pytest | ||
import sys | ||
import os | ||
from unittest.mock import patch, MagicMock, mock_open | ||
|
||
# Add the src directory to the sys.path | ||
sys.path.insert( | ||
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../src")) | ||
) | ||
|
||
from ublue_update.cli import ( | ||
notify, | ||
ask_for_updates, | ||
inhibitor_checks_failed, | ||
run_updates, | ||
) | ||
|
||
|
||
@patch("ublue_update.cli.cfg") | ||
@patch("ublue_update.cli.os") | ||
@patch("ublue_update.cli.log") | ||
@patch("ublue_update.cli.subprocess.run") | ||
def test_notify_no_dbus_notify(mock_run, mock_log, mock_os, mock_cfg): | ||
mock_cfg.dbus_notify = False | ||
assert notify("test_title", "test_body") == None | ||
|
||
|
||
@patch("ublue_update.cli.cfg") | ||
@patch("ublue_update.cli.os") | ||
@patch("ublue_update.cli.log") | ||
@patch("ublue_update.cli.subprocess.run") | ||
def test_notify_uid_user(mock_run, mock_log, mock_os, mock_cfg): | ||
title = "test title" | ||
body = "test body" | ||
mock_cfg.dbus_notify = True | ||
mock_os.getuid.return_value = 1001 | ||
notify(title, body) | ||
mock_run.assert_called_once_with( | ||
[ | ||
"/usr/bin/notify-send", | ||
title, | ||
body, | ||
"--app-name=Universal Blue Updater", | ||
"--icon=software-update-available-symbolic", | ||
f"--urgency=normal", | ||
], | ||
capture_output=True, | ||
) | ||
|
||
|
||
@patch("ublue_update.cli.cfg") | ||
def test_ask_for_updates_no_dbus_notify(mock_cfg): | ||
mock_cfg.dbus_notify = False | ||
assert ask_for_updates(True) == None | ||
|
||
|
||
@patch("ublue_update.cli.cfg") | ||
@patch("ublue_update.cli.notify") | ||
def test_ask_for_updates_notify_none(mock_notify, mock_cfg): | ||
mock_cfg.dbus_notify = True | ||
mock_notify.return_value = None | ||
assert ask_for_updates(True) == None | ||
mock_notify.assert_called_once_with( | ||
"System Updater", | ||
"Update available, but system checks failed. Update now?", | ||
["universal-blue-update-confirm=Confirm"], | ||
"critical", | ||
) | ||
|
||
|
||
@patch("ublue_update.cli.cfg") | ||
@patch("ublue_update.cli.notify") | ||
@patch("ublue_update.cli.run_updates") | ||
def test_ask_for_updates_system(mock_run_updates, mock_notify, mock_cfg): | ||
mock_cfg.dbus_notify = True | ||
mock_notify.return_value = MagicMock(stdout=b"universal-blue-update-confirm") | ||
system = True | ||
ask_for_updates(system) | ||
mock_notify.assert_called_once_with( | ||
"System Updater", | ||
"Update available, but system checks failed. Update now?", | ||
["universal-blue-update-confirm=Confirm"], | ||
"critical", | ||
) | ||
mock_run_updates.assert_called_once_with(system, True) | ||
|
||
|
||
@patch("ublue_update.cli.cfg") | ||
@patch("ublue_update.cli.notify") | ||
@patch("ublue_update.cli.run_updates") | ||
def test_ask_for_updates_user(mock_run_updates, mock_notify, mock_cfg): | ||
mock_cfg.dbus_notify = True | ||
mock_notify.return_value = MagicMock(stdout=b"universal-blue-update-confirm") | ||
system = False | ||
ask_for_updates(system) | ||
mock_notify.assert_called_once_with( | ||
"System Updater", | ||
"Update available, but system checks failed. Update now?", | ||
["universal-blue-update-confirm=Confirm"], | ||
"critical", | ||
) | ||
mock_run_updates.assert_called_once_with(system, True) | ||
|
||
|
||
def test_inhibitor_checks_failed(): | ||
failure_message1 = "Failure 1" | ||
failure_message2 = "Failure 2" | ||
with pytest.raises(Exception, match=f"{failure_message1}\n - {failure_message2}"): | ||
inhibitor_checks_failed([failure_message1, failure_message2], True, True, True) | ||
|
||
|
||
@patch("ublue_update.cli.ask_for_updates") | ||
@patch("ublue_update.cli.log") | ||
def test_inhibitor_checks_failed_no_hw_check(mock_log, mock_ask_for_updates): | ||
failure_message1 = "Failure 1" | ||
failure_message2 = "Failure 2" | ||
with pytest.raises(Exception, match=f"{failure_message1}\n - {failure_message2}"): | ||
inhibitor_checks_failed([failure_message1, failure_message2], False, True, True) | ||
mock_log.assert_called_once_with( | ||
"Precondition checks failed, but update is available" | ||
) | ||
mock_ask_for_updates.assert_called_once() | ||
|
||
|
||
@patch("ublue_update.cli.os") | ||
@patch("ublue_update.cli.acquire_lock") | ||
def test_run_updates_user_in_progress(mock_acquire_lock, mock_os): | ||
mock_os.getuid.return_value = 1001 | ||
mock_os.environ.get.return_value = "/path/to" | ||
mock_os.path.isdir.return_value = True | ||
mock_acquire_lock.return_value = None | ||
with pytest.raises(Exception, match="updates are already running for this user"): | ||
run_updates(False, True) | ||
|
||
|
||
@patch("ublue_update.cli.os") | ||
@patch("ublue_update.cli.acquire_lock") | ||
@patch("ublue_update.cli.transaction_wait") | ||
def test_run_updates_user_system(mock_transaction_wait, mock_acquire_lock, mock_os): | ||
mock_os.getuid.return_value = 1001 | ||
mock_acquire_lock.return_value = 3 | ||
mock_os.path.isdir.return_value = False | ||
with pytest.raises( | ||
Exception, | ||
match="ublue-update needs to be run as root to perform system updates!", | ||
): | ||
run_updates(True, True) | ||
|
||
|
||
@patch("ublue_update.cli.os") | ||
@patch("ublue_update.cli.acquire_lock") | ||
@patch("ublue_update.cli.transaction_wait") | ||
@patch("ublue_update.cli.release_lock") | ||
def test_run_updates_user_no_system( | ||
mock_release_lock, mock_transaction_wait, mock_acquire_lock, mock_os | ||
): | ||
fd = 3 | ||
mock_os.getuid.return_value = 1001 | ||
mock_acquire_lock.return_value = fd | ||
mock_os.path.isdir.return_value = False | ||
run_updates(False, True) | ||
mock_release_lock.assert_called_once_with(fd) | ||
|
||
|
||
@patch("ublue_update.cli.os") | ||
@patch("ublue_update.cli.get_active_sessions") | ||
@patch("ublue_update.cli.acquire_lock") | ||
@patch("ublue_update.cli.transaction_wait") | ||
@patch("ublue_update.cli.subprocess.run") | ||
@patch("ublue_update.cli.log") | ||
@patch("ublue_update.cli.pending_deployment_check") | ||
@patch("ublue_update.cli.cfg") | ||
@patch("ublue_update.cli.release_lock") | ||
@patch("ublue_update.cli.notify") | ||
def test_run_updates_system( | ||
mock_notify, | ||
mock_release_lock, | ||
mock_cfg, | ||
mock_pending_deployment_check, | ||
mock_log, | ||
mock_run, | ||
mock_transaction_wait, | ||
mock_acquire_lock, | ||
mock_get_active_sesions, | ||
mock_os, | ||
): | ||
mock_os.getuid.return_value = 0 | ||
mock_acquire_lock.return_value = 3 | ||
output = MagicMock(stdout=b"test log") | ||
output.returncode = 1 | ||
mock_run.return_value = output | ||
mock_pending_deployment_check.return_value = True | ||
mock_cfg.dbus_notify.return_value = True | ||
run_updates(True, True) | ||
mock_notify.assert_any_call( | ||
"System Updater", | ||
"System passed checks, updating ...", | ||
) | ||
mock_run.assert_any_call( | ||
[ | ||
"/usr/bin/topgrade", | ||
"--config", | ||
"/usr/share/ublue-update/topgrade-system.toml", | ||
], | ||
capture_output=True, | ||
) | ||
mock_notify.assert_any_call( | ||
"System Updater", | ||
"System update complete, pending changes will take effect after reboot. Reboot now?", | ||
["universal-blue-update-reboot=Reboot Now"], | ||
) | ||
|
||
|
||
@patch("ublue_update.cli.os") | ||
@patch("ublue_update.cli.get_active_sessions") | ||
@patch("ublue_update.cli.acquire_lock") | ||
@patch("ublue_update.cli.transaction_wait") | ||
@patch("ublue_update.cli.subprocess.run") | ||
@patch("ublue_update.cli.log") | ||
@patch("ublue_update.cli.pending_deployment_check") | ||
@patch("ublue_update.cli.cfg") | ||
@patch("ublue_update.cli.release_lock") | ||
@patch("ublue_update.cli.notify") | ||
def test_run_updates_system_reboot( | ||
mock_notify, | ||
mock_release_lock, | ||
mock_cfg, | ||
mock_pending_deployment_check, | ||
mock_log, | ||
mock_run, | ||
mock_transaction_wait, | ||
mock_acquire_lock, | ||
mock_get_active_sesions, | ||
mock_os, | ||
): | ||
mock_os.getuid.return_value = 0 | ||
mock_acquire_lock.return_value = 3 | ||
output = MagicMock(stdout=b"test log") | ||
output.returncode = 1 | ||
mock_run.return_value = output | ||
mock_pending_deployment_check.return_value = True | ||
mock_cfg.dbus_notify.return_value = True | ||
reboot = MagicMock(stdout=b"universal-blue-update-reboot") | ||
mock_notify.side_effect = [None, reboot] | ||
run_updates(True, True) | ||
mock_notify.assert_any_call( | ||
"System Updater", | ||
"System passed checks, updating ...", | ||
) | ||
mock_run.assert_any_call( | ||
[ | ||
"/usr/bin/topgrade", | ||
gerblesh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"--config", | ||
"/usr/share/ublue-update/topgrade-system.toml", | ||
], | ||
capture_output=True, | ||
) | ||
mock_notify.assert_any_call( | ||
"System Updater", | ||
"System update complete, pending changes will take effect after reboot. Reboot now?", | ||
["universal-blue-update-reboot=Reboot Now"], | ||
) | ||
mock_run.assert_any_call(["systemctl", "reboot"]) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of checking to see if the command has been ran properly, but this could also be a problem down the line if someone changes the arguments or if notify-send changes its cli. This also goes for a lot of these other subprocess calls. Testing subprocess is hard though and likely will need to be solved by integration tests down the line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya its definitely tricky. the trap with unit tests at times is asserting too much as it makes the tests more inflexible and requires more maintenance on them. I could reduce the number of assertions to a more simple input -> output format that would allow changes to not break the tests as much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but at the end of the day the unit tests are there to maintain stability of the codebase. changes to the unit tests are a often occurrence in my experience