def test_GetNextRefresh(): r = RefreshTimer(IConfig("example.yaml")) c = f"{r.NextRefresh.hour}:{r.NextRefresh.minute}" res = r.GetNextRefresh() if res == c: assert True
def test_SetNextRefreshRollOverMin02(): # This test checks if mins where to be 61 rather then 60 r = RefreshTimer(IConfig("example.yaml")) r.LastRefresh = r.LastRefresh.replace(hour=12, minute=59) r.SetNextRefresh() if r.NextRefresh.hour == 13 and r.NextRefresh.minute == 1: assert True
def test_SecondRemainder(): r = RefreshTimer(IConfig("example.yaml")) now = datetime.datetime.now() now = now.replace(second=23) res = r.GetSec(now, 2) if res[0] == 1 and res[1] == 1: assert True
def test_MinuteRemainder(): r = RefreshTimer(IConfig("example.yaml")) now = datetime.datetime.now() now = now.replace(minute=23) res = r.GetMin(now, 2) if res[0] == 1 and res[1] == 1: assert True
def test_HourRemainder(): r = RefreshTimer(IConfig("example.yaml")) now = datetime.datetime.now() now = now.replace(hour=23) res = r.GetHour(now, 2) if res[0] == 1 and res[1] == 1: assert True
def test_CheckRefreshTwice(): r = RefreshTimer(IConfig("example.yaml")) res = r.CheckRefreshTimer() res = r.CheckRefreshTimer() # should return false as its not time to refresh yet if res == False: assert True
def test_CheckRefreshTimer01(): # Checking for False as the timer has not lapsed yet r = RefreshTimer(IConfig("example.yaml")) r.LastRefresh = r.LastRefresh.replace(minute=r.LastRefresh.minute - 1) res = r.CheckRefreshTimer() if res == False: assert True
def test_HourNoRemainder(): i = IConfig("example.yaml") r = RefreshTimer(i) now = datetime.datetime.now() now = now.replace(hour=1) res = r.GetHour(now, 2) if res[0] == 3: assert True
def test_SetNextRefreshRollOverMin(): # Set it so we roll it over at the exact min # hour +1 # mins 00 r = RefreshTimer(IConfig("example.yaml")) r.LastRefresh = r.LastRefresh.replace(hour=12, minute=58) r.SetNextRefresh() n = r.NextRefresh if r.NextRefresh.hour == 13 and r.NextRefresh.minute == 00: assert True
def __init__(self, iconfig: IConfig): self.__iconfig__: IConfig = iconfig self.__config__: ContextConfig = ContextConfig(self.__iconfig__) self.__config__.GetWorkingConfigClass(True) self.__config__.ReadConfig() self.__configuration__: Configuration = Configuration() self.__configuration__ = self.__config__.configuration self.refresh = RefreshTimer(self.__iconfig__) self.report = [] self.LastRefresh = datetime.datetime.now() self.NextRefresh = datetime.datetime.now() pass
def test_CheckRefreshTimer(): r = RefreshTimer(IConfig("example.yaml")) res = r.CheckRefreshTimer() if res == True: assert True
class Monitor(): """ Monitor is the working class that checks all the requested nodes. """ def __init__(self, iconfig: IConfig): self.__iconfig__: IConfig = iconfig self.__config__: ContextConfig = ContextConfig(self.__iconfig__) self.__config__.GetWorkingConfigClass(True) self.__config__.ReadConfig() self.__configuration__: Configuration = Configuration() self.__configuration__ = self.__config__.configuration self.refresh = RefreshTimer(self.__iconfig__) self.report = [] self.LastRefresh = datetime.datetime.now() self.NextRefresh = datetime.datetime.now() pass def Start(self, force: bool = False) -> None: if force == False: res = self.refresh.CheckRefreshTimer() #res = self.__CheckRefreshTimer() if res == True: self.__Worker() else: self.__Worker() def __ReadConfig__(self) -> None: self.__config__.ReadConfig() self.__configuration__ = Configuration() self.__configuration__ = self.__config__.configuration def __Worker(self) -> None: self.__ReadConfig__() report = self.__configuration__.nodes requirement: bool = True for node in report: if requirement == True: np = node.protocol.lower() if np == "icmp": cp = ContextProtocols(IProtocols(node.address, 'ICMP')) cp.GetWorkingClass(True) cp.configuration = self.__configuration__ cp.Start() elif np == "http:get": cp = ContextProtocols(IProtocols(node.address, "HTTP:GET")) cp.GetWorkingClass(True) cp.Start() elif np == "http:post": cp = ContextProtocols(IProtocols(node.address, "Http:Post")) cp.GetWorkingClass(True) cp.Start() else: raise InvalidProtocol( f"{node.protocol} is invalid. Use ICMP, HTTP:Get, HTTP:POST." ) node.ms = cp.MS node.status = cp.Status if node.required == True and node.status == "Offline": requirement = False else: node.status = "Offline" # Work has been finished #Copy our local version to the global scope self.LastRefresh = datetime.datetime.now() self.report = report