diff --git a/tests/__init__.py b/tests/__init__.py index 30ecb3a..d180120 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,4 @@ -# -*- coding: UTF-8 -*- -# -# Copyright 2020 Flavio Goncalves Garcia +# Copyright 2020-2024 Flavio Garcia # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,8 +12,47 @@ # See the License for the specific language governing permissions and # limitations under the License. +from importlib import reload import os TEST_ROOT = os.path.abspath(os.path.dirname(__file__)) FIXTURES_ROOT = os.path.join(TEST_ROOT, "fixtures") PROJECT_ROOT = os.path.abspath(os.path.join(TEST_ROOT, "..")) + + +def chdir_fixture_app(app_name, **kwargs): + dir_name = kwargs.get("dir_name", None) + suppress_log = kwargs.get("suppress_log", False) + fixture_root = kwargs.get("fixture_root", "fixtures") + test_app_dirname = os.path.join(TEST_ROOT, fixture_root, app_name) + if dir_name is not None: + test_app_dirname = os.path.join(TEST_ROOT, fixture_root, + dir_name, app_name) + os.chdir(test_app_dirname) + if suppress_log: + import logging + for handler in logging.root.handlers[:]: + # clearing loggers, solution from: https://bit.ly/2yTchyx + logging.root.removeHandler(handler) + return test_app_dirname + + +def chdir_app(app_name, directory=None): + """ Change to the application directory located at the resource directory + for conf tests. + + The conf resources directory is firenado/tests/resources/conf. + + :param app_name: The application name + :param directory: The directory to be changed + """ + import firenado.conf + + test_dirname, filename = os.path.split(os.path.abspath(__file__)) + if directory: + test_app_dirname = os.path.join(test_dirname, 'resources', + directory, app_name) + else: + test_app_dirname = os.path.join(test_dirname, 'resources', app_name) + os.chdir(test_app_dirname) + reload(firenado.conf) diff --git a/tests/fixtures/bastiontest/__init__.py b/tests/fixtures/bastiontest/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/bastiontest/app.py b/tests/fixtures/bastiontest/app.py new file mode 100644 index 0000000..e68e887 --- /dev/null +++ b/tests/fixtures/bastiontest/app.py @@ -0,0 +1,10 @@ +from bastiontest import handlers +from firenado import tornadoweb + + +class BastiontestComponent(tornadoweb.TornadoComponent): + + def get_handlers(self): + return [ + (r"/", handlers.IndexHandler), + ] diff --git a/tests/fixtures/bastiontest/conf/firenado.yml b/tests/fixtures/bastiontest/conf/firenado.yml new file mode 100644 index 0000000..966019d --- /dev/null +++ b/tests/fixtures/bastiontest/conf/firenado.yml @@ -0,0 +1,32 @@ +app: + component: 'bastiontest' + pythonpath: ".." + #type: 'tornado' + port: 8888 + +data: + sources: + - name: session + connector: redis + # host: localhost + # port: 6379 + # db: 0 +components: + - id: bastiontest + class: bastiontest.app.BastiontestComponent + enabled: true + #- id: admin + # enabled: true + #- id: info + # enabled: true + +# Session types could be: +# file or redis. +session: + type: redis + enabled: false + # Redis session handler configuration + data: + source: session + # File session handler related configuration + # path: /tmp diff --git a/tests/fixtures/bastiontest/handlers.py b/tests/fixtures/bastiontest/handlers.py new file mode 100644 index 0000000..0d6ffc4 --- /dev/null +++ b/tests/fixtures/bastiontest/handlers.py @@ -0,0 +1,7 @@ +from firenado import tornadoweb + + +class IndexHandler(tornadoweb.TornadoHandler): + + def get(self): + self.write("IndexHandler output") diff --git a/tests/runtests.py b/tests/runtests.py new file mode 100644 index 0000000..f31bd96 --- /dev/null +++ b/tests/runtests.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# +# Copyright 2020-2024 Flavio Garcia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# print current directory +import unittest +from tests import tornado_test + + +def suite(): + testLoader = unittest.TestLoader() + alltests = unittest.TestSuite() + alltests.addTests(testLoader.loadTestsFromModule(tornado_test)) + return alltests + + +if __name__ == "__main__": + runner = unittest.TextTestRunner(verbosity=3) + result = runner.run(suite()) + if not result.wasSuccessful(): + exit(2) diff --git a/tests/tornado_test.py b/tests/tornado_test.py new file mode 100644 index 0000000..2cf87c6 --- /dev/null +++ b/tests/tornado_test.py @@ -0,0 +1,38 @@ +# Copyright 2020-2024 Flavio Garcia +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from firenado.testing import TornadoAsyncTestCase +from firenado.launcher import ProcessLauncher +from tests import chdir_fixture_app, PROJECT_ROOT +from tornado.httpclient import AsyncHTTPClient +from tornado.testing import gen_test + + +class TornadoTransportTestCase(TornadoAsyncTestCase): + """ Tornado based client test case. """ + + def get_launcher(self): + application_dir = chdir_fixture_app("bastiontest") + return ProcessLauncher( + dir=application_dir, path=PROJECT_ROOT) + + @gen_test + async def test_get(self): + http_client = AsyncHTTPClient() + try: + response = await http_client.fetch( + f"http://localhost:{self.http_port()}/") + except Exception as e: + raise e + self.assertEqual(response.body, b"IndexHandler output")