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

Renderer tests #534

Merged
merged 2 commits into from
Jul 25, 2024
Merged
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
13 changes: 13 additions & 0 deletions promgen/fixtures/testcases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@
service: 1
shard: 1
farm: 1
- model: promgen.exporter
pk: 1
fields:
project: 1
job: node
port: 9100
- model: promgen.exporter
pk: 2
fields:
project: 2
job: node
port: 9100
enabled: false
7 changes: 7 additions & 0 deletions promgen/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def rules(self, request):
headers={"Content-Disposition": "attachment; filename=alert.rule.yml"},
)

@action(detail=False, methods=["get"], renderer_classes=[renderers.renderers.JSONRenderer])
def targets(self, request):
return HttpResponse(
prometheus.render_config(),
content_type="application/json",
)


class ShardViewSet(viewsets.ModelViewSet):
queryset = models.Shard.objects.all()
Expand Down
2 changes: 1 addition & 1 deletion promgen/templates/promgen/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<li role="separator" class="divider"></li>

<li><a href="{% url 'api:api-root' %}">API</a></li>
<li><a href="{% url 'config-targets' %}">Export Targets</a></li>
<li><a href="{% url 'api:all-targets' %}">Export Targets</a></li>
<li><a href="{% url 'api:all-rules' %}">Export Rules</a></li>
<li><a href="{% url 'config-urls' %}">Export URL</a></li>
</ul>
Expand Down
11 changes: 11 additions & 0 deletions promgen/tests/examples/export.rule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
groups:
- name: promgen.example.com
rules:
- alert: example-rule
annotations:
rule: http://promgen.example.com/rule/1
summary: Example rule summary
expr: up==1
for: 1s
labels:
severity: high
16 changes: 16 additions & 0 deletions promgen/tests/examples/export.targets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"labels": {
"__farm_source": "promgen",
"__scheme__": "http",
"__shard": "test-shard",
"farm": "test-farm",
"job": "node",
"project": "test-project",
"service": "test-service"
},
"targets": [
"example.com:9100"
]
}
]
6 changes: 3 additions & 3 deletions promgen/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def test_register_job(self, mock_signal):
management.call_command("register-job", "test-project", "example", 1234)

# Ensure the jobs we expect exist
self.assertCount(models.Exporter, 1)
self.assertCount(models.Exporter, 3, "Import a new exporter")

# Registering the same job again shouldn't change our count
management.call_command("register-job", "test-project", "example", 1234)
self.assertCount(models.Exporter, 1)
self.assertCount(models.Exporter, 3, "Import additional exporter")

# But registering a new one will
management.call_command("register-job", "test-project", "example", 4321)
self.assertCount(models.Exporter, 2)
self.assertCount(models.Exporter, 4, 'Import additional exporter')

@mock.patch("promgen.signals._trigger_write_config")
def test_register_host(self, mock_signal):
Expand Down
25 changes: 25 additions & 0 deletions promgen/tests/test_renderers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2024 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

from django.urls import reverse
from yaml import safe_load

from promgen import tests


class RendererTests(tests.PromgenTest):
fixtures = ["testcases.yaml", "extras.yaml"]

def test_global_rule(self):
expected = tests.Data("examples", "export.rule.yml").yaml()
response = self.client.get(reverse("api:all-rules"))
self.assertEqual(response.status_code, 200)
# The test client does not have a shortcut to decode yaml
data = safe_load(response.content)
self.assertEqual(data, expected)

def test_global_targets(self):
expected = tests.Data("examples", "export.targets.json").json()
response = self.client.get(reverse("api:all-targets"))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), expected)
4 changes: 2 additions & 2 deletions promgen/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_import(self, mock_write, mock_reload):
self.assertRoute(response, views.Import, 302, "Redirect to imported object")
self.assertCount(models.Service, 3, "Import one service (Fixture has two services)")
self.assertCount(models.Project, 4, "Import two projects")
self.assertCount(models.Exporter, 2, "Import two exporters")
self.assertCount(models.Exporter, 4, "Import two more exporters")
self.assertCount(models.Host, 3, "Import three hosts")

@override_settings(PROMGEN=TEST_SETTINGS)
Expand All @@ -50,7 +50,7 @@ def test_replace(self, mock_write, mock_reload):

self.assertCount(models.Service, 3, "Import one service (Fixture has two services)")
self.assertCount(models.Project, 4, "Import two projects (Fixture has 2 projectsa)")
self.assertCount(models.Exporter, 2, "Import two exporters")
self.assertCount(models.Exporter, 4, "Import two more exporters")
self.assertCount(
models.Farm, 4, "Original two farms and one new farm (fixture has one farm)"
)
Expand Down