Skip to content

Commit

Permalink
Add test for no_monitor behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ealdwulf committed Sep 18, 2018
1 parent 257bed5 commit 1a5cb6b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
2 changes: 1 addition & 1 deletion kano_updater/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def run(cmdargs):
error code (integer)
"""
if 'MONITOR_PID' in os.environ:
os.execvp(cmdargs[0], cmdargs[1:])
os.execvp(cmdargs[0], cmdargs)

os.environ["MONITOR_PID"] = str(os.getpid())
subproc = subprocess.Popen(cmdargs, shell=False)
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
from tests.fixtures.run_cmd import *
from tests.fixtures.state import *
from tests.fixtures.version import *
from tests.fixtures.environ import *
24 changes: 24 additions & 0 deletions tests/fixtures/environ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# environ.py
#
# Copyright (C) 2018 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#
# Fixture to clean environment
#


import pytest
import os


@pytest.fixture(scope='function', params=(True, False))
def monitor_pid():
"""
ensures environment variable MONITOR_PID is not propagated across tests
"""
try:
del os.environ["MONITOR_PID"]
except:
pass

18 changes: 18 additions & 0 deletions tests/monitor_wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
#
# monitor_wrap.py
#
# Copyright (C) 2018 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2x
#
# Wrapper for when the monitor needs to be tested in a separate process to pytest
#
import sys
import os
sys.path.append(os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'fixtures/mock_imports'
))
import kano_updater.monitor
import sys
test_cmd = sys.argv[1:]
sys.exit(kano_updater.monitor.run(test_cmd))
41 changes: 37 additions & 4 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ def run_monitor(mon_timeout, test_cmd):

return rc

def run_monitor_wrap(mon_timeout, test_cmd):
import subprocess
return subprocess.call(["./tests/monitor_wrap.py"]+test_cmd, shell=False)

def test_return_code():

def test_return_code(monitor_pid, apt):
import os
import random
# When command returns in time,
Expand All @@ -29,7 +33,7 @@ def test_return_code():
assert rc == expected_rc


def test_return_timeout():
def test_return_timeout(monitor_pid, apt):
import os
import random
import kano_updater.return_codes
Expand All @@ -40,7 +44,7 @@ def test_return_timeout():
assert rc == kano_updater.return_codes.RC.HANGED_INDEFINITELY


def test_forking():
def test_forking(monitor_pid, apt):
import os
import random
# When command takes longer than timeout but forks,
Expand All @@ -50,7 +54,7 @@ def test_forking():
assert rc == expected_rc


def test_signal():
def test_signal(monitor_pid, apt):
import os
import random
# When command takes longer than timeout but signals,
Expand All @@ -60,5 +64,34 @@ def test_signal():
assert rc == expected_rc


def test_no_monitor(monitor_pid, apt):
import os
import random
os.environ["MONITOR_PID"] = str(os.getpid())
# When MONITOR_PID is already set, don't execute monitor behavior

command_rc = random.randint(1, 20)
rc = run_monitor_wrap(5, ["./tests/monitor_stub.py", "10", str(command_rc)])
assert rc == command_rc # would be HANGED_INDEFINITELY except that we are not monitoring


def test_monitor_term(monitor_pid, apt):

import random
import subprocess
import time
import signal
# When we send a term signal to the monitor process, it sends it to the
# child process.
# Note that this test assumes that the monitor process correctly
# reports the child process exit code
command_rc = random.randint(1, 20)
proc = subprocess.Popen(["./tests/monitor_stub.py", "10", str(command_rc)])
time.sleep(2)
proc.terminate()
proc.wait()
rc = proc.returncode
assert rc == -signal.SIGTERM

# TBD:
# - test gui mode

0 comments on commit 1a5cb6b

Please sign in to comment.