Skip to content

Commit c22be59

Browse files
committed
add preup tests
1 parent 7d32f79 commit c22be59

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

tests/integration/preup/__init__.py

Whitespace-only changes.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import socket
2+
import subprocess
3+
4+
from mytoncore import MyTonCore
5+
from mytonctrl import mytonctrl
6+
from pytest_mock import MockerFixture
7+
8+
9+
def test_check_mytonctrl_update(cli, monkeypatch):
10+
monkeypatch.setattr(mytonctrl, 'warnings', lambda *_: None)
11+
monkeypatch.setattr(mytonctrl, 'check_installer_user', lambda *_: None)
12+
monkeypatch.setattr(mytonctrl, 'check_vport', lambda *_: None)
13+
14+
monkeypatch.setattr(mytonctrl, 'check_git_update', lambda *_: True)
15+
output = cli.run_pre_up()
16+
assert 'MyTonCtrl update available' in output
17+
18+
monkeypatch.setattr(mytonctrl, 'check_git_update', lambda *_: False)
19+
output = cli.run_pre_up()
20+
assert 'MyTonCtrl update available' not in output
21+
22+
23+
def test_check_installer_user(cli, monkeypatch, mocker: MockerFixture):
24+
monkeypatch.setattr(mytonctrl, 'CheckMytonctrlUpdate', lambda *_: None)
25+
monkeypatch.setattr(mytonctrl, 'warnings', lambda *_: None)
26+
monkeypatch.setattr(mytonctrl, 'check_vport', lambda *_: None)
27+
28+
whoami_result = mocker.Mock()
29+
whoami_result.stdout = b'testuser\n'
30+
ls_result = mocker.Mock()
31+
ls_result.stdout = b'total 0\n-rw-r--r-- 1 testuser testuser 0 Jan 1 00:00 file\n'
32+
33+
run_mock = mocker.Mock(side_effect=[whoami_result, ls_result])
34+
monkeypatch.setattr(subprocess, 'run', run_mock)
35+
output = cli.run_pre_up()
36+
assert 'mytonctrl was installed by another user' not in output
37+
38+
whoami_result.stdout = b'currentuser\n'
39+
ls_result.stdout = b'total 0\n-rw-r--r-- 1 installeruser testuser 0 Jan 1 00:00 file\n'
40+
41+
run_mock = mocker.Mock(side_effect=[whoami_result, ls_result])
42+
monkeypatch.setattr(subprocess, 'run', run_mock)
43+
output = cli.run_pre_up()
44+
assert 'mytonctrl was installed by another user' in output
45+
assert f'launch mtc with `installeruser` user'
46+
47+
48+
def test_check_vport(cli, monkeypatch, mocker: MockerFixture):
49+
monkeypatch.setattr(mytonctrl, 'CheckMytonctrlUpdate', lambda *_: None)
50+
monkeypatch.setattr(mytonctrl, 'warnings', lambda *_: None)
51+
monkeypatch.setattr(mytonctrl, 'check_installer_user', lambda *_: None)
52+
53+
monkeypatch.setattr(MyTonCore, 'GetValidatorConfig', mocker.Mock(side_effect=Exception('test error')))
54+
output = cli.run_pre_up()
55+
assert 'GetValidatorConfig error' in output
56+
57+
vconfig_mock = mocker.Mock()
58+
addr_mock = mocker.Mock()
59+
addr_mock.ip = 16777343 # 127.0.0.1
60+
addr_mock.port = 8080
61+
vconfig_mock.addrs = [addr_mock]
62+
monkeypatch.setattr(MyTonCore, 'GetValidatorConfig', lambda *_: vconfig_mock)
63+
64+
socket_mock = mocker.Mock()
65+
socket_mock.connect_ex.return_value = 0
66+
socket_mock.__enter__ = mocker.Mock(return_value=socket_mock)
67+
socket_mock.__exit__ = mocker.Mock()
68+
monkeypatch.setattr(socket, 'socket', mocker.Mock(return_value=socket_mock))
69+
70+
output = cli.run_pre_up()
71+
assert 'UDP port of the validator is not accessible' not in output
72+
73+
vconfig_mock.addrs = [addr_mock]
74+
socket_mock.connect_ex.return_value = 1
75+
output = cli.run_pre_up()
76+
assert 'UDP port of the validator is not accessible' in output

0 commit comments

Comments
 (0)