def test_find_violations_wrong_parent(self):
        rule = """
rules:
- name: Resource test rule
  mode: required
  resource_types: [project, bucket]
  resource_trees:
  - type: project
    resource_id: p1
  - type: project
    resource_id: p2
    children:
    - type: bucket
      resource_id: p1-bucket1
"""
        rules_engine = get_rules_engine_with_rule(rule)
        got_violations = list(
            rules_engine.find_violations(
                [data.PROJECT1, data.PROJECT2, data.BUCKET]))
        node_violation = resource_rules_engine.RuleViolation(
            resource_id='p1-bucket1',
            resource_name='p1-bucket1',
            resource_type='bucket',
            full_name='p1-bucket1',
            rule_index=0,
            rule_name='Resource test rule',
            violation_type='RESOURCE_VIOLATION',
            violation_data='',
            resource_data='',
        )
        self.assertEqual(got_violations,
                         data.build_violations(data.BUCKET) + [node_violation])
    def test_find_violations_missing(self):
        rule = """
rules:
- name: Resource test rule
  mode: required
  resource_types: [project]
  resource_trees:
  - type: project
    resource_id: p1
"""
        rules_engine = get_rules_engine_with_rule(rule)
        got_violations = list(rules_engine.find_violations([]))
        violation = data.build_violations(data.PROJECT2)[0]
        violation = resource_rules_engine.RuleViolation(
            resource_id='p1',
            resource_name='p1',
            resource_type='project',
            full_name='p1',
            rule_index=0,
            rule_name='Resource test rule',
            violation_type='RESOURCE_VIOLATION',
            violation_data='',
            resource_data='',
        )
        self.assertEqual(got_violations, [violation])
예제 #3
0
    def test_find_violations_empty_tree(self):
        rule = """
rules:
- name: Resource test rule
  mode: required
  resource_types: [organization]
  resource_trees: []
"""
        rules_engine = get_rules_engine_with_rule(rule)
        got_violations = list(rules_engine.find_violations([data.ORGANIZATION]))
        self.assertEqual(got_violations,
                         data.build_violations(data.ORGANIZATION))
    def test_find_violations_single_node_no_match(self):
        rule = """
rules:
- name: Resource test rule
  mode: required
  resource_types: [project]
  resource_trees:
  - type: project
    resource_id: p1
"""
        rules_engine = get_rules_engine_with_rule(rule)
        got_violations = list(
            rules_engine.find_violations([data.PROJECT1, data.PROJECT2]))
        self.assertEqual(got_violations, data.build_violations(data.PROJECT2))