mgr/dashboard_v2: Fix pylint executor and warnings/errors

Signed-off-by: Ricardo Dias <rdias@suse.com>
This commit is contained in:
Ricardo Dias 2018-01-25 17:14:42 +00:00
parent 17ac1cd6f1
commit 694a3052af
No known key found for this signature in database
GPG Key ID: 74390C579BD37B68
12 changed files with 66 additions and 56 deletions

View File

@ -126,7 +126,13 @@ disable=print-statement,
next-method-defined,
dict-items-not-iterating,
dict-keys-not-iterating,
dict-values-not-iterating
dict-values-not-iterating,
missing-docstring,
invalid-name,
no-self-use,
too-few-public-methods,
no-member,
fixme
# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option

View File

@ -1,20 +1,24 @@
# -*- coding: utf-8 -*-
from mock import Mock
class BasePyOSDMap(object):
pass
class BasePyOSDMapIncremental(object):
pass
class BasePyCRUSH(object):
pass
class BaseMgrStandbyModule(object):
pass
class BaseMgrModule(object):
# pylint: disable=W0613
def __init__(self, py_modules_ptr, this_ptr):
self.config_key_map = {}
@ -32,5 +36,3 @@ class BaseMgrModule(object):
def _ceph_log(self, *args):
pass

View File

@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import bcrypt
import cherrypy
import time
import sys
from ..tools import ApiController, AuthRequired, RESTController
import bcrypt
import cherrypy
from ..tools import ApiController, RESTController
@ApiController('auth')
@ -30,6 +31,7 @@ class Auth(RESTController):
DEFAULT_SESSION_EXPIRE = 1200.0
def __init__(self):
# pylint: disable=E1101
self._mod = Auth._mgr_module_
self._log = self._mod.log
@ -46,10 +48,10 @@ class Auth(RESTController):
cherrypy.session[Auth.SESSION_KEY_TS] = now
self._log.debug("Login successful")
return {'username': username}
else:
cherrypy.response.status = 403
self._log.debug("Login fail")
return {'detail': 'Invalid credentials'}
cherrypy.response.status = 403
self._log.debug("Login fail")
return {'detail': 'Invalid credentials'}
def bulk_delete(self):
self._log.debug("Logout successful")
@ -62,8 +64,7 @@ class Auth(RESTController):
salt_password = bcrypt.gensalt()
if sys.version_info > (3, 0):
return bcrypt.hashpw(password, salt_password)
else:
return bcrypt.hashpw(password.encode('utf8'), salt_password)
return bcrypt.hashpw(password.encode('utf8'), salt_password)
@staticmethod
def check_auth():
@ -76,8 +77,7 @@ class Auth(RESTController):
'that resource')
now = time.time()
expires = float(module.get_localized_config(
'session-expire',
Auth.DEFAULT_SESSION_EXPIRE))
'session-expire', Auth.DEFAULT_SESSION_EXPIRE))
if expires > 0:
username_ts = cherrypy.session.get(Auth.SESSION_KEY_TS, None)
if username_ts and float(username_ts) < (now - expires):

View File

@ -10,7 +10,7 @@ from ..tools import ApiController, AuthRequired, RESTController
@AuthRequired()
class Ping(object):
@cherrypy.expose
def default(self, *args):
def default(self):
return "pong"

View File

@ -4,10 +4,10 @@ openATTIC mgr plugin (based on CherryPy)
"""
from __future__ import absolute_import
import errno
import os
import cherrypy
from cherrypy import tools
from mgr_module import MgrModule
from .controllers.auth import Auth
@ -15,10 +15,12 @@ from .tools import load_controllers
# cherrypy likes to sys.exit on error. don't let it take us down too!
# pylint: disable=W0613
def os_exit_noop(*args):
pass
# pylint: disable=W0212
os._exit = os_exit_noop
@ -37,9 +39,6 @@ class Module(MgrModule):
}
]
def __init__(self, *args, **kwargs):
super(Module, self).__init__(*args, **kwargs)
def serve(self):
server_addr = self.get_localized_config('server_addr', '::')
server_port = self.get_localized_config('server_port', '8080')
@ -48,13 +47,13 @@ class Module(MgrModule):
'no server_addr configured; '
'try "ceph config-key put mgr/{}/{}/server_addr <ip>"'
.format(self.module_name, self.get_mgr_id()))
self.log.info("server_addr: %s server_port: %s" % (server_addr,
server_port))
self.log.info("server_addr: %s server_port: %s", server_addr,
server_port)
cherrypy.config.update({
'server.socket_host': server_addr,
'server.socket_port': int(server_port),
})
'server.socket_host': server_addr,
'server.socket_port': int(server_port),
})
cherrypy.tools.autenticate = cherrypy.Tool('before_handler',
Auth.check_auth)
@ -86,9 +85,9 @@ class Module(MgrModule):
hashed_passwd = Auth.password_hash(cmd['password'])
self.set_localized_config('password', hashed_passwd)
return 0, 'Username and password updated', ''
else:
return (-errno.EINVAL, '', 'Command not found \'{0}\''.format(
cmd['prefix']))
return (-errno.EINVAL, '', 'Command not found \'{0}\''
.format(cmd['prefix']))
class ApiRoot(object):
def __init__(self, mgrmod):

View File

@ -16,6 +16,8 @@ pbr==3.1.1
pluggy==0.6.0
portend==2.2
py==1.5.2
pycodestyle==2.3.1
pycparser==2.18
pylint==1.8.2
pytest==3.3.2
pytest-cov==2.5.1

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# pylint: disable=W0212
from __future__ import absolute_import
import json

View File

@ -29,8 +29,8 @@ class AuthTest(ApiControllerTestCase):
cherrypy.tree.mount(Ping(), "/api/test",
config={'/': {'tools.autenticate.on': True}})
module.set_localized_config('session-expire','2')
module.set_localized_config('username','admin')
module.set_localized_config('session-expire', '2')
module.set_localized_config('username', 'admin')
module.set_localized_config('password', Auth.password_hash('admin'))
def test_login_valid(self):
@ -39,7 +39,7 @@ class AuthTest(ApiControllerTestCase):
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
self.assertStatus('201 Created')
self.assertBody('{"username": "admin"}')
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), 'admin')
def test_login_invalid(self):
sess_mock = RamSession()
@ -47,35 +47,35 @@ class AuthTest(ApiControllerTestCase):
self._post("/api/auth", {'username': 'admin', 'password': 'inval'})
self.assertStatus('403 Forbidden')
self.assertBody('{"detail": "Invalid credentials"}')
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), None)
def test_logout(self):
sess_mock = RamSession()
with patch('cherrypy.session', sess_mock, create=True):
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), 'admin')
self._delete("/api/auth")
self.assertStatus('204 No Content')
self.assertBody('')
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), None)
def test_session_expire(self):
sess_mock = RamSession()
with patch('cherrypy.session', sess_mock, create=True):
self._post("/api/auth", {'username': 'admin', 'password': 'admin'})
self.assertStatus('201 Created')
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), 'admin')
self._post("/api/test/ping")
self.assertStatus('200 OK')
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), 'admin')
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), 'admin')
time.sleep(3)
self._post("/api/test/ping")
self.assertStatus('401 Unauthorized')
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), None)
def test_unauthorized(self):
sess_mock = RamSession()
with patch('cherrypy.session', sess_mock, create=True):
self._post("/api/test/ping")
self.assertStatus('401 Unauthorized')
self.assertEquals(sess_mock.get(Auth.SESSION_KEY), None)
self.assertEqual(sess_mock.get(Auth.SESSION_KEY), None)

View File

@ -21,4 +21,3 @@ class SimpleCPTest(ApiControllerTestCase):
def test_echo_args(self):
self._post("/api/echo1", {'msg': 'Hello World'})
self.assertStatus('201 Created')

View File

@ -10,7 +10,7 @@ from mock import patch
from .helper import ApiControllerTestCase
from ..tools import RESTController
# pylint: disable=W0613
class FooResource(RESTController):
elems = []
@ -30,7 +30,7 @@ class FooResource(RESTController):
def bulk_delete(self):
FooResource.elems = []
# pylint: disable=C0102
class Root(object):
foo = FooResource()

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# pylint: disable=W0212
from __future__ import absolute_import
import importlib
@ -130,7 +131,7 @@ class RESTController(object):
is_element = len(vpath) > 0
(method_name, status_code) = self._method_mapping[
(cherrypy.request.method, is_element)]
(cherrypy.request.method, is_element)]
method = getattr(self, method_name, None)
if not method:
self._not_implemented(is_element)
@ -150,6 +151,7 @@ class RESTController(object):
func._args_from_json_ = True
return func
# pylint: disable=W1505
@staticmethod
def _takes_json(func):
def inner(*args, **kwargs):
@ -157,25 +159,28 @@ class RESTController(object):
body = cherrypy.request.body.read(content_length)
if not body:
raise cherrypy.HTTPError(400, 'Empty body. Content-Length={}'
.format(content_length))
.format(content_length))
try:
data = json.loads(body.decode('utf-8'))
except Exception as e:
raise cherrypy.HTTPError(400, 'Failed to decode JSON: {}'
.format(str(e)))
.format(str(e)))
if hasattr(func, '_args_from_json_'):
f_args = inspect.getargspec(func).args
if sys.version_info > (3, 0):
f_args = list(inspect.signature(func).parameters.keys())
else:
f_args = inspect.getargspec(func).args[1:]
n_args = []
for arg in args:
n_args.append(arg)
for arg in f_args[1:]:
for arg in f_args:
if arg in data:
n_args.append(data[arg])
data.pop(arg)
kwargs.update(data)
return func(*n_args, **kwargs)
else:
return func(data, *args, **kwargs)
return func(data, *args, **kwargs)
return inner
@staticmethod

View File

@ -19,10 +19,7 @@ commands=
{envbindir}/py.test --cov=. --cov-report=term --cov-report=xml --junitxml=junit.xml tests/
[testenv:lint]
deps=
pylint
pycodestyle
deps=-r{toxinidir}/requirements.txt
commands=
pylint --rcfile=.pylintrc --jobs=5 .
pycodestyle --max-line-length=100 --exclude=ceph_module_mock.py,python2.7,tests,.tox,venv .
pylint --rcfile=.pylintrc --jobs=5 . module.py tools.py ceph_module_mock.py controllers tests
pycodestyle --max-line-length=100 --exclude=python2.7,.tox,venv .