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

ARGO-4416 During bulk delete of metric templates data is not invalidated properly #613

Open
wants to merge 10 commits into
base: devel
Choose a base branch
from
7 changes: 5 additions & 2 deletions poem/Poem/api/internal_views/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ def get(self, request, name=None):
if name:
metrics = poem_models.Metric.objects.filter(name=name)
if metrics.count() == 0:
raise NotFound(status=404,
detail='Metric not found')
raise NotFound(status=404, detail='Metric not found')
else:
metrics = poem_models.Metric.objects.all()

profiles4metrics = get_metrics_in_profiles(request.tenant)

results = []
for metric in metrics:
if metric.probeversion:
Expand Down Expand Up @@ -88,6 +89,8 @@ def get(self, request, name=None):
name=metric.name,
mtype=mt.mtype.name,
tags=[tag.name for tag in mt.tags.all()],
profiles=profiles4metrics[metric.name]
if metric.name in profiles4metrics else [],
probeversion=metric.probeversion if metric.probeversion else "",
group=group,
description=mt.description,
Expand Down
22 changes: 15 additions & 7 deletions poem/Poem/api/internal_views/metrictemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ def get(self, request, name=None):
return Response({
"id": tag.id,
"name": tag.name,
"metrics": sorted([metric.name for metric in metrics])
"metrics": sorted(
[{"name": metric.name} for metric in metrics],
key=lambda m: m["name"]
)
})

except admin_models.MetricTags.DoesNotExist:
Expand All @@ -777,7 +780,10 @@ def get(self, request, name=None):
data.append({
"id": tag.id,
"name": tag.name,
"metrics": sorted([metric.name for metric in metrics])
"metrics": sorted(
[{"name": metric.name} for metric in metrics],
key=lambda m: m["name"]
)
})

return Response(data)
Expand Down Expand Up @@ -831,14 +837,16 @@ def post(self, request):

if len(missing_metrics) > 0:
if len(missing_metrics) == 1:
warn_msg = \
f"{warn_msg}Metric {list(missing_metrics)[0]} " \
warn_msg = (
f"{warn_msg}Metric {list(missing_metrics)[0]} "
f"does not exist."
)

else:
warn_msg = "{}Metrics {} do not exist.".format(
warn_msg,
", ".join(sorted(list(missing_metrics)))
warn_msg = (
f"{warn_msg}Metrics "
f"{', '.join(sorted(list(missing_metrics)))} "
f"do not exist."
)

if warn_msg:
Expand Down
36 changes: 24 additions & 12 deletions poem/Poem/api/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3587,10 +3587,13 @@ def test_sync_active_metrics(self, mock_get_metrics):
self.assertEqual(err, [])
self.assertEqual(unavailable, [])
self.assertEqual(
sorted(deleted),
["eu.egi.cloud.OpenStack-Swift", "test.Metric-Template"]
sorted(deleted), [
"argo.poem-tools.check",
"eu.egi.cloud.OpenStack-Swift",
"test.Metric-Template"
]
)
self.assertEqual(len(poem_models.Metric.objects.all()), 5)
self.assertEqual(len(poem_models.Metric.objects.all()), 4)
self.assertRaises(
poem_models.Metric.DoesNotExist,
poem_models.Metric.objects.get,
Expand Down Expand Up @@ -3635,10 +3638,13 @@ def test_sync_passive_metrics(self, mock_get_metrics):
self.assertEqual(err, [])
self.assertEqual(unavailable, [])
self.assertEqual(
sorted(deleted),
["eu.egi.cloud.OpenStack-Swift", "test.Metric-Template"]
sorted(deleted), [
"argo.poem-tools.check",
"eu.egi.cloud.OpenStack-Swift",
"test.Metric-Template"
]
)
self.assertEqual(len(poem_models.Metric.objects.all()), 5)
self.assertEqual(len(poem_models.Metric.objects.all()), 4)
self.assertRaises(
poem_models.Metric.DoesNotExist,
poem_models.Metric.objects.get,
Expand Down Expand Up @@ -3683,10 +3689,13 @@ def test_sync_metrics_with_warning(self, mock_get_metrics):
self.assertEqual(err, [])
self.assertEqual(unavailable, [])
self.assertEqual(
sorted(deleted),
["eu.egi.cloud.OpenStack-Swift", "test.Metric-Template"]
sorted(deleted), [
"argo.poem-tools.check",
"eu.egi.cloud.OpenStack-Swift",
"test.Metric-Template"
]
)
self.assertEqual(len(poem_models.Metric.objects.all()), 5)
self.assertEqual(len(poem_models.Metric.objects.all()), 4)
self.assertRaises(
poem_models.Metric.DoesNotExist,
poem_models.Metric.objects.get,
Expand Down Expand Up @@ -3731,10 +3740,13 @@ def test_sync_metrics_if_version_unavailable(self, mock_get_metrics):
self.assertEqual(err, [])
self.assertEqual(unavailable, ["test.MetricTemplate"])
self.assertEqual(
sorted(deleted),
["eu.egi.cloud.OpenStack-Swift", "test.Metric-Template"]
sorted(deleted), [
"argo.poem-tools.check",
"eu.egi.cloud.OpenStack-Swift",
"test.Metric-Template"
]
)
self.assertEqual(len(poem_models.Metric.objects.all()), 4)
self.assertEqual(len(poem_models.Metric.objects.all()), 3)
self.assertRaises(
poem_models.Metric.DoesNotExist,
poem_models.Metric.objects.get,
Expand Down
19 changes: 17 additions & 2 deletions poem/Poem/api/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ def mock_db():
content_type=ct
)

profiles4metrics = {
"argo.AMS-Check": ["ARGO_MON", "ARGO_MON_CRITICAL"],
"argo.AMSPublisher-Check": ["ARGO_MON_INTERNAL"]
}


class ListAllMetricsAPIViewTests(TenantTestCase):
@factory.django.mute_signals(pre_save)
Expand Down Expand Up @@ -424,8 +429,11 @@ def setUp(self):
name="org.apel.APEL-Pub"
)

def test_get_metric_list(self):
@patch("Poem.api.internal_views.metrics.get_metrics_in_profiles")
def test_get_metric_list(self, mock_profiles4metrics):
mock_profiles4metrics.return_value = profiles4metrics
request = self.factory.get(self.url)
request.tenant = self.tenant
force_authenticate(request, user=self.user)
response = self.view(request)
self.assertEqual(
Expand All @@ -436,6 +444,7 @@ def test_get_metric_list(self):
'name': 'argo.AMS-Check',
'mtype': 'Active',
'tags': ['test_tag1', 'test_tag2'],
"profiles": ["ARGO_MON", "ARGO_MON_CRITICAL"],
'probeversion': 'ams-probe (0.1.7)',
'group': 'EGI',
'description': 'Description of argo.AMS-Check',
Expand Down Expand Up @@ -490,6 +499,7 @@ def test_get_metric_list(self):
'name': 'argo.AMSPublisher-Check',
'mtype': 'Active',
'tags': ['test_tag1'],
"profiles": ["ARGO_MON_INTERNAL"],
'probeversion': 'ams-publisher-probe (0.1.7)',
'group': 'EUDAT',
'description': '',
Expand Down Expand Up @@ -538,6 +548,7 @@ def test_get_metric_list(self):
'name': 'org.apel.APEL-Pub',
'mtype': 'Passive',
'tags': [],
"profiles": [],
'probeversion': '',
'group': 'EGI',
'description': '',
Expand All @@ -563,8 +574,11 @@ def test_get_metric_list(self):
]
)

def test_get_metric_by_name(self):
@patch("Poem.api.internal_views.metrics.get_metrics_in_profiles")
def test_get_metric_by_name(self, mock_profiles4metrics):
mock_profiles4metrics.return_value = profiles4metrics
request = self.factory.get(self.url + 'argo.AMS-Check')
request.tenant = self.tenant
force_authenticate(request, user=self.user)
response = self.view(request, 'argo.AMS-Check')
self.assertEqual(
Expand All @@ -574,6 +588,7 @@ def test_get_metric_by_name(self):
'name': 'argo.AMS-Check',
'mtype': 'Active',
'tags': ['test_tag1', 'test_tag2'],
"profiles": ["ARGO_MON", "ARGO_MON_CRITICAL"],
'probeversion': 'ams-probe (0.1.7)',
'group': 'EGI',
'description': 'Description of argo.AMS-Check',
Expand Down
72 changes: 56 additions & 16 deletions poem/Poem/api/tests/test_metrictemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10022,17 +10022,25 @@ def test_get_metric_tags_admin_superuser(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [{
"name": "argo.AMS-Check"
}]
},
{
"id": self.tag3.id,
"name": "test_tag1",
"metrics": ["argo.AMS-Check", "argo.EGI-Connectors-Check"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "argo.EGI-Connectors-Check"}
]
},
{
"id": self.tag4.id,
"name": "test_tag2",
"metrics": ["argo.AMS-Check", "org.apel.APEL-Pub"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "org.apel.APEL-Pub"}
]
}
]
)
Expand All @@ -10052,17 +10060,25 @@ def test_get_metric_tags_admin_regular_user(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [
{"name": "argo.AMS-Check"}
]
},
{
"id": self.tag3.id,
"name": "test_tag1",
"metrics": ["argo.AMS-Check", "argo.EGI-Connectors-Check"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "argo.EGI-Connectors-Check"}
]
},
{
"id": self.tag4.id,
"name": "test_tag2",
"metrics": ["argo.AMS-Check", "org.apel.APEL-Pub"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "org.apel.APEL-Pub"}
]
}
]
)
Expand All @@ -10082,17 +10098,25 @@ def test_get_metric_tags_tenant_superuser(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [
{"name": "argo.AMS-Check"}
]
},
{
"id": self.tag3.id,
"name": "test_tag1",
"metrics": ["argo.AMS-Check", "argo.EGI-Connectors-Check"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "argo.EGI-Connectors-Check"}
]
},
{
"id": self.tag4.id,
"name": "test_tag2",
"metrics": ["argo.AMS-Check", "org.apel.APEL-Pub"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "org.apel.APEL-Pub"}
]
}
]
)
Expand All @@ -10112,17 +10136,25 @@ def test_get_metric_tags_tenant_regular_user(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [
{"name": "argo.AMS-Check"}
]
},
{
"id": self.tag3.id,
"name": "test_tag1",
"metrics": ["argo.AMS-Check", "argo.EGI-Connectors-Check"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "argo.EGI-Connectors-Check"}
]
},
{
"id": self.tag4.id,
"name": "test_tag2",
"metrics": ["argo.AMS-Check", "org.apel.APEL-Pub"]
"metrics": [
{"name": "argo.AMS-Check"},
{"name": "org.apel.APEL-Pub"}
]
}
]
)
Expand All @@ -10141,7 +10173,9 @@ def test_get_metric_tag_by_name_admin_superuser(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [
{"name": "argo.AMS-Check"}
]
}
)

Expand All @@ -10154,7 +10188,9 @@ def test_get_metric_tag_by_name_admin_regular_user(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [
{"name": "argo.AMS-Check"}
]
}
)

Expand All @@ -10167,7 +10203,9 @@ def test_get_metric_tag_by_name_tenant_superuser(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [
{"name": "argo.AMS-Check"}
]
}
)

Expand All @@ -10180,7 +10218,9 @@ def test_get_metric_tag_by_name_tenant_regular_user(self):
{
"id": self.tag1.id,
"name": "internal",
"metrics": ["argo.AMS-Check"]
"metrics": [
{"name": "argo.AMS-Check"}
]
}
)

Expand Down
Loading