Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/local_system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import subprocess

import flask

logger = logging.getLogger(__name__)


Expand All @@ -22,7 +24,17 @@ def restart():
return _exec_shutdown(restart_after=True)


def _is_debug():
return flask.current_app.debug


def _exec_shutdown(restart_after):
# In debug mode, don't actually shut down or restart the system, as that
# would be disruptive to a developer running the app locally.
if _is_debug():
logger.info('Skipping system shutdown because app is in debug mode')
return True

if restart_after:
param = '--reboot'
else:
Expand Down
51 changes: 51 additions & 0 deletions app/local_system_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import subprocess
import unittest
from unittest import mock

import local_system


class ExecShutdownTest(unittest.TestCase):

def setUp(self):
# Run all unit tests with debug mode disabled.
is_debug_patch = mock.patch.object(local_system,
'_is_debug',
return_value=False)
self.addCleanup(is_debug_patch.stop)
is_debug_patch.start()

@mock.patch.object(subprocess, 'run')
def test_shutdown_runs_poweroff_command(self, mock_run):
mock_run.return_value = subprocess.CompletedProcess(args=[],
returncode=0,
stdout='',
stderr='')
self.assertTrue(local_system.shutdown())
mock_run.assert_called_once()
self.assertIn('--poweroff', mock_run.call_args[0][0])

@mock.patch.object(subprocess, 'run')
def test_restart_runs_reboot_command(self, mock_run):
mock_run.return_value = subprocess.CompletedProcess(args=[],
returncode=0,
stdout='',
stderr='')
self.assertTrue(local_system.restart())
mock_run.assert_called_once()
self.assertIn('--reboot', mock_run.call_args[0][0])


class DebugModeTest(unittest.TestCase):

@mock.patch.object(subprocess, 'run')
def test_shutdown_does_not_run_command_in_debug_mode(self, mock_run):
with mock.patch.object(local_system, '_is_debug', return_value=True):
self.assertTrue(local_system.shutdown())
mock_run.assert_not_called()

@mock.patch.object(subprocess, 'run')
def test_restart_does_not_run_command_in_debug_mode(self, mock_run):
with mock.patch.object(local_system, '_is_debug', return_value=True):
self.assertTrue(local_system.restart())
mock_run.assert_not_called()