Commit 9526fd66 authored by nimrod's avatar nimrod
Browse files

Start with testing.

Going to try some TDD here. Still not usable.
parent 05e7e62c
Loading
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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))


+18 −6
Original line number Diff line number Diff line
"""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
+1 −0
Original line number Diff line number Diff line
@@ -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"

tests/__init__.py

0 → 100644
+0 −0

Empty file added.

Loading