From e0a67eee06918650f8166406e1302e6314d09fac Mon Sep 17 00:00:00 2001 From: "Andrei V. Ostapenko" Date: Wed, 28 Dec 2016 15:38:47 +0200 Subject: [PATCH] Adds custom exceptions Change-Id: I07c36343e5324e37c648424bd97227972d79a4a3 --- ramdisk_func_test/environment.py | 23 ++++----- ramdisk_func_test/exception.py | 81 ++++++++++++++++++++++++++++++++ ramdisk_func_test/network.py | 3 +- ramdisk_func_test/node.py | 7 +-- ramdisk_func_test/utils.py | 3 +- 5 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 ramdisk_func_test/exception.py diff --git a/ramdisk_func_test/environment.py b/ramdisk_func_test/environment.py index f70fa00..277f566 100644 --- a/ramdisk_func_test/environment.py +++ b/ramdisk_func_test/environment.py @@ -28,6 +28,7 @@ from oslo_config import cfg import ramdisk_func_test from ramdisk_func_test import conf +from ramdisk_func_test import exception from ramdisk_func_test import network from ramdisk_func_test import node from ramdisk_func_test import utils @@ -243,7 +244,7 @@ class Environment(object): elif source_type == 'rsync': return self._get_rsync_tenant_image_url(image_name) else: - raise Exception("Unknown deploy_driver") + raise exception.UnknownDeployDriver() def get_url_for_stub_image(self): return "http://{0}:{1}/fake".format(self.network.address, @@ -261,7 +262,8 @@ class Environment(object): # Image already mounted. if not os.path.exists( os.path.join(self.image_mount_point, 'etc/passwd')): - raise Exception('Previously mounted image no longer present') + raise exception.ImageMountError( + 'Previously mounted image no longer present.') return url image_path = os.path.join(CONF.tenant_images_dir, image_name) @@ -272,10 +274,10 @@ class Environment(object): sh.sudo.mount('-o', 'loop,ro', image_path, image_mount_point) if not os.path.exists('{0}/etc/passwd'.format( image_mount_point)): - raise Exception('Mounting of image did not happen') + raise exception.ImageMountError() else: - raise Exception("There is no such file '{0}' in '{1}'".format( - image_name, CONF.tenant_images_dir)) + raise exception.ImageNotFound(image_name=image_name, + directory=CONF.tenant_images_dir) return url def _save_provision_json_for_node(self, deploy_config): @@ -324,20 +326,19 @@ class Environment(object): rsync_ironic_section_name = 'ironic_rsync' if not utils._pid_of('rsync'): - raise Exception('No rsync process is running') + raise exception.RsyncProcessNotFound() if os.path.exists(rsync_config_path): cfg = utils.read_config(rsync_config_path) else: - raise Exception('No rsyncd config file found at {0}'.format( - rsync_config_path - )) + raise exception.RsyncConfigNotFound(path=rsync_config_path) if rsync_ironic_section_name in cfg.sections(): self.rsync_dir = cfg.get(rsync_ironic_section_name, 'path') else: - raise Exception('There is no ironic section ({0}) in rsync ' - 'config file'.format(rsync_ironic_section_name)) + raise exception.RsyncIronicSectionNotFound( + section=rsync_ironic_section_name + ) def _teardown_rsync(self): if self.image_mount_point: diff --git a/ramdisk_func_test/exception.py b/ramdisk_func_test/exception.py new file mode 100644 index 0000000..c206a18 --- /dev/null +++ b/ramdisk_func_test/exception.py @@ -0,0 +1,81 @@ +# +# Copyright 2016 Cray Inc., All Rights Reserved +# +# 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 RamDiskTestException(Exception): + _msg = "An unknown error occured." + + def __init__(self, message=None, **kwargs): + if not message: + message = self._msg % kwargs + super(RamDiskTestException, self).__init__(message) + + +class RsyncException(RamDiskTestException): + _msg = "Rsync error occured." + + +class RsyncProcessNotFound(RsyncException): + _msg = "No rsync process is running." + + +class RsyncConfigNotFound(RsyncException): + _msg = "No rsyncd config file found at %(path)s." + + +class RsyncIronicSectionNotFound(RsyncException): + _msg = 'There is no ironic section (%(section)s) in rsync config file.' + + +class ImageException(RamDiskTestException): + _msg = "Image error occured." + + +class MountedImageNotPresent(ImageException): + _msg = "Previously mounted image no longer present." + + +class ImageMountError(ImageException): + _msg = "Mounting of image did not happen." + + +class ImageNotFound(ImageException): + _msg = "There is no such file '%(image_name)s' in '%(directory)s'." + + +class UnknownDeployDriver(RamDiskTestException): + _msg = "Unknown deploy_driver." + + +class TimeoutException(RamDiskTestException): + _msg = "Timeout expired." + + +class NetServiceStartTimeout(TimeoutException): + _msg = ("Timeout %(timeout)ss for waiting for IP %(ip)s port %(port)s " + "to start expired.") + + +class NodeCallbackTimeout(TimeoutException): + _msg = ("Waiting timeout %(timeout)ss for node %(node_name)s callback " + "expired.") + + +class NonZeroCmdRetCode(RamDiskTestException): + _msg = "Non-zero code: %(ret_code)s from cmd: %(cmd)s." + + +class VacantNetworkNotFound(RamDiskTestException): + _msg = "Cannot find free libvirt net in %(head)s." diff --git a/ramdisk_func_test/network.py b/ramdisk_func_test/network.py index a4efd4f..02944ab 100644 --- a/ramdisk_func_test/network.py +++ b/ramdisk_func_test/network.py @@ -22,6 +22,7 @@ from oslo_config import cfg from ramdisk_func_test import base from ramdisk_func_test import conf +from ramdisk_func_test import exception from ramdisk_func_test import utils @@ -110,4 +111,4 @@ class Network(base.LibvirtBase): unique = all([pattern not in net_xml for net_xml in existing_nets]) if unique: return pattern - raise Exception("Cannot find free libvirt net in {0}".format(head)) + raise exception.VacantNetworkNotFound(head=head) diff --git a/ramdisk_func_test/node.py b/ramdisk_func_test/node.py index 0ae0dc4..3fd9e26 100644 --- a/ramdisk_func_test/node.py +++ b/ramdisk_func_test/node.py @@ -25,6 +25,7 @@ from oslo_config import cfg from ramdisk_func_test import base from ramdisk_func_test import conf +from ramdisk_func_test import exception from ramdisk_func_test import utils @@ -128,8 +129,7 @@ class Node(base.LibvirtBase): LOG.info("{0} end bareon log {0}".format("#"*40)) if check_ret_code and ret_code: - raise Exception("bareon returned non-zero code: " - "{0}".format(ret_code)) + raise exception.NonZeroCmdRetCode(cmd=cmd, ret_code=ret_code) return out, ret_code @@ -152,7 +152,8 @@ class Node(base.LibvirtBase): return sleep(1) - raise Exception("Timeout expired") + raise exception.NodeCallbackTimeout(timeout=timeout, + node_name=self.name) @contextmanager def _connect_ssh(self): diff --git a/ramdisk_func_test/utils.py b/ramdisk_func_test/utils.py index 27cf17a..b4f6b48 100644 --- a/ramdisk_func_test/utils.py +++ b/ramdisk_func_test/utils.py @@ -26,6 +26,7 @@ from time import sleep from oslo_config import cfg from ramdisk_func_test import conf +from ramdisk_func_test import exception CONF = conf.CONF @@ -98,7 +99,7 @@ def wait_net_service(ip, port, timeout, try_interval=2): s.close() return - raise Exception("Timeout expired") + raise exception.NetServiceStartTimeout(timeout=timeout, ip=ip, port=port) class FakeGlobalSectionHead(object):