# Path: simplemonitor/Monitors/service.py
# class MonitorSvc(Monitor):
# class MonitorService(Monitor):
# class MonitorRC(Monitor):
# class MonitorUnixService(Monitor):
# class MonitorSystemdUnit(Monitor):
# class MonitorProcess(Monitor):
# class MonitorEximQueue(Monitor):
# class MonitorWindowsDHCPScope(Monitor):
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     def get_params(self) -> Tuple:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     def get_params(self) -> Tuple:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(self, name: str = "unnamed", config_options: dict = None) -> None:
#     def run_test(self) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def _list_units(cls) -> List[Any]:
#     def run_test(self) -> bool:
#     def _check_unit(
#         self, name: str, load_state: str, active_state: str, sub_state: str
#     ) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(
#         self, name: str = "unnamed", config_options: Optional[dict] = None
#     ) -> None:
#     def _find_process_by_name(
#         name: str, username: Optional[str] = None
#     ) -> List[psutil.Process]:
#     def run_test(self) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     def get_params(self) -> Tuple:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     CACHE_LIFETIME = 1  # in seconds


import unittest
import subprocess
from simplemonitor.Monitors import service
from unittest.mock import patch
# type: ignore


class TestUnixServiceMonitors(unittest.TestCase):
    @patch("subprocess.run")
    def test_UnixService_ok(self, subprocess_run_fn):
        config_options = {"service": "unittest"}

        subprocess_run_fn.return_value = subprocess.CompletedProcess(
            ["service", config_options.get("service"), "status"], returncode=1
        )
        m = service.MonitorUnixService("unittest", config_options)

        m.run_test()
        self.assertFalse(m.test_success())
        self.assertEqual(m.error_count, 1)

        m.run_test()
        self.assertFalse(m.test_success())
        self.assertEqual(m.error_count, 2)

    @patch("subprocess.run")
    def test_UnixService_raiseFileNotFoundError(self, subprocess_run_fn):
        config_options = {"service": "unittest"}

        subprocess_run_fn.side_effect = FileNotFoundError(
            "[Errno 2] No such file or directory: 'service'"
        )

# Path: simplemonitor/Monitors/service.py
# class MonitorSvc(Monitor):
# class MonitorService(Monitor):
# class MonitorRC(Monitor):
# class MonitorUnixService(Monitor):
# class MonitorSystemdUnit(Monitor):
# class MonitorProcess(Monitor):
# class MonitorEximQueue(Monitor):
# class MonitorWindowsDHCPScope(Monitor):
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     def get_params(self) -> Tuple:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     def get_params(self) -> Tuple:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(self, name: str = "unnamed", config_options: dict = None) -> None:
#     def run_test(self) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def _list_units(cls) -> List[Any]:
#     def run_test(self) -> bool:
#     def _check_unit(
#         self, name: str, load_state: str, active_state: str, sub_state: str
#     ) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(
#         self, name: str = "unnamed", config_options: Optional[dict] = None
#     ) -> None:
#     def _find_process_by_name(
#         name: str, username: Optional[str] = None
#     ) -> List[psutil.Process]:
#     def run_test(self) -> bool:
#     def get_params(self) -> Tuple:
#     def describe(self) -> str:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     def get_params(self) -> Tuple:
#     def __init__(self, name: str, config_options: dict) -> None:
#     def run_test(self) -> bool:
#     def describe(self) -> str:
#     CACHE_LIFETIME = 1  # in seconds


# Path: tests/test_service.py
import unittest
import subprocess
from simplemonitor.Monitors import service
from unittest.mock import patch

# type: ignore


class TestUnixServiceMonitors(unittest.TestCase):
    @patch("subprocess.run")
    def test_UnixService_ok(self, subprocess_run_fn):
        config_options = {"service": "unittest"}

        subprocess_run_fn.return_value = subprocess.CompletedProcess(
            ["service", config_options.get("service"), "status"], returncode=1
        )
        m = service.MonitorUnixService("unittest", config_options)

        m.run_test()
        self.assertFalse(m.test_success())
        self.assertEqual(m.error_count, 1)

        m.run_test()
        self.assertFalse(m.test_success())
        self.assertEqual(m.error_count, 2)

    @patch("subprocess.run")
    def test_UnixService_raiseFileNotFoundError(self, subprocess_run_fn):
        config_options = {"service": "unittest"}

        subprocess_run_fn.side_effect = FileNotFoundError(
            "[Errno 2] No such file or directory: 'service'"
        )

        m = service.MonitorUnixService("unittest", config_options)