Пример #1
0
class LogQuerierTest(unittest.TestCase):
    @staticmethod
    def custom_post_url_http(url, params):
        global query_list
        query_list.append(json.loads(params))
        return '{"href": "/analytics/query/a415fe1e-51cb-11e5-aab0-00000a540d2d"}'

    @staticmethod
    def custom_get_query_result(opserver_ip, opserver_port, qid):
        try:
            return query_result[test_num]
        except KeyError:
            return []

    def custom_display(self, result):
        if result == [] or result is None:
            return
        try:
            self.assertTrue(result == query_result[test_num])
        except KeyError:
            self.assertTrue(False)

    def custom_process_start(self):
        self._querier.display(self._querier.read_result(
            query_result[test_num]))

    def custom_process_end(self):
        return

    def setUp(self):
        self.maxDiff = None
        self._querier = LogQuerier()

        flexmock(OpServerUtils).should_receive('post_url_http').replace_with(
            lambda x, y, w, z: self.custom_post_url_http(x, y))
        self.query_expectations = flexmock(OpServerUtils).should_receive(
            'get_query_result').replace_with(
                lambda x, y, z, a, b: self.custom_get_query_result(x, y, z))
        self.display_expectations = flexmock(LogQuerier).should_receive(
            'display').replace_with(lambda x: self.custom_display(x))
        self.process_start_expectations = flexmock(Process).should_receive(
            'start').replace_with(lambda: self.custom_process_start())
        self.process_end_expectations = flexmock(Process).should_receive(
            'join').replace_with(lambda: self.custom_process_end())

    #@unittest.skip("skip test_1_no_arg")
    def test_1_no_arg(self):
        global test_num
        global query_list
        query_list = []
        test_num = 1

        argv = sys.argv
        sys.argv = "contrail-logs".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable"}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    # a few args
    #@unittest.skip("skip test_2_message_query")
    def test_2_message_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 2

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 --node-type Analytics --module contrail-collector --instance-id 0 --message-type GeneratorDbStatsUve".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "GeneratorDbStatsUve", "op": 1}]], "filter": [[{"suffix": null, "value2": null, "name": "NodeType", "value": "Analytics", "op": 1}, {"suffix": null, "value2": null, "name": "InstanceId", "value": 0, "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    # a object values query
    #@unittest.skip("skip test_3_object_value")
    def test_3_object_value(self):
        global test_num
        global query_list
        query_list = []
        test_num = 3

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-values".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"table": "ConfigObjectTable", "select_fields": ["ObjectId"]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    # a object id query
    #@unittest.skip("skip test_4_object_id")
    def test_4_object_id(self):
        global test_num
        global query_list
        query_list = []
        test_num = 4

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-id virtual_network:default-domain:admin:vn1-take2".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Messagetype", "ObjectLog", "SystemLog"], "table": "ConfigObjectTable", "where": [[{"suffix": null, "value2": null, "name": "ObjectId", "value": "virtual_network:default-domain:admin:vn1-take2", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    # prefix query
    #@unittest.skip("skip test_5_prefix_query")
    def test_5_prefix_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 5

        argv = sys.argv
        sys.argv = "contrail-logs --source node* --message-type UveVirtualNetwork*".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "node", "op": 7}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "UveVirtualNetwork", "op": 7}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    # end test_5_prefix_query

    #@unittest.skip("skip test_6_long_query")
    def test_6_long_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 6

        argv = sys.argv
        sys.argv = "contrail-logs --last 25m".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable"}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(self.query_expectations.times_called, 3)
        self.assertEqual(self.display_expectations.times_called, 4)
        self.assertEqual(self.process_start_expectations.times_called, 3)
        for i in range(len(query_list) - 1):
            self.assertEqual(
                int(query_list[i]['end_time']) -
                int(query_list[i]['start_time']), 10 * 60 * pow(10, 6))
            del query_list[i]['start_time']
            del query_list[i]['end_time']
            self.assertEqual(expected_result_dict, query_list[i])
        self.assertEqual(
            int(query_list[2]['end_time']) - int(query_list[2]['start_time']),
            5 * 60 * pow(10, 6) - 2)
        del query_list[2]['start_time']
        del query_list[2]['end_time']
        self.assertEqual(expected_result_dict, query_list[2])

    #@unittest.skip("skip test_8_multiple_sources_query")
    def test_7_multiple_sources_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 7

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 a6s30 --category __default__".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s30", "op": 1}]], "filter": [[{"suffix": null, "value2": null, "name": "Category", "value": "__default__", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    #@unittest.skip("skip test_8_multiple_modules_query")
    def test_8_multiple_modules_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 8

        argv = sys.argv
        sys.argv = "contrail-logs --module contrail-control contrail-collector --source a6s45".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-control", "op": 1}], [ {"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    #@unittest.skip("skip test_9_multiple_message_types_query")
    def test_9_multiple_message_types_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 9

        argv = sys.argv
        sys.argv = "contrail-logs --message-type CollectorDbStatsTrace Alarm* ".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Messagetype", "value": "CollectorDbStatsTrace", "op": 1}], [{"suffix": null, "value2": null, "name": "Messagetype", "value": "Alarm", "op": 7}]]}'

        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)

    #@unittest.skip("skip test_10_multiple_or_query")
    def test_10_multiple_or_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 10

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 a6s35 --module contrail-control contrail-collector contrail-analytics-api".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-control", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-analytics-api", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s35", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-control", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s35", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s35", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-analytics-api", "op": 1}]]}'

        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(
            int(query_list[0]['end_time']) - int(query_list[0]['start_time']),
            10 * 60 * pow(10, 6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called, 1)
        self.assertEqual(self.display_expectations.times_called, 2)
        self.assertEqual(self.process_start_expectations.times_called, 1)
Пример #2
0
class LogQuerierTest(unittest.TestCase):
    @staticmethod
    def custom_post_url_http(url, params):
        global query_dict
        query_dict = json.loads(params)
        return '{"href": "/analytics/query/a415fe1e-51cb-11e5-aab0-00000a540d2d"}'

    @staticmethod
    def custom_get_query_result(opserver_ip, opserver_port, qid):
        try:
            return query_result[test_num]
        except KeyError:
            return []

    def custom_display(self, result):
        try:
            self.assertTrue(result == query_result[test_num])
        except KeyError:
            self.assertTrue(False)

    def setUp(self):
        self.maxDiff = None
        self._querier = LogQuerier()

        flexmock(OpServerUtils).should_receive('post_url_http').replace_with(
            lambda x, y: self.custom_post_url_http(x, y))
        flexmock(OpServerUtils).should_receive(
            'get_query_result').replace_with(
                lambda x, y, z: self.custom_get_query_result(x, y, z))
        flexmock(self._querier).should_receive('display').replace_with(
            lambda x: self.custom_display(x))

    #@unittest.skip("skip test_1_no_arg")
    def test_1_no_arg(self):
        global test_num
        global query_dict
        test_num = 1

        argv = sys.argv
        sys.argv = "contrail-logs".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable"}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # a few args
    #@unittest.skip("skip test_2_message_query")
    def test_2_message_query(self):
        global test_num
        global query_dict
        test_num = 2

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 --node-type Analytics --module contrail-collector --instance-id 0 --message-type GeneratorDbStatsUve".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "GeneratorDbStatsUve", "op": 1}]], "filter": [[{"suffix": null, "value2": null, "name": "NodeType", "value": "Analytics", "op": 1}, {"suffix": null, "value2": null, "name": "InstanceId", "value": 0, "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # a object values query
    #@unittest.skip("skip test_3_object_value")
    def test_3_object_value(self):
        global test_num
        global query_dict
        test_num = 3

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-values".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"table": "ConfigObjectTable", "start_time": "now-10m", "end_time": "now", "select_fields": ["ObjectId"]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # a object id query
    #@unittest.skip("skip test_4_object_id")
    def test_4_object_id(self):
        global test_num
        global query_dict
        test_num = 4

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-id virtual_network:default-domain:admin:vn1-take2".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Messagetype", "ObjectLog", "SystemLog"], "table": "ConfigObjectTable", "where": [[{"suffix": null, "value2": null, "name": "ObjectId", "value": "virtual_network:default-domain:admin:vn1-take2", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # prefix query
    #@unittest.skip("skip test_5_prefix_query")
    def test_5_prefix_query(self):
        global test_num
        global query_dict
        test_num = 5

        argv = sys.argv
        sys.argv = "contrail-logs --source node* --message-type UveVirtualNetwork*".split(
        )
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "node", "op": 7}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "UveVirtualNetwork", "op": 7}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)
class LogQuerierTest(unittest.TestCase):

    @staticmethod
    def custom_post_url_http(url, params):
        global query_dict
        query_dict = json.loads(params)
        return '{"href": "/analytics/query/a415fe1e-51cb-11e5-aab0-00000a540d2d"}'

    @staticmethod
    def custom_get_query_result(opserver_ip, opserver_port, qid):
        try:
            return query_result[test_num]
        except KeyError:
            return []

    def custom_display(self, result):
        try:
            self.assertTrue(result == query_result[test_num])
        except KeyError:
            self.assertTrue(False)

    def setUp(self):
        self.maxDiff = None
        self._querier = LogQuerier()

        flexmock(OpServerUtils).should_receive('post_url_http').replace_with(lambda x, y, w, z: self.custom_post_url_http(x, y))
        flexmock(OpServerUtils).should_receive('get_query_result').replace_with(lambda x, y, z, a, b: self.custom_get_query_result(x, y, z))
        flexmock(self._querier).should_receive('display').replace_with(lambda x: self.custom_display(x))


    #@unittest.skip("skip test_1_no_arg")
    def test_1_no_arg(self):
        global test_num
        global query_dict
        test_num = 1

        argv = sys.argv
        sys.argv = "contrail-logs".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable"}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # a few args
    #@unittest.skip("skip test_2_message_query")
    def test_2_message_query(self):
        global test_num
        global query_dict
        test_num = 2

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 --node-type Analytics --module contrail-collector --instance-id 0 --message-type GeneratorDbStatsUve".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "GeneratorDbStatsUve", "op": 1}]], "filter": [[{"suffix": null, "value2": null, "name": "NodeType", "value": "Analytics", "op": 1}, {"suffix": null, "value2": null, "name": "InstanceId", "value": 0, "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # a object values query
    #@unittest.skip("skip test_3_object_value")
    def test_3_object_value(self):
        global test_num
        global query_dict
        test_num = 3

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-values".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"table": "ConfigObjectTable", "start_time": "now-10m", "end_time": "now", "select_fields": ["ObjectId"]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # a object id query
    #@unittest.skip("skip test_4_object_id")
    def test_4_object_id(self):
        global test_num
        global query_dict
        test_num = 4

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-id virtual_network:default-domain:admin:vn1-take2".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Messagetype", "ObjectLog", "SystemLog"], "table": "ConfigObjectTable", "where": [[{"suffix": null, "value2": null, "name": "ObjectId", "value": "virtual_network:default-domain:admin:vn1-take2", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)

    # prefix query
    #@unittest.skip("skip test_5_prefix_query")
    def test_5_prefix_query(self):
        global test_num
        global query_dict
        test_num = 5

        argv = sys.argv
        sys.argv = "contrail-logs --source node* --message-type UveVirtualNetwork*".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "start_time": "now-10m", "sort_fields": ["MessageTS"], "end_time": "now", "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "node", "op": 7}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "UveVirtualNetwork", "op": 7}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(expected_result_dict, query_dict)
Пример #4
0
class LogQuerierTest(unittest.TestCase):

    @staticmethod
    def custom_post_url_http(url, params):
        global query_list
        query_list.append(json.loads(params))
        return '{"href": "/analytics/query/a415fe1e-51cb-11e5-aab0-00000a540d2d"}'

    @staticmethod
    def custom_get_query_result(opserver_ip, opserver_port, qid):
        try:
            return query_result[test_num]
        except KeyError:
            return []

    def custom_display(self, result):
        if result == [] or result is None:
            return
        try:
            self.assertTrue(result == query_result[test_num])
        except KeyError:
            self.assertTrue(False)

    def custom_process_start(self):
        self._querier.display(self._querier.read_result(query_result[test_num]))

    def custom_process_end(self):
        return

    def setUp(self):
        self.maxDiff = None
        self._querier = LogQuerier()

        flexmock(OpServerUtils).should_receive('post_url_http').replace_with(lambda x, y, w, z: self.custom_post_url_http(x, y))
        self.query_expectations = flexmock(OpServerUtils).should_receive('get_query_result').replace_with(lambda x, y, z, a, b: self.custom_get_query_result(x, y, z))
        self.display_expectations = flexmock(LogQuerier).should_receive('display').replace_with(lambda x: self.custom_display(x))
        self.process_start_expectations = flexmock(Process).should_receive('start').replace_with(lambda:self.custom_process_start())
        self.process_end_expectations = flexmock(Process).should_receive('join').replace_with(lambda:self.custom_process_end())

    #@unittest.skip("skip test_1_no_arg")
    def test_1_no_arg(self):
        global test_num
        global query_list
        query_list = []
        test_num = 1

        argv = sys.argv
        sys.argv = "contrail-logs".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable"}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)

    # a few args
    #@unittest.skip("skip test_2_message_query")
    def test_2_message_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 2

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 --node-type Analytics --module contrail-collector --instance-id 0 --message-type GeneratorDbStatsUve".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "GeneratorDbStatsUve", "op": 1}]], "filter": [[{"suffix": null, "value2": null, "name": "NodeType", "value": "Analytics", "op": 1}, {"suffix": null, "value2": null, "name": "InstanceId", "value": 0, "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)

    # a object values query
    #@unittest.skip("skip test_3_object_value")
    def test_3_object_value(self):
        global test_num
        global query_list
        query_list = []
        test_num = 3

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-values".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"table": "ConfigObjectTable", "select_fields": ["ObjectId"]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)

    # a object id query
    #@unittest.skip("skip test_4_object_id")
    def test_4_object_id(self):
        global test_num
        global query_list
        query_list = []
        test_num = 4

        argv = sys.argv
        sys.argv = "contrail-logs --object-type config --object-id virtual_network:default-domain:admin:vn1-take2".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Messagetype", "ObjectLog", "SystemLog"], "table": "ConfigObjectTable", "where": [[{"suffix": null, "value2": null, "name": "ObjectId", "value": "virtual_network:default-domain:admin:vn1-take2", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)

    # prefix query
    #@unittest.skip("skip test_5_prefix_query")
    def test_5_prefix_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 5

        argv = sys.argv
        sys.argv = "contrail-logs --source node* --message-type UveVirtualNetwork*".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "node", "op": 7}, {"suffix": null, "value2": null, "name": "Messagetype", "value": "UveVirtualNetwork", "op": 7}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)
    # end test_5_prefix_query

    #@unittest.skip("skip test_6_long_query")
    def test_6_long_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 6

        argv = sys.argv
        sys.argv = "contrail-logs --last 25m".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable"}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(self.query_expectations.times_called,3)
        self.assertEqual(self.display_expectations.times_called,4)
        self.assertEqual(self.process_start_expectations.times_called,3)
        for i in range(len(query_list) - 1):
            self.assertEqual(int(query_list[i]['end_time']) - int(query_list[i]['start_time']),10*60*pow(10,6))
            del query_list[i]['start_time']
            del query_list[i]['end_time']
            self.assertEqual(expected_result_dict, query_list[i])
        self.assertEqual(int(query_list[2]['end_time']) - int(query_list[2]['start_time']),5*60*pow(10,6) - 2)
        del query_list[2]['start_time']
        del query_list[2]['end_time']
        self.assertEqual(expected_result_dict, query_list[2])

    #@unittest.skip("skip test_8_multiple_sources_query")
    def test_7_multiple_sources_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 7

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 a6s30 --category __default__".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "Category", "value": "__default__", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s30", "op": 1}, {"suffix": null, "value2": null, "name": "Category", "value": "__default__", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)

    #@unittest.skip("skip test_8_multiple_modules_query")
    def test_8_multiple_modules_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 8

        argv = sys.argv
        sys.argv = "contrail-logs --module contrail-control contrail-collector --source a6s45".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-control", "op": 1}], [ {"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}]]}'
        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)

    #@unittest.skip("skip test_9_multiple_message_types_query")
    def test_9_multiple_message_types_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 9

        argv = sys.argv
        sys.argv = "contrail-logs --message-type CollectorDbStatsTrace Alarm* ".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Messagetype", "value": "CollectorDbStatsTrace", "op": 1}], [{"suffix": null, "value2": null, "name": "Messagetype", "value": "Alarm", "op": 7}]]}'

        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)

    #@unittest.skip("skip test_10_multiple_or_query")
    def test_10_multiple_or_query(self):
        global test_num
        global query_list
        query_list = []
        test_num = 10

        argv = sys.argv
        sys.argv = "contrail-logs --source a6s45 a6s35 --module contrail-control contrail-collector contrail-analytics-api".split()
        self._querier.run()
        sys.argv = argv

        expected_result_str = '{"sort": 1, "sort_fields": ["MessageTS"], "select_fields": ["MessageTS", "Source", "ModuleId", "Category", "Messagetype", "SequenceNum", "Xmlmessage", "Type", "Level", "NodeType", "InstanceId"], "table": "MessageTable", "where": [[{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-control", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s45", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-analytics-api", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s35", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-control", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s35", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-collector", "op": 1}], [{"suffix": null, "value2": null, "name": "Source", "value": "a6s35", "op": 1}, {"suffix": null, "value2": null, "name": "ModuleId", "value": "contrail-analytics-api", "op": 1}]]}'

        expected_result_dict = json.loads(expected_result_str)
        self.assertEqual(int(query_list[0]['end_time']) - int(query_list[0]['start_time']),10*60*pow(10,6))
        del query_list[0]['start_time']
        del query_list[0]['end_time']
        self.assertEqual(expected_result_dict, query_list[0])
        self.assertEqual(self.query_expectations.times_called,1)
        self.assertEqual(self.display_expectations.times_called,2)
        self.assertEqual(self.process_start_expectations.times_called,1)