Ejemplo n.º 1
0
    def test_no_deps(self):
        a = TestGraphNode("a", [])
        b = TestGraphNode("b", [])
        c = TestGraphNode("c", [])

        nodes = [a, b, c]
        graph = Graph(nodes)

        expected = [a, b, c]
        actual = list(graph.walk())
        self.assertListEqual(expected, actual)
Ejemplo n.º 2
0
    def test_reverse_chain(self):
        a = TestGraphNode("a", ["b"])
        b = TestGraphNode("b", ["c"])
        c = TestGraphNode("c", [])

        nodes = [a, b, c]
        graph = Graph(nodes)

        expected = [c, b, a]
        actual = list(graph.walk())
        self.assertListEqual(expected, actual)
Ejemplo n.º 3
0
    def test_self_reference(self):
        a = TestGraphNode("a", ["a"])

        nodes = [a]
        graph = Graph(nodes)

        with self.assertRaises(GraphWalkError) as context:
            next(graph.walk())

        expected = {"a": {"a"}}
        self.assertEqual(expected, context.exception.not_visited)
Ejemplo n.º 4
0
    def test_diamond(self):
        a = TestGraphNode("a", [])
        b = TestGraphNode("b", ["a"])
        c = TestGraphNode("c", ["a"])
        d = TestGraphNode("d", ["b", "c"])

        nodes = [a, b, c, d]
        graph = Graph(nodes)

        expected = [a, b, c, d]
        actual = list(graph.walk())
        self.assertListEqual(expected, actual)
Ejemplo n.º 5
0
    def test_reverse_branch(self):
        a = TestGraphNode("a", ["b"])
        b = TestGraphNode("b", ["e"])
        c = TestGraphNode("c", ["d"])
        d = TestGraphNode("d", ["e"])
        e = TestGraphNode("e", [])

        nodes = [a, b, c, d, e]
        graph = Graph(nodes)

        expected = [e, b, d, a, c]
        actual = list(graph.walk())
        self.assertListEqual(expected, actual)
Ejemplo n.º 6
0
    def __init__(self, specification: Specification):
        super().__init__()

        configuration = Configuration(
            shell="shell",
            dd_bs="dd_bs",
            random_device="random_device",
            media_dir="media_dir",
            tmp_dir="tmp_dir",
        )
        graph = Graph(
            [
                TestSpecification("device"),
                TestSpecification("file"),
                TestSpecification("filesystem"),
                TestSpecification("keyfile"),
                TestSpecification("lvm_logical_volume"),
                TestSpecification("lvm_physical_volume"),
                TestSpecification("lvm_cachedata_volume"),
                TestSpecification("lvm_cachemeta_volume"),
                TestSpecification("mountpoint"),
                TestSpecification("name"),
                TestSpecification("partition_table"),
            ]
        )
        self.context = CommandContext(configuration, graph)
        self.specification = specification
Ejemplo n.º 7
0
    def test_run(self, make_mode, make_action):
        spec1 = TestSpecification("spec1", [Command(["1"]), Command(["2"])])
        spec2 = TestSpecification("spec2", [Command(["3"]), Command(["4"])])

        config = Configuration(
            shell="",
            dd_bs="",
            random_device="",
            media_dir="",
            tmp_dir="",
        )
        graph = Graph([spec1, spec2])

        mode = MagicMock()
        action = MagicMock()

        make_mode.return_value = mode
        make_action.return_value = action

        run(config, graph, "action", "mode")

        action.assert_called_once_with(mode, AnyIter([spec1, spec2]))

        make_action.assert_called_once_with("action", AnyType())
        make_mode.assert_called_once_with("mode")
Ejemplo n.º 8
0
    def setUp(self):
        configuration = Configuration(
            shell="shell",
            dd_bs="dd_bs",
            random_device="random_device",
            media_dir="media_dir",
            tmp_dir="tmp_dir",
        )
        graph = Graph([])
        self.context = CommandContext(configuration, graph)

        self.generators = [
            TestActionCommandGenerator(
                "gen_1",
                apply=TestCommandGenerator([
                    Command(["apply_1"]),
                    Command(["apply_2"]),
                ]),
                post_apply=TestCommandGenerator([
                    Command(["post_apply_1"]),
                    Command(["post_apply_2"]),
                ]),
                up=TestCommandGenerator([
                    Command(["up_1"]),
                    Command(["up_2"]),
                ]),
                pre_down=TestCommandGenerator([
                    Command(["pre_down_1"]),
                    Command(["pre_down_2"]),
                ]),
                down=TestCommandGenerator([
                    Command(["down_1"]),
                    Command(["down_2"]),
                ]),
            ),
            TestActionCommandGenerator(
                "gen_2",
                apply=TestCommandGenerator([
                    Command(["apply_3"]),
                    Command(["apply_4"]),
                ]),
                post_apply=TestCommandGenerator([
                    Command(["post_apply_3"]),
                    Command(["post_apply_4"]),
                ]),
                up=TestCommandGenerator([
                    Command(["up_3"]),
                    Command(["up_4"]),
                ]),
                pre_down=TestCommandGenerator([
                    Command(["pre_down_3"]),
                    Command(["pre_down_4"]),
                ]),
                down=TestCommandGenerator([
                    Command(["down_3"]),
                    Command(["down_4"]),
                ]),
            ),
        ]
Ejemplo n.º 9
0
    def test_unknown_reference(self):
        a = TestGraphNode("a", [], ["b"])

        nodes = [a]
        with self.assertRaises(GraphEdgeError) as context:
            Graph(nodes)

        self.assertEqual("a", context.exception.name)
        self.assertEqual("b", context.exception.dependency)
Ejemplo n.º 10
0
    def test_repeat_name(self):
        a1 = TestGraphNode("a", [])
        a2 = TestGraphNode("a", [])

        nodes = [a1, a2]
        with self.assertRaises(GraphNameError) as context:
            Graph(nodes)

        self.assertEqual("a", context.exception.name)
Ejemplo n.º 11
0
    def test_successful_direct_resolve(self):
        for msg, node_factory, graph_resolve in GraphResolveTest.parameterization:
            with self.subTest(msg=msg):
                a = node_factory("a", [], resolve=ResolveLink(None, "x"))

                nodes = [a]
                graph = Graph(nodes)

                self.assertEqual("x", graph_resolve(graph, "a"))
Ejemplo n.º 12
0
def main(argv):
    args = parse_args(argv)

    logging.basicConfig(level=args.log_level)

    config = load_config(args.config)
    graph = Graph(parse(load_spec(args.specification)))

    run(config, graph, args.action, args.mode)

    return 0
Ejemplo n.º 13
0
    def test_cycle(self):
        a = TestGraphNode("a", [])
        b = TestGraphNode("b", ["a", "c"])
        c = TestGraphNode("c", ["b"])
        d = TestGraphNode("d", ["c"])

        nodes = [a, b, c, d]
        graph = Graph(nodes)
        walk = graph.walk()

        self.assertEqual(a, next(walk))

        with self.assertRaises(GraphWalkError) as context:
            next(walk)

        expected = {
            "b": {"c"},
            "c": {"b"},
            "d": {"c"},
        }
        self.assertEqual(expected, context.exception.not_visited)
Ejemplo n.º 14
0
    def test_unknown_direct_resolve(self):
        for msg, node_factory, graph_resolve in GraphResolveTest.parameterization:
            with self.subTest(msg=msg):
                a = node_factory("a", [])

                nodes = [a]
                graph = Graph(nodes)

                with self.assertRaises(GraphResolveError) as context:
                    graph_resolve(graph, "b")

                self.assertEqual("b", context.exception.reference)
Ejemplo n.º 15
0
    def test_command_context(self):
        configuration = Configuration(
            shell="shell",
            dd_bs="dd_bs",
            random_device="random_device",
            media_dir="media_dir",
            tmp_dir="tmp_dir",
        )
        graph = Graph([])
        context = CommandContext(configuration, graph)

        self.assertEqual(configuration, context.config)
        self.assertEqual(graph, context.graph)
Ejemplo n.º 16
0
    def test_undeclared_recursive_resolve(self):
        for msg, node_factory, graph_resolve in GraphResolveTest.parameterization:
            with self.subTest(msg=msg):
                a = node_factory("a", [], resolve=ResolveLink("b", "y"))
                b = node_factory("b", [], resolve=ResolveLink(None, "x"))

                nodes = [a, b]
                graph = Graph(nodes)

                with self.assertRaises(GraphResolveError) as context:
                    graph_resolve(graph, "a")

                self.assertEqual("b", context.exception.reference)
Ejemplo n.º 17
0
    def test_successful_recursive_resolve_with_join(self):
        for msg, node_factory, graph_resolve in GraphResolveTest.parameterization:
            with self.subTest(msg=msg):
                a = node_factory("a", ["b"],
                                 resolve=ResolveLink("b", "y",
                                                     lambda l, r: f"{l}j{r}"))
                b = node_factory("b", [],
                                 resolve=ResolveLink(None, "x",
                                                     lambda l, r: f"{l}i{r}"))

                nodes = [a, b]
                graph = Graph(nodes)

                self.assertEqual("xiy", graph_resolve(graph, "a"))
Ejemplo n.º 18
0
 def test_empty(self):
     graph = Graph([])
     with self.assertRaises(StopIteration):
         next(graph.walk())