diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ba4c1e0 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,26 @@ +name: Run test + +on: + push: + branches: + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "pypy3.9", "pypy3.10"] + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + cache: "pip" + cache-dependency-path: requirements-dev.txt + - name: Install dependencies + run: python -m pip install -r requirements-dev.txt + - name: Run tests + run: pytest --cov=fluent diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0e5e0c8..0000000 --- a/.travis.yml +++ /dev/null @@ -1,33 +0,0 @@ -sudo: false -language: python -python: - - "3.5" - - "3.6" - - "3.7" - - "3.8" - - "3.9" - - pypy3 - - nightly -# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors -install: - - "pip install -e ." - - "pip install 'coverage~=4.5.4' coveralls" -script: - - "PYTHONFAULTHANDLER=x timeout -sABRT 30s nosetests -vsd" -after_success: - - coveralls - -deploy: - provider: pypi - user: repeatedly - server: https://upload.pypi.org/legacy/ - password: - secure: CpNaj4F3TZvpP1aSJWidh/XexrWODV2sBdObrYU79Gyh9hFl6WLsA3JM9BfVsy9cGb/P/jP6ly4Z0/6qdIzZ5D6FPOB1B7rn5GZ2LAMOypRCA6W2uJbRjUU373Wut0p0OmQcMPto6XJsMlpvOEq+1uAq+LLAnAGEmmYTeskZebs= - on: - tags: true - condition: '"$TRAVIS_PYTHON_VERSION" = "3.9" || "$TRAVIS_PYTHON_VERSION" = "2.7"' - distributions: "sdist bdist_wheel" - -matrix: - allow_failures: - - python: nightly diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..3707664 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,3 @@ +pytest +pytest-cov +msgpack diff --git a/tests/mockserver.py b/tests/mockserver.py index 77ecdd3..f1462a1 100644 --- a/tests/mockserver.py +++ b/tests/mockserver.py @@ -92,6 +92,3 @@ def close(self): pass self.join() - - def __del__(self): - self.close() diff --git a/tests/test_asynchandler.py b/tests/test_asynchandler.py index e88a041..bbbf52e 100644 --- a/tests/test_asynchandler.py +++ b/tests/test_asynchandler.py @@ -20,6 +20,12 @@ from tests import mockserver +def get_logger(name, level=logging.INFO): + logger = logging.getLogger(name) + logger.setLevel(level) + return logger + + class TestHandler(unittest.TestCase): def setUp(self): super(TestHandler, self).setUp() @@ -40,8 +46,7 @@ def test_simple(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({ @@ -63,8 +68,7 @@ def test_custom_fmt(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -86,8 +90,7 @@ def test_custom_fmt_with_format_style(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '{name}', @@ -109,8 +112,7 @@ def test_custom_fmt_with_template_style(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '${name}', @@ -131,8 +133,7 @@ def test_custom_field_raise_exception(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -147,8 +148,7 @@ def test_custom_field_raise_exception(self): def test_custom_field_fill_missing_fmt_key_is_true(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -172,8 +172,7 @@ def test_json_encoded_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('{"key": "hello world!", "param": "value"}') @@ -186,8 +185,7 @@ def test_unstructured_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello %s', 'world') @@ -200,8 +198,7 @@ def test_unstructured_formatted_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello world, %s', 'you!') @@ -214,8 +211,7 @@ def test_number_string_simple_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info("1") @@ -227,8 +223,7 @@ def test_non_string_simple_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info(42) @@ -240,8 +235,7 @@ def test_non_string_dict_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({42: 'root'}) @@ -254,8 +248,7 @@ def test_exception_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) try: @@ -297,8 +290,7 @@ def test_simple(self): self.assertEqual(handler.sender.queue_circular, True) self.assertEqual(handler.sender.queue_maxsize, self.Q_SIZE) - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({'cnt': 1, 'from': 'userA', 'to': 'userB'}) @@ -359,8 +351,7 @@ def custom_full_queue(): self.assertEqual(handler.sender.queue_circular, True) self.assertEqual(handler.sender.queue_maxsize, self.Q_SIZE) - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) diff --git a/tests/test_event.py b/tests/test_event.py index d341616..0f47ffa 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -6,7 +6,8 @@ from tests import mockserver -class TestException(BaseException): pass +class TestException(BaseException): + __test__ = False # teach pytest this is not test class. class TestEvent(unittest.TestCase): @@ -18,7 +19,7 @@ def tearDown(self): from fluent.sender import _set_global_sender sender.close() _set_global_sender(None) - + def test_logging(self): # XXX: This tests succeeds even if the fluentd connection failed # send event with tag app.follow diff --git a/tests/test_handler.py b/tests/test_handler.py index 45fea86..2ef0695 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -8,6 +8,12 @@ from tests import mockserver +def get_logger(name, level=logging.INFO): + logger = logging.getLogger(name) + logger.setLevel(level) + return logger + + class TestHandler(unittest.TestCase): def setUp(self): super(TestHandler, self).setUp() @@ -24,8 +30,7 @@ def test_simple(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) @@ -50,8 +55,7 @@ def test_custom_fmt(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -73,8 +77,7 @@ def test_exclude_attrs(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(exclude_attrs=[]) ) @@ -91,8 +94,7 @@ def test_exclude_attrs_with_exclusion(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(exclude_attrs=["funcName"]) ) @@ -109,8 +111,7 @@ def test_exclude_attrs_with_extra(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(exclude_attrs=[]) ) @@ -138,8 +139,7 @@ def formatter(record): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt=formatter) ) @@ -157,8 +157,7 @@ def test_custom_fmt_with_format_style(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '{name}', @@ -181,8 +180,7 @@ def test_custom_fmt_with_template_style(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '${name}', @@ -204,8 +202,7 @@ def test_custom_field_raise_exception(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -223,8 +220,7 @@ def test_custom_field_fill_missing_fmt_key_is_true(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -248,8 +244,7 @@ def test_json_encoded_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) @@ -265,8 +260,7 @@ def test_json_encoded_message_without_json(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter(format_json=False)) log.addHandler(handler) @@ -282,8 +276,7 @@ def test_unstructured_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello %s', 'world') @@ -297,8 +290,7 @@ def test_unstructured_formatted_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello world, %s', 'you!') @@ -312,8 +304,7 @@ def test_number_string_simple_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info("1") @@ -326,8 +317,7 @@ def test_non_string_simple_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info(42) @@ -340,8 +330,7 @@ def test_non_string_dict_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({42: 'root'}) @@ -355,8 +344,7 @@ def test_exception_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - logging.basicConfig(level=logging.INFO) - log = logging.getLogger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) try: diff --git a/tox.ini b/tox.ini index 6c3f032..14634e9 100644 --- a/tox.ini +++ b/tox.ini @@ -4,6 +4,8 @@ envlist = py27, py32, py33, py34, py35, py36, py37, py38 skip_missing_interpreters = True [testenv] -deps = nose - coverage~=4.5.4 -commands = python setup.py nosetests +deps = + pytest + pytest-cov + msgpack +commands = pytest --cov=fluent