diff --git a/HACKING.rst b/HACKING.rst index 8ec767b7e96c..dcf71e453774 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -9,7 +9,8 @@ Nova Specific Commandments --------------------------- - ``nova.db`` imports are not allowed in ``nova/virt/*`` - +- [N309] no db session in public API methods (disabled) + This enforces a guideline defined in ``nova.openstack.common.db.sqlalchemy.session`` Creating Unit Tests ------------------- diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index 89ad7c554ef7..aee63dde3683 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -13,6 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. +import re + +session_check = re.compile("\w*def [a-zA-Z0-9].*[(].*session.*[)]") + def import_no_db_in_virt(logical_line, filename): """Check for db calls from nova/virt @@ -50,6 +54,13 @@ def except_python3x_compatible(logical_line, filename): yield(0, "N308: Python 3.x incompatible 'except x,y:' construct") +def no_db_session_in_public_api(logical_line, filename): + if "db/api.py" in filename or "db/sqlalchemy/api.py" in filename: + if session_check.match(logical_line): + yield (0, "N309: public db api methods may not accept session") + + def factory(register): register(import_no_db_in_virt) register(except_python3x_compatible) + register(no_db_session_in_public_api) diff --git a/tox.ini b/tox.ini index bc2ae8d6a018..d82b949fbfa7 100644 --- a/tox.ini +++ b/tox.ini @@ -39,7 +39,7 @@ commands = commands = {posargs} [flake8] -ignore = E121,E122,E123,E124,E125,E126,E127,E128,E711,E712,H302,H303,H404,F403,F811,F841 +ignore = E121,E122,E123,E124,E125,E126,E127,E128,E711,E712,H302,H303,H404,F403,F811,F841,N309 builtins = _ exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,plugins,tools