File: conftest.py

package info (click to toggle)
vorta 0.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,532 kB
  • sloc: python: 12,262; makefile: 89; xml: 65; sh: 51
file content (240 lines) | stat: -rw-r--r-- 7,109 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import subprocess

import pytest
from packaging.version import Version
from peewee import SqliteDatabase

import vorta
import vorta.application
import vorta.borg.jobs_manager
from vorta.store.models import (
    ArchiveModel,
    BackupProfileModel,
    EventLogModel,
    RepoModel,
    RepoPassword,
    SchemaVersion,
    SettingsModel,
    SourceFileModel,
    WifiSettingModel,
)
from vorta.utils import borg_compat
from vorta.views.main_window import ArchiveTab, MainWindow

models = [
    RepoModel,
    RepoPassword,
    BackupProfileModel,
    SourceFileModel,
    SettingsModel,
    ArchiveModel,
    WifiSettingModel,
    EventLogModel,
    SchemaVersion,
]


@pytest.fixture(scope='function', autouse=True)
def borg_version():
    borg_version = os.getenv('BORG_VERSION')
    if not borg_version:
        borg_version = subprocess.run(['borg', '--version'], stdout=subprocess.PIPE).stdout.decode('utf-8')
        borg_version = borg_version.split(' ')[1]

    # test window does not automatically set borg version
    borg_compat.set_version(borg_version, borg_compat.path)

    parsed_borg_version = Version(borg_version)
    return borg_version, parsed_borg_version


@pytest.fixture(scope='function', autouse=True)
def create_test_repo(tmpdir_factory, borg_version):
    repo_path = tmpdir_factory.mktemp('repo')
    source_files_dir = tmpdir_factory.mktemp('borg_src')

    is_borg_v2 = borg_version[1] >= Version('2.0.0b1')

    if is_borg_v2:
        subprocess.run(['borg', '-r', str(repo_path), 'rcreate', '--encryption=none'], check=True)
    else:
        subprocess.run(['borg', 'init', '--encryption=none', str(repo_path)], check=True)

    def create_archive(timestamp, name):
        if is_borg_v2:
            subprocess.run(
                ['borg', '-r', str(repo_path), 'create', '--timestamp', timestamp, name, str(source_files_dir)],
                cwd=str(repo_path),
                check=True,
            )
        else:
            subprocess.run(
                ['borg', 'create', '--timestamp', timestamp, f'{repo_path}::{name}', str(source_files_dir)],
                cwd=str(repo_path),
                check=True,
            )

    # /src/file
    file_path = os.path.join(source_files_dir, 'file')
    with open(file_path, 'w') as f:
        f.write('test')

    # /src/dir/
    dir_path = os.path.join(source_files_dir, 'dir')
    os.mkdir(dir_path)

    # /src/dir/file
    file_path = os.path.join(dir_path, 'file')
    with open(file_path, 'w') as f:
        f.write('test')

    # Create first archive
    create_archive('2023-06-14T01:00:00', 'test-archive1')

    # /src/dir/symlink
    symlink_path = os.path.join(dir_path, 'symlink')
    os.symlink(file_path, symlink_path)

    # /src/dir/hardlink
    hardlink_path = os.path.join(dir_path, 'hardlink')
    os.link(file_path, hardlink_path)

    # /src/dir/fifo
    fifo_path = os.path.join(dir_path, 'fifo')
    os.mkfifo(fifo_path)

    # /src/dir/chrdev
    supports_chrdev = True
    try:
        chrdev_path = os.path.join(dir_path, 'chrdev')
        os.mknod(chrdev_path, mode=0o600 | 0o020000)
    except PermissionError:
        supports_chrdev = False

    create_archive('2023-06-14T02:00:00', 'test-archive2')

    # Rename dir to dir1
    os.rename(dir_path, os.path.join(source_files_dir, 'dir1'))

    create_archive('2023-06-14T03:00:00', 'test-archive3')

    # Rename all files under dir1
    for file in os.listdir(os.path.join(source_files_dir, 'dir1')):
        os.rename(os.path.join(source_files_dir, 'dir1', file), os.path.join(source_files_dir, 'dir1', file + '1'))

    create_archive('2023-06-14T04:00:00', 'test-archive4')

    # Delete all file under dir1
    for file in os.listdir(os.path.join(source_files_dir, 'dir1')):
        os.remove(os.path.join(source_files_dir, 'dir1', file))

    create_archive('2023-06-14T05:00:00', 'test-archive5')

    # change permission of dir1
    os.chmod(os.path.join(source_files_dir, 'dir1'), 0o700)

    create_archive('2023-06-14T06:00:00', 'test-archive6')

    return repo_path, source_files_dir, supports_chrdev


@pytest.fixture(scope='function', autouse=True)
def init_db(qapp, qtbot, tmpdir_factory, create_test_repo):
    tmp_db = tmpdir_factory.mktemp('Vorta').join('settings.sqlite')
    mock_db = SqliteDatabase(
        str(tmp_db),
        pragmas={
            'journal_mode': 'wal',
        },
    )
    vorta.store.connection.init_db(mock_db)

    default_profile = BackupProfileModel(name='Default')
    default_profile.save()

    repo_path, source_dir, _ = create_test_repo

    new_repo = RepoModel(url=repo_path)
    new_repo.encryption = 'none'
    new_repo.save()

    default_profile.repo = new_repo.id
    default_profile.dont_run_on_metered_networks = False
    default_profile.validation_on = False
    default_profile.save()

    source_dir = SourceFileModel(dir=source_dir, repo=new_repo, dir_size=12, dir_files_count=3, path_isdir=True)
    source_dir.save()

    qapp.main_window.deleteLater()
    del qapp.main_window
    qapp.main_window = MainWindow(qapp)  # Re-open main window to apply mock data in UI

    qapp.scheduler.schedule_changed.disconnect()

    yield

    qapp.jobs_manager.cancel_all_jobs()
    qapp.backup_finished_event.disconnect()
    qtbot.waitUntil(lambda: not qapp.jobs_manager.is_worker_running(), **pytest._wait_defaults)
    mock_db.close()


@pytest.fixture
def choose_file_dialog(tmpdir):
    class MockFileDialog:
        def __init__(self, *args, **kwargs):
            self.directory = kwargs.get('directory', None)
            self.subdirectory = kwargs.get('subdirectory', None)

        def open(self, func):
            func()

        def selectedFiles(self):
            if self.subdirectory:
                return [str(tmpdir.join(self.subdirectory))]
            elif self.directory:
                return [str(self.directory)]
            else:
                return [str(tmpdir)]

    return MockFileDialog


@pytest.fixture
def rootdir():
    return os.path.dirname(os.path.abspath(__file__))


@pytest.fixture()
def archive_env(qapp, qtbot):
    """
    Common setup for integration tests involving the archive tab.
    """
    main: MainWindow = qapp.main_window
    tab: ArchiveTab = main.archiveTab
    main.tabWidget.setCurrentIndex(3)
    tab.refresh_archive_list()
    qtbot.waitUntil(lambda: tab.archiveTable.rowCount() > 0, **pytest._wait_defaults)
    return main, tab


@pytest.fixture(autouse=True)
def min_borg_version(borg_version, request):
    if request.node.get_closest_marker('min_borg_version'):
        parsed_borg_version = borg_version[1]

        if parsed_borg_version < Version(request.node.get_closest_marker('min_borg_version').args[0]):
            pytest.skip(
                'skipped due to borg version requirement for test: {}'.format(
                    request.node.get_closest_marker('min_borg_version').args[0]
                )
            )


def pytest_configure(config):
    config.addinivalue_line(
        "markers",
        "min_borg_version(): set minimum required borg version for a test",
    )