From 7a76ead7cf8079d354c0cc4ba8403a219fcd6f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20MOHIER?= Date: Sun, 20 May 2018 09:13:25 +0200 Subject: [PATCH] Python3 version: - update packaging script and process - adding rpm and freebsd - updating a test --- test/alignak_test.py | 6 +- test/test_external_commands.py | 192 ++++++++++++++++++++++----------- 2 files changed, 133 insertions(+), 65 deletions(-) diff --git a/test/alignak_test.py b/test/alignak_test.py index 31292b114..da0378628 100644 --- a/test/alignak_test.py +++ b/test/alignak_test.py @@ -1044,12 +1044,12 @@ def check_monitoring_logs(self, expected_logs, dump=True): # data['level'], data['message'])) index+=1 + for log_level, log_message in expected_logs: + assert (log_level, log_message) in monitoring_logs, "Not found :%s" % log_message + assert len(expected_logs) == len(monitoring_logs), "Length do not match: %d" \ % len(monitoring_logs) - for log_level, log_message in expected_logs: - assert (log_level, log_message) in monitoring_logs, "No found :%s" % log_message - def assert_actions_count(self, number): """ Check the number of actions diff --git a/test/test_external_commands.py b/test/test_external_commands.py index e6f9befec..1a226778e 100644 --- a/test/test_external_commands.py +++ b/test/test_external_commands.py @@ -876,7 +876,7 @@ def test_host_comments(self): #  --- # External command: delete an host comment (unknown comment) - excmd = '[%d] DEL_HOST_COMMENT;qsdqszerzerzd' % now + excmd = '[%d] DEL_HOST_COMMENT;unknown_id' % now self._scheduler.run_external_commands([excmd]) self.external_command_loop() self.scheduler_loop(1, []) @@ -915,9 +915,9 @@ def test_host_comments(self): u'EXTERNAL COMMAND: [%s] ADD_HOST_COMMENT;' u'test_host_0;1;test_contact;My accented é"{|:âàç comment' % now), ('info', - u'EXTERNAL COMMAND: [%s] DEL_HOST_COMMENT;qsdqszerzerzd' % now), + u'EXTERNAL COMMAND: [%s] DEL_HOST_COMMENT;unknown_id' % now), ('warning', - u'DEL_HOST_COMMENT: comment id: qsdqszerzerzd does not exist and cannot be deleted.'), + u'DEL_HOST_COMMENT: comment id: unknown_id does not exist and cannot be deleted.'), ('info', u'EXTERNAL COMMAND: [%s] DEL_ALL_HOST_COMMENTS;test_host_0' % now), ] @@ -969,7 +969,7 @@ def test_service_comments(self): #  --- # External command: delete an host comment (unknown comment) - excmd = '[%d] DEL_SVC_COMMENT;qsdqszerzerzd' % now + excmd = '[%d] DEL_SVC_COMMENT;unknown_id' % now self._scheduler.run_external_commands([excmd]) self.external_command_loop() self.scheduler_loop(1, []) @@ -1008,9 +1008,9 @@ def test_service_comments(self): u'EXTERNAL COMMAND: [%s] ADD_SVC_COMMENT;' u'test_host_0;test_ok_0;1;test_contact;My accented é"{|:âàç comment' % now), ('info', - u'EXTERNAL COMMAND: [%s] DEL_SVC_COMMENT;qsdqszerzerzd' % now), + u'EXTERNAL COMMAND: [%s] DEL_SVC_COMMENT;unknown_id' % now), ('warning', - u'DEL_SVC_COMMENT: comment id: qsdqszerzerzd does not exist and cannot be deleted.'), + u'DEL_SVC_COMMENT: comment id: unknown_id does not exist and cannot be deleted.'), ('info', u'EXTERNAL COMMAND: [%s] DEL_ALL_SVC_COMMENTS;test_host_0;test_ok_0' % now), ] @@ -1534,24 +1534,32 @@ def test_host_downtimes_host_down(self): assert (log_level, log_message) in monitoring_logs def test_host_downtimes_host_delete(self): - """ Test the downtime for hosts - host is DOWN + """ Test the downtime for hosts - host is DOWN - create and delete downtimes :return: None """ - for x in range(1, 10): - # An host... - host = self._scheduler.hosts.find_by_name("test_host_0") - host.act_depend_of = [] # ignore the host which we depend of - host.checks_in_progress = [] - host.event_handler_enabled = False - assert host.downtimes == {} + # An host... + host = self._scheduler.hosts.find_by_name("test_host_0") + host.act_depend_of = [] # ignore the host which we depend of + host.checks_in_progress = [] + host.event_handler_enabled = False + host.notifications_interval = 720 + assert host.downtimes == {} - # Its service - svc = self._scheduler.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0") - svc.checks_in_progress = [] - svc.act_depend_of = [] # ignore the host which we depend of - svc.event_handler_enabled = False + # Its service + svc = self._scheduler.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0") + svc.checks_in_progress = [] + svc.act_depend_of = [] # ignore the host which we depend of + svc.event_handler_enabled = False + svc.notifications_interval = 720 - now = int(time.time()) + # Clean broks to delete scheduler retention load message + self._main_broker.broks = [] + + # Freeze the time ! + initial_datetime = datetime.datetime(year=2017, month=6, day=1, + hour=18, minute=30, second=0) + with freeze_time(initial_datetime) as frozen_datetime: + assert frozen_datetime() == initial_datetime # Passive checks for hosts # --------------------------------------------- @@ -1562,7 +1570,10 @@ def test_host_downtimes_host_delete(self): assert 'UP' == host.state assert 'HARD' == host.state_type assert 'Host is alive' == host.output - time.sleep(0.1) + + # Time warp 1 minute + frozen_datetime.tick(delta=datetime.timedelta(minutes=1)) + # Receive passive host check Down excmd = '[%d] PROCESS_HOST_CHECK_RESULT;test_host_0;2;Host is dead' % int(time.time()) self._scheduler.run_external_commands([excmd]) @@ -1570,24 +1581,40 @@ def test_host_downtimes_host_delete(self): assert 'DOWN' == host.state assert 'SOFT' == host.state_type assert 'Host is dead' == host.output - time.sleep(0.1) + + # Time warp 1 minute + frozen_datetime.tick(delta=datetime.timedelta(minutes=1)) + excmd = '[%d] PROCESS_HOST_CHECK_RESULT;test_host_0;2;Host is dead' % int(time.time()) self._scheduler.run_external_commands([excmd]) self.external_command_loop() assert 'DOWN' == host.state assert 'SOFT' == host.state_type assert 'Host is dead' == host.output - time.sleep(0.1) + + # Time warp 1 minute + frozen_datetime.tick(delta=datetime.timedelta(minutes=1)) + excmd = '[%d] PROCESS_HOST_CHECK_RESULT;test_host_0;2;Host is dead' % int(time.time()) self._scheduler.run_external_commands([excmd]) - time.sleep(1.0) + # The notifications are created to be launched in the next second when they happen ! + # Time warp 1 second + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) + self.external_command_loop() + # Time warp 1 second + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) + self.external_command_loop() + self.external_command_loop() assert 'DOWN' == host.state assert 'HARD' == host.state_type assert 'Host is dead' == host.output + # Time warp 1 minutes + frozen_datetime.tick(delta=datetime.timedelta(minutes=1)) + #  --- - # External command: add another host downtime + # External command: add an host downtime # Simulate that the host is now a problem but the downtime starts in some seconds host.is_problem = True host.problem_has_been_acknowledged = False @@ -1598,10 +1625,14 @@ def test_host_downtimes_host_delete(self): svc.state = 'CRITICAL' # and the problem is not acknowledged assert False == host.problem_has_been_acknowledged - # Schedule a downtime for the host + # Schedule a downtime for the host - 15 minutes downtime + now = int(time.time()) excmd = '[%d] SCHEDULE_HOST_DOWNTIME;test_host_0;%s;%s;1;0;1200;' \ - 'test_contact;My first downtime' % (now, now+2, now + 4) + 'test_contact;My first downtime' % (now, now+2, now + 15*60) self._scheduler.run_external_commands([excmd]) + + # Time warp 1 second - the downtime is not yet started + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) self.external_command_loop() # Host is a problem - @@ -1615,67 +1646,86 @@ def test_host_downtimes_host_delete(self): assert downtime.comment == "My first downtime" assert downtime.author == "test_contact" assert downtime.start_time == now + 2 - assert downtime.end_time == now + 4 - assert downtime.duration == 2 + assert downtime.end_time == now + 15*60 + assert downtime.duration == 15*60 -2 assert downtime.fixed == True assert downtime.trigger_id == "0" - time.sleep(1) + frozen_datetime.tick(delta=datetime.timedelta(minutes=1)) + # Time warp 1 minute- the downtime is started now! self.external_command_loop() #  --- # External command: yet another host downtime excmd = '[%d] SCHEDULE_HOST_DOWNTIME;test_host_0;%s;%s;1;0;1200;test_contact;' \ - u'My accented é"{|:âàç downtime' % (now, now + 180, now + 360) + u'My accented é"{|:âàç downtime' % (now + 10, now + 180, now + 360) self._scheduler.run_external_commands([excmd]) - time.sleep(1) + # Time warp 1 second + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) + self.external_command_loop() + + # Time warp 1 second - the new downtime is now started + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) self.external_command_loop() + assert len(host.downtimes) == 2 #  --- # External command: delete an host downtime (unknown downtime) - excmd = '[%d] DEL_HOST_DOWNTIME;qsdqszerzerzd' % now + excmd = '[%d] DEL_HOST_DOWNTIME;unknown_id' % (now + 100) self._scheduler.run_external_commands([excmd]) - time.sleep(1) + # Time warp 1 second + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) self.external_command_loop() + # Still remaining 2 downtimes assert len(host.downtimes) == 2 #  --- # External command: delete an host downtime downtime = list(host.downtimes.values())[0] - excmd = '[%d] DEL_HOST_DOWNTIME;%s' % (now, downtime.uuid) + excmd = '[%d] DEL_HOST_DOWNTIME;%s' % (now + 200, downtime.uuid) self._scheduler.run_external_commands([excmd]) - time.sleep(1) + # Time warp 1 second + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) self.external_command_loop() assert len(host.downtimes) == 1 #  --- # External command: delete all host downtime - excmd = '[%d] DEL_ALL_HOST_DOWNTIMES;test_host_0' % now + excmd = '[%d] DEL_ALL_HOST_DOWNTIMES;test_host_0' % (now + 300) self._scheduler.run_external_commands([excmd]) + # Time warp 1 second + frozen_datetime.tick(delta=datetime.timedelta(seconds=1)) self.external_command_loop() assert len(host.downtimes) == 0 # We got 'monitoring_log' broks for logging to the monitoring logs... - monitoring_logs = [] - for brok in self._main_broker.broks: - if brok.type == 'monitoring_log': - data = unserialize(brok.data) - monitoring_logs.append((data['level'], data['message'])) - - print(monitoring_logs) + # monitoring_logs = [] + # for brok in self._main_broker.broks: + # if brok.type == 'monitoring_log': + # data = unserialize(brok.data) + # monitoring_logs.append((data['level'], data['message'])) + # + # print(monitoring_logs) + # el = [ + # ('info', 'EXTERNAL COMMAND: [1496342582] DEL_HOST_DOWNTIME;unknown_id'), ('warning', 'DEL_HOST_DOWNTIME: downtime_id id: unknown_id does not exist and cannot be deleted.'), ('info', 'EXTERNAL COMMAND: [1496342582] DEL_HOST_DOWNTIME;02dc73b6-6cb2-4c56-84ec-f98f3a8ebe59'), ('info', 'HOST DOWNTIME ALERT: test_host_0;CANCELLED; Scheduled downtime for host has been cancelled.'), ('info', 'EXTERNAL COMMAND: [1496342582] DEL_ALL_HOST_DOWNTIMES;test_host_0') + # ] expected_logs = [ + ('info', + u'PASSIVE HOST CHECK: test_host_0;0;Host is alive;;'), + ('warning', u'PASSIVE HOST CHECK: test_host_0;2;Host is dead;;'), - ('warning', - u'PASSIVE HOST CHECK: test_host_0;2;Host is dead;;'), - ('warning', - u'PASSIVE HOST CHECK: test_host_0;2;Host is dead;;'), - ('error', u'HOST ALERT: test_host_0;DOWN;SOFT;1;Host is dead'), + + ('warning', + u'PASSIVE HOST CHECK: test_host_0;2;Host is dead;;'), ('error', u'HOST ALERT: test_host_0;DOWN;SOFT;2;Host is dead'), + + ('warning', + u'PASSIVE HOST CHECK: test_host_0;2;Host is dead;;'), ('error', u'HOST ALERT: test_host_0;DOWN;HARD;3;Host is dead'), @@ -1684,20 +1734,38 @@ def test_host_downtimes_host_delete(self): ('info', u'EXTERNAL COMMAND: [%s] SCHEDULE_HOST_DOWNTIME;test_host_0;%s;%s;1;0;1200;' - u'test_contact;My first downtime' % (now, now + 2, now + 4)), + u'test_contact;My first downtime' % (now, now + 2, now + 15*60)), + ('error', + u'HOST NOTIFICATION: test_contact;test_host_0;DOWN;notify-host;Host is dead'), + + ('info', + u'HOST DOWNTIME ALERT: test_host_0;STARTED; Host has entered a period of scheduled downtime'), + ('info', + u'HOST ACKNOWLEDGE ALERT: test_host_0;STARTED; Host problem has been acknowledged'), + ('info', + u'HOST NOTIFICATION: test_contact;test_host_0;DOWNTIMESTART (DOWN);notify-host;Host is dead'), + ('info', + u'SERVICE ACKNOWLEDGE ALERT: test_host_0;test_ok_0;STARTED; Service problem has been acknowledged'), + ('info', u'EXTERNAL COMMAND: [%s] SCHEDULE_HOST_DOWNTIME;test_host_0;%s;%s;1;0;1200;' - u'test_contact;My accented é"{|:âàç downtime' % (now, now + 180, now + 360)), + u'test_contact;My accented é"{|:âàç downtime' % (now + 10, now + 180, now + 360)), ('info', - u'EXTERNAL COMMAND: [%s] DEL_HOST_DOWNTIME;qsdqszerzerzd' % now), + u'EXTERNAL COMMAND: [%s] DEL_HOST_DOWNTIME;unknown_id' % (now + 100)), ('warning', - u'DEL_HOST_DOWNTIME: downtime_id id: qsdqszerzerzd ' + u'DEL_HOST_DOWNTIME: downtime_id id: unknown_id ' u'does not exist and cannot be deleted.'), + + ('info', + u'EXTERNAL COMMAND: [%s] DEL_HOST_DOWNTIME;%s' % (now + 200, downtime.uuid)), + ('info', + u'HOST DOWNTIME ALERT: test_host_0;CANCELLED; Scheduled downtime for host has been cancelled.'), + + ('info', + u'EXTERNAL COMMAND: [%s] DEL_ALL_HOST_DOWNTIMES;test_host_0' % (now + 300)), ] - for log_level, log_message in expected_logs: - print(log_message) - assert (log_level, log_message) in monitoring_logs + self.check_monitoring_logs(expected_logs) def test_service_downtimes(self): """ Test the downtimes for services @@ -1748,7 +1816,7 @@ def test_service_downtimes(self): #  --- # External command: delete a service downtime (unknown downtime) - excmd = '[%d] DEL_SVC_DOWNTIME;qsdqszerzerzd' % now + excmd = '[%d] DEL_SVC_DOWNTIME;unknown_id' % now self._scheduler.run_external_commands([excmd]) self.external_command_loop() self.scheduler_loop(1, []) @@ -1788,9 +1856,9 @@ def test_service_downtimes(self): u'%s;%s;1;0;1200;test_contact;My accented é"{|:âàç downtime' % ( now, now + 2120, now + 21200)), ('info', - u'EXTERNAL COMMAND: [%s] DEL_SVC_DOWNTIME;qsdqszerzerzd' % now), + u'EXTERNAL COMMAND: [%s] DEL_SVC_DOWNTIME;unknown_id' % now), ('warning', - u'DEL_SVC_DOWNTIME: downtime_id id: qsdqszerzerzd does ' + u'DEL_SVC_DOWNTIME: downtime_id id: unknown_id does ' u'not exist and cannot be deleted.'), ('info', u'EXTERNAL COMMAND: [%s] DEL_SVC_DOWNTIME;%s' % (now, downtime_id)), @@ -1846,7 +1914,7 @@ def test_contact_downtimes(self): #  --- # External command: delete a contact downtime (unknown downtime) - excmd = '[%d] DEL_CONTACT_DOWNTIME;qsdqszerzerzd' % now + excmd = '[%d] DEL_CONTACT_DOWNTIME;unknown_id' % now self._scheduler.run_external_commands([excmd]) self.external_command_loop() self.scheduler_loop(1, []) @@ -1885,9 +1953,9 @@ def test_contact_downtimes(self): u'EXTERNAL COMMAND: [%s] SCHEDULE_CONTACT_DOWNTIME;test_contact;' u'%s;%s;test_contact;My accented é"{|:âàç downtime' % (now, now + 2120, now + 21200)), ('info', - u'EXTERNAL COMMAND: [%s] DEL_CONTACT_DOWNTIME;qsdqszerzerzd' % now), + u'EXTERNAL COMMAND: [%s] DEL_CONTACT_DOWNTIME;unknown_id' % now), ('warning', - u'DEL_CONTACT_DOWNTIME: downtime_id id: qsdqszerzerzd does ' + u'DEL_CONTACT_DOWNTIME: downtime_id id: unknown_id does ' u'not exist and cannot be deleted.'), ('info', u'EXTERNAL COMMAND: [%s] DEL_CONTACT_DOWNTIME;%s' % (now, downtime_id)),