Skip to content
Snippets Groups Projects
Commit 9526fd66 authored by nimrod's avatar nimrod
Browse files

Start with testing.

Going to try some TDD here. Still not usable.
parent 05e7e62c
No related branches found
No related tags found
No related merge requests found
Pipeline #1892 canceled
......@@ -45,3 +45,14 @@ run-executable:
needs:
- job: build-executable
artifacts: true
pytest:
extends: .python3
stage: test
before_script:
- poetry install
script:
- poetry run pytest --junit-xml test.xml
artifacts:
reports:
junit: test.xml
......@@ -52,7 +52,7 @@ def main():
check = nagios.Check(args.command, args.arguments)
try:
check.run(args.timeout, args.dry_run)
except Exception as ex:
except Exception as ex: # pylint: disable=broad-except
parser.error(str(ex))
......
"""Nagios check implementation."""
"""Nagios check implementation.
Based on the specification from
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/pluginapi.html"""
import enum
import subprocess
import subprocess # nosemgrep: rules.bandit.B404
DEFAULT_TIMEOUT = 10 # In seconds.
......@@ -21,11 +24,11 @@ class Check:
Command = None
Arguments = []
ExitCode = None
Result = None
Output = None
PerfData = None
LongOutput = None
AdditionalOutput = None
StandardError = None
stderr = None
def __init__(self, command, args):
if not command:
......@@ -34,9 +37,16 @@ class Check:
if args:
self.Arguments = args
def run(self, timeout=DEFAULT_TIMEOUT, dryrun=False):
def _parse_output(self):
"""Parses the plugin output to get the output, perf data and long
output. Can be run after the plugin has run and finished
successfully."""
if self.ExitCode is None:
raise RuntimeError("Check hasn't run yet.")
def run(self, timeout=DEFAULT_TIMEOUT):
"""Run the check (if it's hasn't run yet)."""
if self.Result is not None:
if self.ExitCode is not None:
raise Exception("Check has already run.")
try:
proc = subprocess.run(
......@@ -53,3 +63,5 @@ class Check:
raise RuntimeError(f"Command {self.Command} not found.")
except subprocess.TimeoutExpired:
raise RuntimeError("Timeout exceeded.")
self.ExitCode = proc.returncode
self.stderr = proc.stderr
......@@ -32,6 +32,7 @@ requests = "^2.25.1"
[tool.poetry.dev-dependencies]
pre-commit = "^2.13.0"
pyinstaller = "^4.3"
pytest = "^6.2.4"
[tool.poetry.scripts]
mnpw = "mnpw:main"
......
import pytest
from mnpw import nagios
@pytest.mark.parametrize(
"command,args,exit_code",
[
("/bin/sh", ["-c", "exit 0"], nagios.NagiosCode.OK),
("/bin/sh", ["-c", "exit 1"], nagios.NagiosCode.WARNING),
("/bin/sh", ["-c", "exit 2"], nagios.NagiosCode.CRITICAL),
("/bin/sh", ["-c", "exit 3"], nagios.NagiosCode.UNKNOWN),
],
)
def test_check_exec(command, args, exit_code):
"""Test the executation of checks."""
check = nagios.Check(command, args)
check.run()
assert check.ExitCode == exit_code
def test_check_command_not_found():
"""Test check with a nonexistant command."""
check = nagios.Check("/abcdef", [])
with pytest.raises(RuntimeError):
check.run()
def test_check_timeout():
"""Test check with a timed out command."""
check = nagios.Check("sleep", ["20"])
with pytest.raises(RuntimeError):
check.run(3)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment