Exemplo n.º 1
0
    def _error(self, inner, req):
        LOG.exception(_LE("Caught error: %s"), unicode(inner))

        safe = getattr(inner, 'safe', False)
        headers = getattr(inner, 'headers', None)
        status = getattr(inner, 'code', 500)
        if status is None:
            status = 500

        msg_dict = dict(url=req.url, status=status)
        LOG.info(_LI("%(url)s returned with HTTP %(status)d"), msg_dict)
        outer = self.status_to_type(status)
        if headers:
            outer.headers = headers
        # NOTE(johannes): We leave the explanation empty here on
        # purpose. It could possibly have sensitive information
        # that should not be returned back to the user. See
        # bugs 868360 and 874472
        # NOTE(eglynn): However, it would be over-conservative and
        # inconsistent with the EC2 API to hide every exception,
        # including those that are safe to expose, see bug 1021373
        if safe:
            user_locale = req.best_match_language()
            inner_msg = translate(inner.message, user_locale)
            outer.explanation = '%s: %s' % (inner.__class__.__name__,
                                            inner_msg)

        notifications.send_api_fault(req.url, status, inner)
        return wsgi.Fault(outer)
Exemplo n.º 2
0
    def test_send_api_fault(self):
        self.flags(notify_api_faults=True)
        exception = None
        try:
            # Get a real exception with a call stack.
            raise test.TestingException("junk")
        except test.TestingException as e:
            exception = e

        notifications.send_api_fault("http://example.com/foo", 500, exception)

        self.assertEqual(1, len(fake_notifier.NOTIFICATIONS))
        n = fake_notifier.NOTIFICATIONS[0]
        self.assertEqual(n.priority, 'ERROR')
        self.assertEqual(n.event_type, 'api.fault')
        self.assertEqual(n.payload['url'], 'http://example.com/foo')
        self.assertEqual(n.payload['status'], 500)
        self.assertIsNotNone(n.payload['exception'])
Exemplo n.º 3
0
    def test_send_api_fault(self):
        self.flags(notify_api_faults=True)
        exception = None
        try:
            # Get a real exception with a call stack.
            raise test.TestingException("junk")
        except test.TestingException as e:
            exception = e

        notifications.send_api_fault("http://example.com/foo", 500, exception)

        self.assertEquals(1, len(test_notifier.NOTIFICATIONS))
        n = test_notifier.NOTIFICATIONS[0]
        self.assertEquals(n["priority"], "ERROR")
        self.assertEquals(n["event_type"], "api.fault")
        self.assertEquals(n["payload"]["url"], "http://example.com/foo")
        self.assertEquals(n["payload"]["status"], 500)
        self.assertTrue(n["payload"]["exception"] is not None)
Exemplo n.º 4
0
    def test_send_api_fault_fresh_context(self):
        self.flags(notify_api_faults=True)
        exception = None
        try:
            # Get a real exception with a call stack.
            raise test.TestingException("junk")
        except test.TestingException as e:
            exception = e

        ctxt = context.RequestContext(overwrite=True)
        notifications.send_api_fault("http://example.com/foo", 500, exception)

        self.assertEqual(1, len(fake_notifier.NOTIFICATIONS))
        n = fake_notifier.NOTIFICATIONS[0]
        self.assertEqual(n.priority, "ERROR")
        self.assertEqual(n.event_type, "api.fault")
        self.assertEqual(n.payload["url"], "http://example.com/foo")
        self.assertEqual(n.payload["status"], 500)
        self.assertIsNotNone(n.payload["exception"])
        self.assertEqual(ctxt, n.context)
Exemplo n.º 5
0
    def test_send_api_fault_admin_context(self):
        self.flags(notify_api_faults=True)
        exception = None
        try:
            # Get a real exception with a call stack.
            raise test.TestingException("junk")
        except test.TestingException as e:
            exception = e

        self.fixture._remove_cached_context()
        self.assertIsNone(o_context.get_current())
        notifications.send_api_fault("http://example.com/foo", 500, exception)

        self.assertEqual(1, len(fake_notifier.NOTIFICATIONS))
        n = fake_notifier.NOTIFICATIONS[0]
        self.assertEqual(n.priority, "ERROR")
        self.assertEqual(n.event_type, "api.fault")
        self.assertEqual(n.payload["url"], "http://example.com/foo")
        self.assertEqual(n.payload["status"], 500)
        self.assertIsNotNone(n.payload["exception"])
        self.assertIsNotNone(n.context)
        self.assertTrue(n.context.is_admin)
Exemplo n.º 6
0
 def test_send_api_fault_disabled(self):
     self.flags(notify_api_faults=False)
     notifications.send_api_fault("http://example.com/foo", 500, None)
     self.assertEqual(0, len(fake_notifier.NOTIFICATIONS))
Exemplo n.º 7
0
class NotificationsTestCase(test.TestCase):
    def setUp(self):
        super(NotificationsTestCase, self).setUp()

        self.net_info = fake_network.fake_get_instance_nw_info(
            self.stubs, 1, 1, spectacular=True)

        def fake_get_nw_info(cls, ctxt, instance):
            self.assertTrue(ctxt.is_admin)
            return self.net_info

        self.stubs.Set(network_api.API, 'get_instance_nw_info',
                       fake_get_nw_info)
        fake_network.set_stub_network_methods(self.stubs)

        self.flags(compute_driver='nova.virt.fake.FakeDriver',
                   notification_driver=[test_notifier.__name__],
                   network_manager='nova.network.manager.FlatManager',
                   notify_on_state_change="vm_and_task_state",
                   host='testhost')

        self.user_id = 'fake'
        self.project_id = 'fake'
        self.context = context.RequestContext(self.user_id, self.project_id)
        test_notifier.NOTIFICATIONS = []

        self.instance = self._wrapped_create()

    def tearDown(self):
        notifier_api._reset_drivers()
        super(NotificationsTestCase, self).tearDown()

    def _wrapped_create(self, params=None):
        inst = {}
        inst['image_ref'] = 1
        inst['user_id'] = self.user_id
        inst['project_id'] = self.project_id
        type_id = instance_types.get_instance_type_by_name('m1.tiny')['id']
        inst['instance_type_id'] = type_id
        inst['root_gb'] = 0
        inst['ephemeral_gb'] = 0
        inst['access_ip_v4'] = '1.2.3.4'
        inst['access_ip_v6'] = 'feed:5eed'
        inst['display_name'] = 'test_instance'
        if params:
            inst.update(params)
        return db.instance_create(self.context, inst)

    def test_send_api_fault_disabled(self):
        self.flags(notify_api_faults=False)
        notifications.send_api_fault("http://example.com/foo", 500, None)
        self.assertEquals(0, len(test_notifier.NOTIFICATIONS))

    def test_send_api_fault(self):
        self.flags(notify_api_faults=True)
        exception = None
        try:
            # Get a real exception with a call stack.
            raise test.TestingException("junk")
        except test.TestingException, e:
            exception = e

        notifications.send_api_fault("http://example.com/foo", 500, exception)

        self.assertEquals(1, len(test_notifier.NOTIFICATIONS))
        n = test_notifier.NOTIFICATIONS[0]
        self.assertEquals(n['priority'], 'ERROR')
        self.assertEquals(n['event_type'], 'api.fault')
        self.assertEquals(n['payload']['url'], 'http://example.com/foo')
        self.assertEquals(n['payload']['status'], 500)
        self.assertTrue(n['payload']['exception'] is not None)