Skip to content

Commit

Permalink
More minor renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
kfdm committed Feb 26, 2020
1 parent 687aac1 commit 5480038
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
31 changes: 0 additions & 31 deletions promgen/tests/test_api.py

This file was deleted.

71 changes: 71 additions & 0 deletions promgen/tests/test_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (c) 2018 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

from unittest import mock

import requests

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

from promgen import models, tests, views

TEST_ALERT = tests.Data("examples", "alertmanager.json").raw()
TEST_HEARTBEAT = tests.Data("examples", "heartbeat.json").raw()
TEST_SETTINGS = tests.Data("examples", "promgen.yml").yaml()


class RestAPITest(tests.PromgenTest):
@override_settings(PROMGEN=TEST_SETTINGS)
@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
def test_alert_blackhole(self):
response = self.client.post(
reverse("alert"), data=TEST_HEARTBEAT, content_type="application/json"
)
self.assertRoute(response, views.Alert, 202)
self.assertCount(models.Alert, 0, "Heartbeat alert should be deleted")

@override_settings(PROMGEN=TEST_SETTINGS)
@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
def test_alert(self):
response = self.client.post(
reverse("alert"), data=TEST_ALERT, content_type="application/json"
)
self.assertEqual(response.status_code, 202)

@mock.patch("django.dispatch.dispatcher.Signal.send")
@mock.patch("promgen.util.get")
def test_scrape(self, mock_signal, mock_get):
farm = models.Farm.objects.create(name="test_scrape")
farm.host_set.create(name="example.com")

# Uses the scrape target as the key, and the POST body that should
# result in that URL
exporters = {
"http://example.com:8000/metrics": {
"target": "#exporterresult",
"job": "foo",
"port": 8000,
"scheme": "http",
},
"https://example.com:8000/foo": {
"target": "#exporterresult",
"job": "foo",
"port": 8000,
"path": "/foo",
"scheme": "https",
},
}

for url, body in exporters.items():
response = requests.Response()
response.url = url
mock_get.return_value = response

# For each POST body, check to see that we generate and attempt to
# scrape the correct URL
response = self.client.post(
reverse("exporter-scrape", args=(farm.pk,)), body
)
self.assertRoute(response, views.ExporterScrape, 200)
self.assertEqual(mock_get.call_args[0][0], url)
38 changes: 0 additions & 38 deletions promgen/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# These sources are released under the terms of the MIT license: see LICENSE
from unittest import mock

import requests

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

Expand Down Expand Up @@ -78,42 +76,6 @@ def test_hosts(self):
response = self.client.get(reverse("host-list"))
self.assertRoute(response, views.HostList, 200)

@mock.patch("promgen.util.get")
def test_scrape(self, mock_get):
farm = models.Farm.objects.create(name="test_scrape")
farm.host_set.create(name="example.com")

# Uses the scrape target as the key, and the POST body that should
# result in that URL
exporters = {
"http://example.com:8000/metrics": {
"target": "#exporterresult",
"job": "foo",
"port": 8000,
"scheme": "http",
},
"https://example.com:8000/foo": {
"target": "#exporterresult",
"job": "foo",
"port": 8000,
"path": "/foo",
"scheme": "https",
},
}

for url, body in exporters.items():
response = requests.Response()
response.url = url
mock_get.return_value = response

# For each POST body, check to see that we generate and attempt to
# scrape the correct URL
response = self.client.post(
reverse("exporter-scrape", args=(farm.pk,)), body
)
self.assertRoute(response, views.ExporterScrape, 200)
self.assertEqual(mock_get.call_args[0][0], url)

def test_failed_permission(self):
# Test for redirect
for request in [{"viewname": "rule-new", "args": ("site", 1)}]:
Expand Down

0 comments on commit 5480038

Please sign in to comment.