diff options
author | Andrey Zelenkov <zelenkov@nginx.com> | 2019-04-09 16:14:42 +0300 |
---|---|---|
committer | Andrey Zelenkov <zelenkov@nginx.com> | 2019-04-09 16:14:42 +0300 |
commit | 549f0873c2beed9517fb4006e1f0a63e0019abb3 (patch) | |
tree | fc77a4c4b93a333b08e7b7d8ff28248bd8f74ba6 /test/test_access_log.py | |
parent | cfcf4da7fab046cf4dad47b499fa44abbc8f62b8 (diff) | |
download | unit-549f0873c2beed9517fb4006e1f0a63e0019abb3.tar.gz unit-549f0873c2beed9517fb4006e1f0a63e0019abb3.tar.bz2 |
Tests: wait_for_record() introduced.
Diffstat (limited to '')
-rw-r--r-- | test/test_access_log.py | 91 |
1 files changed, 31 insertions, 60 deletions
diff --git a/test/test_access_log.py b/test/test_access_log.py index 1c8c29d7..b5bfff7a 100644 --- a/test/test_access_log.py +++ b/test/test_access_log.py @@ -13,9 +13,8 @@ class TestAccessLog(TestApplicationPython): self.conf('"' + self.testdir + '/access.log"', 'access_log') - def search_in_log(self, pattern, name='access.log'): - with open(self.testdir + '/' + name, 'r') as f: - return re.search(pattern, f.read()) + def wait_for_record(self, pattern, name='access.log'): + return super().wait_for_record(pattern, name) def test_access_log_keepalive(self): self.load('mirror') @@ -30,10 +29,8 @@ class TestAccessLog(TestApplicationPython): body='01234', ) - time.sleep(0.2) - self.assertIsNotNone( - self.search_in_log(r'"POST / HTTP/1.1" 200 5'), 'keepalive 1' + self.wait_for_record(r'"POST / HTTP/1.1" 200 5'), 'keepalive 1' ) resp = self.post( @@ -46,12 +43,10 @@ class TestAccessLog(TestApplicationPython): body='0123456789', ) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"POST / HTTP/1.1" 200 10'), 'keepalive 2' + self.wait_for_record(r'"POST / HTTP/1.1" 200 10'), 'keepalive 2' ) def test_access_log_pipeline(self): @@ -76,20 +71,18 @@ Connection: close raw=True, ) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 200 0 "Referer-1" "-"'), + self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "Referer-1" "-"'), 'pipeline 1', ) self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 200 0 "Referer-2" "-"'), + self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "Referer-2" "-"'), 'pipeline 2', ) self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 200 0 "Referer-3" "-"'), + self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "Referer-3" "-"'), 'pipeline 3', ) @@ -100,12 +93,10 @@ Connection: close self.get(sock_type='ipv6') - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log( + self.wait_for_record( r'::1 - - \[.+\] "GET / HTTP/1.1" 200 0 "-" "-"' ), 'ipv6', @@ -120,12 +111,10 @@ Connection: close self.get(sock_type='unix', addr=addr) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log( + self.wait_for_record( r'unix: - - \[.+\] "GET / HTTP/1.1" 200 0 "-" "-"' ), 'unix', @@ -142,12 +131,12 @@ Connection: close } ) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 200 0 "referer-value" "-"'), + self.wait_for_record( + r'"GET / HTTP/1.1" 200 0 "referer-value" "-"' + ), 'referer', ) @@ -162,12 +151,10 @@ Connection: close } ) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log( + self.wait_for_record( r'"GET / HTTP/1.1" 200 0 "-" "user-agent-value"' ), 'user agent', @@ -178,12 +165,10 @@ Connection: close self.get(http_10=True) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.0" 200 0 "-" "-"'), 'http 1.0' + self.wait_for_record(r'"GET / HTTP/1.0" 200 0 "-" "-"'), 'http 1.0' ) def test_access_log_partial(self): @@ -191,12 +176,10 @@ Connection: close self.http(b"""GE""", raw=True) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GE" 400 0 "-" "-"'), 'partial' + self.wait_for_record(r'"GE" 400 0 "-" "-"'), 'partial' ) def test_access_log_partial_2(self): @@ -204,12 +187,10 @@ Connection: close self.http(b"""GET /\n""", raw=True) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET /" 400 \d+ "-" "-"'), 'partial 2' + self.wait_for_record(r'"GET /" 400 \d+ "-" "-"'), 'partial 2' ) def test_access_log_partial_3(self): @@ -217,12 +198,10 @@ Connection: close self.http(b"""GET / HTTP/1.1""", raw=True) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET /" 400 0 "-" "-"'), 'partial 3' + self.wait_for_record(r'"GET /" 400 0 "-" "-"'), 'partial 3' ) def test_access_log_partial_4(self): @@ -230,12 +209,11 @@ Connection: close resp = self.http(b"""GET / HTTP/1.1\n""", raw=True) - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 400 0 "-" "-"'), 'partial 4' + self.wait_for_record(r'"GET / HTTP/1.1" 400 0 "-" "-"'), + 'partial 4', ) def test_access_log_partial_5(self): @@ -246,7 +224,8 @@ Connection: close self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 200 0 "-" "-"'), 'partial 5' + self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"'), + 'partial 5', ) def test_access_log_get_parameters(self): @@ -254,12 +233,10 @@ Connection: close self.get(url='/?blah&var=val') - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log( + self.wait_for_record( r'"GET /\?blah&var=val HTTP/1.1" 200 0 "-" "-"' ), 'get parameters', @@ -272,11 +249,11 @@ Connection: close self.get(url='/delete') - time.sleep(0.2) - self.stop() - self.assertIsNone(self.search_in_log(r'/delete'), 'delete') + self.assertIsNone( + self.search_in_log(r'/delete', 'access.log'), 'delete' + ) def test_access_log_change(self): self.load('empty') @@ -287,12 +264,10 @@ Connection: close self.get() - time.sleep(0.2) - self.stop() self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 200 0 "-" "-"', 'new.log'), + self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', 'new.log'), 'change', ) @@ -309,10 +284,8 @@ Connection: close self.get() - time.sleep(0.2) - self.assertIsNotNone( - self.search_in_log(r'"GET / HTTP/1.1" 200 0 "-" "-"', 'new.log'), + self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', 'new.log'), 'rename new', ) self.assertFalse(os.path.isfile(log_path), 'rename old') @@ -326,15 +299,13 @@ Connection: close self.get(url='/usr1') - time.sleep(0.2) - - self.assertIsNone( - self.search_in_log(r'/usr1', 'new.log'), 'rename new 2' - ) self.assertIsNotNone( - self.search_in_log(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"'), + self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"'), 'reopen 2', ) + self.assertIsNone( + self.search_in_log(r'/usr1', 'new.log'), 'rename new 2' + ) if __name__ == '__main__': |