Ejemplo n.º 1
0
 def test_notification_callback_custom_header(self):
     definition = self.factory.make_job_data_from_file(
         "qemu_callback_custom_header.yaml")
     dt = self.factory.make_device_type(name="qemu")
     device = self.factory.make_device(device_type=dt, hostname="qemu-1")
     user = self.factory.make_user()
     token, _ = AuthToken.objects.get_or_create(user=user,
                                                description="secrettoken",
                                                secret="abc123")
     job = TestJob.from_yaml_and_user(definition, user)
     create_notification(job, yaml_safe_load(definition)["notify"])
     job.refresh_from_db()
     self.assertIsNotNone(job.notification.notificationcallback_set.first())
     callback = job.notification.notificationcallback_set.first()
     self.assertEqual(callback.method, NotificationCallback.POST)
     self.assertEqual(callback.url, "https://example.com/foo/bar")
     self.assertEqual(callback.token, "abc123")
     self.assertEqual(callback.header, "PRIVATE-TOKEN")
Ejemplo n.º 2
0
def testjob_notifications(sender, **kwargs):
    job = kwargs["instance"]
    # If it's a new TestJob, no need to send notifications.
    if not job.id:
        return

    # Only notify when the state changed
    if job._old_state == job.state:
        return
    if job.state not in [TestJob.STATE_RUNNING, TestJob.STATE_FINISHED]:
        return

    job_def = yaml.safe_load(job.definition)
    if "notify" in job_def:
        if notification_criteria(job_def["notify"]["criteria"], job.state, job.health, job._old_health):
            try:
                job.notification
            except ObjectDoesNotExist:
                create_notification(job, job_def["notify"])
            send_notifications(job)
Ejemplo n.º 3
0
def async_send_notifications(job_id: int, state: int, health: int,
                             old_health: int) -> None:
    try:
        job = TestJob.objects.get(id=job_id)
    except TestJob.DoesNotExist:
        return

    job_def = yaml_safe_load(job.definition)
    if "notify" in job_def:
        if notification_criteria(job.id, job_def["notify"]["criteria"], state,
                                 health, old_health):
            # Set state and health as the task can run later while the job
            # state and health already changed.
            # The code is *not* saving the job so this won't have any effect on the db.
            job.state = state
            job.health = health
            try:
                job.notification
            except ObjectDoesNotExist:
                create_notification(job, job_def["notify"])
            send_notifications(job)