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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Test the App components provides by backups app.
"""
from unittest.mock import call, patch
import pytest
from plinth import kvstore
from .. import components
from ..components import BackupRestore
# pylint: disable=protected-access
@pytest.fixture(name='backup_restore')
def fixture_backup_restore():
"""Fixture to create a domain type after clearing all existing ones."""
value = {'files': ['a', 'b'], 'directories': ['a', 'b']}
services = ['service-1', {'type': 'system', 'name': 'service-2'}]
settings = ['setting-1', 'setting-2']
delete_before_restore = ['path1', 'path2']
return BackupRestore('test-backup-restore', config=value, data=value,
secrets=value, services=services, settings=settings,
delete_before_restore=delete_before_restore)
@pytest.mark.parametrize('section', [
None,
{
'directories': ['a', 'b']
},
{
'files': ['a', 'b']
},
{
'directories': ['a'],
'files': ['a']
},
{
'extra': 'value'
},
])
def test_valid_directories_and_files(section):
"""Test that valid values of files and directories."""
components._validate_directories_and_files(section)
@pytest.mark.parametrize('section', [
'invalid',
10,
['invalid'],
{
'files': None
},
{
'files': 10
},
{
'files': {}
},
{
'files': [10],
},
{
'files': [None],
},
{
'files': [[]],
},
{
'directories': None
},
{
'directories': [10],
},
])
def test_invalid_directories_and_files(section):
"""Test that invalid values of files and directories."""
with pytest.raises(AssertionError):
components._validate_directories_and_files(section)
@pytest.mark.parametrize('services', [
None,
[],
['service'],
[{
'type': 'uwsgi',
'name': 'service'
}],
[{
'type': 'system',
'name': 'service'
}],
[{
'type': 'apache',
'name': 'service',
'kind': 'config'
}],
[{
'type': 'apache',
'name': 'service',
'kind': 'site'
}],
[{
'type': 'apache',
'name': 'service',
'kind': 'module'
}],
])
def test_valid_services(services):
"""Test that valid values of services."""
components._validate_services(services)
@pytest.mark.parametrize('services', [
10,
'invalid',
[10],
[[]],
[{}],
[{
'type': 'invalid',
'name': 'service'
}],
[{
'type': 10,
'name': 'service'
}],
[{
'type': 'system',
'name': 10
}],
[{
'type': 'system',
'name': None
}],
[{
'type': 'system',
'name': []
}],
[{
'type': 'apache',
'name': 'service'
}],
[{
'type': 'apache',
'name': 'service',
'kind': 'invalid-kind'
}],
])
def test_invalid_services(services):
"""Test that invalid values of services."""
with pytest.raises((AssertionError, KeyError)):
components._validate_services(services)
@pytest.mark.parametrize('paths', [
10,
'invalid',
[None],
[10],
[[]],
[{}],
])
def test_invalid_paths(paths):
"""Test invalid values for paths."""
with pytest.raises(AssertionError):
components._validate_paths(paths)
def test_backup_restore_init_default_arguments():
"""Test initialization of the backup restore object."""
component = BackupRestore('test-backup-restore')
assert component.component_id == 'test-backup-restore'
assert component.config == {}
assert component.data == {}
assert component.secrets == {}
assert component.services == []
assert component.settings == []
assert not component.has_data
@pytest.mark.parametrize('key', ['config', 'data', 'secrets'])
def test_backup_restore_init(key):
"""Test initialization of the backup restore object."""
with pytest.raises(AssertionError):
BackupRestore('test-backup-restore', **{key: 'invalid-value'})
value = {'files': ['a', 'b'], 'directories': ['a', 'b']}
component = BackupRestore('test-backup-restore', **{key: value})
assert getattr(component, key) == value
assert component.has_data
def test_backup_restore_init_services():
"""Test initialization of the backup restore object."""
with pytest.raises(AssertionError):
BackupRestore('test-backup-restore', services='invalid-value')
services = ['service-1', {'type': 'system', 'name': 'service-2'}]
component = BackupRestore('test-backup-restore', services=services)
assert component.services == services
assert not component.has_data
def test_backup_restore_init_settings():
"""Test initialization of the backup restore object."""
with pytest.raises(AssertionError):
BackupRestore('test-backup-restore', settings='invalid-value')
settings = ['setting1', 'setting2']
component = BackupRestore('test-backup-restore', settings=settings)
assert component.settings == settings
assert component.has_data
assert component.data == {}
component.app_id = 'testapp'
settings_file = '/var/lib/plinth/backups-data/testapp-settings.json'
assert component.data == {'files': [settings_file]}
def test_backup_restore_equal(backup_restore):
"""Test equality operator on the backup restore object."""
assert backup_restore == BackupRestore('test-backup-restore')
assert backup_restore != BackupRestore('test-different')
def test_backup_restore_manifest(backup_restore):
"""Test manifest retrieval from backup restore object."""
manifest = backup_restore.manifest
assert isinstance(manifest, dict)
assert manifest['config'] == backup_restore.config
assert manifest['data'] == backup_restore.data
assert manifest['secrets'] == backup_restore.secrets
assert manifest['services'] == backup_restore.services
assert manifest['settings'] == backup_restore.settings
assert BackupRestore('test-backup-restore').manifest == {}
def test_backup_restore_hooks(backup_restore):
"""Test running hooks on backup restore object."""
packet = None
backup_restore.backup_post(packet)
@pytest.mark.django_db
@patch('plinth.modules.backups.privileged.dump_settings')
def test_backup_restore_backup_pre(dump_settings, backup_restore):
"""Test running backup-pre hook."""
packet = None
kvstore.set('setting-1', 'value-1')
backup_restore.app_id = 'testapp'
component = BackupRestore('test-backup-restore')
component.backup_pre(packet)
dump_settings.assert_has_calls([])
backup_restore.backup_pre(packet)
dump_settings.assert_has_calls([call('testapp', {'setting-1': 'value-1'})])
@patch('plinth.modules.backups.privileged.delete_before_restore')
def test_backup_restore_restore_pre(delete_before_restore, backup_restore):
"""Test running restore-pre hook."""
packet = None
backup_restore.app_id = 'testapp'
component = BackupRestore('test-backup-restore')
component.restore_pre(packet)
delete_before_restore.assert_has_calls([])
backup_restore.restore_pre(packet)
delete_before_restore.assert_has_calls([call('testapp')])
@pytest.mark.django_db
@patch('plinth.modules.backups.privileged.load_settings')
def test_backup_restore_restore_post(load_settings, backup_restore):
"""Test running restore-post hook."""
packet = None
backup_restore.app_id = 'testapp'
component = BackupRestore('test-backup-restore')
component.restore_post(packet)
load_settings.assert_has_calls([])
output = {'setting-1': 'value-1'}
load_settings.return_value = output
backup_restore.restore_post(packet)
load_settings.assert_has_calls([call('testapp')])
assert kvstore.get('setting-1') == 'value-1'
with pytest.raises(Exception):
kvstore.get('setting-2')
|