Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape \x00 in raw_data to avoid psycopg2 error #655

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2019-01-10 22:33
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('cabotapp', '0007_statuscheckresult_consecutive_failures'),
]

operations = [
migrations.AddField(
model_name='statuscheck',
name='text_match_expected_result',
field=models.BooleanField(default=True, help_text=b'Text match expected result positive or negative. (default True)'),
),
]
14 changes: 11 additions & 3 deletions cabot/cabotapp/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ class StatusCheck(PolymorphicModel):
null=True,
help_text='Regex to match against source of page.',
)
text_match_expected_result = models.BooleanField(
default=True,
help_text='Text match expected result positive or negative. (default True)',
)
status_code = models.TextField(
default=200,
null=True,
Expand Down Expand Up @@ -783,8 +787,12 @@ def _run(self):
result.succeeded = False
result.raw_data = resp.text
elif self.text_match:
if not self._check_content_pattern(self.text_match, resp.text):
result.error = u'Failed to find match regex /%s/ in response body' % self.text_match
matched = self._check_content_pattern(self.text_match, resp.text)
if self.text_match_expected_result != bool(matched):
if self.text_match_expected_result:
result.error = u'Failed to find match regex /%s/ in response body' % self.text_match
else:
result.error = u'Found unwanted regex /%s/ in response body' % self.text_match
result.raw_data = resp.text
result.succeeded = False
else:
Expand Down Expand Up @@ -851,7 +859,7 @@ def short_error(self):

def save(self, *args, **kwargs):
if isinstance(self.raw_data, basestring):
self.raw_data = self.raw_data[:RAW_DATA_LIMIT]
self.raw_data = self.raw_data[:RAW_DATA_LIMIT].replace('\x00', '\\x00')
return super(StatusCheckResult, self).save(*args, **kwargs)

class AlertAcknowledgement(models.Model):
Expand Down
4 changes: 4 additions & 0 deletions cabot/cabotapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class Meta:
'username',
'password',
'text_match',
'text_match_expected_result',
'status_code',
'timeout',
'verify_ssl_certificate',
Expand All @@ -266,6 +267,9 @@ class Meta:
'style': 'width: 100%',
'placeholder': '[Aa]rachnys\s+[Rr]ules',
}),
'text_match_expected_result': forms.RadioSelect(choices=(
(True, 'Positive',),
(False, 'Negative',))),
'status_code': forms.TextInput(attrs={
'style': 'width: 20%',
'placeholder': '200',
Expand Down