|
| 1 | +from subprocess import CompletedProcess |
1 | 2 | from unittest import mock |
2 | 3 | from .. import computer |
| 4 | +import os |
3 | 5 |
|
4 | 6 | @mock.patch("linuxmusterLinuxclient7.computer.socket.gethostname") |
5 | 7 | def test_hostname(mockSocketGetHostname): |
6 | 8 | mockSocketGetHostname.return_value = "computer1.linuxmuster.lan" |
7 | 9 | assert computer.hostname() == "computer1" |
8 | 10 |
|
| 11 | +@mock.patch("linuxmusterLinuxclient7.computer.hostname") |
| 12 | +def test_hostnameIsValid(mockHostname): |
| 13 | + mockHostname.return_value = "computer1" |
| 14 | + assert computer.hostnameIsValid() |
| 15 | + mockHostname.return_value = "aabk-r100-pc012" |
| 16 | + assert computer.hostnameIsValid() |
| 17 | + mockHostname.return_value = "aabk-r100-pc0123" |
| 18 | + assert not computer.hostnameIsValid() |
| 19 | + |
| 20 | +@mock.patch("linuxmusterLinuxclient7.computer._setLoginScreenWarning") |
| 21 | +@mock.patch("linuxmusterLinuxclient7.computer.hostname") |
| 22 | +def test_checkHostname(mockHostname, mockSetLoginScreenWarning): |
| 23 | + mockHostname.return_value = "computer1" |
| 24 | + assert computer.checkHostname() |
| 25 | + assert mockSetLoginScreenWarning.call_args.args[0] is None |
| 26 | + |
| 27 | + mockHostname.return_value = "aabk-r100-pc0123" |
| 28 | + assert not computer.checkHostname() |
| 29 | + assert "aabk-r100-pc0123" in mockSetLoginScreenWarning.call_args.args[0] |
| 30 | + |
| 31 | +@mock.patch("linuxmusterLinuxclient7.computer._userIsLoggedIn") |
| 32 | +@mock.patch("linuxmusterLinuxclient7.computer.subprocess.call") |
| 33 | +def test_setLoginScreenWarning(mockSubprocessCall, mockUserIsLoggedIn, tmp_path): |
| 34 | + warningFile = str(tmp_path / "95-linuxmuster-linuxclient7") |
| 35 | + mockUserIsLoggedIn.return_value = False |
| 36 | + |
| 37 | + with mock.patch("linuxmusterLinuxclient7.computer.constants.gdmDconfDir", str(tmp_path)), mock.patch("linuxmusterLinuxclient7.computer.constants.loginScreenWarningFile", warningFile): |
| 38 | + assert not computer._setLoginScreenWarning(None) |
| 39 | + assert mockSubprocessCall.call_count == 0 |
| 40 | + |
| 41 | + assert computer._setLoginScreenWarning("Hostname too long") |
| 42 | + with open(warningFile) as file: |
| 43 | + content = file.read() |
| 44 | + assert "banner-message-enable=true" in content |
| 45 | + assert 'banner-message-text="Hostname too long"' in content |
| 46 | + assert ["systemctl", "try-restart", "gdm"] in [c.args[0] for c in mockSubprocessCall.call_args_list] |
| 47 | + |
| 48 | + mockSubprocessCall.reset_mock() |
| 49 | + assert not computer._setLoginScreenWarning("Hostname too long") |
| 50 | + assert mockSubprocessCall.call_count == 0 |
| 51 | + |
| 52 | + mockUserIsLoggedIn.return_value = True |
| 53 | + assert computer._setLoginScreenWarning(None) |
| 54 | + assert not os.path.exists(warningFile) |
| 55 | + assert ["systemctl", "try-restart", "gdm"] not in [c.args[0] for c in mockSubprocessCall.call_args_list] |
| 56 | + |
| 57 | + with mock.patch("linuxmusterLinuxclient7.computer.constants.gdmDconfDir", str(tmp_path / "missing")): |
| 58 | + assert not computer._setLoginScreenWarning("Hostname too long") |
| 59 | + |
| 60 | +@mock.patch("linuxmusterLinuxclient7.computer.subprocess.run") |
| 61 | +def test_userIsLoggedIn(mockSubprocessRun): |
| 62 | + mockSubprocessRun.return_value = CompletedProcess(args=[], returncode=0, stdout="60578 gdm-greeter no active\n") |
| 63 | + assert not computer._userIsLoggedIn() |
| 64 | + |
| 65 | + mockSubprocessRun.return_value = CompletedProcess(args=[], returncode=0, stdout="60578 gdm-greeter no active\n1662401376 yukigrun no active\n") |
| 66 | + assert computer._userIsLoggedIn() |
| 67 | + |
9 | 68 | @mock.patch("linuxmusterLinuxclient7.computer.hostname") |
10 | 69 | def test_krbHostName(mockHostname): |
11 | 70 | mockHostname.return_value = "computer1" |
|
0 commit comments