From 46e22c422bcfab7d5cc4a2a88777f6eb1597d931 Mon Sep 17 00:00:00 2001 From: Thomas Bechtold Date: Thu, 9 Jan 2020 08:28:22 +0100 Subject: [PATCH] qa: Enable basic mypy support for qa/ directory A first step to do more automatic code checks on the qa/ directory. This is useful while transitioning to python3. Also use log_exc to top-level to not run into: error: Argument 1 to "log_exc" has incompatible type "Callable[[OSDThrasher], Any]"; expected "OSDThrasher" Signed-off-by: Thomas Bechtold --- qa/mypy.ini | 2 ++ qa/tasks/ceph_manager.py | 27 ++++++++++++++------------- qa/tasks/cephfs/cephfs_test_case.py | 2 +- qa/tasks/cephfs/test_full.py | 13 ++++++++----- qa/tox.ini | 6 +++++- src/test/rgw/rgw_multi/tests.py | 2 +- src/test/rgw/rgw_multi/zone_cloud.py | 2 +- src/test/rgw/rgw_multi/zone_es.py | 2 +- src/test/rgw/rgw_multi/zone_rados.py | 2 +- 9 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 qa/mypy.ini diff --git a/qa/mypy.ini b/qa/mypy.ini new file mode 100644 index 00000000000..1215375ed9b --- /dev/null +++ b/qa/mypy.ini @@ -0,0 +1,2 @@ +[mypy] +ignore_missing_imports = True \ No newline at end of file diff --git a/qa/tasks/ceph_manager.py b/qa/tasks/ceph_manager.py index eae0fe0b503..aea091f65cf 100644 --- a/qa/tasks/ceph_manager.py +++ b/qa/tasks/ceph_manager.py @@ -29,7 +29,7 @@ import six try: from subprocess import DEVNULL # py3k except ImportError: - DEVNULL = open(os.devnull, 'r+') + DEVNULL = open(os.devnull, 'r+') # type: ignore DEFAULT_CONF_PATH = '/etc/ceph/ceph.conf' @@ -116,6 +116,17 @@ def mount_osd_data(ctx, remote, cluster, osd): ) +def log_exc(func): + @wraps(func) + def wrapper(self): + try: + return func(self) + except: + self.log(traceback.format_exc()) + raise + return wrapper + + class PoolType: REPLICATED = 1 ERASURE_CODED = 3 @@ -1067,16 +1078,6 @@ class OSDThrasher(Thrasher): # Allow successful completion so gevent doesn't see an exception. # The DaemonWatchdog will observe the error and tear down the test. - def log_exc(func): - @wraps(func) - def wrapper(self): - try: - return func(self) - except: - self.log(traceback.format_exc()) - raise - return wrapper - @log_exc def do_sighup(self): """ @@ -2179,13 +2180,13 @@ class CephManager: ret[status] += 1 return ret - @wait_for_pg_stats + @wait_for_pg_stats # type: ignore def with_pg_state(self, pool, pgnum, check): pgstr = self.get_pgid(pool, pgnum) stats = self.get_single_pg_stats(pgstr) assert(check(stats['state'])) - @wait_for_pg_stats + @wait_for_pg_stats # type: ignore def with_pg(self, pool, pgnum, check): pgstr = self.get_pgid(pool, pgnum) stats = self.get_single_pg_stats(pgstr) diff --git a/qa/tasks/cephfs/cephfs_test_case.py b/qa/tasks/cephfs/cephfs_test_case.py index 98a18b3d550..d9464dd4135 100644 --- a/qa/tasks/cephfs/cephfs_test_case.py +++ b/qa/tasks/cephfs/cephfs_test_case.py @@ -58,7 +58,7 @@ class CephFSTestCase(CephTestCase): # requires REQUIRE_FILESYSTEM = True REQUIRE_RECOVERY_FILESYSTEM = False - LOAD_SETTINGS = [] + LOAD_SETTINGS = [] # type: ignore def setUp(self): super(CephFSTestCase, self).setUp() diff --git a/qa/tasks/cephfs/test_full.py b/qa/tasks/cephfs/test_full.py index 10f111ae635..112407de18c 100644 --- a/qa/tasks/cephfs/test_full.py +++ b/qa/tasks/cephfs/test_full.py @@ -1,10 +1,13 @@ - - import json import logging import os from textwrap import dedent import time +try: + from typing import Optional +except: + # make it work for python2 + pass from teuthology.orchestra.run import CommandFailedError from tasks.cephfs.fuse_mount import FuseMount from tasks.cephfs.cephfs_test_case import CephFSTestCase @@ -20,7 +23,7 @@ class FullnessTestCase(CephFSTestCase): data_only = False # Subclasses define how many bytes should be written to achieve fullness - pool_capacity = None + pool_capacity = None # type: Optional[int] fill_mb = None # Subclasses define what fullness means to them @@ -364,8 +367,8 @@ class TestQuotaFull(FullnessTestCase): """ Test per-pool fullness, which indicates quota limits exceeded """ - pool_capacity = 1024 * 1024 * 32 # arbitrary low-ish limit - fill_mb = pool_capacity / (1024 * 1024) + pool_capacity = 1024 * 1024 * 32 # arbitrary low-ish limit + fill_mb = pool_capacity / (1024 * 1024) # type: ignore # We are only testing quota handling on the data pool, not the metadata # pool. diff --git a/qa/tox.ini b/qa/tox.ini index 16792011cff..041c6ed298e 100644 --- a/qa/tox.ini +++ b/qa/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = flake8-py2, flake8-py3 +envlist = flake8-py2, flake8-py3, mypy skipsdist = True [testenv:flake8-py2] @@ -14,3 +14,7 @@ deps= flake8 commands=flake8 --select=F,E9 --exclude=venv,.tox +[testenv:mypy] +basepython = python3 +deps = mypy +commands = mypy {posargs:.} diff --git a/src/test/rgw/rgw_multi/tests.py b/src/test/rgw/rgw_multi/tests.py index 6cada052d3d..598356dff0e 100644 --- a/src/test/rgw/rgw_multi/tests.py +++ b/src/test/rgw/rgw_multi/tests.py @@ -7,7 +7,7 @@ import logging import errno try: - from itertools import izip_longest as zip_longest + from itertools import izip_longest as zip_longest # type: ignore except ImportError: from itertools import zip_longest from itertools import combinations diff --git a/src/test/rgw/rgw_multi/zone_cloud.py b/src/test/rgw/rgw_multi/zone_cloud.py index fdfca4f1eca..83bfae19e6c 100644 --- a/src/test/rgw/rgw_multi/zone_cloud.py +++ b/src/test/rgw/rgw_multi/zone_cloud.py @@ -12,7 +12,7 @@ import re from nose.tools import eq_ as eq try: - from itertools import izip_longest as zip_longest + from itertools import izip_longest as zip_longest # type: ignore except ImportError: from itertools import zip_longest diff --git a/src/test/rgw/rgw_multi/zone_es.py b/src/test/rgw/rgw_multi/zone_es.py index ec9b178fd16..1f85744a49c 100644 --- a/src/test/rgw/rgw_multi/zone_es.py +++ b/src/test/rgw/rgw_multi/zone_es.py @@ -9,7 +9,7 @@ import dateutil.parser from nose.tools import eq_ as eq try: - from itertools import izip_longest as zip_longest + from itertools import izip_longest as zip_longest # type: ignore except ImportError: from itertools import zip_longest diff --git a/src/test/rgw/rgw_multi/zone_rados.py b/src/test/rgw/rgw_multi/zone_rados.py index e645e9abe89..3b298e8b42f 100644 --- a/src/test/rgw/rgw_multi/zone_rados.py +++ b/src/test/rgw/rgw_multi/zone_rados.py @@ -2,7 +2,7 @@ import logging from boto.s3.deletemarker import DeleteMarker try: - from itertools import izip_longest as zip_longest + from itertools import izip_longest as zip_longest # type: ignore except ImportError: from itertools import zip_longest -- 2.39.5