Skip to content

Commit 44c8c46

Browse files
committed
add preup tests
1 parent 7d32f79 commit 44c8c46

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

mytonctrl/mytonctrl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def check_installer_user(local):
214214

215215
def pre_up(local: MyPyClass, ton: MyTonCore):
216216
try:
217-
CheckMytonctrlUpdate(local)
217+
check_mytonctrl_update(local)
218218
check_installer_user(local)
219219
check_vport(local, ton)
220220
warnings(local, ton)
@@ -446,12 +446,11 @@ def run_benchmark(ton, args):
446446
print_table(table)
447447
#end define
448448

449-
def CheckMytonctrlUpdate(local):
449+
def check_mytonctrl_update(local):
450450
git_path = local.buffer.my_dir
451451
result = check_git_update(git_path)
452452
if result is True:
453453
color_print(local.translate("mytonctrl_update_available"))
454-
#end define
455454

456455
def print_warning(local, warning_name: str):
457456
color_print("============================================================================================")

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, 'check_mytonctrl_update', 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' in output
46+
47+
48+
def test_check_vport(cli, monkeypatch, mocker: MockerFixture):
49+
monkeypatch.setattr(mytonctrl, 'check_mytonctrl_update', 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
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@pytest.fixture(autouse=True)
1010
def before_test(monkeypatch):
11-
monkeypatch.setattr(mytonctrl, 'CheckMytonctrlUpdate', lambda *_: None)
11+
monkeypatch.setattr(mytonctrl, 'check_mytonctrl_update', lambda *_: None)
1212
monkeypatch.setattr(mytonctrl, 'check_installer_user', lambda *_: None)
1313
monkeypatch.setattr(mytonctrl, 'check_vport', lambda *_: None)
1414

@@ -96,11 +96,11 @@ def test_check_validator_balance(cli, monkeypatch, mocker: MockerFixture):
9696

9797

9898
def test_check_vps(cli, monkeypatch):
99-
monkeypatch.setattr(mytonctrl, 'is_host_virtual', lambda *_: {'virtual': True})
99+
monkeypatch.setattr('mytonctrl.mytonctrl.is_host_virtual', lambda : {'virtual': True, 'product_name': 'VirtualBox'})
100100
output = cli.run_pre_up()
101101
assert 'Virtualization detected' in output
102102

103-
monkeypatch.setattr(mytonctrl, 'is_host_virtual', lambda *_: {'virtual': False})
103+
monkeypatch.setattr('mytonctrl.mytonctrl.is_host_virtual', lambda : {'virtual': False})
104104
output = cli.run_pre_up()
105105
assert 'Virtualization detected' not in output
106106

0 commit comments

Comments
 (0)