def testInvalidRegexAutoBisect(self): with self.assertRaisesRegex(validator.InvalidPattern, 'no argument'): _ = validator.Validate(""" subscriptions: [ { name: "Empty Regex", contact_email: "bad-pattern@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{regex: ".*"}] } auto_triage { rules: { match: {regex: ".*"} } } auto_bisection { rules: { match: {regex: "*"} } } } ] """) with self.assertRaisesRegex(validator.InvalidPattern, 'no argument'): _ = validator.Validate(""" subscriptions: [ { name: "Empty Regex", contact_email: "bad-pattern@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{regex: ".*"}] exclude: [{regex: ".*"}] } auto_triage { rules: { match: {regex: ".*"} } } auto_bisection { rules: { match: {regex: ".*"} exclude: {regex: "*"} } } } ] """)
def testGoodCase(self): subscriptions = validator.Validate(""" subscriptions: [{ name: "Release Team" contact_email: "*****@*****.**" bug_labels: ["release-blocker"] bug_components: ["Sample>Component"] rules: { match: [{glob: "project/**"}] } }, { name: "Memory Team", contact_email: "*****@*****.**", bug_labels: ["memory-regressions"], rules: { match: [{regex: "^project/.*memory_.*$"}] }, anomaly_configs: [ { min_relative_change: 0.01 rules: { match: { regex: "^project/platform/.*/memory_peak$" } } } ] }]""") self.assertIsNotNone(subscriptions)
def testValidAutoTriageAndAutoBisect(self): _ = validator.Validate(""" subscriptions: [ { name: "Empty Regex", contact_email: "bad-pattern@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{regex: ".*"}] exclude: [{regex: ".*"}] } auto_triage { rules: { match: {regex: ".*"} exclude: {regex: ".*"} } } auto_bisection { rules: { match: {regex: ".*"} exclude: {regex: ".*"} } } } ] """)
def Transform(config): missing_fields = required_fields.difference(config) if len(missing_fields): raise InvalidConfigError(config, missing_fields) # We use a combination of the config set, revision, and content_hash to # identify this particular configuration. key = client.key( 'Subscription', '{config_set}:{revision}'.format(**config), parent=subscription_index_key) try: sheriff_config = validator.Validate( base64.standard_b64decode(config['content'])) except validator.Error as error: raise InvalidContentError(config, error) entity = datastore.Entity( key=key, exclude_from_indexes=['sheriff_config', 'url']) entity.update({ 'config_set': config['config_set'], 'revision': config['revision'], 'content_hash': config['content_hash'], 'url': config['url'], 'sheriff_config': sheriff_config.SerializeToString() }) return (key, entity)
def testInvalidJSON(self): with self.assertRaisesRegexp(validator.InvalidJSON, 'JSON Validation Error'): _ = validator.Validate(""" { "subscriptions": ... } """)
def testMissingName(self): with self.assertRaises(validator.MissingName): _ = validator.Validate(""" subscriptions: [ { notification_email: "missing-name@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], patterns: [{glob: "project/**"}] } ] """)
def testMissingName(self): with self.assertRaises(validator.MissingName): _ = validator.Validate(""" subscriptions: [ { contact_email: "missing-name@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{glob: "project/**"}] } } ] """)
def testMissingPattern(self): with self.assertRaises(validator.MissingPatterns): _ = validator.Validate(""" subscriptions: [ { name: "No Patterns", contact_email: "no-patterns@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"] } ] """)
def testMissingEmail(self): with self.assertRaises(validator.MissingEmail): _ = validator.Validate(""" subscriptions: [ { name: "Missing Email", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{glob: "project/**"}] } } ] """)
def testInvalidRegexMatch(self): with self.assertRaisesRegex(validator.InvalidPattern, 'no argument'): _ = validator.Validate(""" subscriptions: [ { name: "Empty Regex", contact_email: "bad-pattern@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{regex: "*"}] } } ] """)
def testMissingEmptyPattern(self): with self.assertRaises(validator.MissingPatterns): _ = validator.Validate(""" subscriptions: [ { name: "Empty List Patterns", notification_email: "no-patterns@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], patterns: [] } ] """)
def UnionAsBinaryStringWithIndexedOffsetData(o, schema, path): representation = cStringIO.StringIO() for idx, possibleSchemaType in enumerate(schema['optionTypes']): if len(validator.Validate(possibleSchemaType, o)) == 0: binaryIndex = uint32.pack(idx) representation.write(binaryIndex) itemRepresentation, indexedOffsetData = RepresentAsBinaryWithIndexedOffsetData(o, possibleSchemaType, FsdDataPathObject('<%s>' % possibleSchemaType['type'], parent=path)) if not indexedOffsetData.isEmpty(): indexedOffsetData.AddOffset(len(binaryIndex)) representation.write(itemRepresentation) return (representation.getvalue(), indexedOffsetData) raise ValueError('Could not represent %s as any type in union' % str(o))
def testInvalidEmptyGlob(self): with self.assertRaisesRegex(validator.InvalidPattern, 'glob must not be empty'): _ = validator.Validate(""" subscriptions: [ { name: "Empty Glob", contact_email: "bad-pattern@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{glob: ""}] } } ] """)
def testInvalidUndefinedPattern(self): with self.assertRaisesRegexp(validator.InvalidPattern, 'must provide either \'glob\' or \'regex\''): _ = validator.Validate(""" subscriptions: [ { name: "Bad Pattern", notification_email: "bad-pattern@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], patterns: [{}] } ] """)
def testInvalidEmptyRegex(self): with self.assertRaisesRegexp(validator.InvalidPattern, 'regex must not be empty'): _ = validator.Validate(""" subscriptions: [ { name: "Empty Regex", notification_email: "bad-pattern@domain", bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], patterns: [{regex: ""}] } ] """)
def testMissingPattern(self): with self.assertRaises(validator.MissingPatterns): _ = validator.Validate(""" { "subscriptions": [ { "name": "No Patterns", "notification_email": "no-patterns@domain", "bug_labels": ["test-blocker"], "bug_components": ["Sample>Component"] } ] } """)
def testMissingName(self): with self.assertRaises(validator.MissingName): _ = validator.Validate(""" { "subscriptions": [ { "notification_email": "missing-name@domain", "bug_labels": ["test-blocker"], "bug_components": ["Sample>Component"], "patterns": [{"glob": "project/**"}] } ] } """)
def testMissingEmail(self): with self.assertRaises(validator.MissingEmail): _ = validator.Validate(""" { "subscriptions": [ { "name": "Missing Email", "bug_labels": ["test-blocker"], "bug_components": ["Sample>Component"], "patterns": [{"glob": "project/**"}] } ] } """)
def testInvalidEmptyRegex(self): with self.assertRaisesRegexp(validator.InvalidPattern, 'regex must not be empty'): _ = validator.Validate(""" { "subscriptions": [ { "name": "Empty Regex", "notification_email": "bad-pattern@domain", "bug_labels": ["test-blocker"], "bug_components": ["Sample>Component"], "patterns": [{"regex": ""}] } ] } """)
def testValidAnomalyConfigs(self): _ = validator.Validate(""" subscriptions: [ { name: "Subscription" contact_email: "user@domain" bug_labels: ["test-blocker"], bug_components: ["Sample>Component"], rules: { match: [{regex: ".*"}] } anomaly_configs { rules { match: [{regex: ".*"}] } } } ]""")
def Validate(): # pylint: disable=unused-variable validation_request = request.get_json() if validation_request is None: return u'Invalid request.', 400 for member in ('config_set', 'path', 'content'): if member not in validation_request: return u'Missing \'%s\' member in request.' % (member), 400 try: _ = validator.Validate(validation_request['content']) except validator.Error as error: return jsonify({ 'messages': [{ 'path': validation_request['path'], 'severity': 'ERROR', 'text': '%s' % (error) }] }) return jsonify({})
def testGoodCase(self): subscriptions = validator.Validate("""{ "subscriptions": [{ "name": "Release Team", "notification_email": "*****@*****.**", "bug_labels": ["release-blocker"], "bug_components": ["Sample>Component"], "patterns": [{"glob": "project/**"}] }, { "name": "Memory Team", "notification_email": "*****@*****.**", "bug_labels": ["memory-regressions"], "patterns": [{"regex": "^project/.*memory_.*$"}], "anomaly_configs": [ { "min_relative_change": 0.01, "patterns": [{"regex": "^project/platform/.*/memory_peak$"}] } ] }]}""") self.assertIsNotNone(subscriptions)
def testMonorailProjectId(self): subscriptions = validator.Validate(""" subscriptions: [{ name: "Non-Chromium Team" contact_email: "*****@*****.**" bug_labels: ["release-blocker"] bug_components: ["Sample>Component"] monorail_project_id: "not-chromium" patterns: [{glob: "project/**"}] }, { name: "Memory Team", contact_email: "*****@*****.**", bug_labels: ["memory-regressions"], patterns: [{regex: "^project/.*memory_.*$"}], anomaly_configs: [ { min_relative_change: 0.01 patterns: [{regex: "^project/platform/.*/memory_peak$"}] } ] }]""") self.assertIsNotNone(subscriptions)
def __init__(self, cfg_file='./midpoint.cfg', logFile=None): self.logger = Logger() self.connection = {} self.validate = validator.Validate(self.logger) self.read_config(cfg_file)
def testInvalidJSON(self): with self.assertRaisesRegex(validator.InvalidConfig, 'SheriffConfig Validation Error'): _ = validator.Validate(""" subscriptions: ... """)