Exemplo n.º 1
0
    def _print_schedule_default(self):
        """
        Print the scheduling table in normal or detailed mode.
        """
        from forest import Forest
        from rcColor import color
        tree = Forest()
        head_node = tree.add_node()
        head_node.add_column("Action", color.BOLD)
        head_node.add_column("Last Run", color.BOLD)
        if self.options.verbose:
            head_node.add_column("Next Run", color.BOLD)
        head_node.add_column("Config Parameter", color.BOLD)
        head_node.add_column("Schedule Definition", color.BOLD)

        for data in self._print_schedule_data():
            node = head_node.add_node()
            node.add_column(data["action"], color.LIGHTBLUE)
            node.add_column(data["last_run"])
            if self.options.verbose:
                node.add_column(data["next_run"])
            node.add_column(data["config_parameter"])
            node.add_column(data["schedule_definition"])

        tree.out()
Exemplo n.º 2
0
def format_service(path, idata, mon_data=None, discard_disabled=False, nodename=None):
    name, namespace, kind = split_path(path)
    svc_notice = get_svc_notice(idata)

    tree = Forest(
        separator=" ",
        widths=(
            (14, None),
            None,
            10,
            None,
        ),
    )
    node_name = tree.add_node()
    node_name.add_column(strip_path(path, os.environ.get("OSVC_NAMESPACE")), color.BOLD)
    node_name.add_column()
    if "cluster" in idata:
        node_name.add_column(idata["cluster"].get("avail", "n/a"), STATUS_COLOR[idata["cluster"].get("avail", "n/a")])
    else:
        node_name.add_column()
    node_name.add_column(svc_notice)
    node_instances = node_name.add_node()
    node_instances.add_column("instances")
    add_instances(node_instances, path, nodename, mon_data)
    if nodename in service_nodes(path, mon_data):
        add_node_node(node_instances, nodename, idata, mon_data, discard_disabled=discard_disabled)
    add_parents(node_name, idata, mon_data, namespace)
    add_children(node_name, idata, mon_data, namespace)
    add_scaler_slaves(node_name, idata, mon_data, namespace)
    add_slaves(node_name, idata, mon_data, namespace)

    tree.out()
Exemplo n.º 3
0
 def test_forest_class():
     """
     Forest class
     """
     tree = Forest()
     tree.load({})
     tree.out()
     overall_node = tree.add_node()
     overall_node.add_column("overall")
     node = overall_node.add_node()
     node.add_column("avail")
     node.add_column()
     node.add_column("up", color.GREEN)
     node = node.add_node()
     node.add_column("res#id")
     node.add_column("....")
     node.add_column("up", color.GREEN)
     col = node.add_column(
         "docker container [email protected]"
         "nsvc.com/busybox:latest")
     col.add_text("warn", color.BROWN)
     col.add_text("err", color.RED)
     node = overall_node.add_node()
     node.add_column("accessory")
     node = overall_node.add_node()
     node.load("loaded text", title="loaded title")
     node = overall_node.add_node()
     node.load({"text": "loaded dict"})
     node = overall_node.add_node()
     node.load([{"text": "loaded list"}])
     buff = str(tree)
     assert "loaded" in buff
Exemplo n.º 4
0
 def print_checks(self, data):
     from forest import Forest
     from rcColor import color
     tree = Forest()
     head_node = tree.add_node()
     head_node.add_column(rcEnv.nodename, color.BOLD)
     for chk_type, instances in data.items():
         node = head_node.add_node()
         node.add_column(chk_type, color.BROWN)
         for instance in instances:
             _node = node.add_node()
             _node.add_column(str(instance["instance"]), color.LIGHTBLUE)
             _node.add_column(instance["path"])
             _node.add_column(str(instance["value"]))
             if instance["driver"] == "generic":
                 _node.add_column()
             else:
                 _node.add_column(instance["driver"])
     tree.out()
Exemplo n.º 5
0
    def print_tree(self, devices=None, verbose=False):
        ftree = Forest()
        node = ftree.add_node()
        node.add_column(rcEnv.nodename, color.BOLD)
        node.add_column("Type", color.BOLD)
        node.add_column("Size", color.BOLD, align="right")
        node.add_column("Pct of Parent", color.BOLD, align="right")

        filtered = devices is not None and len(devices) > 0
        if filtered:
            devs = [self.get_dev_by_devpath(devpath) for devpath in devices]
        else:
            devs = [self.dev[r.child] for r in self.root]
        for dev in devs:
            if dev is None or (not filtered and dev.parents != []):
                continue
            dev.print_dev(node=node, highlight=devices, verbose=verbose)

        ftree.out()
Exemplo n.º 6
0
 def network_status(self):
     data = self.network_status_data(self.options.name)
     if self.options.format in ("json", "flat_json"):
         return data
     from forest import Forest
     from rcColor import color
     tree = Forest()
     head = tree.add_node()
     head.add_column("name", color.BOLD)
     head.add_column("type", color.BOLD)
     head.add_column("network", color.BOLD)
     head.add_column("size", color.BOLD)
     head.add_column("used", color.BOLD)
     head.add_column("free", color.BOLD)
     head.add_column("pct", color.BOLD)
     for name in sorted(data):
         ndata = data[name]
         net_node = head.add_node()
         net_node.add_column(name, color.BROWN)
         net_node.add_column(data[name]["type"])
         net_node.add_column(data[name]["network"])
         net_node.add_column("%d" % data[name]["size"])
         net_node.add_column("%d" % data[name]["used"])
         net_node.add_column("%d" % data[name]["free"])
         net_node.add_column("%.2f%%" % data[name]["pct"])
         if not self.options.verbose:
             continue
         ips_node = net_node.add_node()
         ips_node.add_column("ip", color.BOLD)
         ips_node.add_column("node", color.BOLD)
         ips_node.add_column("service", color.BOLD)
         ips_node.add_column("resource", color.BOLD)
         for ip in sorted(ndata.get("ips", []),
                          key=lambda x:
                          (x["ip"], x["node"], x["path"], x["rid"])):
             ip_node = ips_node.add_node()
             ip_node.add_column(ip["ip"])
             ip_node.add_column(ip["node"])
             ip_node.add_column(ip["path"])
             ip_node.add_column(ip["rid"])
     print(tree)
Exemplo n.º 7
0
    def print_tree_bottom_up(self, devices=None, verbose=False):
        ftree = Forest()
        node = ftree.add_node()
        node.add_column(rcEnv.nodename, color.BOLD)
        node.add_column("Type", color.BOLD)
        node.add_column("Parent Use", color.BOLD, align="right")
        node.add_column("Size", color.BOLD, align="right")
        node.add_column("Ratio", color.BOLD, align="right")

        if devices is None:
            devices = set()
        else:
            devices = set(devices)
        for dev in self.get_bottom_devs():
            if len(devices) > 0 and len(set(dev.devpath) & devices) == 0:
                continue
            dev.print_dev_bottom_up(node=node,
                                    highlight=devices,
                                    verbose=verbose)

        ftree.out()
Exemplo n.º 8
0
class ForestTests(unittest.TestCase):
    def setUp(self):
        self.forest = Forest()

    def assert_equal_sets(self, a, b):
        self.assertEqual(set(a), set(b))

    def assert_size(self, size):
        self.assertEqual(self.forest.size, size)

    def make_x_tree(self):
        f = Forest()
        f.add_children(0, [1, 2])
        f.add_parents(0, [3, 4])
        return f

    def test_empty_forest(self):
        self.assertTrue(self.forest.empty())
        self.assert_size(0)

    def test_exceptions(self):
        with self.assertRaises(NotInForest):
            self.forest.parents(0)

        with self.assertRaises(NotInForest):
            self.forest.children(1)

    def test_is_root(self):
        for node in range(5):
            self.forest.add_node(node)
        
        for node in range(5):
            self.assertTrue(self.forest.is_root(node))

    def test_contains(self):
        for i in range(5):
            self.forest.add_node(i)

        for i in range(5):
            self.assertTrue(i in self.forest)
        
    def test_add_node(self):
        self.forest.add_node(0)
        self.assertEqual(self.forest.size, 1)

    def test_add_children(self):
        self.forest.add_node(0)
        self.forest.add_children(0, [1, 2])
        self.assert_size(3)
        self.assert_equal_sets(self.forest.children(0), [1, 2])

    def test_add_children_to_existing_parent(self):
        self.forest.add_node(0)
        self.forest.add_children(0, [1, 2, 3])
        self.forest.add_children(0, [4, 5])
        self.assert_size(6)
        self.assert_equal_sets(self.forest.children(0), [1, 2, 3, 4, 5])

    def test_add_preexisting_children(self):
        self.forest.add_children(0, [1, 2])
        self.forest.add_children(0, [1, 2, 3])
        self.assert_size(4)
        self.assert_equal_sets(self.forest.children(0), [1, 2, 3])

    def test_add_grandchildren(self):
        self.forest.add_children(0, [1, 2])
        self.forest.add_children(1, [3, 4, 5])
        self.forest.add_children(2, [6, 7, 5])
        self.assert_size(8)
        self.assert_equal_sets(self.forest.children(0), [1, 2])
        self.assert_equal_sets(self.forest.children(1), [3, 4, 5])
        self.assert_equal_sets(self.forest.children(2), [6, 7, 5])

    def test_add_parent(self):
        self.forest.add_node(0)
        self.forest.add_parent(0, 1)
        self.assert_size(2)
        self.assert_equal_sets(self.forest.parents(0), [1])

    def test_add_parent_to_existing_child(self):
        self.forest.add_child(0, 1)
        self.forest.add_parent(1, 2)
        self.assert_size(3)
        self.assert_equal_sets(self.forest.parents(1), [0, 2])

    def test_add_preexisting_parent(self):
        self.forest.add_parents(0, [1, 2, 3])
        self.forest.add_parents(0, [2, 3, 4])
        self.assert_size(5)
        self.assert_equal_sets(self.forest.parents(0), [1, 2, 3, 4])

    def test_add_grandparent(self):
        self.forest.add_parent(0, 1)
        self.forest.add_parent(1, 2)
        self.assert_size(3)
        self.assert_equal_sets(self.forest.parents(0), [1])
        self.assert_equal_sets(self.forest.parents(1), [2])

    def test_roots(self):
        self.forest.add_children(0, [1, 2])
        self.forest.add_children(3, [4, 5])
        self.forest.add_children(6, [5, 1])
        self.assert_equal_sets(self.forest.roots, [0, 3, 6])

    def test_roots_with_intersected_tree(self):
        pass

    def test_eq(self):
        self.forest.add_children(3, [4, 5])
        self.forest.add_children(4, [6, 7])
        self.forest.add_children(5, [8 ,9])
        self.assert_size(7)
        other_forest = Forest()
        other_forest.add_children(3, [4, 5])
        other_forest.add_children(4, [6, 7])
        other_forest.add_children(5, [8 ,9])
        self.assertEqual(other_forest.size, self.forest.size)
        self.assertEqual(other_forest, self.forest)

    def test_neq(self):
        self.forest.add_children(3, [4, 5])
        self.forest.add_children(4, [6, 7])
        self.forest.add_children(5, [8 ,9])
        self.assert_size(7)
        other_forest = Forest()
        self.assertEqual(other_forest.size, 0)
        self.assertNotEqual(other_forest, self.forest)

    def test_replace(self):
        self.forest = self.make_x_tree()
        self.assert_size(5)
        self.forest.replace(0, 5)
        self.assert_size(5)
        self.assertTrue(5 in self.forest)
        self.assert_equal_sets(self.forest.children(5), [1, 2])
        self.assert_equal_sets(self.forest.parents(5), [3, 4])
        self.assertFalse(0 in self.forest)

    def test_replace_nonexistent_raises_error(self):
        with self.assertRaises(NotInForest):
            self.forest.replace(0, 1)

    def test_subtree_leaf(self):
        self.forest.add_children(0, [1, 2, 3])
        expected = Forest().add_node(1)

        leaf = self.forest.subtree(1)

        self.assert_size(4)
        self.assertEqual(leaf.size, 1)
        self.assertEqual(leaf, expected)

    def test_subtree_tree(self):
        self.forest = self.make_x_tree()
        expected = Forest().add_children(0, [1, 2])

        tree = self.forest.subtree(0)

        self.assert_size(5)
        self.assertEqual(tree.size, 3)
        self.assertEqual(tree, expected)

    def test_subtree_with_loop(self):
        self.forest = self.make_x_tree()
        self.forest.add_parents(5, [1, 2])
        self.forest.add_child(5, 6)
        expected = self.make_x_tree()
        expected.add_child(5, 6)

        tree = self.forest.subtree(0)

        self.assert_size(7)
        self.assertEqual(tree.size, 5)
        self.assertEqual(tree, expected)

    def test_bfs(self):
        pass