Commit dd4e6d79 authored by James Valleroy's avatar James Valleroy

backups: Fix and test service shutdown and restore

Signed-off-by: James Valleroy's avatarJames Valleroy <jvalleroy@mailbox.org>
parent 15ef4e93
...@@ -295,10 +295,11 @@ def _shutdown_services(manifests): ...@@ -295,10 +295,11 @@ def _shutdown_services(manifests):
state[service] = {'app_name': app_name, 'app': app} state[service] = {'app_name': app_name, 'app': app}
for service in state: 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): for service in reversed(state):
if service['was_running']: if state[service]['was_running']:
actions.superuser_run('service', ['stop', service]) actions.superuser_run('service', ['stop', service])
return state return state
...@@ -310,7 +311,7 @@ def _restore_services(original_state): ...@@ -310,7 +311,7 @@ def _restore_services(original_state):
Maintain exact order of services so dependencies are satisfied. Maintain exact order of services so dependencies are satisfied.
""" """
for service in original_state: for service in original_state:
if service['was_running']: if original_state[service]['was_running']:
actions.superuser_run('service', ['start', service]) actions.superuser_run('service', ['start', service])
......
...@@ -18,11 +18,13 @@ ...@@ -18,11 +18,13 @@
Tests for backups module. Tests for backups module.
""" """
import collections
import unittest import unittest
from unittest.mock import patch
from plinth.module_loader import load_modules from plinth.module_loader import load_modules
from ..backups import _list_of_all_apps_for_backup, _get_apps_in_order, \ 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): def _get_test_manifest(name):
...@@ -78,3 +80,24 @@ class TestBackups(unittest.TestCase): ...@@ -78,3 +80,24 @@ class TestBackups(unittest.TestCase):
names_index = ordered_app_names.index('names') names_index = ordered_app_names.index('names')
config_index = ordered_app_names.index('config') config_index = ordered_app_names.index('config')
assert names_index < config_index 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'])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment