def parse_labels(profile_id, raw_json): labels = safe_decode_json(raw_json, log_tag="profile labels for %s" % profile_id) try: common.validate_labels(profile_id, labels) except ValidationFailed: _log.exception("Validation failed for profile %s labels : %s", profile_id, labels) return None else: return labels
def test_labels_validation(self): common.validate_labels("prof_id", {"a": "b"}) assert_raises(ValidationFailed, common.validate_labels, "prof_id", {"a": ["b"]}) assert_raises(ValidationFailed, common.validate_labels, "prof_id", {"a": [1]}) assert_raises(ValidationFailed, common.validate_labels, "prof_id", {"a": [None]}) assert_raises(ValidationFailed, common.validate_labels, "prof_id", {"a": None}) assert_raises(ValidationFailed, common.validate_labels, "prof_id", {"a": 1}) assert_raises(ValidationFailed, common.validate_labels, "+", {"a": "b"})
def test_label_regex(self, label): """ Test that the label validation logic matches the selector parsing logic in what it allows. """ # Whitespace is ignored in selectors. assume(not re.search(r'\s', label)) try: common.validate_labels("foo", {label: "foo"}) except ValidationFailed: # Validation failed, should fail to parse as a selector too. _log.exception("Validation failed for label %r", label) assert_raises(BadSelector, parse_selector, "%s == 'a'" % label) else: # Validation passed, should be allowed in expression too. parse_selector("%s == 'a'" % label)