Add back pep8 test

In Change I523a4a85867f82d234ba1f3e6fad8b8cd2291182, the pep8 test was
accidentally dropped.

Fix up code so that pep8 passes.

In addition to that following change has been added here to unbreak CI:

Add WebTest as an indirect test dependency

Pecan has made webtest an optional dependency for testing only [1].
Since it is still used for testing we need to add it to our
test-requirements.txt.

[1]: https://github.com/pecan/pecan/pull/140

Change-Id: I2f85adb4ef29a43389897c201e6152fd4c7be9d6
This commit is contained in:
Jake Yip 2022-05-27 12:43:22 +10:00 committed by Michal Nasiadka
parent a5797a5eac
commit cb40fb3685
14 changed files with 42 additions and 29 deletions

View File

@ -72,7 +72,7 @@ def _delete_loadbalancers(context, lbs, cluster, octavia_client,
return candidates return candidates
def delete_loadbalancers(context, cluster): def delete_loadbalancers(context, cluster): # noqa: C901
"""Delete loadbalancers for the cluster. """Delete loadbalancers for the cluster.
The following load balancers are deleted: The following load balancers are deleted:

View File

@ -45,7 +45,7 @@ class Handler(object):
LOG.debug("Creating self signed x509 certificate") LOG.debug("Creating self signed x509 certificate")
try: try:
ca_cert_type = certificate.ca_cert_type ca_cert_type = certificate.ca_cert_type
except Exception as e: except Exception:
LOG.debug("There is no CA cert type specified for the CSR") LOG.debug("There is no CA cert type specified for the CSR")
ca_cert_type = "kubernetes" ca_cert_type = "kubernetes"

View File

@ -18,8 +18,7 @@ from magnum.conductor.handlers.common.cert_manager import create_client_files
class KubernetesAPI: class KubernetesAPI:
""" """Simple Kubernetes API client using requests.
Simple Kubernetes API client using requests.
This API wrapper allows for a set of very simple operations to be This API wrapper allows for a set of very simple operations to be
performed on a Kubernetes cluster using the `requests` library. The performed on a Kubernetes cluster using the `requests` library. The
@ -55,9 +54,7 @@ class KubernetesAPI:
return response.text return response.text
def get_healthz(self): def get_healthz(self):
""" """Get the health of the cluster from API"""
Get the health of the cluster from API
"""
return self._request( return self._request(
'GET', 'GET',
f"{self.cluster.api_address}/healthz", f"{self.cluster.api_address}/healthz",
@ -65,8 +62,7 @@ class KubernetesAPI:
) )
def list_node(self): def list_node(self):
""" """List all nodes in the cluster.
List all nodes in the cluster.
:return: List of nodes. :return: List of nodes.
""" """
@ -76,8 +72,7 @@ class KubernetesAPI:
) )
def list_namespaced_pod(self, namespace): def list_namespaced_pod(self, namespace):
""" """List all pods in the given namespace.
List all pods in the given namespace.
:param namespace: Namespace to list pods from. :param namespace: Namespace to list pods from.
:return: List of pods. :return: List of pods.
@ -88,9 +83,9 @@ class KubernetesAPI:
) )
def __del__(self): def __del__(self):
""" """Close all of the file descriptions for the certificates
Close all of the file descriptions for the certificates, since they
are left open by `create_client_files`. They are left open by `create_client_files`.
TODO(mnaser): Use a context manager and avoid having these here. TODO(mnaser): Use a context manager and avoid having these here.
""" """

View File

@ -344,7 +344,7 @@ class Connection(api.Connection):
query = self._add_tenant_filters(context, query) query = self._add_tenant_filters(context, query)
public_q = model_query(models.ClusterTemplate).filter_by(public=True) public_q = model_query(models.ClusterTemplate).filter_by(public=True)
query = query.union(public_q) query = query.union(public_q)
query = query.filter(models.ClusterTemplate.id==cluster_template_id) query = query.filter(models.ClusterTemplate.id == cluster_template_id)
try: try:
return query.one() return query.one()
except NoResultFound: except NoResultFound:
@ -356,7 +356,8 @@ class Connection(api.Connection):
query = self._add_tenant_filters(context, query) query = self._add_tenant_filters(context, query)
public_q = model_query(models.ClusterTemplate).filter_by(public=True) public_q = model_query(models.ClusterTemplate).filter_by(public=True)
query = query.union(public_q) query = query.union(public_q)
query = query.filter(models.ClusterTemplate.uuid==cluster_template_uuid) query = query.filter(
models.ClusterTemplate.uuid == cluster_template_uuid)
try: try:
return query.one() return query.one()
except NoResultFound: except NoResultFound:
@ -368,7 +369,8 @@ class Connection(api.Connection):
query = self._add_tenant_filters(context, query) query = self._add_tenant_filters(context, query)
public_q = model_query(models.ClusterTemplate).filter_by(public=True) public_q = model_query(models.ClusterTemplate).filter_by(public=True)
query = query.union(public_q) query = query.union(public_q)
query = query.filter(models.ClusterTemplate.name==cluster_template_name) query = query.filter(
models.ClusterTemplate.name == cluster_template_name)
try: try:
return query.one() return query.one()
except MultipleResultsFound: except MultipleResultsFound:

View File

@ -10,8 +10,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import ast
from oslo_utils import strutils from oslo_utils import strutils
from magnum.common import utils from magnum.common import utils

View File

@ -44,4 +44,3 @@ class Certificate(base.MagnumPersistentObject, base.MagnumObject):
return cls(project_id=cluster['project_id'], return cls(project_id=cluster['project_id'],
user_id=cluster['user_id'], user_id=cluster['user_id'],
cluster_uuid=cluster['uuid']) cluster_uuid=cluster['uuid'])

View File

@ -189,7 +189,8 @@ class TestListCluster(api_base.FunctionalTest):
@mock.patch("magnum.common.context.make_context") @mock.patch("magnum.common.context.make_context")
@mock.patch("magnum.objects.Cluster.obj_load_attr") @mock.patch("magnum.objects.Cluster.obj_load_attr")
@mock.patch("magnum.objects.Cluster.cluster_template") @mock.patch("magnum.objects.Cluster.cluster_template")
def test_get_all_with_all_projects(self, mock_context, mock_policy, mock_load, mock_template): def test_get_all_with_all_projects(self, mock_context, mock_policy,
mock_load, mock_template):
for id_ in range(4): for id_ in range(4):
temp_uuid = uuidutils.generate_uuid() temp_uuid = uuidutils.generate_uuid()
obj_utils.create_test_cluster(self.context, id=id_, obj_utils.create_test_cluster(self.context, id=id_,

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives import serialization
from unittest import mock from unittest import mock

View File

@ -247,8 +247,8 @@ class CertManagerTestCase(base.BaseTestCase):
mock_cluster, ca_cert_type="front-proxy") mock_cluster, ca_cert_type="front-proxy")
self.CertManager.get_cert.assert_called_once_with( self.CertManager.get_cert.assert_called_once_with(
mock_cluster.front_proxy_ca_cert_ref, resource_ref=mock_cluster.uuid, mock_cluster.front_proxy_ca_cert_ref,
context=None) resource_ref=mock_cluster.uuid, context=None)
self.assertEqual(mock_ca_cert, cluster_ca_cert) self.assertEqual(mock_ca_cert, cluster_ca_cert)
def test_get_cluster_magnum_cert(self): def test_get_cluster_magnum_cert(self):

View File

@ -610,7 +610,8 @@ class MonitorsTestCase(base.TestCase):
'api': 'ok', 'k8s-cluster-node-1.Ready': True}) 'api': 'ok', 'k8s-cluster-node-1.Ready': True})
@mock.patch('magnum.conductor.k8s_api.create_client_files') @mock.patch('magnum.conductor.k8s_api.create_client_files')
def test_k8s_monitor_health_unreachable_cluster(self, mock_create_client_files): def test_k8s_monitor_health_unreachable_cluster(
self, mock_create_client_files):
mock_create_client_files.return_value = ( mock_create_client_files.return_value = (
tempfile.NamedTemporaryFile(), tempfile.NamedTemporaryFile(),
tempfile.NamedTemporaryFile(), tempfile.NamedTemporaryFile(),
@ -634,7 +635,8 @@ class MonitorsTestCase(base.TestCase):
m_fields.ClusterHealthStatus.UNKNOWN) m_fields.ClusterHealthStatus.UNKNOWN)
@mock.patch('magnum.conductor.k8s_api.create_client_files') @mock.patch('magnum.conductor.k8s_api.create_client_files')
def test_k8s_monitor_health_unreachable_with_master_lb(self, mock_create_client_files): def test_k8s_monitor_health_unreachable_with_master_lb(
self, mock_create_client_files):
mock_create_client_files.return_value = ( mock_create_client_files.return_value = (
tempfile.NamedTemporaryFile(), tempfile.NamedTemporaryFile(),
tempfile.NamedTemporaryFile(), tempfile.NamedTemporaryFile(),

View File

@ -190,7 +190,8 @@ class TestK8sScaleManager(base.TestCase):
@mock.patch('magnum.objects.Cluster.get_by_uuid') @mock.patch('magnum.objects.Cluster.get_by_uuid')
@mock.patch('magnum.conductor.k8s_api.create_client_files') @mock.patch('magnum.conductor.k8s_api.create_client_files')
def test_get_hosts_with_container(self, mock_create_client_files, mock_get): def test_get_hosts_with_container(
self, mock_create_client_files, mock_get):
mock_cluster = mock.MagicMock() mock_cluster = mock.MagicMock()
mock_cluster.api_address = "https://foobar.com:6443" mock_cluster.api_address = "https://foobar.com:6443"

View File

@ -624,7 +624,8 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
'master_lb_allowed_cidrs') 'master_lb_allowed_cidrs')
octavia_provider = mock_cluster.labels.get('octavia_provider') octavia_provider = mock_cluster.labels.get('octavia_provider')
octavia_lb_algorithm = mock_cluster.labels.get('octavia_lb_algorithm') octavia_lb_algorithm = mock_cluster.labels.get('octavia_lb_algorithm')
octavia_lb_healthcheck = mock_cluster.labels.get('octavia_lb_healthcheck') octavia_lb_healthcheck = mock_cluster.labels.get(
'octavia_lb_healthcheck')
k8s_def = k8sa_tdef.AtomicK8sTemplateDefinition() k8s_def = k8sa_tdef.AtomicK8sTemplateDefinition()
@ -1186,7 +1187,8 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
octavia_provider = mock_cluster.labels.get('octavia_provider') octavia_provider = mock_cluster.labels.get('octavia_provider')
octavia_lb_algorithm = mock_cluster.labels.get('octavia_lb_algorithm') octavia_lb_algorithm = mock_cluster.labels.get('octavia_lb_algorithm')
octavia_lb_healthcheck = mock_cluster.labels.get('octavia_lb_healthcheck') octavia_lb_healthcheck = mock_cluster.labels.get(
'octavia_lb_healthcheck')
k8s_def = k8sa_tdef.AtomicK8sTemplateDefinition() k8s_def = k8sa_tdef.AtomicK8sTemplateDefinition()

View File

@ -21,3 +21,4 @@ testrepository>=0.0.20 # Apache-2.0/BSD
stestr>=3.1.0 # Apache-2.0 stestr>=3.1.0 # Apache-2.0
testscenarios>=0.4 # Apache-2.0/BSD testscenarios>=0.4 # Apache-2.0/BSD
testtools>=2.4.0 # MIT testtools>=2.4.0 # MIT
WebTest>=2.0.27 # MIT

13
tox.ini
View File

@ -93,6 +93,19 @@ commands =
find . -type f -name "*.py[c|o]" -delete find . -type f -name "*.py[c|o]" -delete
stestr run {posargs} stestr run {posargs}
[testenv:pep8]
commands =
doc8 -e .rst specs/ doc/source/ contrib/ CONTRIBUTING.rst HACKING.rst README.rst
bash tools/flake8wrap.sh {posargs}
bandit -r magnum -x tests -n5 -ll
bash -c "find {toxinidir} \
-not \( -type d -name .?\* -prune \) \
-not \( -type d -name doc -prune \) \
-not \( -type d -name contrib -prune \) \
-type f \
-name \*.sh \
-print0 | xargs -0 bashate -v -iE006,E010,E042 -eE005"
[testenv:venv] [testenv:venv]
commands = {posargs} commands = {posargs}