Esempio n. 1
0
def getLogger(module):
    logger = logging.getLogger(module)
    handler = logging.StreamHandler(stream=sys.stdout)
    logger.setLevel(logging.DEBUG)
    handler.setFormatter(JsonLogFormatter(logger_name=module))
    logger.addHandler(handler)
    return logger
Esempio n. 2
0
class UserSignupTests(TestCase):

    def setUp(self):
        self.handler = LogCapture()
        self.formatter = JsonLogFormatter(logger_name='testpilot.newuser')

        self.username = '******'
        self.password = '******'
        self.email = '*****@*****.**' % self.username

        self.user = User.objects.create_user(
            username=self.username,
            email=self.email,
            password=self.password)

        UserProfile.objects.filter(user=self.user).delete()

    def tearDown(self):
        self.handler.uninstall()

    def test_newuser_log_event(self):
        """testpilot.newuser log event should be emitted on signup"""
        self.user.is_active = True
        user_signed_up.send(sender=self.user.__class__,
                            request=None,
                            user=self.user)

        self.assertEquals(len(self.handler.records), 1)
        record = self.handler.records[0]

        details = json.loads(self.formatter.format(record))
        self.assertTrue('Fields' in details)

        fields = details['Fields']
        self.assertEqual(fields['uid'], self.user.id)
def on_startup():
    # setup logging
    handler = logging.StreamHandler(stream=sys.stdout)
    app.logger.setLevel(logging.DEBUG)
    handler.setFormatter(JsonLogFormatter(logger_name=__name__))
    app.logger.addHandler(handler)
    # and checkout mozilla schemas repo
    gitcheckout()
Esempio n. 4
0
    def setUp(self):
        self.handler = LogCapture()
        self.formatter = JsonLogFormatter(logger_name='testpilot.newuser')

        self.username = '******'
        self.password = '******'
        self.email = '*****@*****.**' % self.username

        self.user = User.objects.create_user(
            username=self.username,
            email=self.email,
            password=self.password)

        UserProfile.objects.filter(user=self.user).delete()
Esempio n. 5
0
    def test_installations(self):
        """Experiments should support an /installations/ psuedo-collection"""
        experiment = self.experiments['test-1']
        client_id = '8675309'

        # Ensure the installation to be created is initially 404
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id))
        resp = self.client.get(url)
        self.assertEqual(404, resp.status_code)

        # Create the first installation of interest
        UserInstallation.objects.create(
            experiment=experiment, client_id=client_id)

        # Also create a few installations that shouldn't appear in results
        UserInstallation.objects.create(
            experiment=self.experiments['test-2'],
            client_id=client_id)

        UserInstallation.objects.create(
            experiment=experiment,
            client_id='someotherclient')

        # Ensure that the desired installation is found at its URL
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id))
        resp = self.client.get(url)
        self.assertEqual(200, resp.status_code)

        self.assertEqual(2, (UserInstallation.objects
                             .filter(experiment=experiment).count()))

        # Create another client installation via PUT
        self.handler.records = []
        client_id_2 = '123456789'
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id_2))
        resp = self.client.put(url, {})
        self.assertEqual(200, resp.status_code)

        # Ensure that the API list result reflects the addition
        self.assertEqual(3, (UserInstallation.objects
                             .filter(experiment=experiment).count()))

        # Ensure that a testpilot.test-install log event was emitted
        record = self.handler.records[0]
        formatter = JsonLogFormatter(logger_name='testpilot.test-install')
        details = json.loads(formatter.format(record))
        fields = details['Fields']

        self.assertEqual('testpilot.test-install', record.name)
        self.assertEqual(fields['context'], experiment.title)

        # Delete the new client installation with DELETE
        client_id_2 = '123456789'
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id_2))
        resp = self.client.delete(url)
        self.assertEqual(410, resp.status_code)

        # Ensure that the API list result reflects the deletion
        self.assertEqual(2, (UserInstallation.objects
                             .filter(experiment=experiment).count()))
 def setUp(self):
     self.handler = LogCapture()
     self.logger_name = "TestingTestPilot"
     self.formatter = JsonLogFormatter(logger_name=self.logger_name)
class TestJsonLogFormatter(unittest.TestCase):

    def setUp(self):
        self.handler = LogCapture()
        self.logger_name = "TestingTestPilot"
        self.formatter = JsonLogFormatter(logger_name=self.logger_name)

    def tearDown(self):
        self.handler.uninstall()

    def _fetchLastLog(self):
        self.assertEqual(len(self.handler.records), 1)
        details = json.loads(self.formatter.format(self.handler.records[0]))
        jsonschema.validate(details, JSON_LOGGING_SCHEMA)
        return details

    def test_basic_operation(self):
        """Ensure log formatter contains all the expected fields and values"""
        message_text = "simple test"
        logging.debug(message_text)
        details = self._fetchLastLog()

        expected_present = ["Timestamp", "Hostname"]
        for key in expected_present:
            self.assertTrue(key in details)

        expected_meta = {
            "Severity": 7,
            "Type": "root",
            "Pid": os.getpid(),
            "Logger": self.logger_name,
            "EnvVersion": self.formatter.LOGGING_FORMAT_VERSION
        }
        for key, value in expected_meta.items():
            self.assertEqual(value, details[key])

        self.assertEqual(details['Fields']['msg'], message_text)

    def test_custom_paramters(self):
        """Ensure log formatter can handle custom parameters"""
        logger = logging.getLogger("mozsvc.test.test_logging")
        logger.warning("custom test %s", "one", extra={"more": "stuff"})
        details = self._fetchLastLog()

        self.assertEqual(details["Type"], "mozsvc.test.test_logging")
        self.assertEqual(details["Severity"], 4)

        fields = details['Fields']
        self.assertEqual(fields["msg"], "custom test one")
        self.assertEqual(fields["more"], "stuff")

    def test_logging_error_tracebacks(self):
        """Ensure log formatter includes exception traceback information"""
        try:
            raise ValueError("\n")
        except Exception:
            logging.exception("there was an error")
        details = self._fetchLastLog()

        expected_meta = {
            "Severity": 3,
        }
        for key, value in expected_meta.items():
            self.assertEqual(value, details[key])

        fields = details['Fields']
        expected_fields = {
            'msg': 'there was an error',
            'error': "ValueError('\\n',)"
        }
        for key, value in expected_fields.items():
            self.assertEqual(value, fields[key])

        self.assertTrue(fields['traceback'].startswith('Uncaught exception:'))
        self.assertTrue("ValueError" in fields['traceback'])
Esempio n. 8
0
    def test_installations(self):
        """Experiments should support an /installations/ psuedo-collection"""
        experiment = self.experiments['test-1']
        client_id = '8675309'

        # Ensure the installation to be created is initially 404
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id))
        resp = self.client.get(url)
        self.assertEqual(404, resp.status_code)

        # Create the first installation of interest
        UserInstallation.objects.create(experiment=experiment,
                                        client_id=client_id)

        # Also create a few installations that shouldn't appear in results
        UserInstallation.objects.create(experiment=self.experiments['test-2'],
                                        client_id=client_id)

        UserInstallation.objects.create(experiment=experiment,
                                        client_id='someotherclient')

        # Ensure that the desired installation is found at its URL
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id))
        resp = self.client.get(url)
        self.assertEqual(200, resp.status_code)

        self.assertEqual(
            2,
            (UserInstallation.objects.filter(experiment=experiment).count()))

        # Create another client installation via PUT
        self.handler.records = []
        client_id_2 = '123456789'
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id_2))
        resp = self.client.put(url, {})
        self.assertEqual(200, resp.status_code)

        # Ensure that the API list result reflects the addition
        self.assertEqual(
            3,
            (UserInstallation.objects.filter(experiment=experiment).count()))

        # Ensure that a testpilot.test-install log event was emitted
        record = self.handler.records[0]
        formatter = JsonLogFormatter(logger_name='testpilot.test-install')
        details = json.loads(formatter.format(record))
        fields = details['Fields']

        self.assertEqual('testpilot.test-install', record.name)
        self.assertEqual(fields['context'], experiment.title)

        # Delete the new client installation with DELETE
        client_id_2 = '123456789'
        url = reverse('experiment-installation-detail',
                      args=(experiment.pk, client_id_2))
        resp = self.client.delete(url)
        self.assertEqual(410, resp.status_code)

        # Ensure that the API list result reflects the deletion
        self.assertEqual(
            2,
            (UserInstallation.objects.filter(experiment=experiment).count()))