backups: Fix and test service shutdown and restore

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
James Valleroy 2018-08-24 07:13:00 -04:00 committed by Joseph Nuthalapati
parent b272020e94
commit 9554a34cce
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
2 changed files with 28 additions and 4 deletions

View File

@ -295,10 +295,11 @@ def _shutdown_services(manifests):
state[service] = {'app_name': app_name, 'app': app}
for service in state:
state['was_running'] = action_utils.service_is_running('service')
state[service]['was_running'] = action_utils.service_is_running(
service)
for service in reversed(state):
if service['was_running']:
if state[service]['was_running']:
actions.superuser_run('service', ['stop', service])
return state
@ -310,7 +311,7 @@ def _restore_services(original_state):
Maintain exact order of services so dependencies are satisfied.
"""
for service in original_state:
if service['was_running']:
if original_state[service]['was_running']:
actions.superuser_run('service', ['start', service])

View File

@ -18,11 +18,13 @@
Tests for backups module.
"""
import collections
import unittest
from unittest.mock import patch
from plinth.module_loader import load_modules
from ..backups import _list_of_all_apps_for_backup, _get_apps_in_order, \
Packet, validate
Packet, validate, _shutdown_services, _restore_services
def _get_test_manifest(name):
@ -78,3 +80,24 @@ class TestBackups(unittest.TestCase):
names_index = ordered_app_names.index('names')
config_index = ordered_app_names.index('config')
assert names_index < config_index
def test__shutdown_services(self):
"""Test that services are stopped in correct order."""
manifests = [
('a', None, _get_test_manifest('a')),
('b', None, _get_test_manifest('b')),
]
state = _shutdown_services(manifests)
assert 'a' in state
assert 'b' in state
@patch('plinth.actions.superuser_run')
def test__restore_services(self, run):
"""Test that services are restored in correct order."""
original_state = collections.OrderedDict()
original_state['a'] = {
'app_name': 'a', 'app': None, 'was_running': True}
original_state['b'] = {
'app_name': 'b', 'app': None, 'was_running': False}
_restore_services(original_state)
run.assert_called_once_with('service', ['start', 'a'])