]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/tools/advisor/test/test_db_log_parser.py
1 # Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 # This source code is licensed under both the GPLv2 (found in the
3 # COPYING file in the root directory) and Apache 2.0 License
4 # (found in the LICENSE.Apache file in the root directory).
9 from advisor
.db_log_parser
import DatabaseLogs
, Log
, NO_COL_FAMILY
10 from advisor
.rule_parser
import Condition
, LogCondition
13 class TestLog(unittest
.TestCase
):
15 self
.column_families
= ["default", "col_fam_A"]
17 def test_get_column_family(self
):
19 "2018/05/25-14:34:21.047233 7f82ba72e700 [db/flush_job.cc:371] "
20 + "[col_fam_A] [JOB 44] Level-0 flush table #84: 1890780 bytes OK"
22 db_log
= Log(test_log
, self
.column_families
)
23 self
.assertEqual("col_fam_A", db_log
.get_column_family())
26 "2018/05/25-14:34:21.047233 7f82ba72e700 [db/flush_job.cc:371] "
27 + "[JOB 44] Level-0 flush table #84: 1890780 bytes OK"
29 db_log
= Log(test_log
, self
.column_families
)
30 db_log
.append_message("[default] some remaining part of log")
31 self
.assertEqual(NO_COL_FAMILY
, db_log
.get_column_family())
33 def test_get_methods(self
):
34 hr_time
= "2018/05/25-14:30:25.491635"
35 context
= "7f82ba72e700"
37 "[db/flush_job.cc:331] [default] [JOB 10] Level-0 flush table "
40 test_log
= hr_time
+ " " + context
+ " " + message
41 db_log
= Log(test_log
, self
.column_families
)
42 self
.assertEqual(db_log
.get_message(), message
)
43 remaining_message
= "[col_fam_A] some more logs"
44 db_log
.append_message(remaining_message
)
45 self
.assertEqual(db_log
.get_human_readable_time(), "2018/05/25-14:30:25.491635")
46 self
.assertEqual(db_log
.get_context(), "7f82ba72e700")
47 self
.assertEqual(db_log
.get_timestamp(), 1527258625)
48 self
.assertEqual(db_log
.get_message(), str(message
+ "\n" + remaining_message
))
50 def test_is_new_log(self
):
51 new_log
= "2018/05/25-14:34:21.047233 context random new log"
52 remaining_log
= "2018/05/25 not really a new log"
53 self
.assertTrue(Log
.is_new_log(new_log
))
54 self
.assertFalse(Log
.is_new_log(remaining_log
))
57 class TestDatabaseLogs(unittest
.TestCase
):
58 def test_check_and_trigger_conditions(self
):
59 this_path
= os
.path
.abspath(os
.path
.dirname(__file__
))
60 logs_path_prefix
= os
.path
.join(this_path
, "input_files/LOG-0")
61 column_families
= ["default", "col-fam-A", "col-fam-B"]
62 db_logs
= DatabaseLogs(logs_path_prefix
, column_families
)
63 # matches, has 2 col_fams
64 condition1
= LogCondition
.create(Condition("cond-A"))
65 condition1
.set_parameter("regex", "random log message")
66 # matches, multiple lines message
67 condition2
= LogCondition
.create(Condition("cond-B"))
68 condition2
.set_parameter("regex", "continuing on next line")
70 condition3
= LogCondition
.create(Condition("cond-C"))
71 condition3
.set_parameter("regex", "this should match no log")
72 db_logs
.check_and_trigger_conditions([condition1
, condition2
, condition3
])
73 cond1_trigger
= condition1
.get_trigger()
74 self
.assertEqual(2, len(cond1_trigger
.keys()))
75 self
.assertSetEqual({"col-fam-A", NO_COL_FAMILY
}, set(cond1_trigger
.keys()))
76 self
.assertEqual(2, len(cond1_trigger
["col-fam-A"]))
78 "[db/db_impl.cc:563] [col-fam-A] random log message for testing",
79 "[db/db_impl.cc:653] [col-fam-A] another random log message",
81 self
.assertIn(cond1_trigger
["col-fam-A"][0].get_message(), messages
)
82 self
.assertIn(cond1_trigger
["col-fam-A"][1].get_message(), messages
)
83 self
.assertEqual(1, len(cond1_trigger
[NO_COL_FAMILY
]))
85 cond1_trigger
[NO_COL_FAMILY
][0].get_message(),
86 "[db/db_impl.cc:331] [unknown] random log message no column family",
88 cond2_trigger
= condition2
.get_trigger()
89 self
.assertEqual(["col-fam-B"], list(cond2_trigger
.keys()))
90 self
.assertEqual(1, len(cond2_trigger
["col-fam-B"]))
92 cond2_trigger
["col-fam-B"][0].get_message(),
93 "[db/db_impl.cc:234] [col-fam-B] log continuing on next line\n"
94 + "remaining part of the log",
96 self
.assertIsNone(condition3
.get_trigger())