本文整理汇总了Python中c7n.schema.Validator类的典型用法代码示例。如果您正苦于以下问题:Python Validator类的具体用法?Python Validator怎么用?Python Validator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Validator类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_instance_age
def test_instance_age(self):
data = {
'policies': [
{'name': 'ancient-instances',
'resource': 'ec2',
'query': [{'instance-state-name': 'running'}],
'filters': [{'days': 60, 'type': 'instance-age'}]
}]}
schema = generate(['ec2'])
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 0)
示例2: test_instance_age
def test_instance_age(self):
data = {
"policies": [
{
"name": "ancient-instances",
"resource": "ec2",
"query": [{"instance-state-name": "running"}],
"filters": [{"days": 60, "type": "instance-age"}],
}
]
}
schema = generate(["ec2"])
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 0)
示例3: test_value_filter_short_form_invalid
def test_value_filter_short_form_invalid(self):
for rtype in ["elb", "rds", "ec2"]:
data = {
'policies': [
{'name': 'instance-policy',
'resource': 'elb',
'filters': [
{"tag:Role": "webserver"}]}
]}
schema = generate([rtype])
# Disable standard value short form
schema['definitions']['filters']['valuekv'] = {'type': 'number'}
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 1)
示例4: test_mark_for_op
def test_mark_for_op(self):
data = {
'policies': [{
'name': 'ebs-mark-delete',
'resource': 'ebs',
'filters': [],
'actions': [{
'type': 'mark-for-op',
'op': 'delete',
'days': 30}]}]
}
schema = generate(['ebs'])
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 0)
示例5: test_mark_for_op
def test_mark_for_op(self):
data = {
"policies": [
{
"name": "ebs-mark-delete",
"resource": "ebs",
"filters": [],
"actions": [{"type": "mark-for-op", "op": "delete", "days": 30}],
}
]
}
schema = generate(["ebs"])
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 0)
示例6: test_offhours_stop
def test_offhours_stop(self):
data = {
'policies': [
{'name': 'ec2-offhours-stop',
'resource': 'ec2',
'filters': [
{'tag:aws:autoscaling:groupName': 'absent'},
{'type': 'offhour',
'tag': 'maid_downtime',
'default_tz': 'et',
'hour': 19}]
}]
}
schema = generate(['ec2'])
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 0)
示例7: test_value_filter_short_form_invalid
def test_value_filter_short_form_invalid(self):
for rtype in ["elb", "rds", "ec2"]:
data = {
"policies": [
{
"name": "instance-policy",
"resource": "elb",
"filters": [{"tag:Role": "webserver"}],
}
]
}
schema = generate([rtype])
# Disable standard value short form
schema["definitions"]["filters"]["valuekv"] = {"type": "number"}
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 1)
示例8: test_offhours_stop
def test_offhours_stop(self):
data = {
"policies": [
{
"name": "ec2-offhours-stop",
"resource": "ec2",
"filters": [
{"tag:aws:autoscaling:groupName": "absent"},
{
"type": "offhour",
"tag": "c7n_downtime",
"default_tz": "et",
"offhour": 19,
},
],
}
]
}
schema = generate(["ec2"])
validator = Validator(schema)
errors = list(validator.iter_errors(data))
self.assertEqual(len(errors), 0)
示例9: test_schema
def test_schema(self):
try:
schema = generate()
Validator.check_schema(schema)
except Exception:
self.fail("Invalid schema")
示例10: setUp
def setUp(self):
if not self.validator:
self.validator = Validator(generate())
示例11: SchemaTest
class SchemaTest(BaseTest):
validator = None
def findError(self, data, validator):
e = best_match(validator.iter_errors(data))
ex = specific_error(list(validator.iter_errors(data))[0])
return e, ex
def setUp(self):
if not self.validator:
self.validator = Validator(generate())
def test_schema(self):
try:
schema = generate()
Validator.check_schema(schema)
except Exception:
self.fail("Invalid schema")
def test_schema_serialization(self):
try:
dumps(generate())
except:
self.fail("Failed to serialize schema")
def test_empty_skeleton(self):
self.assertEqual(validate({'policies': []}), [])
def test_duplicate_policies(self):
data = {
'policies': [
{'name': 'monday-morning',
'resource': 'ec2'},
{'name': 'monday-morning',
'resource': 'ec2'},
]}
result = validate(data)
self.assertEqual(len(result), 1)
self.assertTrue(isinstance(result[0], ValueError))
self.assertTrue('monday-morning' in str(result[0]))
def test_semantic_error(self):
data = {
'policies': [
{'name': 'test',
'resource': 'ec2',
'filters': {
'type': 'ebs',
'skipped_devices': []}
}]
}
errors = list(self.validator.iter_errors(data))
self.assertEqual(len(errors), 1)
error = specific_error(errors[0])
self.assertTrue(
len(errors[0].absolute_schema_path) < len(
error.absolute_schema_path))
self.assertTrue(
"'skipped_devices': []" in error.message)
self.assertTrue(
"'type': 'ebs'" in error.message)
def test_vars_and_tags(self):
data = {
'vars': {'alpha': 1, 'beta': 2},
'policies': [{
'name': 'test',
'resource': 'ec2',
'tags': ['controls']}]}
self.assertEqual(list(self.validator.iter_errors(data)), [])
def test_semantic_error_on_value_derived(self):
data = {
'policies': [
{'name': 'test',
'resource': 'ec2',
'filters': [
{'type': 'ebs',
'skipped_devices': []}
]}
]}
errors = list(self.validator.iter_errors(data))
self.assertEqual(len(errors), 1)
error = specific_error(errors[0])
self.assertTrue(
len(errors[0].absolute_schema_path) < len(
error.absolute_schema_path))
self.assertEqual(
error.message,
("Additional properties are not allowed "
"('skipped_devices' was unexpected)"))
def test_invalid_resource_type(self):
data = {
'policies': [
{'name': 'instance-policy',
'resource': 'ec3',
#.........这里部分代码省略.........
示例12: SchemaTest
class SchemaTest(BaseTest):
validator = None
def findError(self, data, validator):
e = best_match(validator.iter_errors(data))
ex = specific_error(list(validator.iter_errors(data))[0])
return e, ex
def setUp(self):
if not self.validator:
self.validator = Validator(generate())
def test_schema_plugin_name_mismatch(self):
for k, v in resources.items():
for fname, f in v.filter_registry.items():
if fname in ('or', 'and', 'not'):
continue
self.assertIn(
fname, f.schema['properties']['type']['enum'])
for aname, a in v.action_registry.items():
self.assertIn(
aname, a.schema['properties']['type']['enum'])
def test_schema(self):
try:
schema = generate()
Validator.check_schema(schema)
except Exception:
self.fail("Invalid schema")
def test_schema_serialization(self):
try:
dumps(generate())
except:
self.fail("Failed to serialize schema")
def test_empty_skeleton(self):
self.assertEqual(validate({'policies': []}), [])
def test_duplicate_policies(self):
data = {
'policies': [
{'name': 'monday-morning',
'resource': 'ec2'},
{'name': 'monday-morning',
'resource': 'ec2'},
]}
result = validate(data)
self.assertEqual(len(result), 2)
self.assertTrue(isinstance(result[0], ValueError))
self.assertTrue('monday-morning' in str(result[0]))
def test_semantic_error(self):
data = {
'policies': [
{'name': 'test',
'resource': 'ec2',
'filters': {
'type': 'ebs',
'skipped_devices': []}
}]
}
errors = list(self.validator.iter_errors(data))
self.assertEqual(len(errors), 1)
error = specific_error(errors[0])
self.assertTrue(
len(errors[0].absolute_schema_path) < len(
error.absolute_schema_path))
self.assertTrue(
"'skipped_devices': []" in error.message)
self.assertTrue(
"u'type': u'ebs'" in error.message or
"'type': 'ebs'" in error.message
)
@mock.patch('c7n.schema.specific_error')
def test_handle_specific_error_fail(self, mock_specific_error):
from jsonschema.exceptions import ValidationError
data = {
'policies': [{'name': 'test',
'resource': 'aws.ec2',
'filters': {
'type': 'ebs',
'invalid': []}
}]
}
mock_specific_error.side_effect = ValueError('The specific error crapped out hard')
resp = validate(data)
# if it is 2, then we know we got the exception from specific_error
self.assertEqual(len(resp), 2)
self.assertIsInstance(resp[0], ValidationError)
self.assertIsInstance(resp[1], ValidationError)
def test_vars_and_tags(self):
data = {
'vars': {'alpha': 1, 'beta': 2},
'policies': [{
#.........这里部分代码省略.........
示例13: SchemaTest
class SchemaTest(BaseTest):
validator = None
def findError(self, data, validator):
e = best_match(validator.iter_errors(data))
ex = specific_error(list(validator.iter_errors(data))[0])
return e, ex
def setUp(self):
if not self.validator:
self.validator = Validator(generate())
def test_schema_plugin_name_mismatch(self):
for k, v in resources.items():
for fname, f in v.filter_registry.items():
if fname in ("or", "and", "not"):
continue
self.assertIn(fname, f.schema["properties"]["type"]["enum"])
for aname, a in v.action_registry.items():
self.assertIn(aname, a.schema["properties"]["type"]["enum"])
def test_schema(self):
try:
schema = generate()
Validator.check_schema(schema)
except Exception:
self.fail("Invalid schema")
def test_schema_serialization(self):
try:
dumps(generate())
except Exception:
self.fail("Failed to serialize schema")
def test_empty_skeleton(self):
self.assertEqual(validate({"policies": []}), [])
def test_duplicate_policies(self):
data = {
"policies": [
{"name": "monday-morning", "resource": "ec2"},
{"name": "monday-morning", "resource": "ec2"},
]
}
result = validate(data)
self.assertEqual(len(result), 2)
self.assertTrue(isinstance(result[0], ValueError))
self.assertTrue("monday-morning" in str(result[0]))
def test_py3_policy_error(self):
data = {
'policies': [{
'name': 'policy-ec2',
'resource': 'ec2',
'actions': [
{'type': 'terminate',
'force': 'asdf'}]}]}
result = validate(data)
self.assertEqual(len(result), 2)
err, policy = result
self.assertTrue("'asdf' is not of type 'boolean'" in str(err).replace("u'", "'"))
self.assertEqual(policy, 'policy-ec2')
def test_semantic_mode_error(self):
data = {
'policies': [{
'name': 'test',
'resource': 'ec2',
'mode': {
'type': 'periodic',
'scheduled': 'oops'}}]}
errors = list(self.validator.iter_errors(data))
self.assertEqual(len(errors), 1)
error = specific_error(errors[0])
self.assertTrue(
len(errors[0].absolute_schema_path) < len(error.absolute_schema_path)
)
self.assertTrue("'scheduled' was unexpected" in str(error))
self.assertTrue(len(str(error)) < 2000)
def test_semantic_error_policy_scope(self):
data = {
'policies': [
{'actions': [{'key': 'TagPolicyCompliance',
'type': 'tag',
'value': 'This resource should have tags following policy'}],
'description': 'Identify resources which lack our accounting tags',
'filters': [{'tag:Environment': 'absent'},
{'tag:Service': 'absent'},
{'or': [{'tag:Owner': 'absent'},
{'tag:ResponsibleParty': 'absent'},
{'tag:Contact': 'absent'},
{'tag:Creator': 'absent'}]}],
'name': 'tagging-compliance-waf',
'resource': 'aws.waf'}]}
errors = list(self.validator.iter_errors(data))
#.........这里部分代码省略.........