From cd063f7e56109a7d7d0b32258859fbd63a9f89d0 Mon Sep 17 00:00:00 2001 From: Gourav Date: Wed, 26 Jun 2024 12:07:09 +0530 Subject: [PATCH] Add tests for feature - "Allowing factory supports for python app config" Add the following tests cases: 1. When "factory" key is used inside the "targets" option. 2. When "factory" key is used at the root level of python application config. 3. When factory returns invalid callable or When factory is invalid callable Closes: https://github.com/nginx/unit/issues/1106 Link: Signed-off-by: Andrew Clayton --- test/python/factory/wsgi.py | 23 ++++++ test/test_python_factory.py | 140 ++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 test/python/factory/wsgi.py create mode 100644 test/test_python_factory.py diff --git a/test/python/factory/wsgi.py b/test/python/factory/wsgi.py new file mode 100644 index 000000000..8ad4887b2 --- /dev/null +++ b/test/python/factory/wsgi.py @@ -0,0 +1,23 @@ +def wsgi_a(env, start_response): + start_response("200", [("Content-Length", "1")]) + return [b"1"] + + +def wsgi_b(env, start_response): + start_response("200", [("Content-Length", "1")]) + return [b"2"] + + +def wsgi_a_factory(): + return wsgi_a + + +def wsgi_b_factory(): + return wsgi_b + + +wsgi_invalid_callable = None + + +def wsgi_factory_returning_invalid_callable(): + return wsgi_invalid_callable diff --git a/test/test_python_factory.py b/test/test_python_factory.py new file mode 100644 index 000000000..d1752c248 --- /dev/null +++ b/test/test_python_factory.py @@ -0,0 +1,140 @@ +from unit.applications.lang.python import ApplicationPython +from unit.option import option + +prerequisites = {"modules": {"python": "all"}} + +client = ApplicationPython() + + +def test_python_factory_targets(): + python_dir = f"{option.test_dir}/python" + + assert "success" in client.conf( + { + "listeners": { + "*:8080": {"pass": "applications/targets/1"}, + "*:8081": {"pass": "applications/targets/2"}, + "*:8082": {"pass": "applications/targets/factory-1"}, + "*:8083": {"pass": "applications/targets/factory-2"}, + }, + "applications": { + "targets": { + "type": client.get_application_type(), + "working_directory": f"{python_dir}/factory/", + "path": f"{python_dir}/factory/", + "targets": { + "1": { + "module": "wsgi", + "callable": "wsgi_a", + "factory": False, + }, + "2": { + "module": "wsgi", + "callable": "wsgi_b", + "factory": False, + }, + "factory-1": { + "module": "wsgi", + "callable": "wsgi_a_factory", + "factory": True, + }, + "factory-2": { + "module": "wsgi", + "callable": "wsgi_b_factory", + "factory": True, + }, + }, + } + }, + } + ) + + resp = client.get(port=8080) + assert resp["status"] == 200 + assert resp["body"] == "1" + + resp = client.get(port=8081) + assert resp["status"] == 200 + assert resp["body"] == "2" + + resp = client.get(port=8082) + assert resp["status"] == 200 + assert resp["body"] == "1" + + resp = client.get(port=8083) + assert resp["status"] == 200 + assert resp["body"] == "2" + + +def test_python_factory_without_targets(): + python_dir = f"{option.test_dir}/python" + + assert "success" in client.conf( + { + "listeners": { + "*:8080": {"pass": "applications/python-app-factory"}, + "*:8081": {"pass": "applications/python-app"}, + }, + "applications": { + "python-app-factory": { + "type": client.get_application_type(), + "working_directory": f"{python_dir}/factory/", + "path": f"{python_dir}/factory/", + "module": "wsgi", + "callable": "wsgi_a_factory", + "factory": True, + }, + "python-app": { + "type": client.get_application_type(), + "working_directory": f"{python_dir}/factory/", + "path": f"{python_dir}/factory/", + "module": "wsgi", + "callable": "wsgi_b", + "factory": False, + }, + }, + } + ) + + resp = client.get(port=8080) + assert resp["status"] == 200 + assert resp["body"] == "1" + + resp = client.get(port=8081) + assert resp["status"] == 200 + assert resp["body"] == "2" + + +def test_python_factory_invalid_callable_value(skip_alert): + skip_alert( + r"failed to apply new conf", + r"did not return callable object", + r"can not be called to fetch callable", + ) + python_dir = f"{option.test_dir}/python" + + invalid_callable_values = [ + "wsgi_factory_returning_invalid_callable", + "wsgi_invalid_callable", + ] + + for callable_value in invalid_callable_values: + assert "error" in client.conf( + { + "listeners": {"*:8080": {"pass": "applications/targets/1"}}, + "applications": { + "targets": { + "type": client.get_application_type(), + "working_directory": f"{python_dir}/factory/", + "path": f"{python_dir}/factory/", + "targets": { + "1": { + "module": "wsgi", + "callable": callable_value, + "factory": True, + }, + }, + } + }, + } + )