From 3fe4cf74d716278a1854bd0a2be40293cd14cb24 Mon Sep 17 00:00:00 2001 From: Kien Nguyen Date: Wed, 21 Feb 2018 14:28:27 +0700 Subject: [PATCH] Set consts NAME_PREFIX & SANDBOX_NAME_PREFIX Change-Id: I805ae1be43d07fc296f5c56073c7bf2e958ce1c0 --- zun/common/consts.py | 10 +++++--- zun/common/utils.py | 2 +- zun/container/docker/driver.py | 6 ++--- zun/container/docker/utils.py | 3 ++- .../container/docker/test_docker_driver.py | 23 +++++++++++-------- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/zun/common/consts.py b/zun/common/consts.py index 92f2a4f53..83d6dc87f 100644 --- a/zun/common/consts.py +++ b/zun/common/consts.py @@ -17,7 +17,7 @@ CONTAINER_STATUSES = ( ) = ( 'Error', 'Running', 'Stopped', 'Paused', 'Unknown', 'Creating', 'Created', 'Deleted', 'Deleting' - ) +) TASK_STATES = ( IMAGE_PULLING, CONTAINER_CREATING, SANDBOX_CREATING, @@ -27,7 +27,7 @@ TASK_STATES = ( 'image_pulling', 'container_creating', 'sandbox_creating', 'container_starting', 'container_deleting', 'sandbox_deleting', 'container_stopping', 'container_rebooting', - ) +) RESOURCE_CLASSES = ( VCPU, MEMORY_MB, DISK_GB, PCI_DEVICE, SRIOV_NET_VF, @@ -37,6 +37,10 @@ RESOURCE_CLASSES = ( 'VCPU', 'MEMORY_MB', 'DISK_GB', 'PCI_DEVICE', 'SRIOV_NET_VF', 'NUMA_SOCKET', 'NUMA_CORE', 'NUMA_THREAD', 'NUMA_MEMORY_MB', 'IPV4_ADDRESS' - ) +) ALLOCATED = 'allocated' + +# The name of Docker container is of the form NAME_PREFIX- +NAME_PREFIX = 'zun-' +SANDBOX_NAME_PREFIX = 'zun-sandbox-' diff --git a/zun/common/utils.py b/zun/common/utils.py index f51a85a6a..b8aad8747 100644 --- a/zun/common/utils.py +++ b/zun/common/utils.py @@ -44,7 +44,7 @@ CONF = zun.conf.CONF LOG = logging.getLogger(__name__) NETWORK_ATTACH_EXTERNAL = 'network:attach_external_network' -synchronized = lockutils.synchronized_with_prefix('zun-') +synchronized = lockutils.synchronized_with_prefix(consts.NAME_PREFIX) VALID_STATES = { 'commit': [consts.RUNNING, consts.STOPPED, consts.PAUSED], diff --git a/zun/container/docker/driver.py b/zun/container/docker/driver.py index c437e1031..c5edbd84a 100644 --- a/zun/container/docker/driver.py +++ b/zun/container/docker/driver.py @@ -334,7 +334,7 @@ class DockerDriver(driver.ContainerDriver): def _get_container_uuids(self, containers): # The name of Docker container is of the form '/zun-' - name_prefix = '/zun-' + name_prefix = '/' + consts.NAME_PREFIX uuids = [c['Names'][0].replace(name_prefix, '', 1) for c in containers] return [u for u in uuids if uuidutils.is_uuid_like(u)] @@ -862,10 +862,10 @@ class DockerDriver(driver.ContainerDriver): docker.stop(sandbox_id) def get_sandbox_name(self, container): - return 'zun-sandbox-' + container.uuid + return consts.SANDBOX_NAME_PREFIX + container.uuid def get_container_name(self, container): - return 'zun-' + container.uuid + return consts.NAME_PREFIX + container.uuid def get_host_info(self): with docker_utils.docker_client() as docker: diff --git a/zun/container/docker/utils.py b/zun/container/docker/utils.py index 555e97801..586b89245 100644 --- a/zun/container/docker/utils.py +++ b/zun/container/docker/utils.py @@ -20,6 +20,7 @@ from docker import errors from oslo_serialization import jsonutils from oslo_utils import encodeutils +from zun.common import consts from zun.common import exception from zun.common.i18n import _ import zun.conf @@ -73,7 +74,7 @@ class DockerHTTPClient(docker.APIClient): ) def list_containers(self): - return self.containers(all=True, filters={'name': 'zun-'}) + return self.containers(all=True, filters={'name': consts.NAME_PREFIX}) def read_tar_image(self, image): image_path = image['path'] diff --git a/zun/tests/unit/container/docker/test_docker_driver.py b/zun/tests/unit/container/docker/test_docker_driver.py index c8dfa0d26..a45164d8d 100644 --- a/zun/tests/unit/container/docker/test_docker_driver.py +++ b/zun/tests/unit/container/docker/test_docker_driver.py @@ -126,7 +126,8 @@ class TestDockerDriver(base.DriverTestCase): **host_config) kwargs = { - 'name': 'zun-ea8e2a25-2901-438d-8157-de7ffd68d051', + 'name': '%sea8e2a25-2901-438d-8157-de7ffd68d051' % + consts.NAME_PREFIX, 'command': 'fake_command', 'environment': {'key1': 'val1', 'key2': 'val2'}, 'working_dir': '/home/ubuntu', @@ -185,9 +186,10 @@ class TestDockerDriver(base.DriverTestCase): uuid = uuidutils.generate_uuid() uuid2 = uuidutils.generate_uuid() mock_container_list = [ - {'Names': ['/zun-%s' % uuid]}, - {'Names': ['/zun-sandbox-%s' % uuidutils.generate_uuid()]}, - {'Names': ['/zun-%s' % uuid2]}] + {'Names': ['/%s%s' % (consts.NAME_PREFIX, uuid)]}, + {'Names': ['/%s%s' % (consts.SANDBOX_NAME_PREFIX, + uuidutils.generate_uuid())]}, + {'Names': ['/%s%s' % (consts.NAME_PREFIX, uuid2)]}] uuids = self.driver._get_container_uuids(mock_container_list) self.assertEqual(sorted([uuid, uuid2]), sorted(uuids)) @@ -554,16 +556,19 @@ class TestDockerDriver(base.DriverTestCase): mock_container = mock.MagicMock( uuid='ea8e2a25-2901-438d-8157-de7ffd68d051') result_sanbox_name = self.driver.get_sandbox_name(mock_container) - self.assertEqual(result_sanbox_name, - 'zun-sandbox-ea8e2a25-2901-438d-8157-de7ffd68d051') + self.assertEqual( + result_sanbox_name, + '%sea8e2a25-2901-438d-8157-de7ffd68d051' % + consts.SANDBOX_NAME_PREFIX) def test_get_container_name(self): mock_container = mock.MagicMock( uuid='ea8e2a25-2901-438d-8157-de7ffd68d051') result_container_name = self.driver.get_container_name( mock_container) - self.assertEqual(result_container_name, - 'zun-ea8e2a25-2901-438d-8157-de7ffd68d051') + self.assertEqual( + result_container_name, + '%sea8e2a25-2901-438d-8157-de7ffd68d051' % consts.NAME_PREFIX) def test_execute_resize(self): self.mock_docker.exec_resize = mock.Mock() @@ -629,7 +634,7 @@ class TestDockerDriver(base.DriverTestCase): 'memory_stats': {'usage': 104857600, 'limit': 1048576000}, 'networks': {'eth0': - {'tx_dropped': 0, 'rx_packets': 2, 'rx_bytes': 200, + {'tx_dropped': 0, 'rx_packets': 2, 'rx_bytes': 200, 'tx_errors': 0, 'rx_errors': 0, 'tx_bytes': 200, 'rx_dropped': 0, 'tx_packets': 2}}} stats_info = self.driver.stats(self.context, mock_container)