Skip to content

Commit 438a022

Browse files
Add source argument to create fake incident
1 parent 820b53a commit 438a022

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add source argument to create_fake_incident CLI command

docs/development/management-commands.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ To add a custom description add the `-d` flag to the command as such:
9494
9595
$ python manage.py create_fake_incident -d "Custom description"
9696
97+
To use a different source than 'argus' add the `-s` flag to the command as
98+
such:
99+
100+
.. code:: console
101+
102+
$ python manage.py create_fake_incident -s "Notargus"
103+
97104
To set the level of the incident add the `-l` flag to the command as such
98105
and choose a level between 1 and 5 (1 being the highest severity, 5 the
99106
lowest):

src/argus/dev/management/commands/create_fake_incident.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import json
33
from pathlib import Path
44

5+
from django.core.management import CommandError
56
from django.core.management.base import BaseCommand
7+
from django.core.exceptions import ValidationError
68

79
from argus.incident.constants import Level
810
from argus.incident.models import create_fake_incident
@@ -39,6 +41,12 @@ def add_arguments(self, parser):
3941
help="Set level to <level>, otherwise a random number within the correct range is used",
4042
)
4143
parser.add_argument("-t", "--tags", nargs="+", type=str, help="Add the listed tags to the incident")
44+
parser.add_argument(
45+
"-s",
46+
"--source",
47+
type=str,
48+
help="Use this source for the incident (the source needs to exist, see 'create_source' for creating one)",
49+
)
4250
parser.add_argument("--stateless", action="store_true", help="Create a stateless incident (end_time = None)")
4351
metadata_parser = parser.add_mutually_exclusive_group()
4452
metadata_parser.add_argument(
@@ -55,6 +63,7 @@ def add_arguments(self, parser):
5563
def handle(self, *args, **options):
5664
tags = options.get("tags") or []
5765
description = options.get("description") or None
66+
source = options.get("source") or None
5867
batch_size = options.get("batch_size") or 1
5968
level = options.get("level") or None
6069
stateful = False if options.get("stateless") else True
@@ -65,10 +74,14 @@ def handle(self, *args, **options):
6574
with metadata_path.open() as jsonfile:
6675
metadata = json.load(jsonfile)
6776
for i in range(batch_size):
68-
create_fake_incident(
69-
tags=tags,
70-
description=description,
71-
stateful=stateful,
72-
level=level,
73-
metadata=metadata,
74-
)
77+
try:
78+
create_fake_incident(
79+
tags=tags,
80+
description=description,
81+
source=source,
82+
stateful=stateful,
83+
level=level,
84+
metadata=metadata,
85+
)
86+
except (ValueError, ValidationError) as e:
87+
raise CommandError(str(e))

src/argus/incident/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ def get_or_create_default_instances():
3030
return (argus_user, sst, ss)
3131

3232

33-
def create_fake_incident(tags=None, description=None, stateful=True, level=None, metadata=None):
33+
def create_fake_incident(tags=None, description=None, source=None, stateful=True, level=None, metadata=None):
3434
argus_user, _, source_system = get_or_create_default_instances()
35+
if source:
36+
try:
37+
source_system = SourceSystem.objects.get(name=source)
38+
except SourceSystem.DoesNotExist:
39+
raise ValueError(f"No source with the name '{source}' exists.")
3540
end_time = INFINITY_REPR if stateful else None
3641

3742
MAX_ID = 2**32 - 1

tests/dev/test_create_fake_incident.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.core.management import CommandError, call_command
44
from django.test import TestCase
55

6+
from argus.incident.factories import SourceSystemFactory, SourceSystemTypeFactory, SourceUserFactory
67
from argus.incident.models import Incident, Tag
78
from argus.util.testing import connect_signals, disconnect_signals
89

@@ -67,6 +68,26 @@ def test_create_fake_incident_will_create_single_fake_incident_with_set_descript
6768
.exists()
6869
)
6970

71+
def test_create_fake_incident_will_create_single_fake_incident_with_set_source(self):
72+
previous_incidents_pks = [incident.id for incident in Incident.objects.all()]
73+
74+
source_name = "notargus"
75+
sst = SourceSystemTypeFactory(name="source_type")
76+
user = SourceUserFactory(username=source_name)
77+
SourceSystemFactory(name=source_name, type=sst, user=user)
78+
79+
out = self.call_command(f"--source={source_name}")
80+
81+
self.assertFalse(out)
82+
83+
self.assertTrue(
84+
Incident.objects.exclude(id__in=previous_incidents_pks).filter(source__name=source_name).exists()
85+
)
86+
87+
def test_create_fake_incident_will_raise_error_for_non_existent_source(self):
88+
with self.assertRaises(CommandError):
89+
self.call_command("--source=nonexistent")
90+
7091
def test_create_fake_incident_will_create_single_fake_incident_with_set_level(self):
7192
previous_incidents_pks = [incident.id for incident in Incident.objects.all()]
7293
level = 2

0 commit comments

Comments
 (0)