test_job_lsf_status.py 4.79 KB
Newer Older
1 2 3 4 5 6 7 8
"""
Tests related to the status of the job in LSF
"""
import unittest
import datetime

from app import create_app
from app.models import delayed_job_models
9 10
from app.config import RUN_CONFIG
from app import utils
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51


class TestJobLSFStatus(unittest.TestCase):
    """
    Class to test the columns related to the job lsf status checking
    """

    def setUp(self):
        self.flask_app = create_app()
        self.client = self.flask_app.test_client()

    def tearDown(self):
        with self.flask_app.app_context():
            delayed_job_models.delete_all_jobs()

    def test_determines_when_a_job_needs_to_be_checked(self):
        """
        Test that determines when a job needs to be checked in LSF because of its status
        and when it does noe need to be checked
        """

        with self.flask_app.app_context():
            job_type = 'SIMILARITY'
            params = {
                'search_type': 'SIMILARITY',
                'structure': '[H]C1(CCCN1C(=N)N)CC1=NC(=NO1)C1C=CC(=CC=1)NC1=NC(=CS1)C1C=CC(Br)=CC=1',
                'threshold': '70'
            }
            docker_image_url_must_be = 'some_url'
            job = delayed_job_models.get_or_create(job_type, params, docker_image_url_must_be)
            for status in [delayed_job_models.JobStatuses.QUEUED, delayed_job_models.JobStatuses.RUNNING,
                           delayed_job_models.JobStatuses.UNKNOWN]:
                job.status = status
                needs_to_be_checked_got = job.needs_to_be_checked_in_lsf()
                self.assertTrue(needs_to_be_checked_got, msg=f'A job with status {status} need to be checked in LSF!')

            for status in [delayed_job_models.JobStatuses.CREATED, delayed_job_models.JobStatuses.ERROR,
                           delayed_job_models.JobStatuses.FINISHED]:
                job.status = status
                needs_to_be_checked_got = job.needs_to_be_checked_in_lsf()
                self.assertFalse(needs_to_be_checked_got,
52
                                 msg=f'A job with status {status} does not need to be checked in LSF!')
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    def test_determines_when_the_job_checker_seems_to_have_died(self):
        """
        Test that determines when a job needs to be checked in LSF because the previous script failed
        """

        with self.flask_app.app_context():
            job_type = 'SIMILARITY'
            params = {
                'search_type': 'SIMILARITY',
                'structure': '[H]C1(CCCN1C(=N)N)CC1=NC(=NO1)C1C=CC(=CC=1)NC1=NC(=CS1)C1C=CC(Br)=CC=1',
                'threshold': '70'
            }
            docker_image_url_must_be = 'some_url'
            job = delayed_job_models.get_or_create(job_type, params, docker_image_url_must_be)
68 69 70 71
            death_assumption_seconds = RUN_CONFIG.get('status_agent').get('death_assumption_seconds')
            last_lsf_checked_at = utils.get_utc_now() - datetime.timedelta(seconds=death_assumption_seconds)
            job.last_lsf_checked_at = last_lsf_checked_at
            job.last_lsf_check_status = 1
72

73 74 75 76
            job_checker_seems_to_have_died_got = job.job_checker_seems_to_have_died()
            self.assertTrue(job_checker_seems_to_have_died_got,
                            msg='When the last script execution errors and the last time it reported is greater than '
                                'the assumed dead time it must assume that the checker died.')
77

78 79 80 81 82
            job.last_lsf_checked_at = utils.get_utc_now()
            job_checker_seems_to_have_died_got = job.job_checker_seems_to_have_died()
            self.assertFalse(job_checker_seems_to_have_died_got,
                             msg='When the last script execution errors but the last time it reported is less than '
                                 'the assumed dead time it must NOT assume that the checker died.')
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97
            job.last_lsf_checked_at = utils.get_utc_now()
            job.last_lsf_check_status = 0
            job_checker_seems_to_have_died_got = job.job_checker_seems_to_have_died()
            self.assertFalse(job_checker_seems_to_have_died_got,
                             msg='When the last script execution was successful and the last time it reported is less '
                                 'than the assumed dead time it must NOT assume that the checker died.')

            last_lsf_checked_at = utils.get_utc_now() - datetime.timedelta(seconds=death_assumption_seconds)
            job.last_lsf_checked_at = last_lsf_checked_at
            job.last_lsf_check_status = 0
            job_checker_seems_to_have_died_got = job.job_checker_seems_to_have_died()
            self.assertFalse(job_checker_seems_to_have_died_got,
                             msg='When the last script execution was successful but the last time it reported is '
                                 'greater than the assumed dead time it must NOT assume that the checker died.')