From cecb2b1cde910c6b9dc521ab6d1d90e70c136553 Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Thu, 2 Jul 2020 15:51:01 +0200 Subject: [PATCH] Add flake8 pre-commit hook Change-Id: I1893f9031091fb02e62633d2ee387aff2b967237 --- .pre-commit-config.yaml | 7 ++ tobiko/common/_logging.py | 1 - tobiko/openstack/keystone/_credentials.py | 2 +- tobiko/openstack/neutron/_agent.py | 1 - tobiko/openstack/neutron/_extension.py | 1 - tobiko/podman/_client.py | 6 - tobiko/shell/sh/_io.py | 1 - tobiko/shell/sh/_process.py | 1 - tobiko/shell/ssh/_client.py | 1 - .../faults/agents/test_neutron_agents.py | 4 +- tobiko/tests/faults/ha/cloud_disruptions.py | 3 +- tobiko/tests/functional/docker/test_client.py | 3 +- tobiko/tests/scenario/nova/test_server.py | 3 +- .../unit/openstack/nova/test_cloud_init.py | 3 +- tobiko/tests/unit/podman/test_shell.py | 116 ++++++++---------- tobiko/tests/unit/test_exception.py | 3 +- tobiko/tripleo/validations.py | 14 ++- tox.ini | 12 +- 18 files changed, 88 insertions(+), 94 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aec4347cb..3c6cb6907 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,6 +18,13 @@ repos: args: [requirements.txt, test-requirements.txt, extra-requirements.txt, linters-requirements.txt, doc/requirements.txt, infrared_plugin/requirements.txt] - id: trailing-whitespace + - repo: https://gitlab.com/pycqa/flake8 + rev: '3.8.3' # pick a git hash / tag to point to + hooks: + - id: flake8 + additional_dependencies: [flake8-import-order] + files: ^tobiko/ + # - repo: https://github.com/adrienverge/yamllint.git # rev: v1.23.0 # hooks: diff --git a/tobiko/common/_logging.py b/tobiko/common/_logging.py index a4cb77e4c..c812e3abe 100644 --- a/tobiko/common/_logging.py +++ b/tobiko/common/_logging.py @@ -16,7 +16,6 @@ from __future__ import absolute_import import logging from oslo_log import log - import testtools from testtools import content diff --git a/tobiko/openstack/keystone/_credentials.py b/tobiko/openstack/keystone/_credentials.py index ca9cb28df..048b53bb2 100644 --- a/tobiko/openstack/keystone/_credentials.py +++ b/tobiko/openstack/keystone/_credentials.py @@ -18,10 +18,10 @@ import os import sys from oslo_log import log +import testtools import yaml import tobiko -import testtools LOG = log.getLogger(__name__) diff --git a/tobiko/openstack/neutron/_agent.py b/tobiko/openstack/neutron/_agent.py index c0fc4c094..ad9d73910 100644 --- a/tobiko/openstack/neutron/_agent.py +++ b/tobiko/openstack/neutron/_agent.py @@ -14,7 +14,6 @@ from __future__ import absolute_import import tobiko - from tobiko.openstack.neutron import _client diff --git a/tobiko/openstack/neutron/_extension.py b/tobiko/openstack/neutron/_extension.py index 7fb2c5e0a..433593177 100644 --- a/tobiko/openstack/neutron/_extension.py +++ b/tobiko/openstack/neutron/_extension.py @@ -16,7 +16,6 @@ from __future__ import absolute_import import collections import tobiko - from tobiko.openstack.neutron import _client diff --git a/tobiko/podman/_client.py b/tobiko/podman/_client.py index 38b905919..e2414e4cc 100644 --- a/tobiko/podman/_client.py +++ b/tobiko/podman/_client.py @@ -15,11 +15,8 @@ # under the License. from __future__ import absolute_import -import six - import podman - import tobiko from tobiko.podman import _exception from tobiko.podman import _shell @@ -56,9 +53,6 @@ class PodmanClientFixture(tobiko.SharedFixture): ssh_client = None def __init__(self, ssh_client=None): - if six.PY2: - raise _exception.PodmanError( - "Podman isn't compatible with python 2.7") super(PodmanClientFixture, self).__init__() if ssh_client: self.ssh_client = ssh_client diff --git a/tobiko/shell/sh/_io.py b/tobiko/shell/sh/_io.py index f6ccdab3e..26f0f8c8c 100644 --- a/tobiko/shell/sh/_io.py +++ b/tobiko/shell/sh/_io.py @@ -19,7 +19,6 @@ import io import select from oslo_log import log - import six LOG = log.getLogger(__name__) diff --git a/tobiko/shell/sh/_process.py b/tobiko/shell/sh/_process.py index 0fc71cc57..179f56f2b 100644 --- a/tobiko/shell/sh/_process.py +++ b/tobiko/shell/sh/_process.py @@ -21,7 +21,6 @@ import time from oslo_log import log import tobiko - from tobiko.shell.sh import _command from tobiko.shell.sh import _exception from tobiko.shell.sh import _io diff --git a/tobiko/shell/ssh/_client.py b/tobiko/shell/ssh/_client.py index a61ef380b..9a5bcb7a1 100644 --- a/tobiko/shell/ssh/_client.py +++ b/tobiko/shell/ssh/_client.py @@ -16,7 +16,6 @@ from __future__ import absolute_import import collections - import getpass import os import socket diff --git a/tobiko/tests/faults/agents/test_neutron_agents.py b/tobiko/tests/faults/agents/test_neutron_agents.py index 5ca42dd0c..743db879f 100644 --- a/tobiko/tests/faults/agents/test_neutron_agents.py +++ b/tobiko/tests/faults/agents/test_neutron_agents.py @@ -16,9 +16,8 @@ from __future__ import absolute_import import time -import testtools - from oslo_log import log +import testtools import tobiko from tobiko.openstack import neutron @@ -28,6 +27,7 @@ from tobiko.openstack import topology from tobiko.shell import ping from tobiko.shell import sh + LOG = log.getLogger(__name__) diff --git a/tobiko/tests/faults/ha/cloud_disruptions.py b/tobiko/tests/faults/ha/cloud_disruptions.py index d184d9961..a5f2183b1 100644 --- a/tobiko/tests/faults/ha/cloud_disruptions.py +++ b/tobiko/tests/faults/ha/cloud_disruptions.py @@ -3,6 +3,8 @@ from __future__ import absolute_import import re +from oslo_log import log + import tobiko from tobiko.shell import sh from tobiko.openstack import topology @@ -11,7 +13,6 @@ from tobiko.openstack import keystone from tobiko.tripleo import pacemaker from tobiko.tripleo import containers from tobiko.tripleo import nova -from oslo_log import log from tobiko.tests.faults.ha import test_cloud_recovery diff --git a/tobiko/tests/functional/docker/test_client.py b/tobiko/tests/functional/docker/test_client.py index c78afaf78..2d334231e 100644 --- a/tobiko/tests/functional/docker/test_client.py +++ b/tobiko/tests/functional/docker/test_client.py @@ -15,10 +15,9 @@ # under the License. from __future__ import absolute_import -import testtools - from docker import client as docker_client from docker.models import containers +import testtools import tobiko from tobiko import docker diff --git a/tobiko/tests/scenario/nova/test_server.py b/tobiko/tests/scenario/nova/test_server.py index 5500b37b3..915885ca5 100644 --- a/tobiko/tests/scenario/nova/test_server.py +++ b/tobiko/tests/scenario/nova/test_server.py @@ -14,8 +14,9 @@ # under the License. from __future__ import absolute_import -import tobiko import testtools + +import tobiko from tobiko.openstack import stacks from tobiko.openstack import neutron from tobiko.openstack import nova diff --git a/tobiko/tests/unit/openstack/nova/test_cloud_init.py b/tobiko/tests/unit/openstack/nova/test_cloud_init.py index d7baeb966..109468f2e 100644 --- a/tobiko/tests/unit/openstack/nova/test_cloud_init.py +++ b/tobiko/tests/unit/openstack/nova/test_cloud_init.py @@ -13,9 +13,8 @@ # under the License. from __future__ import absolute_import -import yaml - import testtools +import yaml from tobiko.openstack import nova diff --git a/tobiko/tests/unit/podman/test_shell.py b/tobiko/tests/unit/podman/test_shell.py index 0ca7a01bd..1718bc11d 100644 --- a/tobiko/tests/unit/podman/test_shell.py +++ b/tobiko/tests/unit/podman/test_shell.py @@ -15,74 +15,62 @@ from __future__ import absolute_import import mock -import six +from tobiko import podman +from tobiko.tests import unit -# We need to ignore this code under py2 -# it's not compatible and parser will failed even if we use -# the `unittest.skipIf` decorator, because during the test discovery -# stestr and unittest will load this test -# module before running it and it will load podman -# too which isn't compatible in version leather than python 3 -# Also the varlink mock module isn't compatible with py27, is using -# annotations syntaxe to generate varlink interface for the mocked service -# and it will raise related exceptions too. -# For all these reasons we can't run podman tests under a python 2 environment -if six.PY3: - from tobiko import podman - from tobiko.tests import unit - class TestShell(unit.TobikoUnitTest): +class TestShell(unit.TobikoUnitTest): - @mock.patch('tobiko.shell.sh.execute') - def test_discover_podman_socket(self, mock_execute): - class FakeProcess: - exit_status = 0 - stdout = '/run/podman/io.podman' - stderr = '' - mock_execute.return_value = FakeProcess() - self.assertEqual( - podman.discover_podman_socket(), - '/run/podman/io.podman' - ) + @mock.patch('tobiko.shell.sh.execute') + def test_discover_podman_socket(self, mock_execute): + class FakeProcess: + exit_status = 0 + stdout = '/run/podman/io.podman' + stderr = '' + mock_execute.return_value = FakeProcess() + self.assertEqual( + podman.discover_podman_socket(), + '/run/podman/io.podman' + ) - @mock.patch('tobiko.shell.sh.execute') - def test_discover_podman_socket_none_result(self, mock_execute): - class FakeProcess: - exit_status = 1 - stdout = '' - stderr = 'boom' - mock_execute.return_value = FakeProcess() - self.assertRaises( - podman.PodmanSocketNotFoundError, - podman.discover_podman_socket - ) + @mock.patch('tobiko.shell.sh.execute') + def test_discover_podman_socket_none_result(self, mock_execute): + class FakeProcess: + exit_status = 1 + stdout = '' + stderr = 'boom' + mock_execute.return_value = FakeProcess() + self.assertRaises( + podman.PodmanSocketNotFoundError, + podman.discover_podman_socket + ) - @mock.patch('tobiko.shell.sh.execute') - def test_discover_podman_socket_with_exit_code(self, mock_execute): - class FakeProcess: - exit_status = 0 - stdout = '' - stderr = 'boom' - mock_execute.return_value = FakeProcess() - self.assertRaises( - podman.PodmanSocketNotFoundError, - podman.discover_podman_socket - ) + @mock.patch('tobiko.shell.sh.execute') + def test_discover_podman_socket_with_exit_code(self, mock_execute): + class FakeProcess: + exit_status = 0 + stdout = '' + stderr = 'boom' + mock_execute.return_value = FakeProcess() + self.assertRaises( + podman.PodmanSocketNotFoundError, + podman.discover_podman_socket + ) - @mock.patch('tobiko.shell.sh.execute') - def test_is_podman_running(self, mock_execute): - class FakeProcess: - exit_status = 0 - stdout = '/run/podman/io.podman' - stderr = '' - mock_execute.return_value = FakeProcess() - self.assertEqual(podman.is_podman_running(), True) + @mock.patch('tobiko.shell.sh.execute') + def test_is_podman_running(self, mock_execute): + class FakeProcess: + exit_status = 0 + stdout = '/run/podman/io.podman' + stderr = '' + mock_execute.return_value = FakeProcess() + self.assertEqual(podman.is_podman_running(), True) - @mock.patch('tobiko.shell.sh.execute') - def test_is_podman_running_without_socket(self, mock_execute): - class FakeProcess: - exit_status = 1 - stdout = '' - stderr = 'boom' - mock_execute.return_value = FakeProcess() - self.assertEqual(podman.is_podman_running(), False) + @mock.patch('tobiko.shell.sh.execute') + def test_is_podman_running_without_socket(self, mock_execute): + class FakeProcess: + exit_status = 1 + stdout = '' + stderr = 'boom' + mock_execute.return_value = FakeProcess() + self.assertEqual(podman.is_podman_running(), False) diff --git a/tobiko/tests/unit/test_exception.py b/tobiko/tests/unit/test_exception.py index 7a721d601..ae5ef997e 100644 --- a/tobiko/tests/unit/test_exception.py +++ b/tobiko/tests/unit/test_exception.py @@ -15,9 +15,10 @@ from __future__ import absolute_import import sys +import testtools + import tobiko from tobiko.tests import unit -import testtools class SomeException(tobiko.TobikoException): diff --git a/tobiko/tripleo/validations.py b/tobiko/tripleo/validations.py index 75ece15d9..fd7afc12f 100644 --- a/tobiko/tripleo/validations.py +++ b/tobiko/tripleo/validations.py @@ -1,7 +1,19 @@ +# Copyright 2020 Red Hat +# +# 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 __future__ import absolute_import from oslo_log import log - from validations_libs import validation_actions from validations_libs import constants as v_consts diff --git a/tox.ini b/tox.ini index fc288ecf1..62b1c128e 100644 --- a/tox.ini +++ b/tox.ini @@ -76,7 +76,7 @@ deps = {[testenv]deps} -r{toxinidir}/linters-requirements.txt commands = - flake8 --max-complexity 10 + pre-commit run -a flake8 [testenv:pylint] @@ -95,11 +95,8 @@ whitelist_externals = bash deps = {[testenv:pep8]deps} envdir = {toxworkdir}/pep8 commands = - pre-commit --version - pre-commit autoupdate - pre-commit run --all-files + pre-commit run -a pre-commit install - {[testenv:pep8]commands} mypy --ignore-missing-imports tobiko/ # Ansible lint # [ANSIBLE0012] Commands should not change things if nothing needs doing @@ -120,10 +117,11 @@ commands = enable-extensions = H106,H203,H204,H205,H904 show-source = true -exclude = ./.*,*lib/python*,build,dist,doc,*egg*,releasenotes,.venv +exclude = ./.*,*lib/python*,build,dist,doc,*egg*,releasenotes,.venv,.tox +application-import-names = tobiko +max-complexity = 10 import-order-style = pep8 - # --- integration test environments ------------------------------------------ [integration]