|
| 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