示例#1
0
def test_BGP_summary():
    bgp_summary_text = """

IPv4 Unicast Summary:
BGP router identifier 10.53.151.115, local AS number 65310 vrf-id 0
BGP table version 415
RIB entries 79, using 12 KiB of memory
Peers 2, using 41 KiB of memory

Neighbor        V         AS MsgRcvd MsgSent   TblVer  InQ OutQ  Up/Down State/PfxRcd
172.27.232.15   4      64794  317684  264701        0    0    0 01w6d21h           40
172.27.233.15   4      64894  260245  217685        0    0    0 3d15h04m           40

Total number of neighbors 2
"""

    expected = [{
        'AS': 64794,
        'InQ': 0,
        'MsgRcvd': 317684,
        'MsgSent': 264701,
        'Neighbor': '172.27.232.15',
        'OutQ': 0,
        'PfxRcvd': 40,
        'TblVer': 0,
        'Up/Down': '01w6d21h',
        'Version': 4
    }, {
        'AS': 64894,
        'InQ': 0,
        'MsgRcvd': 260245,
        'MsgSent': 217685,
        'Neighbor': '172.27.233.15',
        'OutQ': 0,
        'PfxRcvd': 40,
        'TblVer': 0,
        'Up/Down': '3d15h04m',
        'Version': 4
    }]

    b = Linux.BGPSummary(debug=True)
    output = bgp_summary_text.splitlines()
    json_dict = b.convert_to_json(output)
    assert json_dict == expected
    def run(self, local_info, router_context, gql_token, fp):
        status = Output.Status.OK

        test_info = self.test_info(local_info, router_context)
        self.output.test_start(test_info, status=status)
        params = self.get_params()

        self.output.progress_start(fp)

        if self.check_user("root") != Output.Status.OK:
            return self.output.test_end(fp)

        router_context.query_node_info()

        # find the node with the active RouteManager and its primary etc.
        active_node_id = router_context.active_process_node('routingManager')
        if active_node_id == '':
            self.output.proc_inactive_routing_manager(local_info,
                                                      router_context)
            return self.output.test_end(fp)

        active_node_type = router_context.node_type(active_node_id)
        if local_info.get_node_type() != 'conductor' and \
           local_info.get_node_name() != active_node_id:
            self.output.proc_inactive_routing_manager(local_info,
                                                      router_context)
            return self.output.test_end(fp)

        error_lines = []
        json_data = []
        bs = Linux.BGPSummary(self.debug, progobj=self)
        # ugly..
        self.fp = fp
        retval = bs.run_linux_args(local_info, router_context,
                                   active_node_type, error_lines, json_data)
        # ugly...
        self.fp = None

        # Lame, but currently the only way to detect an error...
        if len(error_lines) > 0:
            self.output.proc_run_linux_error(retval, error_lines[0])
            return self.output.test_end(fp)

        if self.debug:
            print('........ flattened list ..........')
            pprint.pprint(json_data)

        stats = {}
        Output.init_result_stats(stats)
        stats["total_count"] = len(json_data)

        if params["minimum_count"] > 0 and \
           stats["total_count"] < params["minimum_count"]:
            self.output.proc_too_few_peers(stats, params)
            return self.output.test_end(fp)

        engine = EntryTest.Parser(self.debug)
        for entry in json_data:
            if engine.exclude_entry(entry, params["exclude_tests"]):
                stats["exclude_count"] += 1
                continue
            test_result = engine.eval_entry_by_tests(entry,
                                                     params["entry_tests"])
            Output.update_stats(stats, test_result)
            self.output.proc_entry_result(entry)

        if stats["FAIL"] > 0:
            status = Output.Status.FAIL
        elif stats["WARN"] > 0:
            status = Output.Status.WARN
        elif stats["PASS"] == stats["total_count"]:
            status = Output.Status.OK

        self.output.proc_test_result(active_node_id,
                                     params["entry_tests"],
                                     stats,
                                     status=status)
        return self.output.test_end(fp)