Ejemplo n.º 1
0
    def testInt(self):
        for val in [-100, -3, 0, 16, 128, 923874]:
            self.assertTrue(ht.TInt(val))
            self.assertTrue(ht.TNumber(val))

        for val in [False, True, None, "", [], "Hello", 0.0, 0.23, -3818.163]:
            self.assertFalse(ht.TInt(val))

        for val in range(0, 100, 4):
            self.assertTrue(ht.TNonNegativeInt(val))
            neg = -(val + 1)
            self.assertFalse(ht.TNonNegativeInt(neg))
            self.assertFalse(ht.TPositiveInt(neg))

            self.assertFalse(ht.TNonNegativeInt(0.1 + val))
            self.assertFalse(ht.TPositiveInt(0.1 + val))

        for val in [0, 0.1, 0.9, -0.3]:
            self.assertFalse(ht.TPositiveInt(val))

        for val in range(1, 100, 4):
            self.assertTrue(ht.TPositiveInt(val))
            self.assertFalse(ht.TPositiveInt(0.1 + val))
Ejemplo n.º 2
0
    def Validate(self, set_defaults):  # pylint: disable=W0221
        """Validate opcode parameters, optionally setting default values.

    @type set_defaults: bool
    @param set_defaults: whether to set default values

    @rtype: NoneType
    @return: L{None}, if the validation succeeds
    @raise errors.OpPrereqError: when a parameter value doesn't match
                                 requirements

    """
        for (attr_name, default, test, _) in self.GetAllParams():
            assert callable(test)

            if hasattr(self, attr_name):
                attr_val = getattr(self, attr_name)
            else:
                attr_val = copy.deepcopy(default)

            if test(attr_val):
                if set_defaults:
                    setattr(self, attr_name, attr_val)
            elif ht.TInt(attr_val) and test(float(attr_val)):
                if set_defaults:
                    setattr(self, attr_name, float(attr_val))
            else:
                op_id = self.OP_ID  # pylint: disable=E1101
                logging.error(
                    "OpCode %s, parameter %s, has invalid type %s/value"
                    " '%s' expecting type %s", op_id, attr_name,
                    type(attr_val), attr_val, test)

                if attr_val is None:
                    logging.error(
                        "OpCode %s, parameter %s, has default value None which"
                        " is does not check against the parameter's type: this"
                        " means this parameter is required but no value was"
                        " given", op_id, attr_name)

                raise errors.OpPrereqError(
                    "Parameter '%s.%s' fails validation" % (op_id, attr_name),
                    errors.ECODE_INVAL)