Best Python code snippet using rester_python
log_processing_test.py
Source:log_processing_test.py
...20from cloudwatch_streamer.main import LogLineRedacter, LogLineSampler21__author__ = 'czerwin@scalyr.com'22import unittest23class TestLogLineRedactor(ScalyrTestCase):24 def _run_case(self, redactor, line, expected_line, expected_redaction):25 (result_line, redacted) = redactor.process_line(line)26 self.assertEquals(result_line, expected_line)27 self.assertEquals(redacted, expected_redaction)28 def test_basic_redaction(self):29 redactor = LogLineRedacter()30 redactor.add_redaction_rule('password', 'fake')31 self._run_case(redactor, "auth=password", "auth=fake", True)32 self._run_case(redactor, "another line password", "another line fake", True)33 self._run_case(redactor, "do not touch", "do not touch", False)34 def test_multiple_redactions_in_line(self):35 redactor = LogLineRedacter()36 redactor.add_redaction_rule('password', 'fake')37 self._run_case(redactor, "auth=password foo=password", "auth=fake foo=fake", True)38 def test_regular_expression_redaction(self):39 redactor = LogLineRedacter()40 redactor.add_redaction_rule('password=.*', 'password=fake')41 self._run_case(redactor, "login attempt password=czerwin", "login attempt password=fake", True)42 def test_regular_expression_with_capture_group(self):43 redactor = LogLineRedacter()44 redactor.add_redaction_rule('secret(.*)=.*', 'secret\\1=fake')45 self._run_case(redactor, "foo secretoption=czerwin", "foo secretoption=fake", True)46 def test_unicode_redactions(self):47 redacter = LogLineRedacter()48 # redaction rules are created as unicode, to cause conflict with a utf-8 string49 redacter.add_redaction_rule('(.*)', 'bb\\1bb')50 # build the utf8 string51 utf8_string = chr(8230).encode().decode("utf-8")52 # Different from the python 2 test as regex matching has changed53 expected = 'bb' + utf8_string + 'bbbbbb'54 # go go go55 self._run_case(redacter, utf8_string, expected, True)56 def test_multiple_redactions2(self):57 redactor = LogLineRedacter()58 redactor.add_redaction_rule('secret(.*)=.*', 'secret\\1=fake')59 self._run_case(redactor, "foo password=steve secretoption=czerwin", "foo password=steve secretoption=fake",60 True)61 self._run_case(redactor, "foo password=steve secretoption=czerwin", "foo password=steve secretoption=fake",62 True)63 def test_customer_case(self):64 redactor = LogLineRedacter()65 redactor.add_redaction_rule(66 "(access_token|ccNumber|ccSecurityCode|ccExpirationMonth|ccExpirationYear|pwdField|passwordConfirm|"67 "challengeAnswer|code|taxVat|password[0-9]?|pwd|newpwd[0-9]?Field|currentField|security_answer[0-9]|"68 "tinnumber)=[^&]*", "")69 self._run_case(redactor, "[11/May/2012:16:20:54 -0400] \"GET /api2/profiles/api_contractor?"70 "access_token=E|foo&catId=10179&mode=basic HTTP/1.1\" 200 2045",71 "[11/May/2012:16:20:54 -0400] \"GET /api2/profiles/api_contractor?"72 "&catId=10179&mode=basic HTTP/1.1\" 200 2045", True)73 self._run_case(redactor, "[11/May/2012:16:20:54 -0400] \"GET /api2/profiles/api_contractor?"74 "access_token=E|foo&newpwd5Field=10179&mode=basic HTTP/1.1\" 200 2045",75 "[11/May/2012:16:20:54 -0400] \"GET /api2/profiles/api_contractor?&&mode=basic"76 " HTTP/1.1\" 200 2045", True)77 def test_basic_redaction_hash_no_salt(self):78 redactor = LogLineRedacter()79 redactor.add_redaction_rule('(password)', '\\H1')80 self._run_case(redactor, "auth=password", "auth=%s" % (hashlib.md5("password".encode("utf-8")).hexdigest()), True)81 self._run_case(82 redactor, "another line password",83 "another line %s" % (hashlib.md5("password".encode("utf-8")).hexdigest()),84 True85 )86 def test_basic_redaction_hash_with_salt(self):87 redactor = LogLineRedacter()88 redactor.add_redaction_rule('(password)', '\\H1', hash_salt="himalayan-salt")89 self._run_case(90 redactor,91 "auth=password",92 "auth=%s" % (hashlib.md5(("password" + "himalayan-salt").encode("utf-8")).hexdigest()),93 True94 )95 self._run_case(96 redactor, "another line password",97 "another line %s" % (hashlib.md5(("password" + "himalayan-salt").encode("utf-8")).hexdigest()),98 True99 )100 def test_multiple_redactions_in_line_with_hash(self):101 redactor = LogLineRedacter()102 redactor.add_redaction_rule('(password)', '\\H1')103 self._run_case(104 redactor,105 "auth=password foo=password", "auth=%s foo=%s" % (106 hashlib.md5("password".encode("utf-8")).hexdigest(), hashlib.md5("password".encode("utf-8")).hexdigest()),107 True108 )109 def test_single_regular_expression_redaction_with_hash(self):110 redactor = LogLineRedacter()111 redactor.add_redaction_rule('secret(.*)=([a-z]+).*', 'secret\\1=\\H2')112 self._run_case(113 redactor,114 "sometext.... secretoption=czerwin",115 "sometext.... secretoption=%s" % (hashlib.md5("czerwin".encode("utf-8")).hexdigest()),116 True117 )118 def test_single_regular_expression_redaction_with_multiple_hashes(self):119 redactor = LogLineRedacter()120 redactor.add_redaction_rule('secret(.*)=([a-z]+) ([a-z]+) ([a-z]+)', 'secret\\1=\\H2 \\H3 \\H4')121 self._run_case(122 redactor,123 "sometext.... secretoption=czerwin abc def",124 "sometext.... secretoption=%s %s %s" % (hashlib.md5("czerwin".encode("utf-8")).hexdigest(),125 hashlib.md5("abc".encode("utf-8")).hexdigest(),126 hashlib.md5("def".encode("utf-8")).hexdigest()),127 True128 )129 def test_single_regular_expression_redaction_with_multiple_hashes_including_h1(self):130 redactor = LogLineRedacter()131 redactor.add_redaction_rule('([a-z]+)=([a-z]+) ([a-z]+) ([a-z]+)', '\\H1=\\H2 \\H3 \\H4')132 self._run_case(133 redactor,134 "sometext.... xxx=yyy abc def",135 "sometext.... %s=%s %s %s" % (136 hashlib.md5("xxx".encode("utf-8")).hexdigest(),137 hashlib.md5("yyy".encode("utf-8")).hexdigest(),138 hashlib.md5("abc".encode("utf-8")).hexdigest(),139 hashlib.md5("def".encode("utf-8")).hexdigest()),140 True141 )142 def test_multiple_regular_expression_redaction_with_hash_single_group(self):143 redactor = LogLineRedacter()144 redactor.add_redaction_rule('secret(.*?)=([a-z]+\s?)', 'secret\\1=\\H2')145 self._run_case(146 redactor,147 "sometext.... secretoption=czerwin ,moretextsecretbar=xxx ,andsecret123=saurabh",148 "sometext.... secretoption=%s,moretextsecretbar=%s,andsecret123=%s" % (149 hashlib.md5("czerwin ".encode("utf-8")).hexdigest(),150 hashlib.md5("xxx ".encode("utf-8")).hexdigest(),151 hashlib.md5("saurabh".encode("utf-8")).hexdigest(),152 ),153 True154 )155 def test_multiple_regular_expression_redaction_with_hash_single_group_order_flipped(self):156 redactor = LogLineRedacter()157 redactor.add_redaction_rule('secret(.*?)=([a-z]+\s?)', 'secret\\2=\\H1')158 self._run_case(159 redactor,160 "sometext.... secretoption=czerwin ,andsecret123=saurabh",161 "sometext.... secretczerwin =%s,andsecretsaurabh=%s" % (162 hashlib.md5("option".encode("utf-8")).hexdigest(),163 hashlib.md5("123".encode("utf-8")).hexdigest(),164 ),165 True166 )167 def test_multiple_regular_expression_redaction_with_hash_multiple_groups(self):168 redactor = LogLineRedacter()169 redactor.add_redaction_rule('secret_([\w]+)=([\w]+)__([\w]+)', 'secret_\\H1=\\H2__\\H3')170 self._run_case(171 redactor,172 "sometext.... secret_a1=a2__a3 , secret_b1=b2__b3 , secret_c1=c2__c3",173 "sometext.... secret_%s=%s__%s , secret_%s=%s__%s , secret_%s=%s__%s" % (174 hashlib.md5("a1".encode("utf-8")).hexdigest(),175 hashlib.md5("a2".encode("utf-8")).hexdigest(),176 hashlib.md5("a3".encode("utf-8")).hexdigest(),177 hashlib.md5("b1".encode("utf-8")).hexdigest(),178 hashlib.md5("b2".encode("utf-8")).hexdigest(),179 hashlib.md5("b3".encode("utf-8")).hexdigest(),180 hashlib.md5("c1".encode("utf-8")).hexdigest(),181 hashlib.md5("c2".encode("utf-8")).hexdigest(),182 hashlib.md5("c3".encode("utf-8")).hexdigest(),183 ),184 True185 )186 def test_multiple_regular_expression_AGENT_140(self):187 lead_text = '2019-05-14 17:54:41 192.168.1.45 GET /main/Service/MyKastleAjaxService.svc/js - 443 '188 trail_text = ' 178.211.3.102 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/52.0.2743.116+Safari/537.36+Edge/15.15063 304 0 0 277 1192 93'189 redactor = LogLineRedacter()190 lead_text = ''191 trail_text = ''192 redactor.add_redaction_rule('([\w\.]+)@([\w\.]+)\.([\w]{2,4})', "\\H1 \\H2 \\H3")193 self._run_case(194 redactor,195 lead_text + 'xxx.yyy@aaa.bbb.com' + trail_text,196 lead_text + '%s %s %s' % (hashlib.md5('xxx.yyy'.encode("utf-8")).hexdigest(), hashlib.md5('aaa.bbb'.encode("utf-8")).hexdigest(), hashlib.md5('com'.encode("utf-8")).hexdigest()) + trail_text,197 True198 )199 def test_single_regular_expression_redaction_with_hash_no_indicator(self):200 redactor = LogLineRedacter()201 redactor.add_redaction_rule(202 'secret(.*)=([a-z]+).*', 'secret\\1=\\2')203 self._run_case(204 redactor,205 "sometext.... secretoption=czerwin",206 "sometext.... secretoption=czerwin",207 True208 )209 def test_basic_group_non_hash_case(self):210 redactor = LogLineRedacter()211 redactor.add_redaction_rule('userInfo=([^ ]+) [^ ]+', 'userInfo=\\1')212 self._run_case(213 redactor,214 "userInfo=saurabh abcd1234 ",215 "userInfo=saurabh ",216 True217 )218 def test_basic_group_hash_case(self):219 redactor = LogLineRedacter()220 redactor.add_redaction_rule('userInfo=([^ ]+) [^ ]+', 'userInfo=\\H1')221 self._run_case(222 redactor,223 "userInfo=saurabh abcd1234",224 "userInfo=%s" % (hashlib.md5("saurabh".encode("utf-8")).hexdigest()),225 True226 )227class TestLogLineSampler(ScalyrTestCase):228 class TestableLogLineSampler(LogLineSampler):229 """230 A subclass of LogLineSampler that allows us to fix the generated random numbers to help with testing.231 """232 def __init__(self):233 super(TestLogLineSampler.TestableLogLineSampler, self).__init__()234 self.__pending_numbers = []235 def _get_next_random(self):...
test_solution_08.py
Source:test_solution_08.py
...40 (clues[6], clues[14]),41 ]42 p = Puzzle(clues)43 self.assertEqual(p.col_clues, expected)44 def _run_case(self, name):45 clues = CASES[name]["clues"]46 expected = CASES[name]["expected"]47 self.assertEqual(solve_puzzle(clues), expected)48 def test_case_medium(self):49 self._run_case("medium")50 def test_case_case2(self):51 self._run_case("case2")52 def test_case_case2extra(self):53 self._run_case("case2extra")54 def test_case_hard(self):55 self._run_case("hard")56 def test_case_hard2(self):57 self._run_case("hard2")58 def test_case_very_hard(self):59 self._run_case("very_hard")60 def test_case_very_hard_2(self):61 self._run_case("very_hard2")62 def test_case_medved(self):63 self._run_case("medved")64 def test_case_random1(self):65 self._run_case("random1")66 def test_case_random2(self):67 self._run_case("random2")68 def test_case_random3(self):...
test_solution_06.py
Source:test_solution_06.py
...40 (clues[6], clues[14]),41 ]42 p = Puzzle(clues)43 self.assertEqual(p.col_clues, expected)44 def _run_case(self, name):45 clues = CASES[name]["clues"]46 expected = CASES[name]["expected"]47 self.assertEqual(solve_puzzle(clues), expected)48 def test_case_medium(self):49 self._run_case("medium")50 def test_case_case2(self):51 self._run_case("case2")52 def test_case_case2extra(self):53 self._run_case("case2extra")54 def test_case_hard(self):55 self._run_case("hard")56 def test_case_hard2(self):57 self._run_case("hard2")58 def test_case_very_hard(self):59 self._run_case("very_hard")60 def test_case_very_hard_2(self):61 self._run_case("very_hard2")62 def test_case_medved(self):63 self._run_case("medved")64 def test_case_random1(self):65 self._run_case("random1")66 def test_case_random2(self):67 self._run_case("random2")68 def test_case_random3(self):...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!