Example #1
0
    def test_instance(self):
        class MysteryClass(object):
            a = 10

            def __init__(self):
                self.b = 1

        x = MysteryClass()
        self.assertEquals(utils.to_primitive(x, convert_instances=True),
                          dict(b=1))

        self.assertEquals(utils.to_primitive(x), x)
Example #2
0
    def test_instance(self):
        class MysteryClass(object):
            a = 10

            def __init__(self):
                self.b = 1

        x = MysteryClass()
        self.assertEquals(utils.to_primitive(x, convert_instances=True),
                          dict(b=1))

        self.assertEquals(utils.to_primitive(x), x)
Example #3
0
 def test_nasties(self):
     def foo():
         pass
     x = [datetime, foo, dir]
     ret = utils.to_primitive(x)
     self.assertEquals(len(ret), 3)
     self.assertTrue(ret[0].startswith(u"<module 'datetime' from "))
     self.assertTrue(ret[1].startswith(u'<function foo at 0x'))
     self.assertEquals(ret[2], u'<built-in function dir>')
Example #4
0
    def test_nasties(self):
        def foo():
            pass

        x = [datetime, foo, dir]
        ret = utils.to_primitive(x)
        self.assertEquals(len(ret), 3)
        self.assertTrue(ret[0].startswith(u"<module 'datetime' from "))
        self.assertTrue(ret[1].startswith(u'<function foo at 0x'))
        self.assertEquals(ret[2], u'<built-in function dir>')
Example #5
0
def notify(publisher_id, event_type, priority, payload):
    """
    Sends a notification using the specified driver

    Notify parameters:

    publisher_id - the source worker_type.host of the message
    event_type - the literal type of event (ex. Instance Creation)
    priority - patterned after the enumeration of Python logging levels in
               the set (DEBUG, WARN, INFO, ERROR, CRITICAL)
    payload - A python dictionary of attributes

    Outgoing message format includes the above parameters, and appends the
    following:

    message_id - a UUID representing the id for this notification
    timestamp - the GMT timestamp the notification was sent at

    The composite message will be constructed as a dictionary of the above
    attributes, which will then be sent via the transport mechanism defined
    by the driver.

    Message example:

    {'message_id': str(uuid.uuid4()),
     'publisher_id': 'compute.host1',
     'timestamp': utils.utcnow(),
     'priority': 'WARN',
     'event_type': 'compute.create_instance',
     'payload': {'instance_id': 12, ... }}

    """
    if priority not in log_levels:
        raise BadPriorityException(
                 _('%s not in valid priorities' % priority))

    # Ensure everything is JSON serializable.
    payload = utils.to_primitive(payload, convert_instances=True)

    driver = utils.import_object(FLAGS.notification_driver)
    msg = dict(message_id=str(uuid.uuid4()),
                   publisher_id=publisher_id,
                   event_type=event_type,
                   priority=priority,
                   payload=payload,
                   timestamp=str(utils.utcnow()))
    try:
        driver.notify(msg)
    except Exception, e:
        LOG.exception(_("Problem '%(e)s' attempting to "
                        "send to notification system. Payload=%(payload)s" %
                        locals()))
Example #6
0
def notify(publisher_id, event_type, priority, payload):
    """
    Sends a notification using the specified driver

    Notify parameters:

    publisher_id - the source worker_type.host of the message
    event_type - the literal type of event (ex. Instance Creation)
    priority - patterned after the enumeration of Python logging levels in
               the set (DEBUG, WARN, INFO, ERROR, CRITICAL)
    payload - A python dictionary of attributes

    Outgoing message format includes the above parameters, and appends the
    following:

    message_id - a UUID representing the id for this notification
    timestamp - the GMT timestamp the notification was sent at

    The composite message will be constructed as a dictionary of the above
    attributes, which will then be sent via the transport mechanism defined
    by the driver.

    Message example:

    {'message_id': str(uuid.uuid4()),
     'publisher_id': 'compute.host1',
     'timestamp': utils.utcnow(),
     'priority': 'WARN',
     'event_type': 'compute.create_instance',
     'payload': {'instance_id': 12, ... }}

    """
    if priority not in log_levels:
        raise BadPriorityException(_('%s not in valid priorities' % priority))

    # Ensure everything is JSON serializable.
    payload = utils.to_primitive(payload, convert_instances=True)

    driver = utils.import_object(FLAGS.notification_driver)
    msg = dict(message_id=str(uuid.uuid4()),
               publisher_id=publisher_id,
               event_type=event_type,
               priority=priority,
               payload=payload,
               timestamp=str(utils.utcnow()))
    try:
        driver.notify(msg)
    except Exception, e:
        LOG.exception(
            _("Problem '%(e)s' attempting to "
              "send to notification system. Payload=%(payload)s" % locals()))
Example #7
0
    def test_iter(self):
        class IterClass(object):
            def __init__(self):
                self.data = [1, 2, 3, 4, 5]
                self.index = 0

            def __iter__(self):
                return self

            def next(self):
                if self.index == len(self.data):
                    raise StopIteration
                self.index = self.index + 1
                return self.data[self.index - 1]

        x = IterClass()
        self.assertEquals(utils.to_primitive(x), [1, 2, 3, 4, 5])
Example #8
0
    def test_iter(self):
        class IterClass(object):
            def __init__(self):
                self.data = [1, 2, 3, 4, 5]
                self.index = 0

            def __iter__(self):
                return self

            def next(self):
                if self.index == len(self.data):
                    raise StopIteration
                self.index = self.index + 1
                return self.data[self.index - 1]

        x = IterClass()
        self.assertEquals(utils.to_primitive(x), [1, 2, 3, 4, 5])
Example #9
0
    def test_iteritems(self):
        class IterItemsClass(object):
            def __init__(self):
                self.data = dict(a=1, b=2, c=3).items()
                self.index = 0

            def __iter__(self):
                return self

            def next(self):
                if self.index == len(self.data):
                    raise StopIteration
                self.index = self.index + 1
                return self.data[self.index - 1]

        x = IterItemsClass()
        ordered = utils.to_primitive(x)
        ordered.sort()
        self.assertEquals(ordered, [['a', 1], ['b', 2], ['c', 3]])
Example #10
0
    def test_iteritems(self):
        class IterItemsClass(object):
            def __init__(self):
                self.data = dict(a=1, b=2, c=3).items()
                self.index = 0

            def __iter__(self):
                return self

            def next(self):
                if self.index == len(self.data):
                    raise StopIteration
                self.index = self.index + 1
                return self.data[self.index - 1]

        x = IterItemsClass()
        ordered = utils.to_primitive(x)
        ordered.sort()
        self.assertEquals(ordered, [['a', 1], ['b', 2], ['c', 3]])
Example #11
0
 def test_datetime(self):
     x = datetime.datetime(1, 2, 3, 4, 5, 6, 7)
     self.assertEquals(utils.to_primitive(x), "0001-02-03 04:05:06.000007")
Example #12
0
 def test_empty_dict(self):
     self.assertEquals(utils.to_primitive({}), {})
Example #13
0
 def test_dict(self):
     self.assertEquals(utils.to_primitive(dict(a=1, b=2, c=3)),
                       dict(a=1, b=2, c=3))
Example #14
0
 def test_tuple(self):
     self.assertEquals(utils.to_primitive((1, 2, 3)), [1, 2, 3])
Example #15
0
 def test_datetime(self):
     x = datetime.datetime(1, 2, 3, 4, 5, 6, 7)
     self.assertEquals(utils.to_primitive(x), "0001-02-03 04:05:06.000007")
Example #16
0
 def test_typeerror(self):
     x = bytearray  # Class, not instance
     self.assertEquals(utils.to_primitive(x), u"<type 'bytearray'>")
Example #17
0
 def test_empty_list(self):
     self.assertEquals(utils.to_primitive([]), [])
Example #18
0
 def serialize(self, data, content_type):
     try:
         return json.dumps(data)
     except TypeError:
         pass
     return json.dumps(utils.to_primitive(data))
Example #19
0
 def test_tuple(self):
     self.assertEquals(utils.to_primitive((1, 2, 3)), [1, 2, 3])
Example #20
0
 def test_typeerror(self):
     x = bytearray  # Class, not instance
     self.assertEquals(utils.to_primitive(x), u"<type 'bytearray'>")
Example #21
0
 def test_dict(self):
     self.assertEquals(utils.to_primitive(dict(a=1, b=2, c=3)),
                       dict(a=1, b=2, c=3))
Example #22
0
 def test_list(self):
     self.assertEquals(utils.to_primitive([1, 2, 3]), [1, 2, 3])
Example #23
0
 def test_list(self):
     self.assertEquals(utils.to_primitive([1, 2, 3]), [1, 2, 3])
Example #24
0
 def test_empty_list(self):
     self.assertEquals(utils.to_primitive([]), [])
Example #25
0
 def serialize(self, data, content_type):
     try:
         return json.dumps(data)
     except TypeError:
         pass
     return json.dumps(utils.to_primitive(data))
Example #26
0
 def test_empty_dict(self):
     self.assertEquals(utils.to_primitive({}), {})