How to use check_log_format method in lettuce-tools

Best Python code snippet using lettuce-tools_python

test_check_widget_snapshot.py

Source:test_check_widget_snapshot.py Github

copy

Full Screen

...54 matches = re.fullmatch(re_expect, actual)55 if not matches:56 return ()57 return matches.groups()58def check_log_format(log_all_args, expected):59 """60 Assert that log arguments would form the expected message61 :param log_args: the arguments given to log.info/warn/debug/error function62 :param expect: the expected output of the log, when those arguments given63 """64 def partial_eq(actual, expect, ignore='***'):65 expected_parts = expect.split(ignore)66 exp_part = expected_parts.pop(0)67 if exp_part:68 assert actual.startswith(exp_part)69 actual = actual[len(exp_part):]70 while expected_parts:71 exp_part = expected_parts.pop(0)72 if not exp_part:73 break74 assert exp_part75 found = actual.find(exp_part)76 assert found > 077 actual = actual[found + len(exp_part):]78 log_args = log_all_args[0]79 actual_msg = log_args[0] % log_args[1:]80 assert actual_msg == expected or eq_portions(actual_msg, expected)81# from doctest import run_docstring_examples82# run_docstring_examples(eq_portions, globals())83class TestCaseChecker:84 @pytest.fixture(autouse=True)85 def qt_app(self):86 self.app = QApplication.instance()87 if self.app is None:88 self.app = QApplication([])89 def except_hook(*args):90 traceback.print_exception(*args)91 prev_hook = sys.excepthook92 sys.excepthook = except_hook93 yield self.app94 sys.excepthook = prev_hook95 def test1_gen_first_image(self):96 ref_image_path = non_existent_path('test1_gen_first_image.png')97 files_before = set(Path(__file__).parent.glob('*'))98 widget = QLabel('test')99 check_widget_snapshot(widget, __file__, str(ref_image_path))100 assert ref_image_path.exists()101 files_after = set(Path(__file__).parent.glob('*'))102 assert files_after.difference(files_before) == set([ref_image_path.resolve()])103 ref_image_path.unlink()104 def test_log_and_ref_folder_instead_of_file(self):105 ref_image_path = non_existent_path('test_ref_folder_instead_of_file.png')106 folder = Path(__file__).parent107 files_before = set(folder.glob('*'))108 widget = QLabel('test')109 log = Mock()110 check_widget_snapshot(widget, str(folder), str(ref_image_path), log=log)111 assert ref_image_path.exists()112 assert log.info.call_args == call('Generating ref snapshot %s in %s for widget %s',113 ref_image_path.name,114 folder, widget)115 expected_log = 'Generating ref snapshot test_ref_folder_instead_of_file.png in +++\\tests for widget ***'116 check_log_format(log.info.call_args, expected_log)117 files_after = set(folder.glob('*'))118 assert files_after.difference(files_before) == set([ref_image_path.resolve()])119 assert files_after.issuperset(files_before)120 ref_image_path.unlink()121 def test2_old_results(self):122 ref_image_path = non_existent_path('test2_old_results.png')123 # create two bogus files that pretend to be previous results:124 actual_img_path = Path(ref_image_path.stem + '_actual.png')125 actual_img_path.write_text('')126 diff_img_path = Path(ref_image_path.stem + '_diff.png')127 diff_img_path.write_text('')128 assert actual_img_path.exists()129 assert diff_img_path.exists()130 # check widget snapshot, with delete-old = False, verify results files still there:131 files_before = set(Path(__file__).parent.glob('*'))132 widget = QLabel('test')133 assert check_widget_snapshot(widget, __file__, ref_image_path.stem, delete_old_results=False)134 ref_image_path.unlink()135 assert actual_img_path.exists()136 assert diff_img_path.exists()137 files_after = set(Path(__file__).parent.glob('*'))138 assert files_after == files_before139 # check it again, this time results removed:140 actual_img_path_str = actual_img_path.resolve()141 diff_img_path_str = diff_img_path.resolve()142 assert check_widget_snapshot(widget, __file__, ref_image_path.stem)143 ref_image_path.unlink()144 assert not actual_img_path.exists()145 assert not diff_img_path.exists()146 files_after = set(Path(__file__).parent.glob('*'))147 assert files_before.difference(files_after) == set([actual_img_path_str, diff_img_path_str])148 assert files_before.issuperset(files_before)149 def test_equal_images(self):150 ref_image_path = non_existent_path('test_equal_images.png')151 # generate reference:152 files_before = set(Path(__file__).parent.glob('*'))153 widget = QLabel('test')154 assert check_widget_snapshot(widget, __file__, ref_image_path.stem)155 # re-check: should find images are identical:156 assert check_widget_snapshot(widget, __file__, ref_image_path.stem)157 assert check_widget_snapshot(widget, __file__, ref_image_path.stem)158 ref_image_path.unlink()159 files_after = set(Path(__file__).parent.glob('*'))160 assert files_before == files_after161 def test_unequal_images_diff_less_than_tol(self, mocker):162 ref_image_path = non_existent_path('test_unequal_images_diff_less_than_tol.png')163 class ImgDiffer_SameWithinTol:164 def get_diff(self, image, ref_image):165 return None166 def report(self):167 return "report"168 # generate reference:169 files_before = set(Path(__file__).parent.glob('*'))170 widget = QLabel('test')171 assert check_widget_snapshot(widget, __file__, ref_image_path.stem)172 # pretend label has changed, but less than tolerance (get_diff() returns None):173 widget = QLabel('test2')174 widget.setObjectName('label')175 mock_log = Mock()176 mock_timer = mocker.patch.object(pyqt_test_sandals, 'perf_counter', side_effect=[0, 123])177 assert check_widget_snapshot(widget, __file__, ref_image_path.stem,178 img_differ=ImgDiffer_SameWithinTol(), log=mock_log)179 ref_image_path.unlink()180 assert mock_log.info.mock_calls == [181 call('Widget %s vs ref %s in %s (%.2f sec):',182 'label', ref_image_path.name, ref_image_path.parent.resolve(), 123),183 call(' report')184 ]185 expect = 'Widget label vs ref test_unequal_images_diff_less_than_tol.png in +++\\tests (123.00 sec):'186 check_log_format(mock_log.info.call_args_list[0], expect)187 # confirm that no results files were created:188 files_after = set(Path(__file__).parent.glob('*'))189 assert files_after == files_before190 def test_unequal_images(self, mocker):191 ref_image_path = non_existent_path('test_unequal_images.png')192 class ImgDiffer:193 def get_diff(self, image, ref_image):194 return QImage(image)195 def report(self):196 return "report"197 # generate reference:198 widget = QLabel('test')199 assert check_widget_snapshot(widget, __file__, ref_image_path.stem)200 # pretend label has changed, but less than tolerance (get_diff() returns None):201 widget = QLabel('test2')202 widget.setObjectName('label')203 mock_log = Mock()204 files_before = set(Path(__file__).parent.glob('*'))205 mock_timer = mocker.patch.object(pyqt_test_sandals, 'perf_counter', side_effect=[0, 123])206 assert not check_widget_snapshot(widget, __file__, ref_image_path.stem,207 img_differ=ImgDiffer(), log=mock_log)208 assert mock_log.method_calls == [209 call.info('Widget %s vs ref %s in %s (%.2f sec):', 'label', ref_image_path.name,210 ref_image_path.parent.resolve(), 123),211 call.info(' report'),212 call.warn(' Snapshot has changed beyond tolerances, saving actual and diff images to folder %s:',213 ref_image_path.parent.resolve()),214 call.warn(' Saving actual image to %s', 'test_unequal_images_actual.png'),215 call.warn(' Saving diff image (White - |ref - widget|) to %s', 'test_unequal_images_diff.png')216 ]217 check_log_format(mock_log.info.call_args_list[0], 'Widget label vs ref test_unequal_images.png in ***\\tests (123.00 sec):')218 # confirm that no results files were create:219 files_after = set(Path(__file__).parent.glob('*'))220 ref_image_path.unlink()221 assert files_after.issuperset(files_before)222 actual_img_path = Path('test_unequal_images_actual.png')223 diff_img_path = Path('test_unequal_images_diff.png')224 assert files_after.difference(files_before) == set([actual_img_path.resolve(), diff_img_path.resolve()])225 actual_img_path.unlink()226 diff_img_path.unlink()227 def test_custom_differ(self):228 ref_image_path = non_existent_path('test_custom_differ.png')229 # generate reference:230 widget = QLabel('test')231 assert check_widget_snapshot(widget, __file__, ref_image_path.stem)...

Full Screen

Full Screen

fg_log_parser.py

Source:fg_log_parser.py Github

copy

Full Screen

...57 if kvdelim in field:58 key, value = field.split(kvdelim)59 logline[key] = value60 return logline61def check_log_format(line, srcipfield, dstipfield):62 """63 checks if srcipfield and dstipfield are in logline64 Examples:65 >>> line ='srcip=192.168.1.1 dstip=8.8.8.8 dstport=53 proto=53'66 >>> check_log_format(line, "srcip", "dstip")67 True68 >>> line ='srcip=192.168.1.1 dstport=53 proto=53'69 >>> check_log_format(line, "srcip", "dstip")70 False71 >>> line = ''72 >>> check_log_format(line, "srcip", "dstip")73 False74 """75 log.info("check_log_format: checking line: ")76 log.info(line)77 if srcipfield in line and dstipfield in line:78 log.info("check_log_format: found srcipfield %s", srcipfield)79 log.info("check_log_format: found dstipfield %s", dstipfield)80 return True81 else:82 return False83def translate_protonr(protocolnr):84 """85 Translates ports as names.86 Examples:87 >>> translate_protonr(53)88 5389 >>> translate_protonr(1)90 'ICMP'91 >>> translate_protonr(6)92 'TCP'93 >>> translate_protonr(17)94 'UDP'95 """96 # check if function input was a integer97 # and translate if we know translation98 try:99 if int(protocolnr) == 1:100 return "ICMP" # icmp has protocol nr 1101 elif int(protocolnr) == 6:102 return "TCP" # tcp has protocol nr 6103 elif int(protocolnr) == 17:104 return "UDP" # udp has protocol nr 17105 else:106 return int(protocolnr)107 # if function input was something else than int108 except (ValueError, AttributeError, TypeError):109 return protocolnr110def get_communication_matrix(logfile,111 logformat,112 countbytes=False,113 noipcheck=False,114 showaction=False):115 """116 Reads firewall logfile and returns communication matrix as a dictionary.117 Parameters:118 logfile Logfile to parse119 logformat dictionary containing log format120 countbytes sum up bytes sent and received121 Sample return matrix (one logline parsed):122 {'192.168.1.1': {'8.8.8.8': {'53': {'UDP': {'count': 1}}}}}123 Example:124 """125 log.info("get_communication_matrix() started with parameters: ")126 log.info("Option logfile: %s", logfile)127 log.info("Option countbytes: %s", countbytes)128 log.info("Option showaction: %s", showaction)129 # assign log format options from logformat dict130 srcipfield = logformat['srcipfield']131 dstipfield = logformat['dstipfield']132 dstportfield = logformat['dstportfield']133 protofield = logformat['protofield']134 sentbytesfield = logformat['sentbytesfield']135 rcvdbytesfield = logformat['rcvdbytesfield']136 actionfield = logformat['actionfield']137 matrix = {} # communication matrix138 with open(logfile, 'r') as infile:139 # parse each line in file140 linecount = 1 # linecount for detailed error message141 for line in infile:142 """143 For loop creates a nested dictionary with multiple levels.144 Level description:145 Level 1: srcips (source ips)146 Level 2: dstips (destination ips)147 Level 3: dstport (destination port number)148 Level 4: proto (protocol number)149 Level 4.5: action (Fortigate action)150 Level 5: occurrence count151 sentbytes152 rcvdbytes153 """154 # check if necessary fields are in first line155 if linecount is 1 and not noipcheck:156 # print error message if srcip or dstip are missing157 if not check_log_format(line, srcipfield, dstipfield):158 log.error("srcipfield or dstipfield not in line: %s ", linecount)159 log.error("Check Log Format options and consult help message!")160 sys.exit(1)161 # split each line in key and value pairs.162 logline = split_kv(line)163 linecount += 1164 # get() does substitute missing values with None165 # missing log fields will show None in the matrix166 srcip = logline.get(srcipfield)167 dstip = logline.get(dstipfield)168 dstport = logline.get(dstportfield)169 proto = translate_protonr(logline.get(protofield))170 # user has set --action171 if showaction:...

Full Screen

Full Screen

Apache_vulnerable_item.py

Source:Apache_vulnerable_item.py Github

copy

Full Screen

1import os23line = '—' * 1004path = '/opt/lampp/etc/httpd.conf'567# 检查是否禁止Apache显示目录结构8def Check_directory_structure():9 res = os.popen(f"cat {path} | grep Options | grep -v '#' | grep -v 'None'").read().strip()10 if 'Indexes' in res:11 print(f'你的服务器目录可以被访问\n请在配置文件httpd.conf中将所有Options关键字后的Indexes去掉\n{line}')12 print(f'Apache显示目录结构通过检验\n{line}')131415# 检查是否禁用PUT、DELETE等危险的HTTP方法16def Check_http_method():17 dangerous_method = ['PATCH', 'TRACE', 'OPTIONS', 'CONNECT', 'DELETE', 'PUT', 'HEAD']18 res = os.popen(f"cat {path} | grep LimitExcept | grep -v '#'").read().strip()19 if len(res) == 0:20 print(21 f'你的服务器开启了危险的HTTP方法,请在配置文件httpd.conf中添加如下内容:\n<LimitExcept GET POST> Deny from all </LimitExcept>\n{line}')22 return23 for d in dangerous_method:24 if d in res:25 print(26 f'你的服务器开启了危险的HTTP方法,请在配置文件httpd.conf中添加如下内容:\n<LimitExcept GET POST> Deny from all </LimitExcept>\n{line}')27 print(f'危险的HTTP方法通过检验\n{line}')282930# 检查是否配置错误日志31def Check_error_log():32 res = os.popen(f"cat {path} | grep ErrorLog | grep -v '#'").read().strip()33 if len(res) == 0:34 print(35 f'你的服务器未设置错误日志文件\n请在配置文件httpd.conf中设置错误日志文件路径,例如:\nErrorLog logs/error_log\n其中logs/error_log为相应的文件路径\n{line}')36 return37 print(f'错误日志通过检验\n{line}')383940# 是否设置错误信息详细程度为notice41def Check_error_loglevel():42 res = os.popen(f"cat {path} | grep LogLevel | grep -v '#'").read().strip()43 if 'notice' not in res:44 print(f'你的服务器未设置错误日志等级\n请在httpd.conf中设置LogLevel字段如下:\nLogLevel notice\n{line}')45 return46 print(f'日志等级通过检验\n{line}')474849# 检查是否配置访问日志50def Check_visit_log():51 res = os.popen(f"cat {path} | grep CustomLog | grep -v '#'").read().strip()52 if len(res) == 0:53 print(54 f'你的服务器未设置访问日志文件\n请在配置文件httpd.conf中设置访问日志文件路径,例如:\nCustomLog log/access_log \n其中log/access_log为相应的文件路径\n{line}')55 return56 print(f'访问日志通过检验\n{line}')575859# 检查是否配置日志记录格式60def Check_log_format():61 res = os.popen(f"cat {path} | grep LogFormat | grep -v '#'").read().strip()62 if len(res) == 0:63 print('你的服务器未设置访问日志文件\n请在配置文件httpd.conf中添加LogFormat命令,例如:\n'64 'LogFormat "%h %l %u % t \"%r\" %>s %b \"%{Accept}i\"\"%{Referer}i\" \"%{User-Agent}i\""')65 print(line)66 return67 print(f'日志记录格式通过检验\n{line}')686970# 检查是否设置错误页面重定向71def Check_error_redirect():72 res = os.popen(f"cat {path} | grep ErrorDocument | grep -v '#'").read().strip()73 if len(res) == 0:74 print(f'在httpd.conf中,配置400错误重定向页面,例如:\nErrorDocument 400 /custom400.html \n/custom400.html为要显示的400错误页面\n{line}')75 return76 print(f'错误页面重定向通过检验\n{line}')777879if __name__ == '__main__':80 Check_directory_structure()81 Check_http_method()82 Check_error_log()83 Check_error_loglevel()84 Check_visit_log()85 Check_log_format() ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lettuce-tools automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful