Skip to content

Commit b9b91ab

Browse files
committed
add backups integration tests
1 parent 708ac52 commit b9b91ab

File tree

3 files changed

+168
-12
lines changed

3 files changed

+168
-12
lines changed

modules/backups.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
import shutil
33
import subprocess
44
import time
5+
import typing
56

67
from modules.module import MtcModule
7-
from mypylib.mypylib import color_print, ip2int, run_as_root, parse, MyPyClass
8+
from mypylib.mypylib import color_print, ip2int, run_as_root, parse
89
from mytoncore.utils import get_package_resource_path
910
from mytonctrl.utils import get_current_user, pop_user_from_args
1011
from mytoninstaller.config import get_own_ip
1112

1213

1314
class BackupModule(MtcModule):
1415

15-
def create_keyring(self, dir_name):
16+
def create_keyring(self, dir_name: str):
1617
keyring_dir = dir_name + '/keyring'
1718
self.ton.validatorConsole.Run(f'exportallprivatekeys {keyring_dir}')
1819

@@ -34,10 +35,10 @@ def run_create_backup(args, user: str = None):
3435
with get_package_resource_path('mytonctrl', 'scripts/create_backup.sh') as backup_script_path:
3536
return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5)
3637

37-
def create_backup(self, args):
38+
def create_backup(self, args: list) -> typing.Union[int, None]:
3839
if len(args) > 3:
3940
color_print("{red}Bad args. Usage:{endc} create_backup [filename] [-u <user>]")
40-
return
41+
return None
4142
tmp_dir = self.create_tmp_ton_dir()
4243
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-t", tmp_dir]
4344
user = pop_user_from_args(args)
@@ -51,7 +52,6 @@ def create_backup(self, args):
5152
color_print("create_backup - {red}Error{endc}")
5253
shutil.rmtree(tmp_dir)
5354
return process.returncode
54-
# end define
5555

5656
@staticmethod
5757
def run_restore_backup(args, user: str = None):
@@ -94,7 +94,6 @@ def restore_backup(self, args):
9494
self.local.exit()
9595
else:
9696
color_print("restore_backup - {red}Error{endc}")
97-
# end define
9897

9998
def add_console_commands(self, console):
10099
console.AddItem("create_backup", self.create_backup, self.local.translate("create_backup_cmd"))

tests/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def get_my_work_dir(self):
2323
def get_my_temp_dir(self):
2424
return ensure_dir(self._temp_dir)
2525

26-
def write_db(self, data):
27-
self.buffer.old_db = Dict(self.db)
28-
29-
def load_db(self, db_path=False):
30-
self.set_default_config()
31-
return True
26+
# def write_db(self, data):
27+
# self.buffer.old_db = Dict(self.db)
28+
#
29+
# def load_db(self, db_path=False):
30+
# self.set_default_config()
31+
# return True
3232

3333
def write_log(self):
3434
pass
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import json
2+
import os
3+
4+
from pytest_mock import MockerFixture
5+
6+
from modules.backups import BackupModule
7+
from modules import backups as backups_module
8+
from mypylib.mypylib import MyPyClass
9+
from mytoncore.mytoncore import MyTonCore
10+
from pathlib import Path
11+
12+
from mytonctrl.utils import get_current_user
13+
14+
15+
def test_create_backup(cli, ton, monkeypatch, tmp_path, mocker: MockerFixture):
16+
tmp_dir = tmp_path / "test"
17+
18+
def create_tmp_ton_dir(_):
19+
os.makedirs(tmp_dir)
20+
return str(tmp_dir)
21+
22+
monkeypatch.setattr(BackupModule, "create_tmp_ton_dir", create_tmp_ton_dir)
23+
24+
fun_args, fun_user = [], None
25+
return_code = 0
26+
27+
def run_create_backup(_, args: list, user: str):
28+
nonlocal fun_args, fun_user, return_code
29+
fun_args = args
30+
fun_user = user
31+
mock = mocker.Mock()
32+
mock.returncode = return_code
33+
return mock
34+
35+
monkeypatch.setattr(BackupModule, "run_create_backup", run_create_backup)
36+
37+
output = cli.execute("create_backup", no_color=True)
38+
assert "create_backup - OK" in output
39+
assert fun_user is None
40+
assert fun_args == ["-m", ton.local.buffer.my_work_dir, "-t", str(tmp_dir)]
41+
42+
output = cli.execute("create_backup /to_dir/", no_color=True)
43+
assert "create_backup - OK" in output
44+
assert fun_user is None
45+
assert fun_args == ["-m", ton.local.buffer.my_work_dir, "-t", str(tmp_dir), "-d", "/to_dir/"]
46+
assert not Path(tmp_dir).exists()
47+
48+
output = cli.execute("create_backup /to_dir/ -u yungwine", no_color=True)
49+
assert "create_backup - OK" in output
50+
assert fun_user == 'yungwine'
51+
assert fun_args == ["-m", ton.local.buffer.my_work_dir, "-t", str(tmp_dir), "-d", "/to_dir/"]
52+
assert not Path(tmp_dir).exists()
53+
54+
return_code = 1
55+
output = cli.execute("create_backup /to_dir/ -u yungwine", no_color=True)
56+
assert "create_backup - Error" in output
57+
58+
59+
60+
def test_restore_backup(cli, ton, monkeypatch, tmp_path, mocker: MockerFixture):
61+
exit_mock = mocker.Mock()
62+
monkeypatch.setattr(MyPyClass, "exit", exit_mock)
63+
64+
create_backup_mock = mocker.Mock()
65+
monkeypatch.setattr(BackupModule, "create_backup", create_backup_mock)
66+
monkeypatch.setattr("modules.backups.get_own_ip", lambda: "127.0.0.1")
67+
68+
return_code = 0
69+
run_args = []
70+
def fake_run_as_root(args: list):
71+
nonlocal return_code, run_args
72+
run_args = args
73+
return return_code
74+
75+
monkeypatch.setattr(backups_module, "run_as_root", fake_run_as_root)
76+
77+
run_restore_backup = BackupModule.run_restore_backup
78+
79+
def fake_run_restore_backup(*args, **kwargs):
80+
# really do update db after restore
81+
with open(ton.local.buffer.db_path, 'w') as f:
82+
f.write(json.dumps(ton.local.db | {"abc": 123}))
83+
return run_restore_backup(*args, **kwargs)
84+
85+
monkeypatch.setattr(BackupModule, "run_restore_backup", staticmethod(fake_run_restore_backup))
86+
87+
current_user = get_current_user()
88+
backup_path = Path(__file__).resolve().parents[2] / 'mytonctrl'/ 'scripts' / 'restore_backup.sh'
89+
90+
def assert_happy_run_args(outp: str, user: str):
91+
assert 'restore_backup - OK' in outp
92+
exit_mock.assert_called_once() # exited after restore_backup
93+
assert run_args == ['bash', backup_path, '-u', user, '-m', ton.local.buffer.my_work_dir, '-n',
94+
'backup.tar.gz', '-i', '2130706433']
95+
assert ton.local.db.get('abc') == 123 # db updated after restore_backup
96+
97+
monkeypatch.setattr(MyTonCore, "using_validator", lambda self: False)
98+
99+
# bad args
100+
output = cli.execute("restore_backup", no_color=True)
101+
assert "Bad args" in output
102+
103+
# prompt: abort
104+
monkeypatch.setattr("builtins.input", lambda *_: "n")
105+
output = cli.execute("restore_backup backup.tar.gz", no_color=True)
106+
assert "aborted." in output
107+
create_backup_mock.assert_not_called()
108+
exit_mock.assert_not_called()
109+
110+
# happy path
111+
monkeypatch.setattr("builtins.input", lambda *_: "y")
112+
output = cli.execute("restore_backup backup.tar.gz", no_color=True)
113+
assert_happy_run_args(output, current_user)
114+
create_backup_mock.assert_called_once()
115+
116+
# happy path custom user
117+
exit_mock.reset_mock()
118+
create_backup_mock.reset_mock()
119+
output = cli.execute("restore_backup backup.tar.gz -u customuser", no_color=True)
120+
create_backup_mock.assert_called_once()
121+
assert_happy_run_args(output, 'customuser')
122+
create_backup_mock.assert_called_once()
123+
124+
# skip prompt
125+
exit_mock.reset_mock()
126+
create_backup_mock.reset_mock()
127+
output = cli.execute("restore_backup backup.tar.gz -y", no_color=True)
128+
assert_happy_run_args(output, current_user)
129+
create_backup_mock.assert_called_once()
130+
131+
# skip create_backup
132+
exit_mock.reset_mock()
133+
create_backup_mock.reset_mock()
134+
output = cli.execute("restore_backup backup.tar.gz --skip-create-backup -y -u abc", no_color=True)
135+
assert_happy_run_args(output, 'abc')
136+
create_backup_mock.assert_not_called()
137+
138+
# check btc teleport installment
139+
exit_mock.reset_mock()
140+
create_backup_mock.reset_mock()
141+
monkeypatch.setattr(MyTonCore, "using_validator", lambda self: True)
142+
143+
from modules import btc_teleport
144+
btc_teleport_mock = mocker.Mock()
145+
monkeypatch.setattr(btc_teleport, "BtcTeleportModule", btc_teleport_mock)
146+
147+
output = cli.execute("restore_backup backup.tar.gz --skip-create-backup -u abc -y", no_color=True)
148+
149+
assert_happy_run_args(output, 'abc')
150+
create_backup_mock.assert_not_called()
151+
btc_teleport_mock.assert_called_once()
152+
btc_teleport_mock.return_value.init.assert_called_once_with(reinstall=True)
153+
154+
# Failed to restore backup
155+
return_code = 1
156+
output = cli.execute("restore_backup backup.tar.gz --skip-create-backup -y", no_color=True)
157+
assert "restore_backup - Error" in output

0 commit comments

Comments
 (0)