Skip to content

Commit

Permalink
[INTERNAL] Clenaup test cases #243
Browse files Browse the repository at this point in the history
- Refactor test data loader to its own `Data` class
- Move/rename some tests to better match the code they're testing
  • Loading branch information
kfdm authored Feb 28, 2020
2 parents 996c862 + 07ae706 commit 9a2b465
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 113 deletions.
2 changes: 1 addition & 1 deletion promgen/management/commands/test-alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class Command(BaseCommand):
data = tests.PromgenTest.data_json("examples", "alertmanager.json")
data = tests.Data("examples", "alertmanager.json").json()

def add_arguments(self, parser):
parser.add_argument("--shard", default="Test Shard")
Expand Down
13 changes: 8 additions & 5 deletions promgen/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""

import os
import pathlib
import warnings

import dj_database_url
Expand All @@ -25,7 +26,7 @@
from promgen.version import __version__

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = pathlib.Path(__file__).parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
Expand Down Expand Up @@ -119,10 +120,12 @@
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {'default': dj_database_url.config(
env='DATABASE_URL',
default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')
)}
DATABASES = {
"default": dj_database_url.config(
env="DATABASE_URL",
default="sqlite:///" + str(BASE_DIR / "db.sqlite3")
)
}


# Password validation
Expand Down
24 changes: 13 additions & 11 deletions promgen/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@
# These sources are released under the terms of the MIT license: see LICENSE

import json
import os

import yaml

from django.contrib.auth.models import Permission, User
from django.test import TestCase
from django.conf import settings


class PromgenTest(TestCase):
@classmethod
def data_json(cls, *args):
with open(os.path.join(os.path.dirname(__file__), *args)) as fp:
class Data:
def __init__(self, *args, test_dir=settings.BASE_DIR / "promgen" / "tests"):
self.path = test_dir.joinpath(*args)

def json(self):
with self.path.open() as fp:
return json.load(fp)

@classmethod
def data_yaml(cls, *args):
with open(os.path.join(os.path.dirname(__file__), *args)) as fp:
def yaml(self):
with self.path.open() as fp:
return yaml.safe_load(fp)

@classmethod
def data(cls, *args):
with open(os.path.join(os.path.dirname(__file__), *args)) as fp:
def raw(self):
with self.path.open() as fp:
return fp.read()


class PromgenTest(TestCase):
def assertRoute(self, response, view, status=200, msg=None):
self.assertEqual(response.status_code, status, msg)
self.assertEqual(response.resolver_match.func.__name__, view.as_view().__name__)
Expand Down
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
from django.test import override_settings
from django.urls import reverse

from promgen import models
from promgen import models, tests
from promgen.notification.email import NotificationEmail
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json')
TEST_SETTINGS = tests.Data('examples', 'promgen.yml').yaml()
TEST_ALERT = tests.Data('examples', 'alertmanager.json').json()


class EmailTest(PromgenTest):
class EmailTest(tests.PromgenTest):
@mock.patch('django.dispatch.dispatcher.Signal.send')
def setUp(self, mock_signal):
self.shard = models.Shard.objects.create(name='test.shard')
Expand Down Expand Up @@ -46,8 +45,8 @@ def test_email(self, mock_email):
content_type='application/json'
)

_SUBJECT = PromgenTest.data('notifications', 'email.subject.txt').strip()
_MESSAGE = PromgenTest.data('notifications', 'email.body.txt').strip()
_SUBJECT = tests.Data('notification', 'email.subject.txt').raw().strip()
_MESSAGE = tests.Data('notification', 'email.body.txt').raw().strip()

mock_email.assert_has_calls([
mock.call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
import json
from unittest import mock

from django.test import TestCase, override_settings
from django.test import override_settings
from django.urls import reverse

from promgen import models
from promgen import models, tests
from promgen.notification.ikasan import NotificationIkasan
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json')
TEST_SETTINGS = tests.Data('examples', 'promgen.yml').yaml()
TEST_ALERT = tests.Data('examples', 'alertmanager.json').yaml()


class IkasanTest(TestCase):
class IkasanTest(tests.PromgenTest):
@mock.patch('django.dispatch.dispatcher.Signal.send')
def setUp(self, mock_signal):
self.shard = models.Shard.objects.create(name='test.shard')
Expand All @@ -37,15 +36,15 @@ def test_ikasan(self, mock_post):
)

# Swap the status to test our resolved alert
SAMPLE = PromgenTest.data_json('examples', 'alertmanager.json')
SAMPLE = tests.Data('examples', 'alertmanager.json').json()
SAMPLE['status'] = 'resolved'
self.client.post(reverse('alert'),
data=json.dumps(SAMPLE),
content_type='application/json'
)

_MESSAGE = PromgenTest.data('notifications', 'ikasan.body.txt').strip()
_RESOLVED = PromgenTest.data('notifications', 'ikasan.resolved.txt').strip()
_MESSAGE = tests.Data('notification', 'ikasan.body.txt').raw().strip()
_RESOLVED = tests.Data('notification', 'ikasan.resolved.txt').raw().strip()

mock_post.assert_has_calls([
mock.call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
from django.test import override_settings
from django.urls import reverse

from promgen import models
from promgen import models, tests
from promgen.notification.linenotify import NotificationLineNotify
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json')
TEST_SETTINGS = tests.Data('examples', 'promgen.yml').yaml()
TEST_ALERT = tests.Data('examples', 'alertmanager.json').yaml()


class LineNotifyTest(PromgenTest):
class LineNotifyTest(tests.PromgenTest):
@mock.patch('django.dispatch.dispatcher.Signal.send')
def setUp(self, mock_signal):
self.shard = models.Shard.objects.create(name='test.shard')
Expand Down Expand Up @@ -44,7 +43,7 @@ def test_line_notify(self, mock_post):
)

# Swap the status to test our resolved alert
SAMPLE = PromgenTest.data_json('examples', 'alertmanager.json')
SAMPLE = tests.Data('examples', 'alertmanager.json').json()
SAMPLE['status'] = 'resolved'
SAMPLE['commonLabels']['service'] = self.service2.name
SAMPLE['commonLabels'].pop('project')
Expand All @@ -53,8 +52,8 @@ def test_line_notify(self, mock_post):
content_type='application/json'
)

_MESSAGE = PromgenTest.data('notifications', 'linenotify.body.txt').strip()
_RESOLVED = PromgenTest.data('notifications', 'linenotify.resolved.txt').strip()
_MESSAGE = tests.Data('notification', 'linenotify.body.txt').raw().strip()
_RESOLVED = tests.Data('notification', 'linenotify.resolved.txt').raw().strip()

mock_post.assert_has_calls([
mock.call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
from django.test import override_settings
from django.urls import reverse

from promgen import models
from promgen import models, tests
from promgen.notification.slack import NotificationSlack
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json')
TEST_SETTINGS = tests.Data('examples', 'promgen.yml').yaml()
TEST_ALERT = tests.Data('examples', 'alertmanager.json').raw()


class SlackTest(PromgenTest):
class SlackTest(tests.PromgenTest):
TestHook1 = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'
TestHook2 = 'https://hooks.slack.com/services/YYYYYYYYY/YYYYYYYYY/YYYYYYYYYYYYYYYYYYYYYYYY'

Expand Down Expand Up @@ -48,7 +47,7 @@ def test_slack(self, mock_post):
)

# Swap the status to test our resolved alert
SAMPLE = PromgenTest.data_json('examples', 'alertmanager.json')
SAMPLE = tests.Data('examples', 'alertmanager.json').json()
SAMPLE['status'] = 'resolved'
SAMPLE['commonLabels']['service'] = self.service2.name
SAMPLE['commonLabels'].pop('project')
Expand All @@ -57,8 +56,8 @@ def test_slack(self, mock_post):
content_type='application/json'
)

_MESSAGE = PromgenTest.data('notifications', 'slack.body.txt').strip()
_RESOLVED = PromgenTest.data('notifications', 'slack.resolved.txt').strip()
_MESSAGE = tests.Data('notification', 'slack.body.txt').raw().strip()
_RESOLVED = tests.Data('notification', 'slack.resolved.txt').raw().strip()

mock_post.assert_has_calls([
mock.call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
from unittest import mock

from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from django.test import override_settings
from django.urls import reverse

from promgen import models
from promgen import models, tests
from promgen.notification.email import NotificationEmail
from promgen.notification.ikasan import NotificationIkasan
from promgen.notification.user import NotificationUser
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json')
TEST_SETTINGS = tests.Data('examples', 'promgen.yml').yaml()
TEST_ALERT = tests.Data('examples', 'alertmanager.json').raw()


class UserEmailTest(TestCase):
class UserEmailTest(tests.PromgenTest):
@mock.patch('django.dispatch.dispatcher.Signal.send')
def setUp(self, mock_signal):
self.user = User.objects.create_user(id=999, username="Foo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
from django.test import override_settings
from django.urls import reverse

from promgen import models, views
from promgen import models, tests, views
from promgen.notification.webhook import NotificationWebhook
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml("examples", "promgen.yml")
TEST_ALERT = PromgenTest.data("examples", "alertmanager.json")
TEST_SETTINGS = tests.Data("examples", "promgen.yml").yaml()
TEST_ALERT = tests.Data("examples", "alertmanager.json").raw()


class WebhookTest(PromgenTest):
class WebhookTest(tests.PromgenTest):
@mock.patch("django.dispatch.dispatcher.Signal.send")
def setUp(self, mock_signal):
self.shard = models.Shard.objects.create(name="test.shard")
Expand Down Expand Up @@ -49,7 +48,7 @@ def test_webhook(self, mock_post):
self.assertEqual(mock_post.call_count, 2, "Two alerts should be sent")

# Our sample is the same as the original, with some annotations added
_SAMPLE = PromgenTest.data_json("examples", "alertmanager.json")
_SAMPLE = tests.Data("examples", "alertmanager.json").json()
_SAMPLE["commonAnnotations"]["service"] = (
"http://example.com" + self.service.get_absolute_url()
)
Expand Down
19 changes: 9 additions & 10 deletions promgen/tests/test_alert_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

from unittest import mock

import promgen.templatetags.promgen as macro
from promgen import models, prometheus, views
from promgen.tests import PromgenTest

from django.core.exceptions import ValidationError
from django.test import override_settings
from django.urls import reverse

import promgen.templatetags.promgen as macro
from promgen import models, prometheus, tests, views

_RULE_V2 = '''
groups:
- name: example.com
Expand All @@ -25,10 +24,10 @@
severity: severe
'''.lstrip().encode('utf-8')

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_SETTINGS = tests.Data('examples', 'promgen.yml').yaml()


class RuleTest(PromgenTest):
class RuleTest(tests.PromgenTest):
@mock.patch('django.dispatch.dispatcher.Signal.send')
def setUp(self, mock_signal):
self.user = self.add_force_login(id=999, username="Foo")
Expand Down Expand Up @@ -67,7 +66,7 @@ def test_import_v2(self, mock_post):
self.add_user_permissions("promgen.change_rule", "promgen.change_site")
response = self.client.post(
reverse("rule-import"),
{"rules": PromgenTest.data("examples", "import.rule.yml")},
{"rules": tests.Data("examples", "import.rule.yml").raw()},
follow=True,
)

Expand All @@ -88,7 +87,7 @@ def test_import_project_rule(self, mock_post):
reverse(
"rule-new", kwargs={"content_type": "project", "object_id": project.id}
),
{"rules": PromgenTest.data("examples", "import.rule.yml")},
{"rules": tests.Data("examples", "import.rule.yml").raw()},
follow=True,
)
self.assertRoute(response, views.ProjectDetail, status=200)
Expand All @@ -105,7 +104,7 @@ def test_import_service_rule(self, mock_post):
"rule-new",
kwargs={"content_type": "service", "object_id": self.service.id},
),
{"rules": PromgenTest.data("examples", "import.rule.yml")},
{"rules": tests.Data("examples", "import.rule.yml").raw()},
follow=True,
)
self.assertRoute(response, views.ServiceDetail, status=200)
Expand All @@ -116,7 +115,7 @@ def test_import_service_rule(self, mock_post):
@mock.patch('django.dispatch.dispatcher.Signal.send')
def test_missing_permission(self, mock_post):
self.client.post(reverse('rule-import'), {
'rules': PromgenTest.data('examples', 'import.rule.yml')
'rules': tests.Data('examples', 'import.rule.yml').raw()
})

# Should only be a single rule from our initial setup
Expand Down
24 changes: 0 additions & 24 deletions promgen/tests/test_api.py

This file was deleted.

Loading

0 comments on commit 9a2b465

Please sign in to comment.