From e294145f66290371dc6f39233d956a872a465c96 Mon Sep 17 00:00:00 2001 From: Manali Latkar Date: Mon, 26 Aug 2013 12:09:55 +0530 Subject: [PATCH] Fixed type check for instance_type_id and rax_options for instanceexists and size for glanceexists changed the order of the validity check added validation level as flag Added empty check for nova image properties --- etc/sample_stacktach_verifier_config.json | 1 + tests/unit/test_glance_verifier.py | 4 +- tests/unit/test_nova_verifier.py | 339 ++++++++++++++-------- tests/unit/utils.py | 1 + verifier/base_verifier.py | 12 +- verifier/config.py | 4 + verifier/glance_verifier.py | 3 +- verifier/nova_verifier.py | 46 ++- 8 files changed, 277 insertions(+), 133 deletions(-) diff --git a/etc/sample_stacktach_verifier_config.json b/etc/sample_stacktach_verifier_config.json index 54b67fa..f18e901 100644 --- a/etc/sample_stacktach_verifier_config.json +++ b/etc/sample_stacktach_verifier_config.json @@ -4,6 +4,7 @@ "settle_units": "minutes", "pool_size": 2, "enable_notifications": true, + "validation_level": "all", "rabbit": { "durable_queue": false, "host": "10.0.0.1", diff --git a/tests/unit/test_glance_verifier.py b/tests/unit/test_glance_verifier.py index f96b375..0bc4e1f 100644 --- a/tests/unit/test_glance_verifier.py +++ b/tests/unit/test_glance_verifier.py @@ -379,7 +379,7 @@ class GlanceVerifierTestCase(StacktachBaseTestCase): def test_should_verify_owner_is_of_type_hex(self): exist = self.mox.CreateMockAnything() exist.id = 23 - exist.size = 1234 + exist.size = 1234L exist.created_at = decimal.Decimal('5.1') exist.uuid = "58fb036d-5ef8-47a8-b503-7571276c400a" exist.owner = "3762854cd6f6435998188d5120e4c271,kl" @@ -396,7 +396,7 @@ class GlanceVerifierTestCase(StacktachBaseTestCase): def test_should_verify_correctly_for_all_non_null_and_valid_types(self): exist = self.mox.CreateMockAnything() exist.id = 23 - exist.size = 983040 + exist.size = 983040L exist.created_at = decimal.Decimal('5.1') exist.uuid = "58fb036d-5ef8-47a8-b503-7571276c400a" exist.owner = "3762854cd6f6435998188d5120e4c271" diff --git a/tests/unit/test_nova_verifier.py b/tests/unit/test_nova_verifier.py index b6b080b..63f2506 100644 --- a/tests/unit/test_nova_verifier.py +++ b/tests/unit/test_nova_verifier.py @@ -290,12 +290,11 @@ class NovaVerifierTestCase(StacktachBaseTestCase): results.count().AndReturn(0) self.mox.ReplayAll() - try: + with self.assertRaises(NotFound) as nf: nova_verifier._verify_for_launch(exist) - self.fail() - except NotFound, nf: - self.assertEqual(nf.object_type, 'InstanceUsage') - self.assertEqual(nf.search_params, {'instance': INSTANCE_ID_1}) + exception = nf.exception + self.assertEqual(exception.object_type, 'InstanceUsage') + self.assertEqual(exception.search_params, {'instance': INSTANCE_ID_1}) self.mox.VerifyAll() @@ -315,14 +314,13 @@ class NovaVerifierTestCase(StacktachBaseTestCase): results.count().AndReturn(2) self.mox.ReplayAll() - try: + with self.assertRaises(AmbiguousResults) as ar: nova_verifier._verify_for_launch(exist) - self.fail() - except AmbiguousResults, nf: - self.assertEqual(nf.object_type, 'InstanceUsage') - search_params = {'instance': INSTANCE_ID_1, - 'launched_at': decimal.Decimal('1.1')} - self.assertEqual(nf.search_params, search_params) + exception = ar.exception + self.assertEqual(exception.object_type, 'InstanceUsage') + search_params = {'instance': INSTANCE_ID_1, + 'launched_at': decimal.Decimal('1.1')} + self.assertEqual(exception.search_params, search_params) self.mox.VerifyAll() @@ -332,12 +330,12 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.id = 23 self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except NullFieldException as nf: - self.assertEqual(nf.field_name, 'tenant') - self.assertEqual(nf.reason, "tenant field was null for exist id 23") + with self.assertRaises(NullFieldException) as nf: + nova_verifier._verify_validity(exist, 'all') + exception = nf.exception + self.assertEqual(exception.field_name, 'tenant') + self.assertEqual(exception.reason, + "tenant field was null for exist id 23") self.mox.VerifyAll() def test_should_verify_that_launched_at_in_exist_is_not_null(self): @@ -347,12 +345,12 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.launched_at = None self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except NullFieldException as nf: - self.assertEqual(nf.field_name, 'launched_at') - self.assertEqual(nf.reason, "launched_at field was null for exist id 23") + with self.assertRaises(NullFieldException) as nf: + nova_verifier._verify_validity(exist, 'all') + exception = nf.exception + self.assertEqual(exception.field_name, 'launched_at') + self.assertEqual(exception.reason, + "launched_at field was null for exist id 23") self.mox.VerifyAll() def test_should_verify_that_instance_type_id_in_exist_is_not_null(self): @@ -363,12 +361,12 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.instance_type_id = None self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except NullFieldException as nf: - self.assertEqual(nf.field_name, 'instance_type_id') - self.assertEqual(nf.reason, "instance_type_id field was null for exist id 23") + with self.assertRaises(NullFieldException) as nf: + nova_verifier._verify_validity(exist, 'all') + exception = nf.exception + self.assertEqual(exception.field_name, 'instance_type_id') + self.assertEqual(exception.reason, + "instance_type_id field was null for exist id 23") self.mox.VerifyAll() def test_should_verify_tenant_id_is_of_type_hex(self): @@ -379,12 +377,13 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.instance_type_id = 2 self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'tenant') - self.assertEqual(wt.reason, "{ tenant : tenant } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'tenant') + self.assertEqual( + exception.reason, + "{ tenant : tenant } of incorrect type for exist id 23") self.mox.VerifyAll() def test_should_verify_flavor_is_of_type_integer(self): @@ -395,12 +394,13 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.instance_type_id = 'flavor' self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'instance_type_id') - self.assertEqual(wt.reason, "{ instance_type_id : flavor } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'instance_type_id') + self.assertEqual( + exception.reason, + "{ instance_type_id : flavor } of incorrect type for exist id 23") self.mox.VerifyAll() def test_should_verify_launched_at_is_of_type_decimal(self): @@ -411,12 +411,13 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.instance_type_id = 4 self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'launched_at') - self.assertEqual(wt.reason, "{ launched_at : 111 } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'launched_at') + self.assertEqual( + exception.reason, + "{ launched_at : 111 } of incorrect type for exist id 23") self.mox.VerifyAll() def test_should_verify_deleted_at_is_of_decimal_type_if_present(self): @@ -428,12 +429,13 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.deleted_at = 20 self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'deleted_at') - self.assertEqual(wt.reason, "{ deleted_at : 20 } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'deleted_at') + self.assertEqual( + exception.reason, + "{ deleted_at : 20 } of incorrect type for exist id 23") self.mox.VerifyAll() @@ -447,12 +449,30 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.rax_options = 'a' self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'rax_options') - self.assertEqual(wt.reason, "{ rax_options : a } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'rax_options') + self.assertEqual( + exception.reason, + "{ rax_options : a } of incorrect type for exist id 23") + self.mox.VerifyAll() + + def test_should_verify_rax_options_should_not_be_empty(self): + exist = self.mox.CreateMockAnything() + exist.tenant = '3762854cd6f6435998188d5120e4c271' + exist.id = 23 + exist.launched_at = decimal.Decimal('1.1') + exist.deleted_at = decimal.Decimal('5.1') + exist.instance_type_id = 4 + exist.rax_options = '' + self.mox.ReplayAll() + + with self.assertRaises(NullFieldException) as nf: + nova_verifier._verify_validity(exist, 'all') + exception = nf.exception + self.assertEqual(exception.field_name, 'rax_options') + self.assertEqual(exception.reason, "rax_options field was null for exist id 23") self.mox.VerifyAll() def test_should_verify_os_arch_should_be_alphanumeric(self): @@ -464,15 +484,37 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.instance_type_id = 4 exist.rax_options = 12 - exist.os_arch = 'x64,' + exist.os_architecture = 'x64,' self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'os_arch') - self.assertEqual(wt.reason, "{ os_arch : x64, } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'os_architecture') + self.assertEqual( + exception.reason, + "{ os_architecture : x64, } of incorrect type for exist id 23") + self.mox.VerifyAll() + + def test_should_verify_os_arch_should_not_be_empty(self): + exist = self.mox.CreateMockAnything() + exist.tenant = '3762854cd6f6435998188d5120e4c271' + exist.id = 23 + exist.launched_at = decimal.Decimal('1.1') + exist.deleted_at = decimal.Decimal('5.1') + + exist.instance_type_id = 4 + exist.rax_options = 12 + exist.os_architecture = '' + self.mox.ReplayAll() + + with self.assertRaises(NullFieldException) as nf: + nova_verifier._verify_validity(exist, 'all') + exception = nf.exception + self.assertEqual(exception.field_name, 'os_architecture') + self.assertEqual( + exception.reason, + "os_architecture field was null for exist id 23") self.mox.VerifyAll() def test_should_verify_os_distro_should_be_alphanumeric(self): @@ -483,16 +525,38 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.deleted_at = decimal.Decimal('5.1') exist.instance_type_id = 4 exist.rax_options = 12 - exist.os_arch = 'x64' + exist.os_architecture = 'x64' exist.os_distro = 'com.microsoft.server,' self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'os_distro') - self.assertEqual(wt.reason, "{ os_distro : com.microsoft.server, } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'os_distro') + self.assertEqual( + exception.reason, + "{ os_distro : com.microsoft.server, } of incorrect type for exist id 23") + self.mox.VerifyAll() + + def test_should_verify_os_distro_should_not_be_empty(self): + exist = self.mox.CreateMockAnything() + exist.tenant = '3762854cd6f6435998188d5120e4c271' + exist.id = 23 + exist.launched_at = decimal.Decimal('1.1') + exist.deleted_at = decimal.Decimal('5.1') + exist.instance_type_id = 4 + exist.rax_options = 12 + exist.os_architecture = 'x64' + exist.os_distro = '' + self.mox.ReplayAll() + + with self.assertRaises(NullFieldException) as nf: + nova_verifier._verify_validity(exist, 'all') + exception = nf.exception + self.assertEqual(exception.field_name, 'os_distro') + self.assertEqual( + exception.reason, + "os_distro field was null for exist id 23") self.mox.VerifyAll() def test_should_verify_os_version_should_be_alphanumeric(self): @@ -503,20 +567,21 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.deleted_at = decimal.Decimal('5.1') exist.instance_type_id = 4 exist.rax_options = 12 - exist.os_arch = 'x64' + exist.os_architecture = 'x64' exist.os_distro = 'com.microsoft.server' exist.os_version = '2008.2,' self.mox.ReplayAll() - try: - nova_verifier._verify_validity(exist) - self.fail() - except WrongTypeException as wt: - self.assertEqual(wt.field_name, 'os_version') - self.assertEqual(wt.reason, "{ os_version : 2008.2, } of incorrect type for exist id 23") + with self.assertRaises(WrongTypeException) as wt: + nova_verifier._verify_validity(exist, 'all') + exception = wt.exception + self.assertEqual(exception.field_name, 'os_version') + self.assertEqual( + exception.reason, + "{ os_version : 2008.2, } of incorrect type for exist id 23") self.mox.VerifyAll() - def test_should_verify_exist_fields_correctly(self): + def test_should_verify_os_version_should_not_be_empty(self): exist = self.mox.CreateMockAnything() exist.tenant = '3762854cd6f6435998188d5120e4c271' exist.id = 23 @@ -524,15 +589,56 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.deleted_at = decimal.Decimal('5.1') exist.instance_type_id = 4 exist.rax_options = 12 - exist.os_arch = 'x64' + exist.os_architecture = 'x64' + exist.os_distro = 'com.microsoft.server' + exist.os_version = '' + self.mox.ReplayAll() + + with self.assertRaises(NullFieldException) as nf: + nova_verifier._verify_validity(exist, 'all') + exception = nf.exception + self.assertEqual(exception.field_name, 'os_version') + self.assertEqual( + exception.reason, "os_version field was null for exist id 23") + self.mox.VerifyAll() + + def test_should_verify_all_exist_fields_when_validity_check_value_is_all(self): + exist = self.mox.CreateMockAnything() + exist.tenant = '3762854cd6f6435998188d5120e4c271' + exist.id = 23 + exist.launched_at = decimal.Decimal('1.1') + exist.deleted_at = decimal.Decimal('5.1') + exist.instance_type_id = '4' + exist.rax_options = '12' + exist.os_architecture = 'x64' exist.os_distro = 'com.microsoft.server' exist.os_version = '2008.2' self.mox.ReplayAll() - nova_verifier._verify_validity(exist) + nova_verifier._verify_validity(exist, 'all') self.mox.VerifyAll() - def test_should_verify_exist_fields_correctly_and_deleted_at_can_be_none(self): + def test_should_verify_only_basic_exist_fields_when_validity_check_value_is_basic(self): + exist = self.mox.CreateMockAnything() + exist.tenant = '3762854cd6f6435998188d5120e4c271' + exist.id = 23 + exist.launched_at = decimal.Decimal('1.1') + exist.deleted_at = decimal.Decimal('5.1') + exist.instance_type_id = '4' + self.mox.ReplayAll() + + nova_verifier._verify_validity(exist, 'basic') + self.mox.VerifyAll() + + def test_should_not_verify_any_fields_if_validity_check_value_is_none(self): + exist = self.mox.CreateMockAnything() + exist.id = 23 + self.mox.ReplayAll() + + nova_verifier._verify_validity(exist, 'none') + self.mox.VerifyAll() + + def test_should_verify_exist_fields_even_if_deleted_at_is_none(self): exist = self.mox.CreateMockAnything() exist.tenant = '3762854cd6f6435998188d5120e4c271' exist.id = 23 @@ -540,12 +646,12 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.deleted_at = None exist.instance_type_id = 4 exist.rax_options = 12 - exist.os_arch = 'x64' + exist.os_architecture = 'x64' exist.os_distro = 'com.microsoft.server' exist.os_version = '2008.2' self.mox.ReplayAll() - nova_verifier._verify_validity(exist) + nova_verifier._verify_validity(exist, 'all') self.mox.VerifyAll() def test_verify_for_delete(self): @@ -618,12 +724,11 @@ class NovaVerifierTestCase(StacktachBaseTestCase): self.mox.ReplayAll() - try: + with self.assertRaises(VerificationException) as ve: nova_verifier._verify_for_delete(exist) - self.fail() - except VerificationException, ve: - msg = 'Found InstanceDeletes for non-delete exist' - self.assertEqual(ve.reason, msg) + exception = ve.exception + msg = 'Found InstanceDeletes for non-delete exist' + self.assertEqual(exception.reason, msg) self.mox.VerifyAll() @@ -636,13 +741,12 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.delete.deleted_at = decimal.Decimal('5.1') self.mox.ReplayAll() - try: + with self.assertRaises(FieldMismatch) as fm: nova_verifier._verify_for_delete(exist) - self.fail() - except FieldMismatch, fm: - self.assertEqual(fm.field_name, 'launched_at') - self.assertEqual(fm.expected, decimal.Decimal('1.1')) - self.assertEqual(fm.actual, decimal.Decimal('2.1')) + exception = fm.exception + self.assertEqual(exception.field_name, 'launched_at') + self.assertEqual(exception.expected, decimal.Decimal('1.1')) + self.assertEqual(exception.actual, decimal.Decimal('2.1')) self.mox.VerifyAll() def test_verify_for_delete_deleted_at_mismatch(self): @@ -654,13 +758,12 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist.delete.deleted_at = decimal.Decimal('6.1') self.mox.ReplayAll() - try: + with self.assertRaises(FieldMismatch) as fm: nova_verifier._verify_for_delete(exist) - self.fail() - except FieldMismatch, fm: - self.assertEqual(fm.field_name, 'deleted_at') - self.assertEqual(fm.expected, decimal.Decimal('5.1')) - self.assertEqual(fm.actual, decimal.Decimal('6.1')) + exception = fm.exception + self.assertEqual(exception.field_name, 'deleted_at') + self.assertEqual(exception.expected, decimal.Decimal('5.1')) + self.assertEqual(exception.actual, decimal.Decimal('6.1')) self.mox.VerifyAll() def test_verify_with_reconciled_data(self): @@ -799,10 +902,10 @@ class NovaVerifierTestCase(StacktachBaseTestCase): self.mox.StubOutWithMock(exist, 'mark_verified') nova_verifier._verify_for_launch(exist) nova_verifier._verify_for_delete(exist) - nova_verifier._verify_validity(exist) + nova_verifier._verify_validity(exist, 'all') exist.mark_verified() self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'all') self.assertTrue(result) self.mox.VerifyAll() @@ -817,7 +920,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): nova_verifier._verify_with_reconciled_data(exist)\ .AndRaise(NotFound('InstanceReconcile', {})) self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'all') self.assertFalse(result) self.mox.VerifyAll() @@ -833,7 +936,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): .AndRaise(VerificationException('test2')) exist.mark_failed(reason='test2') self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'none') self.assertFalse(result) self.mox.VerifyAll() @@ -850,7 +953,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): .AndRaise(NotFound('InstanceReconcile', {})) exist.mark_failed(reason='test') self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'none') self.assertFalse(result) self.mox.VerifyAll() @@ -866,7 +969,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): nova_verifier._verify_with_reconciled_data(exist) exist.mark_verified(reconciled=True) self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'none') self.assertTrue(result) self.mox.VerifyAll() @@ -883,7 +986,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): .AndRaise(Exception()) exist.mark_failed(reason='Exception') self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'none') self.assertFalse(result) self.mox.VerifyAll() @@ -901,7 +1004,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): .AndRaise(NotFound('InstanceReconcile', {})) exist.mark_failed(reason='test') self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'none') self.assertFalse(result) self.mox.VerifyAll() @@ -914,7 +1017,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): nova_verifier._verify_for_launch(exist).AndRaise(Exception()) exist.mark_failed(reason='Exception') self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'none') self.assertFalse(result) self.mox.VerifyAll() @@ -928,7 +1031,7 @@ class NovaVerifierTestCase(StacktachBaseTestCase): nova_verifier._verify_for_delete(exist).AndRaise(Exception()) exist.mark_failed(reason='Exception') self.mox.ReplayAll() - result, exists = nova_verifier._verify(exist) + result, exists = nova_verifier._verify(exist, 'none') self.assertFalse(result) self.mox.VerifyAll() @@ -948,9 +1051,9 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist2.update_status('verifying') exist1.save() exist2.save() - self.pool.apply_async(nova_verifier._verify, args=(exist1,), + self.pool.apply_async(nova_verifier._verify, args=(exist1, 'all'), callback=None) - self.pool.apply_async(nova_verifier._verify, args=(exist2,), + self.pool.apply_async(nova_verifier._verify, args=(exist2, 'all'), callback=None) self.mox.ReplayAll() self.verifier.verify_for_range(when_max) @@ -973,9 +1076,9 @@ class NovaVerifierTestCase(StacktachBaseTestCase): exist2.update_status('verifying') exist1.save() exist2.save() - self.pool.apply_async(nova_verifier._verify, args=(exist1,), + self.pool.apply_async(nova_verifier._verify, args=(exist1, 'all'), callback=callback) - self.pool.apply_async(nova_verifier._verify, args=(exist2,), + self.pool.apply_async(nova_verifier._verify, args=(exist2, 'all'), callback=callback) self.mox.ReplayAll() self.verifier.verify_for_range(when_max, callback=callback) diff --git a/tests/unit/utils.py b/tests/unit/utils.py index 04b72cc..a940b96 100644 --- a/tests/unit/utils.py +++ b/tests/unit/utils.py @@ -163,6 +163,7 @@ class FakeVerifierConfig(object): self.durable_queue = lambda: durable_queue self.topics = lambda: topics self.enable_notifications = lambda: notifs + self.validation_level = lambda: 'all' def make_verifier_config(notifs): diff --git a/verifier/base_verifier.py b/verifier/base_verifier.py index 01f9209..b6bc46f 100644 --- a/verifier/base_verifier.py +++ b/verifier/base_verifier.py @@ -80,8 +80,15 @@ def _is_like_date(attr_name, attr_value, exist_id): raise WrongTypeException(attr_name, attr_value, exist_id) -def _is_int(attr_name, attr_value, exist_id): - if not isinstance(attr_value, int): +def _is_long(attr_name, attr_value, exist_id): + if not isinstance(attr_value, long): + raise WrongTypeException(attr_name, attr_value, exist_id) + + +def _is_int_in_char(attr_name, attr_value, exist_id): + try: + int(attr_value) + except ValueError: raise WrongTypeException(attr_name, attr_value, exist_id) @@ -89,6 +96,7 @@ def _is_hex_owner_id(attr_name, attr_value, exist_id): if not re.match("[0-9a-f]{32}$", attr_value): raise WrongTypeException(attr_name, attr_value, exist_id) + def _is_alphanumeric(attr_name, attr_value, exist_id): if not re.match("[a-zA-Z0-9.]+$", attr_value): raise WrongTypeException(attr_name, attr_value, exist_id) diff --git a/verifier/config.py b/verifier/config.py index d48436f..f89b1f8 100644 --- a/verifier/config.py +++ b/verifier/config.py @@ -87,3 +87,7 @@ def password(): def virtual_host(): return config['rabbit']['virtual_host'] + + +def validation_level(): + return config['validation_level'] diff --git a/verifier/glance_verifier.py b/verifier/glance_verifier.py index 0ba611c..3ed49b6 100644 --- a/verifier/glance_verifier.py +++ b/verifier/glance_verifier.py @@ -65,11 +65,10 @@ def _verify_validity(exist): raise NullFieldException(field_name, exist.id) base_verifier._is_like_uuid('uuid', exist.uuid, exist.id) base_verifier._is_like_date('created_at', exist.created_at, exist.id) - base_verifier._is_int('size', exist.size, exist.id) + base_verifier._is_long('size', exist.size, exist.id) base_verifier._is_hex_owner_id('owner', exist.owner, exist.id) - def _verify_for_usage(exist, usage=None): usage_type = "ImageUsage" if not usage and exist.usage: diff --git a/verifier/nova_verifier.py b/verifier/nova_verifier.py index 5a65e2f..f0e4d69 100644 --- a/verifier/nova_verifier.py +++ b/verifier/nova_verifier.py @@ -18,7 +18,6 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import argparse import datetime import json import os @@ -146,22 +145,50 @@ def _verify_for_delete(exist, delete=None, 'deleted_at', exist.deleted_at, delete.deleted_at) -def _verify_validity(exist): - fields = {exist.tenant: 'tenant', exist.launched_at: 'launched_at', +def _verify_basic_validity(exist): + fields = {exist.tenant: 'tenant', + exist.launched_at: 'launched_at', exist.instance_type_id: 'instance_type_id'} for (field_value, field_name) in fields.items(): if field_value is None: raise NullFieldException(field_name, exist.id) base_verifier._is_hex_owner_id('tenant', exist.tenant, exist.id) - base_verifier._is_int('instance_type_id', exist.instance_type_id, exist.id) + base_verifier._is_int_in_char('instance_type_id', exist.instance_type_id, + exist.id) base_verifier._is_like_date('launched_at', exist.launched_at, exist.id) if exist.deleted_at is not None: base_verifier._is_like_date('deleted_at', exist.deleted_at, exist.id) - base_verifier._is_int('rax_options', exist.rax_options, exist.id) - base_verifier._is_alphanumeric('os_arch', exist.os_arch, exist.id) + + +def _verify_optional_validity(exist): + fields = {exist.rax_options: 'rax_options', + exist.os_architecture: 'os_architecture', + exist.os_version: 'os_version', + exist.os_distro: 'os_distro'} + for (field_value, field_name) in fields.items(): + if field_value == '': + raise NullFieldException(field_name, exist.id) + base_verifier._is_int_in_char('rax_options', exist.rax_options, exist.id) + base_verifier._is_alphanumeric('os_architecture', exist.os_architecture, exist.id) base_verifier._is_alphanumeric('os_distro', exist.os_distro, exist.id) base_verifier._is_alphanumeric('os_version', exist.os_version, exist.id) +def verify_fields_not_null(exist_id, null_value, fields): + + for (field_value, field_name) in fields.items(): + print "value: %s, name = %s" % (field_value, field_name) + if field_value == null_value: + raise NullFieldException(field_name, exist_id) + + +def _verify_validity(exist, validation_level): + if validation_level == 'none': + return + if validation_level == 'basic': + _verify_basic_validity(exist) + if validation_level == 'all': + _verify_basic_validity(exist) + _verify_optional_validity(exist) def _verify_with_reconciled_data(exist): @@ -212,15 +239,15 @@ def _attempt_reconciled_verify(exist, orig_e): return verified -def _verify(exist): +def _verify(exist, validation_level): verified = False try: if not exist.launched_at: raise VerificationException("Exists without a launched_at") + _verify_validity(exist, validation_level) _verify_for_launch(exist) _verify_for_delete(exist) - _verify_validity(exist) verified = True exist.mark_verified() @@ -267,8 +294,9 @@ class NovaVerifier(base_verifier.Verifier): for exist in exists[0:1000]: exist.update_status(models.InstanceExists.VERIFYING) exist.save() + validation_level = self.config.validation_level() result = self.pool.apply_async( - _verify, args=(exist,), + _verify, args=(exist, validation_level), callback=callback) self.results.append(result) added += 1