Ensured that image verified event is sent only once per owner for all his images
This commit is contained in:
@@ -16,6 +16,7 @@ import datetime
|
|||||||
import copy
|
import copy
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
from stacktach import datetime_to_decimal as dt
|
from stacktach import datetime_to_decimal as dt
|
||||||
|
|
||||||
@@ -500,6 +501,15 @@ class ImageExists(models.Model):
|
|||||||
self.fail_reason = reason
|
self.fail_reason = reason
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def are_all_exists_for_owner_verified(owner, audit_period_beginning,
|
||||||
|
audit_period_ending):
|
||||||
|
return ImageExists.objects.filter(
|
||||||
|
~Q(status=ImageExists.VERIFIED),
|
||||||
|
audit_period_beginning=audit_period_beginning,
|
||||||
|
audit_period_ending=audit_period_ending,
|
||||||
|
owner=owner).count() == 0
|
||||||
|
|
||||||
|
|
||||||
def get_model_fields(model):
|
def get_model_fields(model):
|
||||||
return model._meta.fields
|
return model._meta.fields
|
||||||
|
@@ -504,6 +504,12 @@ class GlanceVerifierTestCase(StacktachBaseTestCase):
|
|||||||
]
|
]
|
||||||
exist_str = json.dumps(exist_dict)
|
exist_str = json.dumps(exist_dict)
|
||||||
exist.raw.json = exist_str
|
exist.raw.json = exist_str
|
||||||
|
exist.audit_period_beginning = datetime(2013, 10, 10)
|
||||||
|
exist.audit_period_ending = datetime(2013, 10, 10, 23, 59, 59)
|
||||||
|
exist.owner = "1"
|
||||||
|
models.ImageExists.are_all_exists_for_owner_verified(
|
||||||
|
exist.owner, exist.audit_period_beginning,
|
||||||
|
exist.audit_period_ending).AndReturn(True)
|
||||||
self.mox.StubOutWithMock(uuid, 'uuid4')
|
self.mox.StubOutWithMock(uuid, 'uuid4')
|
||||||
uuid.uuid4().AndReturn('some_other_uuid')
|
uuid.uuid4().AndReturn('some_other_uuid')
|
||||||
self.mox.StubOutWithMock(kombu.pools, 'producers')
|
self.mox.StubOutWithMock(kombu.pools, 'producers')
|
||||||
@@ -541,6 +547,12 @@ class GlanceVerifierTestCase(StacktachBaseTestCase):
|
|||||||
]
|
]
|
||||||
exist_str = json.dumps(exist_dict)
|
exist_str = json.dumps(exist_dict)
|
||||||
exist.raw.json = exist_str
|
exist.raw.json = exist_str
|
||||||
|
exist.audit_period_beginning = datetime(2013, 10, 10)
|
||||||
|
exist.audit_period_ending = datetime(2013, 10, 10, 23, 59, 59)
|
||||||
|
exist.owner = "1"
|
||||||
|
models.ImageExists.are_all_exists_for_owner_verified(
|
||||||
|
exist.owner, exist.audit_period_beginning,
|
||||||
|
exist.audit_period_ending).AndReturn(True)
|
||||||
self.mox.StubOutWithMock(kombu.pools, 'producers')
|
self.mox.StubOutWithMock(kombu.pools, 'producers')
|
||||||
self.mox.StubOutWithMock(kombu.common, 'maybe_declare')
|
self.mox.StubOutWithMock(kombu.common, 'maybe_declare')
|
||||||
producer = self.mox.CreateMockAnything()
|
producer = self.mox.CreateMockAnything()
|
||||||
@@ -562,3 +574,28 @@ class GlanceVerifierTestCase(StacktachBaseTestCase):
|
|||||||
connection)
|
connection)
|
||||||
self.mox.VerifyAll()
|
self.mox.VerifyAll()
|
||||||
|
|
||||||
|
def test_do_not_send_verified_if_all_exists_for_owner_not_verified(self):
|
||||||
|
connection = self.mox.CreateMockAnything()
|
||||||
|
exchange = self.mox.CreateMockAnything()
|
||||||
|
exist = self.mox.CreateMockAnything()
|
||||||
|
exist.raw = self.mox.CreateMockAnything()
|
||||||
|
exist_dict = [
|
||||||
|
'monitor.info',
|
||||||
|
{
|
||||||
|
'event_type': 'test',
|
||||||
|
'message_id': 'some_uuid'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
exist_str = json.dumps(exist_dict)
|
||||||
|
exist.raw.json = exist_str
|
||||||
|
exist.audit_period_beginning = datetime(2013, 10, 10)
|
||||||
|
exist.audit_period_ending = datetime(2013, 10, 10, 23, 59, 59)
|
||||||
|
exist.owner = "1"
|
||||||
|
models.ImageExists.are_all_exists_for_owner_verified(
|
||||||
|
exist.owner, exist.audit_period_beginning,
|
||||||
|
exist.audit_period_ending).AndReturn(False)
|
||||||
|
self.mox.ReplayAll()
|
||||||
|
|
||||||
|
self.glance_verifier.send_verified_notification(
|
||||||
|
exist, exchange, connection)
|
||||||
|
self.mox.VerifyAll()
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
from django.db.models import Q
|
||||||
import mox
|
import mox
|
||||||
from stacktach.models import RawData, GlanceRawData, GenericRawData, ImageDeletes, InstanceExists, ImageExists
|
from stacktach.models import RawData, GlanceRawData, GenericRawData, ImageDeletes, InstanceExists, ImageExists
|
||||||
from tests.unit.utils import IMAGE_UUID_1
|
from tests.unit.utils import IMAGE_UUID_1
|
||||||
@@ -94,6 +95,44 @@ class ImageExistsTestCase(unittest.TestCase):
|
|||||||
self.mox.VerifyAll()
|
self.mox.VerifyAll()
|
||||||
self.assertEqual(results, [1, 2])
|
self.assertEqual(results, [1, 2])
|
||||||
|
|
||||||
|
def test_return_true_if_all_exists_for_owner_are_verified(self):
|
||||||
|
owner = "1"
|
||||||
|
audit_period_beginning = datetime(2013, 10, 10)
|
||||||
|
audit_period_ending = datetime(2013, 10, 10, 23, 59, 59)
|
||||||
|
|
||||||
|
results = self.mox.CreateMockAnything()
|
||||||
|
results.count().AndReturn(0)
|
||||||
|
|
||||||
|
self.mox.StubOutWithMock(ImageExists.objects, 'filter')
|
||||||
|
ImageExists.objects.filter(
|
||||||
|
mox.IgnoreArg(), owner=owner,
|
||||||
|
audit_period_beginning=audit_period_beginning,
|
||||||
|
audit_period_ending=audit_period_ending).AndReturn(results)
|
||||||
|
self.mox.ReplayAll()
|
||||||
|
|
||||||
|
self.assertTrue(models.ImageExists.are_all_exists_for_owner_verified(
|
||||||
|
owner, audit_period_beginning, audit_period_ending))
|
||||||
|
self.mox.VerifyAll()
|
||||||
|
|
||||||
|
def test_return_false_if_all_exists_for_owner_are_verified(self):
|
||||||
|
owner = "1"
|
||||||
|
audit_period_beginning = datetime(2013, 10, 10)
|
||||||
|
audit_period_ending = datetime(2013, 10, 10, 23, 59, 59)
|
||||||
|
results = self.mox.CreateMockAnything()
|
||||||
|
results.count().AndReturn(1)
|
||||||
|
|
||||||
|
self.mox.StubOutWithMock(ImageExists.objects, 'filter')
|
||||||
|
ImageExists.objects.filter(
|
||||||
|
mox.IgnoreArg(), owner=owner,
|
||||||
|
audit_period_beginning=audit_period_beginning,
|
||||||
|
audit_period_ending=audit_period_ending).AndReturn(results)
|
||||||
|
self.mox.ReplayAll()
|
||||||
|
|
||||||
|
self.assertFalse(ImageExists.are_all_exists_for_owner_verified(
|
||||||
|
owner=owner, audit_period_beginning=audit_period_beginning,
|
||||||
|
audit_period_ending=audit_period_ending))
|
||||||
|
self.mox.VerifyAll()
|
||||||
|
|
||||||
|
|
||||||
class InstanceExistsTestCase(unittest.TestCase):
|
class InstanceExistsTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@@ -1043,6 +1043,30 @@ class NovaVerifierTestCase(StacktachBaseTestCase):
|
|||||||
self.verifier.verify_for_range(when_max)
|
self.verifier.verify_for_range(when_max)
|
||||||
self.mox.VerifyAll()
|
self.mox.VerifyAll()
|
||||||
|
|
||||||
|
def test_verify_for_range_without_callback(self):
|
||||||
|
when_max = datetime.datetime.utcnow()
|
||||||
|
results = self.mox.CreateMockAnything()
|
||||||
|
models.InstanceExists.PENDING = 'pending'
|
||||||
|
models.InstanceExists.VERIFYING = 'verifying'
|
||||||
|
models.InstanceExists.find(
|
||||||
|
ending_max=when_max, status='pending').AndReturn(results)
|
||||||
|
results.count().AndReturn(2)
|
||||||
|
exist1 = self.mox.CreateMockAnything()
|
||||||
|
exist2 = self.mox.CreateMockAnything()
|
||||||
|
results.__getslice__(0, 1000).AndReturn(results)
|
||||||
|
results.__iter__().AndReturn([exist1, exist2].__iter__())
|
||||||
|
exist1.update_status('verifying')
|
||||||
|
exist2.update_status('verifying')
|
||||||
|
exist1.save()
|
||||||
|
exist2.save()
|
||||||
|
self.pool.apply_async(nova_verifier._verify, args=(exist1, 'all'),
|
||||||
|
callback=None)
|
||||||
|
self.pool.apply_async(nova_verifier._verify, args=(exist2, 'all'),
|
||||||
|
callback=None)
|
||||||
|
self.mox.ReplayAll()
|
||||||
|
self.verifier.verify_for_range(when_max)
|
||||||
|
self.mox.VerifyAll()
|
||||||
|
|
||||||
def test_verify_for_range_with_callback(self):
|
def test_verify_for_range_with_callback(self):
|
||||||
callback = self.mox.CreateMockAnything()
|
callback = self.mox.CreateMockAnything()
|
||||||
when_max = datetime.datetime.utcnow()
|
when_max = datetime.datetime.utcnow()
|
||||||
|
@@ -166,6 +166,10 @@ class GlanceVerifier(Verifier):
|
|||||||
|
|
||||||
def send_verified_notification(self, exist, connection, exchange,
|
def send_verified_notification(self, exist, connection, exchange,
|
||||||
routing_keys=None):
|
routing_keys=None):
|
||||||
|
if not models.ImageExists.are_all_exists_for_owner_verified(
|
||||||
|
exist.owner, exist.audit_period_beginning,
|
||||||
|
exist.audit_period_ending):
|
||||||
|
return
|
||||||
body = exist.raw.json
|
body = exist.raw.json
|
||||||
json_body = json.loads(body)
|
json_body = json.loads(body)
|
||||||
json_body[1]['event_type'] = self.config.glance_event_type()
|
json_body[1]['event_type'] = self.config.glance_event_type()
|
||||||
|
Reference in New Issue
Block a user