Ejemplo n.º 1
0
    def test_match_all_in(self):
        tags = [
            StatusCheckResultTag.objects.get_or_create(value='tag' + str(i))[0]
            for i in range(3)
        ]
        ack = Acknowledgement(status_check=self.http_check,
                              match_if=Acknowledgement.MATCH_ALL_IN)
        ack.save()
        ack.tags.add(tags[0], tags[1])

        now = timezone.now()
        result = StatusCheckResult(status_check=self.http_check,
                                   succeeded=False,
                                   time=now,
                                   time_complete=now)
        result.save()

        # no tags matches
        self.assertTrue(ack.matches_result(result))

        # 1 matching tag matches
        result.tags.add(tags[0])
        self.assertTrue(ack.matches_result(result))

        # 1 matching, 1 not should NOT match
        result.tags.add(tags[2])
        self.assertFalse(ack.matches_result(result))
Ejemplo n.º 2
0
    def run_checks(self, checks, from_service_status=None):
        # type: (List[Tuple[StatusCheck, bool, bool]], Union[None, str]) -> None
        """
        Simulates running the given checks with the given results, then updates the service (triggering alerts).
        All previous StatusCheckResults are cleared by calling this function. A check can be listed more than once.
        You should set up self.service.alerts before calling this.
        :param checks: list of (check, succeeded, acked) tuples
        :param from_service_status: specify the service status to transition from (service.old_overall_status), optional
        """
        # clear any previous results
        StatusCheckResult.objects.all().delete()

        for check, succeeded, acked in checks:
            now = timezone.now()
            result = StatusCheckResult(status_check=check, time=now, time_complete=now, succeeded=succeeded)
            if hasattr(StatusCheckResult, 'acked'):  # forwards-compatible with acks
                result.acked = acked
            result.save()

            check.last_run = now
            check.save()

        if from_service_status:
            self.service.overall_status = from_service_status

        self.service.update_status()
Ejemplo n.º 3
0
class LocalTestCase(TestCase):
    def setUp(self):
        requests.get = Mock()
        requests.post = Mock()
        rest.TwilioRestClient = Mock()
        mail.send_mail = Mock()
        self.create_dummy_data()
        super(LocalTestCase, self).setUp()

    def create_dummy_data(self):
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create(username=self.username)
        self.user.set_password(self.password)
        self.user.save()
        self.graphite_check = GraphiteStatusCheck.objects.create(
            name='Graphite Check',
            metric='stats.fake.value',
            check_type='>',
            value='9.0',
            created_by=self.user,
            importance=Service.ERROR_STATUS,
        )
        self.jenkins_check = JenkinsStatusCheck.objects.create(
            name='Jenkins Check',
            created_by=self.user,
            importance=Service.ERROR_STATUS,
            max_queued_build_time=10,
        )
        self.http_check = HttpStatusCheck.objects.create(
            name='Http Check',
            created_by=self.user,
            importance=Service.CRITICAL_STATUS,
            endpoint='http://arachnys.com',
            timeout=10,
            status_code='200',
            text_match=None,
        )
        self.service = Service.objects.create(name='Service', )

        self.service.status_checks.add(self.graphite_check, self.jenkins_check,
                                       self.http_check)
        # Passing is most recent
        self.most_recent_result = StatusCheckResult(
            check=self.graphite_check,
            time=timezone.now() - timedelta(seconds=1),
            time_complete=timezone.now(),
            succeeded=True)
        self.most_recent_result.save()
        # failing is second most recent
        self.older_result = StatusCheckResult(
            check=self.graphite_check,
            time=timezone.now() - timedelta(seconds=60),
            time_complete=timezone.now() - timedelta(seconds=59),
            succeeded=False)
        self.older_result.save()
        self.graphite_check.save()  # Will recalculate status
Ejemplo n.º 4
0
    def test_print_tags(self):
        StatusCheckResult.objects.all().delete()
        StatusCheckResultTag.objects.all().delete()

        now = timezone.now()
        result = StatusCheckResult(status_check=self.http_check,
                                   time=now,
                                   time_complete=now,
                                   succeeded=False)
        result.save()

        tags = [
            StatusCheckResultTag(value='tag{:03}'.format(i)) for i in range(10)
        ]
        StatusCheckResultTag.objects.bulk_create(tags)

        tags_list = StatusCheckResultTag.objects.all()
        for i in range(len(tags)):
            result.tags.add(tags_list[i])

        self.assertEqual(
            result.print_tags(),
            'tag000\ntag001\ntag002\ntag003\ntag004\ntag005\ntag006\ntag007\ntag008'
            '\ntag009')
Ejemplo n.º 5
0
class LocalTestCase(APITestCase):

    def setUp(self):
        requests.get = Mock()
        requests.post = Mock()
        rest.TwilioRestClient = Mock()
        mail.send_mail = Mock()
        self.create_dummy_data()
        super(LocalTestCase, self).setUp()

    def create_dummy_data(self):
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create(username=self.username)
        self.user.set_password(self.password)
        self.user.user_permissions.add(
            Permission.objects.get(codename='add_instance'),
            Permission.objects.get(codename='add_service'),
            Permission.objects.get(codename='add_httpstatuscheck'),
            Permission.objects.get(codename='add_graphitestatuscheck'),
            Permission.objects.get(codename='add_jenkinsstatuscheck'),
            Permission.objects.get(codename='add_icmpstatuscheck'),
        )
        self.user.save()
        self.graphite_check = GraphiteStatusCheck.objects.create(
            name='Graphite Check',
            metric='stats.fake.value',
            check_type='>',
            value='9.0',
            created_by=self.user,
            importance=Service.ERROR_STATUS,
        )
        self.jenkins_check = JenkinsStatusCheck.objects.create(
            name='Jenkins Check',
            created_by=self.user,
            importance=Service.ERROR_STATUS,
            max_queued_build_time=10,
        )
        self.http_check = HttpStatusCheck.objects.create(
            name='Http Check',
            created_by=self.user,
            importance=Service.CRITICAL_STATUS,
            endpoint='http://arachnys.com',
            timeout=10,
            status_code='200',
            text_match=None,
        )
        self.service = Service.objects.create(
            name='Service',
        )

        self.service.status_checks.add(
            self.graphite_check, self.jenkins_check, self.http_check)
        # failing is second most recent
        self.older_result = StatusCheckResult(
            check=self.graphite_check,
            time=timezone.now() - timedelta(seconds=60),
            time_complete=timezone.now() - timedelta(seconds=59),
            succeeded=False
        )
        self.older_result.save()
        # Passing is most recent
        self.most_recent_result = StatusCheckResult(
            check=self.graphite_check,
            time=timezone.now() - timedelta(seconds=1),
            time_complete=timezone.now(),
            succeeded=True
        )
        self.most_recent_result.save()
        self.graphite_check.save()  # Will recalculate status
Ejemplo n.º 6
0
class LocalTestCase(APITestCase):
    def setUp(self):
        requests.get = Mock()
        requests.post = Mock()
        rest.TwilioRestClient = Mock()
        mail.send_mail = Mock()
        self.create_dummy_data()
        super(LocalTestCase, self).setUp()

    def create_dummy_data(self):
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create(username=self.username)
        self.user.set_password(self.password)
        self.user.user_permissions.add(
            Permission.objects.get(codename='add_service'),
            Permission.objects.get(codename='add_httpstatuscheck'),
            Permission.objects.get(codename='add_jenkinsstatuscheck'),
            Permission.objects.get(codename='add_tcpstatuscheck'),
        )
        self.user.save()

        self.jenkins_check = JenkinsStatusCheck.objects.create(
            id=10101,
            name='Jenkins Check',
            created_by=self.user,
            importance=Service.ERROR_STATUS,
            max_queued_build_time=10,
            max_build_failures=5)

        self.jenkins_check2 = JenkinsStatusCheck.objects.create(
            id=10104,
            name='Jenkins Check 2',
            created_by=self.user,
            importance=Service.ERROR_STATUS,
            max_queued_build_time=10,
            max_build_failures=0)

        self.http_check = HttpStatusCheck.objects.create(
            id=10102,
            name='Http Check',
            created_by=self.user,
            importance=Service.CRITICAL_STATUS,
            endpoint='http://arachnys.com',
            timeout=10,
            status_code='200',
            text_match=None,
        )
        self.tcp_check = TCPStatusCheck.objects.create(
            id=10103,
            name='TCP Check',
            created_by=self.user,
            importance=Service.ERROR_STATUS,
            address='github.com',
            port=80,
            timeout=6,
        )

        # Set ical_url for schedule to filename we're using for mock response
        self.schedule = Schedule.objects.create(
            name='Principal',
            ical_url='calendar_response.ics',
        )
        self.secondary_schedule = Schedule.objects.create(
            name='Secondary',
            ical_url='calendar_response_different.ics',
            fallback_officer=self.user,
        )
        self.schedule.save()
        self.secondary_schedule.save()

        self.service = Service.objects.create(
            id=2194,
            name='Service',
        )
        self.service.save()
        self.service.schedules.add(self.schedule)
        self.service.status_checks.add(self.jenkins_check, self.http_check,
                                       self.tcp_check)

        # Failing is second most recent
        self.older_result = StatusCheckResult(
            status_check=self.http_check,
            time=timezone.now() - timedelta(seconds=60),
            time_complete=timezone.now() - timedelta(seconds=59),
            succeeded=False)
        self.older_result.save()
        # Passing is most recent
        self.most_recent_result = StatusCheckResult(
            status_check=self.http_check,
            time=timezone.now() - timedelta(seconds=1),
            time_complete=timezone.now(),
            succeeded=True)
        self.most_recent_result.save()
        self.http_check.save()  # Will recalculate status
Ejemplo n.º 7
0
class LocalTestCase(APITestCase):

    def setUp(self):
        requests.get = Mock()
        requests.post = Mock()
        rest.TwilioRestClient = Mock()
        mail.send_mail = Mock()
        self.create_dummy_data()
        super(LocalTestCase, self).setUp()

    def create_dummy_data(self):
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create(username=self.username)
        self.user.set_password(self.password)
        self.user.user_permissions.add(
            Permission.objects.get(codename='add_instance'),
            Permission.objects.get(codename='add_service'),
        )
        self.user.save()
        self.service = Service.objects.create(
            name='Service',
        )

        self.port_open_check_model = StatusCheckPluginModel.objects.create(slug='port_open_check')
        self.chat_messenger_alert_model = AlertPluginModel.objects.create(slug='chat_messenger_alert')
        # Refetch User model with new chat messenger settings.
        self.user = User.objects.get(username='******')
        self.user.chat_messenger_alert_settings.nickname = "Xx__CabotMaster420__xX"

        self.port_open_check = StatusCheck.objects.create(
            name = 'Port Open Check for Service',
            check_plugin = StatusCheckPluginModel.objects.get(slug='port_open_check'),
            created_by = self.user,
            importance = Service.ERROR_STATUS,
            port = 123,
            address = 'ports.arachnys.com'
        )
        self.assertEqual(self.port_open_check.get_variable('port'), 123)
        self.assertEqual(StatusCheck.objects.get().port, 123)
        self.assertEqual(StatusCheck.objects.get().address, 'ports.arachnys.com')

        self.port_open_check_2 = StatusCheck.objects.create(
            name = 'Port Open Check for Service 2',
            check_plugin = StatusCheckPluginModel.objects.get(slug='port_open_check'),
            created_by = self.user,
            importance = Service.ERROR_STATUS,
            port = 456,
            address = 'ports.arachnys.com'
        )

        self.service.status_checks.add(self.port_open_check, self.port_open_check_2)
        # failing is second most recent
        self.older_result = StatusCheckResult(
            status_check=self.port_open_check,
            time=timezone.now() - timedelta(seconds=60),
            time_complete=timezone.now() - timedelta(seconds=59),
            succeeded=False
        )
        self.older_result.save()
        # Passing is most recent
        self.most_recent_result = StatusCheckResult(
            status_check=self.port_open_check,
            time=timezone.now() - timedelta(seconds=1),
            time_complete=timezone.now(),
            succeeded=True
        )
        self.most_recent_result.save()
        self.port_open_check.save()  # Will recalculate status