4 # Copyright (C) 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Remote API test utilities.
28 from ganeti import errors
29 from ganeti import opcodes
32 class VerificationError(Exception):
33 """Dedicated error class for test utilities.
35 This class is used to hide all of Ganeti's internal exception, so that
36 external users of these utilities don't have to integrate Ganeti's exception
42 def _GetOpById(op_id):
43 """Tries to get an opcode class based on its C{OP_ID}.
47 return opcodes.OP_MAPPING[op_id]
49 raise VerificationError("Unknown opcode ID '%s'" % op_id)
52 def _HideInternalErrors(fn):
53 """Hides Ganeti-internal exceptions, see L{VerificationError}.
56 def wrapper(*args, **kwargs):
58 return fn(*args, **kwargs)
59 except errors.GenericError, err:
60 raise VerificationError("Unhandled Ganeti error: %s" % err)
66 def VerifyOpInput(op_id, data):
67 """Verifies opcode parameters according to their definition.
70 @param op_id: Opcode ID (C{OP_ID} attribute), e.g. C{OP_CLUSTER_VERIFY}
72 @param data: Opcode parameter values
73 @raise VerificationError: Parameter verification failed
76 op_cls = _GetOpById(op_id)
79 op = op_cls(**data) # pylint: disable=W0142
80 except TypeError, err:
81 raise VerificationError("Unable to create opcode instance: %s" % err)
85 except errors.OpPrereqError, err:
86 raise VerificationError("Parameter validation for opcode '%s' failed: %s" %
91 def VerifyOpResult(op_id, result):
92 """Verifies opcode results used in tests (e.g. in a mock).
95 @param op_id: Opcode ID (C{OP_ID} attribute), e.g. C{OP_CLUSTER_VERIFY}
96 @param result: Mocked opcode result
97 @raise VerificationError: Return value verification failed
100 resultcheck_fn = _GetOpById(op_id).OP_RESULT
102 if not resultcheck_fn:
103 logging.warning("Opcode '%s' has no result type definition", op_id)
104 elif not resultcheck_fn(result):
105 raise VerificationError("Given result does not match result description"
106 " for opcode '%s': %s" % (op_id, resultcheck_fn))