]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/orch: add errno to OrchestratorError
authorMichael Fritch <mfritch@suse.com>
Wed, 22 Jul 2020 23:43:05 +0000 (17:43 -0600)
committerMichael Fritch <mfritch@suse.com>
Tue, 28 Jul 2020 21:54:46 +0000 (15:54 -0600)
add errno to OrchestratorError and ServiceSpecValidationError exceptions

Signed-off-by: Michael Fritch <mfritch@suse.com>
qa/tasks/mgr/test_orchestrator_cli.py
src/pybind/mgr/orchestrator/_interface.py
src/pybind/mgr/test_orchestrator/module.py
src/python-common/ceph/deployment/service_spec.py

index 9600fd57205e2e3bbf29dfd3eab21f43297976a8..49be3d8ab0a907bf167f35eb0777a2d9e7673ad3 100644 (file)
@@ -161,8 +161,10 @@ data_devices:
         self._orch_cmd("apply", "nfs", "service_name", "2")
 
     def test_error(self):
-        ret = self._orch_cmd_result("host", "add", "raise_no_support")
-        self.assertEqual(ret, errno.ENOENT)
+        ret = self._orch_cmd_result("host", "add", "raise_validation_error")
+        self.assertEqual(ret, errno.EINVAL)
+        ret = self._orch_cmd_result("host", "add", "raise_error")
+        self.assertEqual(ret, errno.EINVAL)
         ret = self._orch_cmd_result("host", "add", "raise_bug")
         self.assertEqual(ret, errno.EINVAL)
         ret = self._orch_cmd_result("host", "add", "raise_not_implemented")
index 586662a508f9858d44e97ee31d3e52d184c01feb..f40157c4acb1440416b7420ce3d510465491d6a3 100644 (file)
@@ -47,8 +47,12 @@ class OrchestratorError(Exception):
 
     It's not intended for programming errors or orchestrator internal errors.
     """
-    def __init__(self, msg, event_kind_subject: Optional[Tuple[str, str]]=None):
+    def __init__(self,
+                 msg: str,
+                 errno: int = -errno.EINVAL,
+                 event_kind_subject: Optional[Tuple[str, str]] = None):
         super(Exception, self).__init__(msg)
+        self.errno = errno
         # See OrchestratorEvent.subject
         self.event_subject = event_kind_subject
 
@@ -58,7 +62,7 @@ class NoOrchestrator(OrchestratorError):
     No orchestrator in configured.
     """
     def __init__(self, msg="No orchestrator configured (try `ceph orch set backend`)"):
-        super(NoOrchestrator, self).__init__(msg)
+        super(NoOrchestrator, self).__init__(msg, errno=-errno.ENOENT)
 
 
 class OrchestratorValidationError(OrchestratorError):
@@ -82,8 +86,10 @@ def handle_exception(prefix, cmd_args, desc, perm, func):
     def wrapper(*args, **kwargs):
         try:
             return func(*args, **kwargs)
-        except (OrchestratorError, ImportError, ServiceSpecValidationError) as e:
+        except (OrchestratorError, ServiceSpecValidationError) as e:
             # Do not print Traceback for expected errors.
+            return HandleCommandResult(e.errno, stderr=str(e))
+        except ImportError as e:
             return HandleCommandResult(-errno.ENOENT, stderr=str(e))
         except NotImplementedError:
             msg = 'This Orchestrator does not support `{}`'.format(prefix)
index a9076ec911fd67ee3da71fd729190b356a0936fa..3305a2918b05bb309008ee2f1a5319b9ce23f7a9 100644 (file)
@@ -353,8 +353,10 @@ class TestOrchestrator(MgrModule, orchestrator.Orchestrator):
     def add_host(self, spec):
         # type: (orchestrator.HostSpec) -> None
         host = spec.hostname
-        if host == 'raise_no_support':
+        if host == 'raise_validation_error':
             raise orchestrator.OrchestratorValidationError("MON count must be either 1, 3 or 5")
+        if host == 'raise_error':
+            raise orchestrator.OrchestratorError("host address is empty")
         if host == 'raise_bug':
             raise ZeroDivisionError()
         if host == 'raise_not_implemented':
index a677ff419a0b543157b571029a1bfd1d83abeaeb..efbc512106d6e257672bc903805c74fc716f00cf 100644 (file)
@@ -1,3 +1,4 @@
+import errno
 import fnmatch
 import re
 from collections import namedtuple, OrderedDict
@@ -14,9 +15,11 @@ class ServiceSpecValidationError(Exception):
     Defining an exception here is a bit problematic, cause you cannot properly catch it,
     if it was raised in a different mgr module.
     """
-
-    def __init__(self, msg):
+    def __init__(self,
+                 msg: str,
+                 errno: int = -errno.EINVAL):
         super(ServiceSpecValidationError, self).__init__(msg)
+        self.errno = errno
 
 
 def assert_valid_host(name):