diff options
author | Andrei Belov <defan@nginx.com> | 2020-10-08 19:19:31 +0300 |
---|---|---|
committer | Andrei Belov <defan@nginx.com> | 2020-10-08 19:19:31 +0300 |
commit | d586ac9fdc4a86c142b06a75dde4cdacad5b52f6 (patch) | |
tree | 9817282396f9d2cf5333050e4b5bf807d3617e40 /test/test_share_fallback.py | |
parent | 9be35d9b7418c041e5177f273c20f0fd2d3f00ad (diff) | |
parent | ad516735a65fe109773b60e26214a071411f1734 (diff) | |
download | unit-1.20.0-1.tar.gz unit-1.20.0-1.tar.bz2 |
Merged with the default branch.1.20.0-1
Diffstat (limited to 'test/test_share_fallback.py')
-rw-r--r-- | test/test_share_fallback.py | 125 |
1 files changed, 57 insertions, 68 deletions
diff --git a/test/test_share_fallback.py b/test/test_share_fallback.py index ca5e2678..391d0836 100644 --- a/test/test_share_fallback.py +++ b/test/test_share_fallback.py @@ -1,20 +1,21 @@ import os +from conftest import skip_alert from unit.applications.proto import TestApplicationProto class TestStatic(TestApplicationProto): prerequisites = {} - def setUp(self): - super().setUp() + def setup_method(self): + super().setup_method() - os.makedirs(self.testdir + '/assets/dir') - with open(self.testdir + '/assets/index.html', 'w') as index: + os.makedirs(self.temp_dir + '/assets/dir') + with open(self.temp_dir + '/assets/index.html', 'w') as index: index.write('0123456789') - os.makedirs(self.testdir + '/assets/403') - os.chmod(self.testdir + '/assets/403', 0o000) + os.makedirs(self.temp_dir + '/assets/403') + os.chmod(self.temp_dir + '/assets/403', 0o000) self._load_conf( { @@ -22,48 +23,46 @@ class TestStatic(TestApplicationProto): "*:7080": {"pass": "routes"}, "*:7081": {"pass": "routes"}, }, - "routes": [{"action": {"share": self.testdir + "/assets"}}], + "routes": [{"action": {"share": self.temp_dir + "/assets"}}], "applications": {}, } ) - def tearDown(self): - os.chmod(self.testdir + '/assets/403', 0o777) + def teardown_method(self): + os.chmod(self.temp_dir + '/assets/403', 0o777) - super().tearDown() + super().teardown_method() def action_update(self, conf): - self.assertIn('success', self.conf(conf, 'routes/0/action')) + assert 'success' in self.conf(conf, 'routes/0/action') def test_fallback(self): self.action_update({"share": "/blah"}) - self.assertEqual(self.get()['status'], 404, 'bad path no fallback') + assert self.get()['status'] == 404, 'bad path no fallback' self.action_update({"share": "/blah", "fallback": {"return": 200}}) resp = self.get() - self.assertEqual(resp['status'], 200, 'bad path fallback status') - self.assertEqual(resp['body'], '', 'bad path fallback') + assert resp['status'] == 200, 'bad path fallback status' + assert resp['body'] == '', 'bad path fallback' def test_fallback_valid_path(self): self.action_update( - {"share": self.testdir + "/assets", "fallback": {"return": 200}} + {"share": self.temp_dir + "/assets", "fallback": {"return": 200}} ) resp = self.get() - self.assertEqual(resp['status'], 200, 'fallback status') - self.assertEqual(resp['body'], '0123456789', 'fallback') + assert resp['status'] == 200, 'fallback status' + assert resp['body'] == '0123456789', 'fallback' resp = self.get(url='/403/') - self.assertEqual(resp['status'], 200, 'fallback status 403') - self.assertEqual(resp['body'], '', 'fallback 403') + assert resp['status'] == 200, 'fallback status 403' + assert resp['body'] == '', 'fallback 403' resp = self.post() - self.assertEqual(resp['status'], 200, 'fallback status 405') - self.assertEqual(resp['body'], '', 'fallback 405') + assert resp['status'] == 200, 'fallback status 405' + assert resp['body'] == '', 'fallback 405' - self.assertEqual( - self.get(url='/dir')['status'], 301, 'fallback status 301' - ) + assert self.get(url='/dir')['status'] == 301, 'fallback status 301' def test_fallback_nested(self): self.action_update( @@ -77,62 +76,56 @@ class TestStatic(TestApplicationProto): ) resp = self.get() - self.assertEqual(resp['status'], 200, 'fallback nested status') - self.assertEqual(resp['body'], '', 'fallback nested') + assert resp['status'] == 200, 'fallback nested status' + assert resp['body'] == '', 'fallback nested' def test_fallback_share(self): self.action_update( { "share": "/blah", - "fallback": {"share": self.testdir + "/assets"}, + "fallback": {"share": self.temp_dir + "/assets"}, } ) resp = self.get() - self.assertEqual(resp['status'], 200, 'fallback share status') - self.assertEqual(resp['body'], '0123456789', 'fallback share') + assert resp['status'] == 200, 'fallback share status' + assert resp['body'] == '0123456789', 'fallback share' resp = self.head() - self.assertEqual(resp['status'], 200, 'fallback share status HEAD') - self.assertEqual(resp['body'], '', 'fallback share HEAD') + assert resp['status'] == 200, 'fallback share status HEAD' + assert resp['body'] == '', 'fallback share HEAD' - self.assertEqual( - self.get(url='/dir')['status'], 301, 'fallback share status 301' - ) + assert ( + self.get(url='/dir')['status'] == 301 + ), 'fallback share status 301' def test_fallback_proxy(self): - self.assertIn( - 'success', - self.conf( - [ - { - "match": {"destination": "*:7081"}, - "action": {"return": 200}, - }, - { - "action": { - "share": "/blah", - "fallback": {"proxy": "http://127.0.0.1:7081"}, - } - }, - ], - 'routes', - ), - 'configure fallback proxy route', - ) + assert 'success' in self.conf( + [ + { + "match": {"destination": "*:7081"}, + "action": {"return": 200}, + }, + { + "action": { + "share": "/blah", + "fallback": {"proxy": "http://127.0.0.1:7081"}, + } + }, + ], + 'routes', + ), 'configure fallback proxy route' resp = self.get() - self.assertEqual(resp['status'], 200, 'fallback proxy status') - self.assertEqual(resp['body'], '', 'fallback proxy') + assert resp['status'] == 200, 'fallback proxy status' + assert resp['body'] == '', 'fallback proxy' def test_fallback_proxy_loop(self): - self.skip_alerts.extend( - [ - r'open.*/blah/index.html.*failed', - r'accept.*failed', - r'socket.*failed', - r'new connections are not accepted', - ] + skip_alert( + r'open.*/blah/index.html.*failed', + r'accept.*failed', + r'socket.*failed', + r'new connections are not accepted', ) self.action_update( @@ -140,12 +133,12 @@ class TestStatic(TestApplicationProto): ) self.get(no_recv=True) - self.assertIn('success', self.conf_delete('listeners/*:7081')) + assert 'success' in self.conf_delete('listeners/*:7081') self.get(read_timeout=1) def test_fallback_invalid(self): def check_error(conf): - self.assertIn('error', self.conf(conf, 'routes/0/action')) + assert 'error' in self.conf(conf, 'routes/0/action') check_error({"share": "/blah", "fallback": {}}) check_error({"share": "/blah", "fallback": ""}) @@ -154,7 +147,3 @@ class TestStatic(TestApplicationProto): {"proxy": "http://127.0.0.1:7081", "fallback": {"share": "/blah"}} ) check_error({"fallback": {"share": "/blah"}}) - - -if __name__ == '__main__': - TestStatic.main() |