Skip to content

Commit

Permalink
fix: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gerblesh committed Sep 2, 2024
1 parent 4d74c61 commit 5d6f062
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 105 deletions.
7 changes: 4 additions & 3 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_notify_uid_user(mock_run, mock_log, mock_os, mock_cfg):
body,
"--app-name=Universal Blue Updater",
"--icon=software-update-available-symbolic",
f"--urgency=normal",
"--urgency=normal",
],
capture_output=True,
)
Expand All @@ -51,15 +51,15 @@ def test_notify_uid_user(mock_run, mock_log, mock_os, mock_cfg):
@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
assert ask_for_updates(True) is 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
assert ask_for_updates(True) is None
mock_notify.assert_called_once_with(
"System Updater",
"Update available, but system checks failed. Update now?",
Expand Down Expand Up @@ -210,6 +210,7 @@ def test_run_updates_system(
["universal-blue-update-reboot=Reboot Now"],
)


@patch("ublue_update.cli.os")
@patch("ublue_update.cli.get_active_sessions")
@patch("ublue_update.cli.acquire_lock")
Expand Down
115 changes: 13 additions & 102 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,7 @@
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../src"))
)

from ublue_update.session import get_active_sessions, get_xdg_runtime_dir

loginctl_output = b"""
UID=1001
GID=1001
Name=test
Timestamp=Thu 2024-08-15 18:18:08 UTC
TimestampMonotonic=293807858
RuntimePath=/run/user/1001
[email protected]
Slice=user-1001.slice
Display=c3
State=active
Sessions=c4 c3 c1 3
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
Linger=yes
"""
from ublue_update.session import get_active_sessions

loginctl_json_output = b"""
[
Expand All @@ -37,6 +19,7 @@
"leader" : 6205,
"class" : "manager",
"tty" : null,
"state": "active",
"idle" : false,
"since" : null
},
Expand All @@ -48,105 +31,33 @@
"leader" : 6230,
"class" : "manager",
"tty" : null,
"state": "inactive",
"idle" : false,
"since" : null
}
]
"""

session_info = [
b"""
Id=3
User=1001
Name=test
Timestamp=Thu 2024-08-15 18:18:08 UTC
TimestampMonotonic=293993628
VTNr=0
Remote=no
Service=systemd-user
Leader=6205
Audit=3
Type=wayland
Class=manager
Active=yes
State=active
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
LockedHint=no
""",
b"""
Id=c1
User=1001
Name=test
Timestamp=Thu 2024-08-15 18:18:09 UTC
TimestampMonotonic=295100128
VTNr=0
Remote=no
Service=systemd-user
Leader=6230
Audit=3
Type=unspecified
Class=manager
Active=yes
State=active
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
LockedHint=no
""",
]


@patch("ublue_update.session.subprocess.run")
def test_get_xdg_runtime_dir(mock_run):
mock_run.return_value = MagicMock(stdout=loginctl_output)
assert get_xdg_runtime_dir(1001) == "/run/user/1001"
mock_run.assert_called_once_with(
["/usr/bin/loginctl", "show-user", "1001"], capture_output=True
)


@patch("ublue_update.session.subprocess.run")
def test_get_active_sessions(mock_run):
mock_session1 = MagicMock(stdout=session_info[0])
mock_session2 = MagicMock(stdout=session_info[1])
mock_session1.returncode = 0
mock_session2.returncode = 0
mock_run.side_effect = [
MagicMock(stdout=loginctl_json_output),
mock_session1,
mock_session2,
]
assert get_active_sessions() == [
{
"": "",
"Id": "3",
"User": "1001",
"Name": "test",
"Timestamp": "Thu 2024-08-15 18:18:08 UTC",
"TimestampMonotonic": "293993628",
"VTNr": "0",
"Remote": "no",
"Service": "systemd-user",
"Leader": "6205",
"Audit": "3",
"Type": "wayland",
"Class": "manager",
"Active": "yes",
"State": "active",
"IdleHint": "no",
"IdleSinceHint": "0",
"IdleSinceHintMonotonic": "0",
"LockedHint": "no",
"session": "3",
"uid": 1001,
"user": "test",
"seat": None,
"leader": 6205,
"class": "manager",
"tty": None,
"state": "active",
"idle": False,
"since": None,
}
]
mock_run.assert_any_call(
["/usr/bin/loginctl", "list-sessions", "--output=json"], capture_output=True
)
mock_run.assert_any_call(
["/usr/bin/loginctl", "show-session", "3"], capture_output=True
)
mock_run.assert_any_call(
["/usr/bin/loginctl", "show-session", "c1"], capture_output=True
)

0 comments on commit 5d6f062

Please sign in to comment.