Ejemplo n.º 1
0
    def validate(self, validators, variables_mapping):
        """ Bind named validators to value within the context.
        @param (list) validators
            [
                {"check": "status_code", "comparator": "eq", "expected": 201},
                {"check": "resp_body_success", "comparator": "eq", "expected": True}
            ]
        @param (dict) variables_mapping
            {
                "resp_body_success": True
            }
        @return (list) content differences
            [
                {
                    "check": "status_code",
                    "comparator": "eq", "expected": 201, "value": 200
                }
            ]
        """
        for validator_dict in validators:

            check_item = validator_dict.get("check")
            if not check_item:
                raise exception.ParamsError("invalid check item in testcase validators!")

            if "expected" not in validator_dict:
                raise exception.ParamsError("expected item missed in testcase validators!")

            expected = validator_dict.get("expected")
            comparator = validator_dict.get("comparator", "eq")

            if check_item in variables_mapping:
                validator_dict["actual_value"] = variables_mapping[check_item]
            else:
                try:
                    validator_dict["actual_value"] = self.extract_field(check_item)
                except exception.ParseResponseError:
                    raise exception.ParseResponseError("failed to extract check item in response!")

            utils.match_expected(
                validator_dict["actual_value"],
                expected,
                comparator,
                check_item
            )

        return True
Ejemplo n.º 2
0
    def validate(self, validators, variables_mapping):
        """ Bind named validators to value within the context.
        @param (list) validators
            [
                {"check": "status_code", "comparator": "eq", "expected": 201},
                {"check": "resp_body_success", "comparator": "eq", "expected": True}
            ]
        @param (dict) variables_mapping
            {
                "resp_body_success": True
            }
        @return (list) content differences
            [
                {
                    "check": "status_code",
                    "comparator": "eq", "expected": 201, "value": 200
                }
            ]
        """
        diff_content_list = []

        for validator_dict in validators:

            if "expected" not in validator_dict or "check" not in validator_dict:
                raise exception.ParamsError(
                    "expected not specified in validator")

            validator_key = validator_dict["check"]
            try:
                validator_dict["value"] = variables_mapping[validator_key]
            except KeyError:
                validator_dict["value"] = self.extract_field(validator_key)

            match_expected = utils.match_expected(
                validator_dict["value"], validator_dict["expected"],
                validator_dict.get("comparator", "eq"))

            if not match_expected:
                diff_content_list.append(validator_dict)

        self.success = False if diff_content_list else True
        return diff_content_list
Ejemplo n.º 3
0
    def validate(self, validators, variables_mapping):
        """ Bind named validators to value within the context.
        @param (dict) validators
            {
                "resp_status_code": {"comparator": "eq", "expected": 201},
                "resp_body_success": {"comparator": "eq", "expected": True}
            }
        @param (dict) variables_mapping
            {
                "resp_status_code": 200,
                "resp_body_success": True
            }
        @return (dict) content differences
            {
                "resp_status_code": {
                    "comparator": "eq", "expected": 201, "value": 200
                }
            }
        """
        diff_content_dict = {}

        for validator_key, validator_dict in validators.items():

            try:
                value = variables_mapping[validator_key]
                validator_dict["value"] = value
            except KeyError:
                raise exception.ParamsError("invalid validator %s" % validator_key)

            match_expected = utils.match_expected(
                value,
                validator_dict["expected"],
                validator_dict["comparator"]
            )

            if not match_expected:
                diff_content_dict[validator_key] = validator_dict

        self.success = False if diff_content_dict else True
        return diff_content_dict
Ejemplo n.º 4
0
    def test_match_expected(self):
        self.assertTrue(utils.match_expected(1, 1, "eq"))
        self.assertTrue(utils.match_expected("abc", "abc", "eq"))
        self.assertTrue(utils.match_expected("abc", "abc"))
        self.assertFalse(utils.match_expected(123, "123", "eq"))
        self.assertFalse(utils.match_expected(123, "123"))

        self.assertTrue(utils.match_expected("123", 3, "len_eq"))
        self.assertTrue(utils.match_expected(123, "123", "str_eq"))
        self.assertTrue(utils.match_expected(123, "123", "ne"))

        self.assertTrue(utils.match_expected("123", 2, "len_gt"))
        self.assertTrue(utils.match_expected("123", 3, "len_ge"))
        self.assertTrue(utils.match_expected("123", 4, "len_lt"))
        self.assertTrue(utils.match_expected("123", 3, "len_le"))

        self.assertTrue(utils.match_expected(1, 2, "lt"))
        self.assertTrue(utils.match_expected(1, 1, "le"))
        self.assertTrue(utils.match_expected(2, 1, "gt"))
        self.assertTrue(utils.match_expected(1, 1, "ge"))

        self.assertTrue(utils.match_expected("123abc456", "3ab", "contains"))
        self.assertTrue(
            utils.match_expected("3ab", "123abc456", "contained_by"))

        self.assertTrue(
            utils.match_expected("123abc456", "^123.*456$", "regex"))
        self.assertFalse(
            utils.match_expected("123abc456", "^12b.*456$", "regex"))

        with self.assertRaises(exception.ParamsError):
            utils.match_expected(1, 2, "not_supported_comparator")

        self.assertTrue(
            utils.match_expected("2017-06-29 17:29:58", 19, "str_len"))
        self.assertTrue(
            utils.match_expected("2017-06-29 17:29:58", "19", "str_len"))

        self.assertTrue(utils.match_expected("abc123", "ab", "startswith"))
        self.assertTrue(utils.match_expected("123abc", 12, "startswith"))
        self.assertTrue(utils.match_expected(12345, 123, "startswith"))
Ejemplo n.º 5
0
    def test_match_expected(self):
        self.assertTrue(utils.match_expected(1, 1, "eq"))
        self.assertTrue(utils.match_expected("abc", "abc", "=="))
        self.assertTrue(utils.match_expected("abc", "abc"))

        with self.assertRaises(exception.ValidationError):
            utils.match_expected(123, "123", "eq")
        with self.assertRaises(exception.ValidationError):
            utils.match_expected(123, "123")

        self.assertTrue(utils.match_expected(1, 2, "lt"))
        self.assertTrue(utils.match_expected(1, 1, "le"))
        self.assertTrue(utils.match_expected(2, 1, "gt"))
        self.assertTrue(utils.match_expected(1, 1, "ge"))
        self.assertTrue(utils.match_expected(123, "123", "ne"))

        self.assertTrue(utils.match_expected("123", 3, "len_eq"))
        self.assertTrue(utils.match_expected("123", 2, "len_gt"))
        self.assertTrue(utils.match_expected("123", 3, "len_ge"))
        self.assertTrue(utils.match_expected("123", 4, "len_lt"))
        self.assertTrue(utils.match_expected("123", 3, "len_le"))

        self.assertTrue(utils.match_expected("123abc456", "3ab", "contains"))
        self.assertTrue(utils.match_expected(['1', '2'], "1", "contains"))
        self.assertTrue(utils.match_expected({
            'a': 1,
            'b': 2
        }, "a", "contains"))
        self.assertTrue(
            utils.match_expected("3ab", "123abc456", "contained_by"))

        self.assertTrue(
            utils.match_expected("123abc456", "^123\w+456$", "regex"))
        with self.assertRaises(exception.ValidationError):
            utils.match_expected("123abc456", "^12b.*456$", "regex")

        with self.assertRaises(exception.ParamsError):
            utils.match_expected(1, 2, "not_supported_comparator")

        self.assertTrue(utils.match_expected("abc123", "ab", "startswith"))
        self.assertTrue(utils.match_expected("123abc", 12, "startswith"))
        self.assertTrue(utils.match_expected(12345, 123, "startswith"))
        self.assertTrue(utils.match_expected("abc123", 23, "endswith"))
        self.assertTrue(utils.match_expected("123abc", "abc", "endswith"))
        self.assertTrue(utils.match_expected(12345, 45, "endswith"))

        self.assertTrue(utils.match_expected(None, None, "eq"))
        with self.assertRaises(exception.ValidationError):
            utils.match_expected(None, 3, "len_eq")
        with self.assertRaises(exception.ValidationError):
            utils.match_expected("abc", None, "gt")
Ejemplo n.º 6
0
    def test_match_expected(self):
        self.assertTrue(utils.match_expected(1, 1, "eq"))
        self.assertTrue(utils.match_expected("abc", "abc", "=="))
        self.assertTrue(utils.match_expected("abc", "abc"))

        with self.assertRaises(exception.ValidationError):
            utils.match_expected(123, "123", "eq")
        with self.assertRaises(exception.ValidationError):
            utils.match_expected(123, "123")

        self.assertTrue(utils.match_expected(1, 2, "lt"))
        self.assertTrue(utils.match_expected(1, 1, "le"))
        self.assertTrue(utils.match_expected(2, 1, "gt"))
        self.assertTrue(utils.match_expected(1, 1, "ge"))
        self.assertTrue(utils.match_expected(123, "123", "ne"))

        self.assertTrue(utils.match_expected("123", 3, "len_eq"))
        self.assertTrue(utils.match_expected("123", 2, "len_gt"))
        self.assertTrue(utils.match_expected("123", 3, "len_ge"))
        self.assertTrue(utils.match_expected("123", 4, "len_lt"))
        self.assertTrue(utils.match_expected("123", 3, "len_le"))

        self.assertTrue(utils.match_expected("123abc456", "3ab", "contains"))
        self.assertTrue(utils.match_expected(['1', '2'], "1", "contains"))
        self.assertTrue(utils.match_expected({'a':1, 'b':2}, "a", "contains"))
        self.assertTrue(utils.match_expected("3ab", "123abc456", "contained_by"))

        self.assertTrue(utils.match_expected("123abc456", "^123\w+456$", "regex"))
        with self.assertRaises(exception.ValidationError):
            utils.match_expected("123abc456", "^12b.*456$", "regex")

        with self.assertRaises(exception.ParamsError):
            utils.match_expected(1, 2, "not_supported_comparator")

        self.assertTrue(utils.match_expected("abc123", "ab", "startswith"))
        self.assertTrue(utils.match_expected("123abc", 12, "startswith"))
        self.assertTrue(utils.match_expected(12345, 123, "startswith"))

        self.assertTrue(utils.match_expected(None, None, "eq"))
        with self.assertRaises(exception.ValidationError):
            utils.match_expected(None, 3, "len_eq")
        with self.assertRaises(exception.ValidationError):
            utils.match_expected("abc", None, "gt")