Refactor code

- Fix pep8 error H903: [1]
- Add method _update_task_state to reduce code lines.
- Change single quotes to double quotes in docstring - PEP8 docstring
convention [2].

[1] http://paste.openstack.org/show/614990/
[2] https://www.python.org/dev/peps/pep-0257/

Partials-Bug: #1702587

Change-Id: Idd125665a2a5f0a73337d36f0259a9f9107946ad
This commit is contained in:
Kien Nguyen 2017-07-11 16:52:36 +07:00
parent 076c921229
commit 56592105f9
17 changed files with 171 additions and 178 deletions

View File

@ -172,13 +172,13 @@ class ContainersController(base.Controller):
return view.format_container(pecan.request.host_url, container)
def _generate_name_for_container(self):
'''Generate a random name like: zeta-22-container.'''
"""Generate a random name like: zeta-22-container."""
name_gen = name_generator.NameGenerator()
name = name_gen.generate()
return name + '-container'
def _check_for_restart_policy(self, container_dict):
'''Check for restart policy input'''
"""Check for restart policy input"""
restart_policy = container_dict.get('restart_policy')
if not restart_policy:
return

View File

@ -52,7 +52,7 @@ class ImageCollection(collection.Collection):
class ImagesController(base.Controller):
'''Controller for Images'''
"""Controller for Images"""
_custom_actions = {
'search': ['GET']
@ -61,7 +61,7 @@ class ImagesController(base.Controller):
@pecan.expose('json')
@exception.wrap_pecan_controller_exception
def get_all(self, **kwargs):
'''Retrieve a list of images.'''
"""Retrieve a list of images."""
context = pecan.request.context
policy.enforce(context, "image:get_all",
action="image:get_all")

View File

@ -1,35 +1,35 @@
# Copyright 2014 IBM Corp.
#
# 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.
class VersionedMethod(object):
def __init__(self, name, start_version, end_version, func):
"""Versioning information for a single method
@name: Name of the method
@start_version: Minimum acceptable version
@end_version: Maximum acceptable_version
@func: Method to call
Minimum and maximums are inclusive
"""
self.name = name
self.start_version = start_version
self.end_version = end_version
self.func = func
def __str__(self):
return ("Version Method %s: min: %s, max: %s"
% (self.name, self.start_version, self.end_version))
# Copyright 2014 IBM Corp.
#
# 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.
class VersionedMethod(object):
def __init__(self, name, start_version, end_version, func):
"""Versioning information for a single method
@name: Name of the method
@start_version: Minimum acceptable version
@end_version: Maximum acceptable_version
@func: Method to call
Minimum and maximums are inclusive
"""
self.name = name
self.start_version = start_version
self.end_version = end_version
self.func = func
def __str__(self):
return ("Version Method %s: min: %s, max: %s"
% (self.name, self.start_version, self.end_version))

View File

@ -23,10 +23,10 @@ class NameGenerator(object):
self.random = Random()
def generate(self):
'''Generate a random name compose of a Greek leter and
"""Generate a random name compose of a Greek leter and
a number, like: beta_2.
'''
"""
letter = self.random.choice(self.letters)
number = self.random.randint(1, 24)

View File

@ -170,7 +170,7 @@ def translate_exception(function):
def check_container_id(function):
'''Check container_id property of given container instance.'''
"""Check container_id property of given container instance."""
@functools.wraps(function)
def decorated_function(*args, **kwargs):

View File

@ -33,7 +33,7 @@ LOG = logging.getLogger(__name__)
class Manager(object):
'''Manages the running containers.'''
"""Manages the running containers."""
def __init__(self, container_driver=None):
super(Manager, self).__init__()
@ -73,6 +73,10 @@ class Manager(object):
LOG.error("Error occurred while deleting sandbox: %s",
six.text_type(e))
def _update_task_state(self, context, container, task_state):
container.task_state = task_state
container.save(context)
def _do_container_create(self, context, container, requested_networks,
limits=None, reraise=False):
LOG.debug('Creating container: %s', container.uuid)
@ -86,9 +90,7 @@ class Manager(object):
self._fail_container(self, context, container, msg)
return
container.task_state = consts.SANDBOX_CREATING
container.save(context)
sandbox_id = None
self._update_task_state(context, container, consts.SANDBOX_CREATING)
sandbox_image = CONF.sandbox_image
sandbox_image_driver = CONF.sandbox_image_driver
sandbox_image_pull_policy = CONF.sandbox_image_pull_policy
@ -109,8 +111,7 @@ class Manager(object):
self._fail_container(context, container, six.text_type(e))
return
container.task_state = consts.IMAGE_PULLING
container.save(context)
self._update_task_state(context, container, consts.IMAGE_PULLING)
repo, tag = utils.parse_image_name(container.image)
image_pull_policy = utils.get_image_pull_policy(
container.image_pull_policy, tag)
@ -151,8 +152,7 @@ class Manager(object):
limits):
container = self.driver.create(context, container,
sandbox_id, image)
container.task_state = None
container.save(context)
self._update_task_state(context, container, None)
return container
except exception.DockerError as e:
with excutils.save_and_reraise_exception(reraise=reraise):
@ -173,12 +173,10 @@ class Manager(object):
def _do_container_start(self, context, container, reraise=False):
LOG.debug('Starting container: %s', container.uuid)
container.task_state = consts.CONTAINER_STARTING
container.save(context)
self._update_task_state(context, container, consts.CONTAINER_STARTING)
try:
container = self.driver.start(context, container)
container.task_state = None
container.save(context)
self._update_task_state(context, container, None)
return container
except exception.DockerError as e:
with excutils.save_and_reraise_exception(reraise=reraise):
@ -194,8 +192,7 @@ class Manager(object):
@translate_exception
def container_delete(self, context, container, force):
LOG.debug('Deleting container: %s', container.uuid)
container.task_state = consts.CONTAINER_DELETING
container.save(context)
self._update_task_state(context, container, consts.CONTAINER_DELETING)
reraise = not force
try:
self.driver.delete(container, force)
@ -211,8 +208,8 @@ class Manager(object):
sandbox_id = self.driver.get_sandbox_id(container)
if sandbox_id:
container.task_state = consts.SANDBOX_DELETING
container.save(context)
self._update_task_state(context, container,
consts.SANDBOX_DELETING)
try:
self.driver.delete_sandbox(context, container, sandbox_id)
except Exception as e:
@ -220,8 +217,7 @@ class Manager(object):
LOG.exception("Unexpected exception: %s",
six.text_type(e))
self._fail_container(context, container, six.text_type(e))
container.task_state = None
container.save(context)
self._update_task_state(context, container, None)
container.destroy(context)
self._get_resource_tracker()
@ -250,7 +246,7 @@ class Manager(object):
def container_list(self, context):
LOG.debug('Listing container...')
try:
return self.driver.list()
return self.driver.list(context)
except exception.DockerError as e:
LOG.error("Error occurred while calling Docker list API: %s",
six.text_type(e))
@ -277,12 +273,10 @@ class Manager(object):
def _do_container_reboot(self, context, container, timeout, reraise=False):
LOG.debug('Rebooting container: %s', container.uuid)
container.task_state = consts.CONTAINER_REBOOTING
container.save(context)
self._update_task_state(context, container, consts.CONTAINER_REBOOTING)
try:
container = self.driver.reboot(context, container, timeout)
container.task_state = None
container.save(context)
self._update_task_state(context, container, None)
return container
except exception.DockerError as e:
with excutils.save_and_reraise_exception(reraise=reraise):
@ -300,12 +294,10 @@ class Manager(object):
def _do_container_stop(self, context, container, timeout, reraise=False):
LOG.debug('Stopping container: %s', container.uuid)
container.task_state = consts.CONTAINER_STOPPING
container.save(context)
self._update_task_state(context, container, consts.CONTAINER_STOPPING)
try:
container = self.driver.stop(context, container, timeout)
container.task_state = None
container.save(context)
self._update_task_state(context, container, None)
return container
except exception.DockerError as e:
with excutils.save_and_reraise_exception(reraise=reraise):
@ -561,8 +553,6 @@ class Manager(object):
def _do_container_commit(self, context, snapshot_image, container,
repository, tag=None):
LOG.debug('Creating image...')
container_image = None
container_image_id = None
if tag is None:
tag = 'latest'

View File

@ -39,13 +39,13 @@ def check_container_host(func):
@profiler.trace_cls("rpc")
class API(rpc_service.API):
'''Client side of the container compute rpc API.
"""Client side of the container compute rpc API.
API version history:
* 1.0 - Initial version.
* 1.1 - Add image endpoints.
'''
"""
def __init__(self, transport=None, context=None, topic=None):
if topic is None:

View File

@ -71,7 +71,7 @@ def wrap_docker_error(function):
class DockerDriver(driver.ContainerDriver):
'''Implementation of container drivers for Docker.'''
"""Implementation of container drivers for Docker."""
def __init__(self):
super(DockerDriver, self).__init__()
@ -758,7 +758,7 @@ class NovaDockerDriver(DockerDriver):
return sandbox_id
def _ensure_active(self, novaclient, server, timeout=300):
'''Wait until the Nova instance to become active.'''
"""Wait until the Nova instance to become active."""
def _check_active():
return novaclient.check_active(server)
@ -791,7 +791,7 @@ class NovaDockerDriver(DockerDriver):
novaclient.stop_server(server_name)
def _ensure_deleted(self, novaclient, server_id, timeout=300):
'''Wait until the Nova instance to be deleted.'''
"""Wait until the Nova instance to be deleted."""
def _check_delete_complete():
return novaclient.check_delete_server_complete(server_id)

View File

@ -58,7 +58,7 @@ def load_container_driver(container_driver=None):
class ContainerDriver(object):
'''Base class for container drivers.'''
"""Base class for container drivers."""
def create(self, context, container, sandbox_name=None):
"""Create a container."""
@ -203,6 +203,9 @@ class ContainerDriver(object):
def add_security_group(self, context, container, security_group, **kwargs):
raise NotImplementedError()
def get_available_nodes(self):
pass
def get_available_resources(self, node):
numa_topo_obj = self.get_host_numa_topology()
node.numa_topology = numa_topo_obj

View File

@ -136,7 +136,7 @@ def upload_image_data(context, image, image_tag, image_data,
class ContainerImageDriver(object):
'''Base class for container image driver.'''
"""Base class for container image driver."""
def pull_image(self, context, repo, tag):
"""Pull an image."""

View File

@ -1,45 +1,45 @@
# 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.
"""
Scheduler host filters
"""
from zun.scheduler import base_filters
class BaseHostFilter(base_filters.BaseFilter):
"""Base class for host filters."""
def _filter_one(self, obj, filter_properties, extra_spec):
"""Return True if the object passes the filter, otherwise False."""
return self.host_passes(obj, filter_properties, extra_spec)
def host_passes(self, host_state, filter_properties, extra_spec):
"""Return True if the HostState passes the filter,otherwise False.
Override this in a subclass.
"""
raise NotImplementedError()
class HostFilterHandler(base_filters.BaseFilterHandler):
def __init__(self):
super(HostFilterHandler, self).__init__(BaseHostFilter)
def all_filters():
"""Return a list of filter classes found in this directory.
This method is used as the default for available scheduler filters
and should return a list of all filter classes available.
"""
return HostFilterHandler().get_all_classes()
# 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.
"""
Scheduler host filters
"""
from zun.scheduler import base_filters
class BaseHostFilter(base_filters.BaseFilter):
"""Base class for host filters."""
def _filter_one(self, obj, filter_properties, extra_spec):
"""Return True if the object passes the filter, otherwise False."""
return self.host_passes(obj, filter_properties, extra_spec)
def host_passes(self, host_state, filter_properties, extra_spec):
"""Return True if the HostState passes the filter,otherwise False.
Override this in a subclass.
"""
raise NotImplementedError()
class HostFilterHandler(base_filters.BaseFilterHandler):
def __init__(self):
super(HostFilterHandler, self).__init__(BaseHostFilter)
def all_filters():
"""Return a list of filter classes found in this directory.
This method is used as the default for available scheduler filters
and should return a list of all filter classes available.
"""
return HostFilterHandler().get_all_classes()

View File

@ -23,10 +23,10 @@ LOG = log.getLogger(__name__)
class ZunServicePeriodicTasks(periodic_task.PeriodicTasks):
'''Zun periodic Task class
"""Zun periodic Task class
Any periodic task job need to be added into this class
'''
"""
def __init__(self, conf, binary):
self.zun_service_ref = None

View File

@ -291,7 +291,7 @@ class TestManager(base.TestCase):
@mock.patch.object(fake_driver, 'list')
def test_container_list(self, mock_list):
self.compute_manager.container_list(self.context)
mock_list.assert_called_once_with()
mock_list.assert_called_once_with(self.context)
@mock.patch.object(fake_driver, 'list')
def test_container_list_failed(self, mock_list):

View File

@ -1,43 +1,43 @@
# Copyright 2016 IBM Corp.
#
# 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.
import mock
from zun.common import exception
from zun.compute import rpcapi
from zun import objects
from zun.tests import base
from zun.tests.unit.db import utils
class TestAPI(base.TestCase):
def setUp(self):
super(TestAPI, self).setUp()
self.compute_rpcapi = rpcapi.API()
@mock.patch('zun.api.servicegroup.ServiceGroup.service_is_up')
@mock.patch('zun.objects.ZunService.list_by_binary')
@mock.patch('zun.common.rpc_service.API._call')
def test_container_delete_with_host_no_tup(self, mock_rpc_call,
mock_list, mock_service_is_up):
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context, **test_container)
test_service = utils.get_test_zun_service(host="fake_host")
test_service_obj = objects.ZunService(self.context, **test_service)
mock_list.return_value = [test_service_obj]
mock_service_is_up.return_value = False
self.assertRaises(exception.ContainerHostNotUp,
self.compute_rpcapi.container_delete,
self.context, test_container_obj, False)
# Copyright 2016 IBM Corp.
#
# 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.
import mock
from zun.common import exception
from zun.compute import rpcapi
from zun import objects
from zun.tests import base
from zun.tests.unit.db import utils
class TestAPI(base.TestCase):
def setUp(self):
super(TestAPI, self).setUp()
self.compute_rpcapi = rpcapi.API()
@mock.patch('zun.api.servicegroup.ServiceGroup.service_is_up')
@mock.patch('zun.objects.ZunService.list_by_binary')
@mock.patch('zun.common.rpc_service.API._call')
def test_container_delete_with_host_no_tup(self, mock_rpc_call,
mock_list, mock_service_is_up):
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context, **test_container)
test_service = utils.get_test_zun_service(host="fake_host")
test_service_obj = objects.ZunService(self.context, **test_service)
mock_list.return_value = [test_service_obj]
mock_service_is_up.return_value = False
self.assertRaises(exception.ContainerHostNotUp,
self.compute_rpcapi.container_delete,
self.context, test_container_obj, False)

View File

@ -17,7 +17,7 @@ from zun.container import driver
class FakeDriver(driver.ContainerDriver):
'''Fake driver for testing.'''
"""Fake driver for testing."""
def __init__(self):
super(FakeDriver, self).__init__()

View File

@ -117,7 +117,7 @@ def create_test_image(**kwargs):
def _generate_repo_for_image():
'''Generate a random name like: zeta-22-image.'''
"""Generate a random name like: zeta-22-image."""
name_gen = name_generator.NameGenerator()
name = name_gen.generate()
return name + '-image'

View File

@ -12,10 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.
'''
"""
Websocket proxy that is compatible with OpenStack Zun.
Leverages websockify.py by Joel Martin
'''
"""
import errno
import select
@ -82,10 +82,10 @@ class ZunProxyRequestHandlerBase(object):
return None
def _handle_ins_outs(self, target, ins, outs):
'''Handle the select file ins and outs
"""Handle the select file ins and outs
handle the operation ins and outs from select
'''
Handle the operation ins and outs from select
"""
if self.request in outs:
# Send queued target data to the client
self.c_pend = self.send_frames(self.cqueue)
@ -122,10 +122,10 @@ class ZunProxyRequestHandlerBase(object):
self.cqueue.append(buf)
def do_proxy(self, target):
'''Proxy websocket link
"""Proxy websocket link
Proxy client WebSocket to normal target socket.
'''
"""
self.cqueue = []
self.tqueue = []
self.c_pend = 0