def test_filtered_none_components_deps(self): """ Check that dependencies are not in the graph when they are filtered out. RuleSet: #root -> #deps (filter = NONE) Components: a#root Component a provides a depfinder that returns b#deps Expecting: a#root, b#deps (b#deps has no action, it has been filtered out) """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="R-root", action="Root Action", types=["root@cat"], depsfinder=tools.getMockDepsFinderCmd(["b#deps@cat"]), dependson=["R-Deps"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="R-Deps", action="Deps Action", types=["deps@cat"], filter=NONE)) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("a#root@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 2) self.assertTrue(depgraph.dag is not None) self.assertTrue("a#root@cat" in components) self.assertTrue("R-root" in components["a#root@cat"].actions) self.assertEquals("Root Action", components["a#root@cat"].actions["R-root"]) self.assertTrue("b#deps@cat" in components) self.assertTrue(depgraph.dag.has_node("a#root@cat")) self.assertTrue(depgraph.dag.has_node("b#deps@cat")) self.assertTrue("Deps Action" not in components["b#deps@cat"].actions)
def test_a_nop_b(self): """ Check that action NONE are understood correctly. RuleSet: #root, #dep (filter = ALL), a#root -> b#dep (a.action=NONE) Expecting: a#root -> b#dep But a#root has no attribute in the xml graph. """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Root", types=["root@cat"], action=NONE, depsfinder=tools.getMockDepsFinderCmd(["b#dep@cat"]), dependson=['Dep'])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Dep", types=["dep@cat"], action="Dep Action")) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("a#root@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 2) self.assertTrue("a#root@cat" in components) self.assertTrue("b#dep@cat" in components) self.assertTrue(depgraph.dag is not None) self.assertTrue(depgraph.dag.has_node("a#root@cat")) self.assertTrue(depgraph.dag.has_node("b#dep@cat")) self.assertFalse("Root" in components["a#root@cat"].actions, components) self.assertTrue("Dep" in components["b#dep@cat"].actions) attributes = depgraph.dag.node_attributes("a#root@cat") self.assertEquals(len(attributes), 0) attributes = depgraph.dag.node_attributes("b#dep@cat") self.assertEquals(len(attributes), 1)
def test_checksum_different_types(self): rule1 = tools.create_rule(self.__class__.__name__, "R1") rule2 = tools.create_rule(self.__class__.__name__, "R2") self.db.add_rule(rule1) self.db.add_rule(rule2) (orig_ruleset_h, orig_h_for) = self.db.checksum(rule1.ruleset) self.db.remove_rules(rule1.ruleset) rule2 = tools.create_rule(self.__class__.__name__, "R2", types=["type@cat"]) self.db.add_rule(rule1) self.db.add_rule(rule2) (other_ruleset_h, other_h_for) = self.db.checksum(rule1.ruleset) self.assertNotEquals(orig_ruleset_h.hexdigest(), other_ruleset_h.hexdigest()) self.assertNotEquals(orig_h_for["R2"].hexdigest(), other_h_for["R2"].hexdigest()) self.db.remove_rules(rule1.ruleset) rule1 = tools.create_rule(self.__class__.__name__, "R1", types=["type@cat", "ALL"]) self.db.add_rule(rule1) self.db.add_rule(rule2) (other_ruleset_h, other_h_for) = self.db.checksum(rule1.ruleset) self.assertNotEquals(orig_ruleset_h.hexdigest(), other_ruleset_h.hexdigest()) self.assertNotEquals(orig_h_for["R1"].hexdigest(), other_h_for["R1"].hexdigest())
def test_none_rulename_forbidden(self): try: tools.create_rule(self.__class__.__name__, None) self.fail("Setting rule name to None should raise an exception!") except ValueError: pass
def test_multiple_actions_cycle(self): """ Check that a given component can contain multiple actions. RuleSet: R1:#ta, R2:#ta->R1 Components: a#ta (R1 depsfinder returns itself: a#ta) Expecting: a#ta with 2 actions and a cycle. """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="R1", action="Action for a", types=["ta@cat"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="R2", action="Action for a", types=["ta@cat"], depsfinder=tools.getMockDepsFinderCmd(["a#ta@cat"]), dependson=["R1"])) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("a#ta@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 1, "Map: %s" % components) self.assertTrue("a#ta@cat" in components) self.assertTrue("R1" in components["a#ta@cat"].actions) self.assertTrue("R2" in components["a#ta@cat"].actions) self.assertEquals(len(components["a#ta@cat"].actions), 2) self.assertEquals(len(depgraph.dag.node_attributes("a#ta@cat")), 2) self.assertTrue(depgraph.dag.has_edge(("a#ta@cat", "a#ta@cat")))
def test_simple_deps_ta_tb_b_a_nodepsfinder(self): """ RuleSet: #ta->#tb, Components: b#tb, a#ta (reverse than test_simple_deps_ta_tb_a_b_nodepsfinder) Expecting: a#ta, b#tb (order does not count) """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Ra", action="Action for a", types=["ta@cat"], dependson=["Rb"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Rb", action="Action for b", types=["tb@cat"])) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("b#tb@cat"), Component("a#ta@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 2) self.assertTrue("a#ta@cat" in components) self.assertTrue("b#tb@cat" in components) self.assertTrue(depgraph.dag is not None) self.assertTrue(depgraph.dag.has_node("a#ta@cat")) self.assertTrue(depgraph.dag.has_node("b#tb@cat")) self.assertNoEdgeBetween(depgraph.dag, "a#ta@cat", "b#tb@cat")
def test_simple_nodeps_ta_tb_a_b(self): """ RuleSet: #ta, #tb, Components: a#ta, b#tb Expecting: a#ta, b#tb """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Ra", action="Action for a", types=["ta@cat"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Rb", action="Action for b", types=["tb@cat"])) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("a#ta@cat"), Component("b#tb@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 2) self.assertTrue("a#ta@cat" in components) self.assertTrue("b#tb@cat" in components) self.assertTrue(depgraph.dag is not None) self.assertTrue(depgraph.dag.has_node("a#ta@cat")) self.assertTrue(depgraph.dag.has_node("b#tb@cat")) self.assertTrue("Ra" in components["a#ta@cat"].actions) self.assertEquals("Action for a", components["a#ta@cat"].actions["Ra"]) self.assertTrue("Rb" in components["b#tb@cat"].actions) self.assertEquals("Action for b", components["b#tb@cat"].actions["Rb"]) self.assertNoEdgeBetween(depgraph.dag, "a#ta@cat", "b#tb@cat")
def test_multiple_actions_nodeps(self): """ Check that a given component can contain multiple actions. RuleSet: #ta, #ta Components: a#ta Expecting: a#ta with 2 actions. """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="R1", action="Action for a", types=["ta@cat"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="R2", action="Action for a", types=["ta@cat"])) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("a#ta@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 1, "Map: %s" % components) self.assertTrue("a#ta@cat" in components) self.assertTrue("R1" in components["a#ta@cat"].actions) self.assertTrue("R2" in components["a#ta@cat"].actions) self.assertEquals(len(components["a#ta@cat"].actions), 2) self.assertEquals(len(depgraph.dag.node_attributes("a#ta@cat")), 2)
def test_root_in_simple_cycle(self): """ The root is in a simple cycle. RuleSet: #ta->#tb, #tb->#tc, #tc->#ta (triangle) Components: b#tb Expecting: b#tb """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Ra", action="Action for a", types=["ta@cat"], dependson=["Rb"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Rb", action="Action for b", types=["tb@cat"], dependson=["Rc"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Rc", action="Action for c", dependson=["Ra"], types=["tc@cat"])) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("b#tb@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 1, "Map: %s" % components) self.assertTrue("b#tb@cat" in components) self.assertTrue(depgraph.dag is not None) self.assertTrue(depgraph.dag.has_node("b#tb@cat"))
def test_no_root_with_cycle_in_graph_rules(self): """ There are no match in the given component list. RuleSet: #ta->#tb->#ta Components: c#tc Expecting: c#tc. """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Ra", action="Action for a", types=["ta@cat"], depsfinder=tools.getMockDepsFinderCmd(["foo#bar@cat"]), dependson=["Rb"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Rb", action="Action for b", types=["tb@cat"], depsfinder=tools.getMockDepsFinderCmd(["foo#bar@cat"]), dependson=["Ra"])) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("c#tc@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 1, "Map: %s" % components) self.assertTrue("c#tc@cat" in components) self.assertTrue(depgraph.dag is not None) self.assertEquals(len(depgraph.dag.nodes()), 1) self.assertTrue(depgraph.dag.has_node("c#tc@cat"))
def test_simple_deps_ta_tb_b_a_withdepsfinder(self): """ RuleSet: #ta->#tb, Components: b#tb, a#ta (reverse) Component a does provide a depfinder that returns b#tb Expecting: a#ta -> b#tb """ rules = set() rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Ra", action="Action for a", types=["ta@cat"], depsfinder=tools.getMockDepsFinderCmd(["b#tb@cat"]), dependson=["Rb"])) rules.add(tools.create_rule(ruleset=self.__class__.__name__, name="Rb", action="Action for b", types=["tb@cat"])) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("b#tb@cat"), Component("a#ta@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 2, "Map: %s" % components) self.assertTrue("a#ta@cat" in components) self.assertTrue("b#tb@cat" in components) self.assertTrue(depgraph.dag is not None) self.assertTrue(depgraph.dag.has_node("a#ta@cat")) self.assertTrue(depgraph.dag.has_node("b#tb@cat")) self.assertTrue(depgraph.dag.has_edge(("a#ta@cat", "b#tb@cat")))
def test_empty_depsfinder_forbidden(self): try: tools.create_rule(self.__class__.__name__, "EmptyDepsFinderForbidden", depsfinder="") self.fail("Setting an empty depsfinder should raise an exception!") except ValueError: pass
def test_rule_pass_filter_re_category(self): rule = tools.create_rule("RS", "RN", filter="%category =~ cat") self.assertTrue(rule.pass_filter(Component("foo#bar@cat"))) self.assertFalse(rule.pass_filter(Component("bar#foo@baz"))) rule = tools.create_rule("RS", "RN", filter="%category !~ cat") self.assertFalse(rule.pass_filter(Component("foo#bar@cat"))) self.assertTrue(rule.pass_filter(Component("bar#foo@baz")))
def test_rule_pass_filter_re_type(self): rule = tools.create_rule("RS", "RN", filter="%type =~ bar") self.assertTrue(rule.pass_filter(Component("foo#bar@cat"))) self.assertFalse(rule.pass_filter(Component("bar#foo@cat"))) rule = tools.create_rule("RS", "RN", filter="%type !~ bar") self.assertFalse(rule.pass_filter(Component("foo#bar@cat"))) self.assertTrue(rule.pass_filter(Component("bar#foo@cat")))
def test_empty_types_forbidden(self): try: tools.create_rule(self.__class__.__name__, "EmptyTypesForbidden", types=set()) self.fail("Setting an empty set should raise an exception!") except ValueError: pass
def test_baddep2(self): rule1 = tools.create_rule(self.__class__.__name__, "BadDep2-1") rule2 = tools.create_rule(self.__class__.__name__, "BadDep2-2", dependson="Unknown") rules = set([rule1, rule2]) try: RuleSet(rules) self.fail("Unknown Dependency should be raised: %s" % rule2) except UnknownDepError as ude: print "Exception is: %s" % ude
def test_update_name_deps_simple(self): self.db.add_rule(tools.create_rule("RS", "r1")) self.db.add_rule(tools.create_rule("RS", "r2", dependson=set(['r1']))) self.db.update_rule("RS", "r1", [("name", "foo")]) rules = self.db.get_rules_for("RS") self.assertTrue(rules is not None) self.assertEquals(len(rules), 2) self.assertEquals(len(rules["r2"].dependson), 1) self.assertTrue("foo" in rules["r2"].dependson)
def test_update_ruleset(self): rule = tools.create_rule(self.__class__.__name__, "update") self.db.add_rule(rule) self.db.update_rule(rule.ruleset, rule.name, [("ruleset", "NewRS")]) rules_for = self.db.get_rules_map() self.assertTrue(rules_for is not None) self.assertEquals(len(rules_for), 1, str(rules_for)) self.assertRuleInMap(tools.create_rule("NewRS", "update"), rules_for)
def test_dup_forbidden(self): rule1 = tools.create_rule(self.__class__.__name__, "DupForbidden") rule2 = tools.create_rule(self.__class__.__name__, "DupForbidden") self.assertEquals(rule1, rule2) self.db.add_rule(rule1) try: self.db.add_rule(rule2) self.fail("Duplicate rules are forbidden in the DB") except Exception as e: print "Exception is: %s" % e
def test_rule_match_type_multiple(self): rule = tools.create_rule("RS", "RN", types=["bar@cat", "baz@cat"]) self.assertTrue(rule.match_type(Component("foo#bar@cat"))) self.assertTrue(rule.match_type(Component("foo#baz@cat"))) self.assertFalse(rule.match_type(Component("bar#foo@cat"))) rule = tools.create_rule("RS", "RN", types=["bar@cat1", "baz@cat2"]) self.assertTrue(rule.match_type(Component("foo#bar@cat1"))) self.assertTrue(rule.match_type(Component("foo#baz@cat2"))) self.assertFalse(rule.match_type(Component("foo#bar@cat2"))) self.assertFalse(rule.match_type(Component("foo#baz@cat1")))
def test_dup_forbidden(self): rule1 = tools.create_rule(self.__class__.__name__, "DupForbidden") rule2 = tools.create_rule(self.__class__.__name__, "DupForbidden") self.assertEquals(rule1, rule2) self.db.add_rule(rule1) try: self.db.add_rule(rule2) self.fail("Duplicate rules are forbidden in the DB") except DuplicateRuleError as dre: self.assertEquals(dre.ruleset, self.__class__.__name__) self.assertEquals(dre.name, "DupForbidden")
def test_cycles_simple(self): """Cycles are allowed in the graph rules""" rules = set() rules.add(tools.create_rule(self.__class__.__name__, "nocycles_simple1", dependson=["nocycles_simple2"])) rules.add(tools.create_rule(self.__class__.__name__, "nocycles_simple2", dependson=["nocycles_simple1"])) # This should not raise an exception RuleSet(rules)
def test_ruleset_different(self): rules = set() rules.add(tools.create_rule(self.__class__.__name__, "ruleset_different")) rules.add(tools.create_rule(self.__class__.__name__ + "-2", "ruleset_different")) try: RuleSet(rules) self.fail("Creating a RuleSet with different " + \ "ruleset should raise an exception") except ValueError: pass
def test_remove_ref_deps_single(self): self.db.add_rule(tools.create_rule(self.__class__.__name__, "r1")) self.db.add_rule(tools.create_rule(self.__class__.__name__, "r2", dependson=set(['r1']))) remaining = self.db.remove_rules(self.__class__.__name__, ("r1",)) self.assertEquals(len(remaining), 0) rules = self.db.get_rules_for(self.__class__.__name__) self.assertTrue(rules is not None) self.assertEquals(len(rules), 1) self.assertTrue("r2" in rules) self.assertEquals(len(rules["r2"].dependson), 0)
def test_root_rules_for_a_a(self): rules = set() r1 = tools.create_rule( ruleset=self.__class__.__name__, name="R1", action="A1", types=["ta@cat"], dependson=["R2"] ) r2 = tools.create_rule(ruleset=self.__class__.__name__, name="R2", action="A2", types=["ta@cat"]) rules.add(r1) rules.add(r2) ruleset = RuleSet(rules) types = ruleset.root_rules_for self.assertTrue(len(types) == 1, types) self.assertTrue(r1 in types["ta@cat"], types)
def test_remove_whole_ruleset(self): self.db.add_rule(tools.create_rule(self.__class__.__name__, "remove1")) self.db.add_rule(tools.create_rule(self.__class__.__name__, "remove2")) self.db.add_rule(tools.create_rule(self.__class__.__name__, "remove3")) self.db.add_rule(tools.create_rule("other_ruleset", "a_rule")) remaining = self.db.remove_rules(self.__class__.__name__) self.assertEquals(len(remaining), 0) rules_for = self.db.get_rules_map() self.assertTrue(rules_for is not None) self.assertEquals(len(rules_for), 1) self.assertTrue(self.__class__.__name__ not in rules_for) self.assertEquals(len(rules_for["other_ruleset"]), 1)
def test_multiple_removal(self): self.db.add_rule(tools.create_rule(self.__class__.__name__, "remove1")) self.db.add_rule(tools.create_rule(self.__class__.__name__, "remove2")) self.db.add_rule(tools.create_rule(self.__class__.__name__, "remove3")) remaining = self.db.remove_rules(self.__class__.__name__, ("remove1", "remove2", "unknown")) self.assertEquals(len(remaining), 1) self.assertTrue("unknown" in remaining) rules = self.db.get_rules_for(self.__class__.__name__) self.assertTrue(rules is not None) self.assertEquals(len(rules), 1) self.assertTrue("remove3" in rules)
def test_only_root_rules_applied(self): """ Check that a rule is applied on a parameter only if it is a potential root. RuleSet: #t(action1)->#t(action2) Components: a#t, b#t (a#t depsfinder returns b#t) Expecting: a#t contains only 1 action while b#t contains two actions. """ rules = set() rules.add( tools.create_rule( ruleset=self.__class__.__name__, name="Ra", action="Action for a", types=["t@cat"], filter="%name =~ a", depsfinder=tools.getMockDepsFinderCmd(["b#t@cat"]), dependson=["Rdep"], ) ) rules.add( tools.create_rule( ruleset=self.__class__.__name__, name="Rb", action="Action for b", types=["t@cat"], filter="%name =~ b", dependson=["Rdep"], ) ) rules.add( tools.create_rule(ruleset=self.__class__.__name__, name="Rdep", action="Action for dep", types=["t@cat"]) ) ruleset = RuleSet(rules) depgraph = ruleset.get_depgraph([Component("a#t@cat"), Component("b#t@cat")]) components = depgraph.components_map self.assertTrue(components is not None) self.assertEquals(len(components), 2, "Map: %s" % components) self.assertTrue("a#t@cat" in components) self.assertTrue("b#t@cat" in components) self.assertEquals(len(components["a#t@cat"].actions), 1) self.assertTrue("Ra" in components["a#t@cat"].actions) self.assertEquals(len(components["b#t@cat"].actions), 2) self.assertTrue("Rb" in components["b#t@cat"].actions) self.assertTrue("Rdep" in components["b#t@cat"].actions) self.assertEquals(len(depgraph.dag.node_attributes("a#t@cat")), 1) self.assertEquals(len(depgraph.dag.node_attributes("b#t@cat")), 2)
def test_update_normal_fields(self): rule = tools.create_rule(self.__class__.__name__, "update_normal") self.db.add_rule(rule) self.db.update_rule(rule.ruleset, rule.name, [("action", "NewAction"), ("filter", "NewFilter")]) rules_for = self.db.get_rules_map() self.assertTrue(rules_for is not None) self.assertEquals(len(rules_for), 1, str(rules_for)) self.assertRuleInMap(tools.create_rule(self.__class__.__name__, "update_normal"), rules_for) rule = rules_for[self.__class__.__name__]["update_normal"] self.assertEquals(rule.action, "NewAction") self.assertEquals(rule.filter, "NewFilter")
def test_update_ruleset_deps_multiple(self): self.db.add_rule(tools.create_rule("RS", "r1")) self.db.add_rule(tools.create_rule("RS", "r2", dependson=set(['r1']))) self.db.add_rule(tools.create_rule("RS", "r3", dependson=set(['r1', "r2"]))) self.db.update_rule("RS", "r1", [("ruleset", "newRS")]) rules = self.db.get_rules_for("RS") self.assertTrue(rules is not None) self.assertEquals(len(rules), 2) self.assertEquals(len(rules["r2"].dependson), 0) self.assertEquals(len(rules["r3"].dependson), 1) self.assertTrue("r2" in rules["r3"].dependson) rules = self.db.get_rules_for("newRS") self.assertTrue(rules is not None) self.assertEquals(len(rules), 1) self.assertTrue("r1" in rules)