def test_parse_arguments_with_class_validators(self): class NumberConverter(object): def clean(self, value): conv = {'one': 1, 'two': 2, 'three': 3} try: return conv[value] except KeyError: raise ValueError('No idea?!') # Define a set of filters with some types being non-trivial types # but instead a custom validator. filters = [ ("param1", 0, NumberConverter()), ] arguments = { "param1": "one", } params_exp = util.DotDict() params_exp.param1 = 1 params = external_common.parse_arguments(filters, arguments, modern=True) eq_(params, params_exp) # note that a ValueError becomes a BadArgumentError arguments = { "param1": "will cause a ValueError in NumberConverter.clean", } assert_raises(BadArgumentError, external_common.parse_arguments, filters, arguments, modern=True)
def create(self, **kwargs): """Add a new job to the priority queue """ filters = [ ("uuid", None, "str"), ] params = external_common.parse_arguments(filters, kwargs) if not params.uuid: raise MissingArgumentError('uuid') with self.context() as connection: try: self.config.logger.debug( 'Inserting priority job into RabbitMQ %s', params.uuid ) connection.channel.basic_publish( exchange='', routing_key=self.config.priority_queue_name, body=params.uuid, properties=pika.BasicProperties(delivery_mode=2) ) except ChannelClosed: self.config.logger.error( "Failed inserting priorityjobs data into RabbitMQ", exc_info=True ) return False return True
def test_parse_arguments(self): """Test external_common.parse_arguments(). """ filters = [ ("param1", "default", [str]), ("param2", None, int), ("param3", ["some", "default", "list"], [str]), ("param4", ["list", "of", 4, "values"], [str]), ("param5", None, bool), ("param6", None, datetime.date), ("param7", None, datetime.date), ("param8", None, datetime.datetime), ("param9", None, [str]), ] arguments = { "param1": "value1", "unknown": 12345, "param5": "true", "param7": datetime.date(2016, 2, 9).isoformat(), "param8": datetime.datetime(2016, 2, 9).isoformat(), # note the 'param9' is deliberately not specified. } params_exp = util.DotDict() params_exp.param1 = ["value1"] params_exp.param2 = None params_exp.param3 = ['some', 'default', 'list'] params_exp.param4 = ["list", "of", "4", "values"] params_exp.param5 = True params_exp.param6 = None params_exp.param7 = datetime.date(2016, 2, 9) params_exp.param8 = datetime.datetime(2016, 2, 9).replace( tzinfo=isodate.UTC ) params_exp.param9 = None params = external_common.parse_arguments( filters, arguments, modern=True ) for key in params: eq_(params[key], params_exp[key], '{}: {!r} != {!r}'.format( key, params[key], params_exp[key] )) eq_(params, params_exp)
def test_parse_arguments_old_way(self): """Test external_common.parse_arguments(). """ filters = [("param1", "default", ["list", "str"]), ("param2", None, "int"), ("param3", ["list", "of", 4, "values"], ["list", "str"])] arguments = {"param1": "value1", "unknown": 12345} params_exp = util.DotDict() params_exp.param1 = ["value1"] params_exp.param2 = None params_exp.param3 = ["list", "of", "4", "values"] params = external_common.parse_arguments( filters, arguments, modern=False, ) eq_(params, params_exp)
def test_parse_arguments_with_class_validators(self): class NumberConverter(object): def clean(self, value): conv = {'one': 1, 'two': 2, 'three': 3} try: return conv[value] except KeyError: raise ValueError('No idea?!') # Define a set of filters with some types being non-trivial types # but instead a custom validator. filters = [ ("param1", 0, NumberConverter()), ] arguments = { "param1": "one", } params_exp = util.DotDict() params_exp.param1 = 1 params = external_common.parse_arguments( filters, arguments, modern=True ) eq_(params, params_exp) # note that a ValueError becomes a BadArgumentError arguments = { "param1": "will cause a ValueError in NumberConverter.clean", } assert_raises( BadArgumentError, external_common.parse_arguments, filters, arguments, modern=True )
def test_parse_arguments(self): """Test external_common.parse_arguments(). """ filters = [ ("param1", "default", [str]), ("param2", None, int), ("param3", ["some", "default", "list"], [str]), ("param4", ["list", "of", 4, "values"], [str]), ("param5", None, bool), ("param6", None, datetime.date), ("param7", None, datetime.date), ("param8", None, datetime.datetime), ("param9", None, [str]), ] arguments = { "param1": "value1", "unknown": 12345, "param5": "true", "param7": datetime.date(2016, 2, 9).isoformat(), "param8": datetime.datetime(2016, 2, 9).isoformat(), # note the 'param9' is deliberately not specified. } params_exp = util.DotDict() params_exp.param1 = ["value1"] params_exp.param2 = None params_exp.param3 = ['some', 'default', 'list'] params_exp.param4 = ["list", "of", "4", "values"] params_exp.param5 = True params_exp.param6 = None params_exp.param7 = datetime.date(2016, 2, 9) params_exp.param8 = datetime.datetime(2016, 2, 9).replace(tzinfo=isodate.UTC) params_exp.param9 = None params = external_common.parse_arguments(filters, arguments, modern=True) for key in params: eq_(params[key], params_exp[key], '{}: {!r} != {!r}'.format(key, params[key], params_exp[key])) eq_(params, params_exp)
def test_parse_arguments_old_way(self): """Test external_common.parse_arguments(). """ filters = [ ("param1", "default", ["list", "str"]), ("param2", None, "int"), ("param3", ["list", "of", 4, "values"], ["list", "str"]) ] arguments = { "param1": "value1", "unknown": 12345 } params_exp = util.DotDict() params_exp.param1 = ["value1"] params_exp.param2 = None params_exp.param3 = ["list", "of", "4", "values"] params = external_common.parse_arguments( filters, arguments, modern=False, ) eq_(params, params_exp)
def get(self, **kwargs): """Return JSON data of a crash report, given its uuid. """ filters = [ ('uuid', None, 'str'), ('datatype', None, 'str'), ('name', None, 'str') # only applicable if datatype == 'raw' ] params = external_common.parse_arguments(filters, kwargs) if not params.uuid: raise MissingArgumentError('uuid') if not params.datatype: raise MissingArgumentError('datatype') # get a generic crashstorage instance from whatever external resource # is implementing this service. store = self.get_storage() datatype_method_mapping = { 'raw': 'get_raw_dump', 'meta': 'get_raw_crash', 'processed': 'get_processed', 'unredacted': 'get_unredacted_processed', } get = store.__getattribute__(datatype_method_mapping[params.datatype]) try: if params.datatype == 'raw': return ( get(params.uuid, name=params.name), 'application/octet-stream' ) else: return get(params.uuid) except CrashIDNotFound: if params.datatype in ('processed', 'unredacted'): # try to fetch a raw crash just to ensure that the raw crash # exists. If this line fails, there's no reason to actually # submit the priority job. try: store.get_raw_crash(params.uuid) except CrashIDNotFound: raise ResourceNotFound(params.uuid) # search through the existing other services to find the # Priorityjob service. try: priorityjob_service_impl = self.all_services[ 'Priorityjobs' ] except KeyError: raise ServiceUnavailable('Priorityjobs') # get the underlying implementation of the Priorityjob # service and instantiate it. priority_job_service = priorityjob_service_impl.cls( config=self.config ) # create the priority job for this crash_ids priority_job_service.create(uuid=params.uuid) raise ResourceUnavailable(params.uuid) raise ResourceNotFound(params.uuid)