def test_delayed_jobs(self):
     with session.begin():
         # Create a queued job that was submitted a long time ago
         recipe = data_setup.create_recipe()
         job = data_setup.create_job_for_recipes([recipe])
         job.owner = self.user
         data_setup.mark_job_queued(job)
         job.recipesets[0].queue_time = datetime.utcnow() - timedelta(days=self.delayed_job_age)
         # create a job with two recipes, one Queued and one Scheduled
         # which was submitted a long time ago
         queued_recipe = data_setup.create_recipe()
         scheduled_recipe = data_setup.create_recipe()
         job_with_multiple_recipes = data_setup.create_job_for_recipes([queued_recipe, scheduled_recipe])
         job_with_multiple_recipes.owner = self.user
         # mark recipe Queued
         queued_recipe.process()
         queued_recipe.queue()
         # mark recipe Scheduled
         scheduled_recipe.process()
         scheduled_recipe.queue()
         scheduled_recipe.schedule()
         data_setup.mark_job_queued(job_with_multiple_recipes)
         job_with_multiple_recipes.recipesets[0].queue_time = datetime.utcnow()\
             - timedelta(days=self.delayed_job_age)
         # create a new submmited job for just now
         recently_submmited_job = data_setup.create_job_for_recipes([recipe])
         recently_submmited_job.owner = self.user
         data_setup.mark_job_queued(recently_submmited_job)
     beaker_usage = BeakerUsage(self.user, self.reservation_expiry, self.reservation_length,
                            self.waiting_recipe_age, self.delayed_job_age)
     delayed_jobs = beaker_usage.delayed_jobs()
     self.assertEqual(len(delayed_jobs), 2)
     self.assertEqual(absolute_url('/jobs/%s' % job.id), delayed_jobs[0][1])
     self.assertEqual(absolute_url('/jobs/%s' % job_with_multiple_recipes.id), delayed_jobs[1][1])
Beispiel #2
0
 def test_open_in_demand_systems(self):
     with session.begin():
         # system with waiting recipes
         system_with_waiting_recipes = data_setup.create_system()
         data_setup.create_manual_reservation(system_with_waiting_recipes,
                                              start=datetime.utcnow() - timedelta(days=self.reservation_length),
                                              user=self.user)
         recipe = data_setup.create_recipe()
         recipe.systems[:] = [system_with_waiting_recipes]
         job = data_setup.create_job_for_recipes([recipe])
         data_setup.mark_job_queued(job)
         job.recipesets[0].queue_time = datetime.utcnow() - timedelta(hours=self.waiting_recipe_age)
         # Create another system with waiting recipes and delete the job
         recipe_in_deleted_job = data_setup.create_recipe()
         recipe_in_deleted_job.systems[:] = [system_with_waiting_recipes]
         deleted_job = data_setup.create_job_for_recipes([recipe_in_deleted_job])
         data_setup.mark_job_queued(deleted_job)
         deleted_job.recipesets[0].queue_time = datetime.utcnow() - timedelta(hours=self.waiting_recipe_age)
         deleted_job.delete()
         # system with no waiting recipes
         system_without_waiting_recipes = data_setup.create_system()
         data_setup.create_manual_reservation(system_without_waiting_recipes,
                                              start=datetime.utcnow() - timedelta(days=self.reservation_length),
                                              user=self.user)
     beaker_usage = BeakerUsage(self.user, self.reservation_expiry, self.reservation_length,
                            self.waiting_recipe_age, self.delayed_job_age)
     open_in_demand_systems = beaker_usage.open_in_demand_systems()
     self.assertEqual(len(open_in_demand_systems), 1)
     self.assertEqual(open_in_demand_systems[0][1], 1)
     self.assertEqual(open_in_demand_systems[0][2], system_with_waiting_recipes.fqdn)
 def test_expiring_reservation(self):
     with session.begin():
         # recipe running /distribution/reservesys expiring soon
         expiring_soon = data_setup.create_recipe_reservation(self.user, u'/distribution/reservesys',
                                                       (self.reservation_expiry - 1) * 3600)
         # recipe running /distribution/reservesys expiring after the cut-off
         expiring_later = data_setup.create_recipe_reservation(self.user, u'/distribution/reservesys',
                                                       (self.reservation_expiry * 2) * 3600)
         # recipe expiring soon but running a real task
         not_reserved = data_setup.create_recipe_reservation(self.user, u'/something/else',
                                                      (self.reservation_expiry - 1) * 3600)
     beaker_usage = BeakerUsage(self.user, self.reservation_expiry, self.reservation_length,
                                self.waiting_recipe_age, self.delayed_job_age)
     expiring_reservations = beaker_usage.expiring_reservations()
     self.assertEqual(len(expiring_reservations), 1)
     self.assertEqual(expiring_reservations[0][1], expiring_soon.resource.fqdn)
Beispiel #4
0
    def test_send_usage_reminder(self):
        with session.begin():
            email_content = self._create_expiring_reservation()
            email_content += self._create_open_reservation()
            email_content += self._create_delayed_job()

        beaker_usage = BeakerUsage(self.user, self.reservation_expiry, self.reservation_length,
                                   self.waiting_recipe_age, self.delayed_job_age)
        current_date = datetime.utcnow().strftime("%Y-%m-%d")
        data = {
            'user_name': self.user.user_name,
            'current_date': current_date,
            'beaker_fqdn': absolute_url('/'),
            'reservation_expiry': self.reservation_expiry,
            'reservation_length': self.reservation_length,
            'waiting_recipe_age': self.waiting_recipe_age,
            'delayed_job_age': self.delayed_job_age,
            'expiring_reservations': beaker_usage.expiring_reservations(),
            'open_reservations': beaker_usage.open_in_demand_systems(),
            'delayed_jobs': beaker_usage.delayed_jobs()
        }

        mail_capture_thread.start_capturing()
        with session.begin():
            bkr.server.mail.send_usage_reminder(self.user, data)
        captured_mails = mail_capture_thread.stop_capturing()
        self.assertEqual(len(captured_mails),1)
        sender, rcpts, raw_msg = captured_mails[0]
        self.assertEqual(rcpts, [self.user.email_address])
        msg = email.message_from_string(raw_msg)
        self.assertEqual(msg['To'], self.user.email_address)
        self.assertTrue(msg['Subject'], '[Beaker] Usage report for %s (%s)' % (self.user.user_name, current_date))
        expected_mail_body = u"""=========
[Beaker] Usage report for %s (%s)
=========

Hi %s,
%s
=========""" % (self.user.user_name,
                current_date,
                self.user.user_name,
                email_content)
        actual_mail_body = msg.get_payload(decode=True)
        self.assertEqual(actual_mail_body, expected_mail_body)
        self.assertEqual(msg['X-Beaker-Notification'], 'usage-report')