Example #1
0
def test_get_queue():
    """
    /SA310/
    """
    jobs = Job.get_jobs(None, [JobStatus.QUEUED])
    for current_job in jobs:
        current_job.set_status(JobStatus.DONE)
    assert len(Job.get_jobs(None, [JobStatus.QUEUED])) == 0

    timestamp = datetime.datetime.now()

    try:
        admin = User.get_user_by_username("queueadmin1")
    except UserNotFoundException:
        admin = Administrator("queueadmin1", "token")

    job1 = Job('storageA', 'storageB', '~/.data/', [True, True, True], admin)
    job1_id = job1.get_job_id()
    job1.set_status(JobStatus.QUEUED)
    job1.set_enqueue_time(timestamp)
    job1.set_priority(1)

    try:
        user = User.get_user_by_username("queueuser1")
    except UserNotFoundException:
        user = Administrator("queueuser1", "token")

    job2 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user)
    job2_id = job2.get_job_id()
    job2.set_status(JobStatus.QUEUED)
    job2.set_enqueue_time(timestamp)
    job2.set_priority(2)

    job3 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user)
    job3_id = job3.get_job_id()
    job3.set_status(JobStatus.QUEUED)
    job3.set_enqueue_time(timestamp)
    job3.set_priority(3)

    response = json.loads(app.get_queue())
    assert len(response) == 3
    for job_json in response:
        job = Job.get_job_by_id(job_json['job_id'])
        assert job_json['job_id'] == job1_id or job_json[
            'job_id'] == job2_id or job_json['job_id'] == job3_id
        assert job_json['job_id'] == job.get_job_id()
        assert job_json['source'] == job.get_source_alias(
        ) + ":" + job.get_source_relative_path()
        assert job_json['enqueue_time'] == job.get_enqueue_time().isoformat()
        assert job_json['creator'] == job.get_user().get_username()
        assert job_json['priority'] == job.get_priority()
        assert job_json['status'] == job.get_status().name
Example #2
0
    def check_create_job(self, job, calling_user):
        """
        Checks permissions for job creation. Checks:
        1) If calling_user can create job for for_user
        2) If user did not exceed the allowed amount of jobs
        :param job: job to be checked
        """
        if job.get_user().get_username() != calling_user.get_username():
            if calling_user.get_user_type() != UserRole.Administrator:
                raise NoAdministratorRightsException(
                    calling_user.get_username() + " doesn't have "
                    "rights to create job for " +
                    job.get_user().get_username())

        allowed_jobs_number = self._config_handler.get_max_tasks_number_for_user(
        )
        if len(Job.get_jobs([job.get_user().get_username()],
                            [JobStatus.ACTIVE, JobStatus.QUEUED, JobStatus.PAUSED])) \
                > allowed_jobs_number:
            raise PermissionException("Max. number of jobs (" +
                                      str(allowed_jobs_number) +
                                      ") for user " +
                                      job.get_user().get_username() +
                                      " exceeded")
        return
Example #3
0
def test_get_log_different_days():
    """
    /SA240/
    """
    try:
        user1 = User.get_user_by_username("loguser2")
        jobs = Job.get_jobs([user1.get_username()], [
            JobStatus.PAUSED, JobStatus.QUEUED, JobStatus.ACTIVE,
            JobStatus.DONE
        ])
        for current_job in jobs:
            current_job.set_status(JobStatus.ACTIVE)
    except UserNotFoundException:
        user1 = User("loguser2", "token")

    timestamp = datetime.date.today() - datetime.timedelta(days=2)

    active_job_ids_user1 = []
    job1 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1.set_status(JobStatus.DONE)
    job1.set_start_time(timestamp)
    job1.set_end_time(timestamp)
    job1.set_enqueue_time(timestamp)
    active_job_ids_user1.append(job1.get_job_id())

    job2 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job2.set_status(JobStatus.DONE)
    job2.set_start_time(timestamp)
    job2.set_end_time(timestamp)
    job2.set_enqueue_time(timestamp)

    active_job_ids_user1.append(job2.get_job_id())
    job3 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job3.set_status(JobStatus.DONE)
    job3.set_start_time(timestamp)
    job3.set_end_time(timestamp)
    job3.set_enqueue_time(timestamp)
    active_job_ids_user1.append(job3.get_job_id())

    # Log for last 3 days
    request = {"days": "3", "user": "******", "for_user": "******"}

    json_string = json.dumps(request)
    response = json.loads(app.get_log(json_string))

    assert len(response) == 3
    for job in response:
        assert job['job_id'] in active_job_ids_user1

    # Log for last day
    request = {"days": "1", "user": "******", "for_user": "******"}

    json_string = json.dumps(request)
    response = json.loads(app.get_log(json_string))

    assert len(response) == 0
Example #4
0
    def get_jobs(self, days, user=None, for_user=None, statuses=None):
        """
        :raises UserNotFoundException: if given 'user' or 'for_user' doesn't exist
        :raises NoProjectManagerRightsException: @see access_handler
        """
        calling_user = User.get_user_by_username(user) if user else None
        team = True if for_user == "team" else False
        for_user = None if for_user in [None, "team"] else User.get_user_by_username(for_user)

        if team:
            self.access_handler.check_read_rights(None, calling_user)
            jobs = Job.get_jobs(calling_user.get_team_members(), statuses, days)
        elif for_user is not None and calling_user is not None:
            self.access_handler.check_read_rights(for_user, calling_user)
            jobs = Job.get_jobs([for_user.get_username()], statuses, days)
        else:
            jobs = Job.get_jobs(None, statuses)

        return build_log(jobs)
Example #5
0
 def look_for_work(self, worker):
     """
     Looks wether a job is available for the given worker
     :param worker: The worker to assign a job to
     :type worker: Worker
     :returns: a job
     """
     storages = list(storage.alias for storage in worker.get_storages())
     jobs = Job.get_jobs(statuses=[JobStatus.QUEUED], storages=storages)
     if not jobs:
         return None
     else:
         return jobs[0]
Example #6
0
def test_get_log():
    """
    1. Admin requests logs of user. /SA210/
    2. User requests his own logs /SA220/
    3. Another user(not admin) requests logs of user /SA230/
    :return:
    """
    try:
        user1 = User.get_user_by_username("loguser1")
        jobs = Job.get_jobs([user1.get_username()], [
            JobStatus.PAUSED, JobStatus.QUEUED, JobStatus.ACTIVE,
            JobStatus.DONE
        ])
        for current_job in jobs:
            current_job.set_status(JobStatus.ACTIVE)
    except UserNotFoundException:
        user1 = User("loguser1", "token")

    timestamp = datetime.datetime.now()

    active_job_ids_user1 = []
    job1 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1.set_status(JobStatus.DONE)
    job1.set_start_time(timestamp)
    job1.set_end_time(timestamp)
    job1.set_enqueue_time(timestamp)
    active_job_ids_user1.append(job1.get_job_id())

    job2 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job2.set_status(JobStatus.DONE)
    job2.set_start_time(timestamp)
    job2.set_end_time(timestamp)
    job2.set_enqueue_time(timestamp)

    active_job_ids_user1.append(job2.get_job_id())
    job3 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job3.set_status(JobStatus.DONE)
    job3.set_start_time(timestamp)
    job3.set_end_time(timestamp)
    job3.set_enqueue_time(timestamp)
    active_job_ids_user1.append(job3.get_job_id())

    # -----------1------------
    try:
        admin = User.get_user_by_username("logadmin1")
    except UserNotFoundException:
        admin = Administrator("logadmin1", "token")

    request = {"days": "5", "user": "******", "for_user": "******"}

    json_string = json.dumps(request)
    response = json.loads(app.get_log(json_string))

    assert len(response) == 3
    for job in response:
        assert job['job_id'] in active_job_ids_user1

    # -----------2------------
    request = {"days": "5", "user": "******", "for_user": "******"}

    json_string = json.dumps(request)
    response = json.loads(app.get_log(json_string))

    assert len(response) == 3
    for job in response:
        assert job['job_id'] in active_job_ids_user1

    # -----------3------------
    try:
        false_admin = User.get_user_by_username("logadminfalse1")
    except UserNotFoundException:
        false_admin = User("logadminfalse1", "token")

    request = {"days": "5", "user": "******", "for_user": "******"}

    json_string = json.dumps(request)
    with pytest.raises(PermissionException):
        app.get_log(json_string)
Example #7
0
from master import *
from master.worker import Worker
from master.worker_status import WorkerStatus

from application.system.job import Job
from application.system.user import User

from exceptions.user_not_found_exception import UserNotFoundException

try:
    user = User.get_user_by_username("xyz")
except UserNotFoundException:
    user = User("xyz", "token")

job = Job.get_jobs([user.name])
if job == []:
    job = Job('storageA', 'storageB', '~/.data/', [True, True, True], user)


@pytest.mark.order1
def test_constructor():
    if Worker.get_worker_by_name('testworker') is None:
        test_worker = Worker('testworker', 'WAITING', None, 'asdf@localhost',
                             ['storageA', 'storageB'])
    else:
        test_worker = Worker.get_worker_by_name('testworker')
    assert type(test_worker) is Worker


@pytest.mark.order2
Example #8
0
def test_get_jobs():
    #Build users and jobs
    try:
        user1 = User.get_user_by_username("user1")
        jobs = Job.get_jobs([user1.get_username()], [
            JobStatus.PAUSED, JobStatus.QUEUED, JobStatus.ACTIVE,
            JobStatus.PAUSED
        ])
        for current_job in jobs:
            current_job.set_status(JobStatus.DONE)
    except UserNotFoundException:
        user1 = User("user1", "token")

    try:
        user2 = User.get_user_by_username("user2")
        jobs = Job.get_jobs([user2.get_username()], [
            JobStatus.PAUSED, JobStatus.QUEUED, JobStatus.ACTIVE,
            JobStatus.PAUSED
        ])
        for current_job in jobs:
            current_job.set_status(JobStatus.DONE)
    except UserNotFoundException:
        user2 = User("user2", "token")

    try:
        user3 = User.get_user_by_username("user3")
        jobs = Job.get_jobs([user3.get_username()], [
            JobStatus.PAUSED, JobStatus.QUEUED, JobStatus.ACTIVE,
            JobStatus.PAUSED
        ])
        for current_job in jobs:
            current_job.set_status(JobStatus.DONE)
    except UserNotFoundException:
        user1 = User("user3", "token")

    active_job_ids_user1 = []
    job1_1 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_1.set_status(JobStatus.ACTIVE)
    active_job_ids_user1.append(job1_1.get_job_id())
    job1_2 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_2.set_status(JobStatus.ACTIVE)
    active_job_ids_user1.append(job1_2.get_job_id())
    job1_3 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_3.set_status(JobStatus.ACTIVE)
    active_job_ids_user1.append(job1_3.get_job_id())

    queued_job_ids_user1 = []
    job1_4 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_4.set_status(JobStatus.QUEUED)
    queued_job_ids_user1.append(job1_4.get_job_id())
    job1_5 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_5.set_status(JobStatus.QUEUED)
    queued_job_ids_user1.append(job1_5.get_job_id())
    job1_6 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_6.set_status(JobStatus.QUEUED)
    queued_job_ids_user1.append(job1_6.get_job_id())

    canceled_job_ids_user1 = []
    job1_7 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_7.set_status(JobStatus.CANCELED)
    canceled_job_ids_user1.append(job1_7.get_job_id())
    job1_8 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_8.set_status(JobStatus.CANCELED)
    canceled_job_ids_user1.append(job1_8.get_job_id())
    job1_9 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user1)
    job1_9.set_status(JobStatus.CANCELED)
    canceled_job_ids_user1.append(job1_9.get_job_id())

    active_jobs_user1 = active_job_ids_user1.copy()
    queued_jobs_user1 = queued_job_ids_user1.copy()
    canceled_jobs_user1 = canceled_job_ids_user1.copy()

    jobs = Job.get_jobs([user1.get_username()], [JobStatus.ACTIVE])
    assert len(jobs) == 3
    for job in jobs:
        if job.get_job_id() not in active_jobs_user1:
            assert 1 == 0
        else:
            assert 1 == 1
            active_jobs_user1.remove(job.get_job_id())
    assert active_jobs_user1 == []
    active_jobs_user1 = active_job_ids_user1.copy()

    jobs = Job.get_jobs([user1.get_username()],
                        [JobStatus.ACTIVE, JobStatus.QUEUED])
    assert len(jobs) == 6
    for job in jobs:
        if job.get_job_id() not in active_jobs_user1:
            if job.get_job_id() not in queued_jobs_user1:
                assert 1 == 0
            else:
                assert 1 == 1
                queued_jobs_user1.remove(job.get_job_id())
        else:
            assert 1 == 1
            active_jobs_user1.remove(job.get_job_id())
    assert active_jobs_user1 == []
    assert queued_jobs_user1 == []
    active_jobs_user1 = active_job_ids_user1.copy()
    queued_jobs_user1 = queued_job_ids_user1.copy()

    user2 = User.get_user_by_username("user2")
    active_job_ids_user2 = []
    job2_1 = Job('storageC', 'storageD', '~/.data/', [True, True, True], user2)
    job2_1.set_status(JobStatus.ACTIVE)
    active_job_ids_user2.append(job2_1.get_job_id())
    job2_2 = Job('storageC', 'storageD', '~/.data/', [True, True, True], user2)
    job2_2.set_status(JobStatus.ACTIVE)
    active_job_ids_user2.append(job2_2.get_job_id())
    job2_3 = Job('storageC', 'storageD', '~/.data/', [True, True, True], user2)
    job2_3.set_status(JobStatus.ACTIVE)
    active_job_ids_user2.append(job2_3.get_job_id())

    queued_job_ids_user2 = []
    job2_4 = Job('storageC', 'storageD', '~/.data/', [True, True, True], user2)
    job2_4.set_status(JobStatus.QUEUED)
    queued_job_ids_user2.append(job2_4.get_job_id())
    job2_5 = Job('storageC', 'storageD', '~/.data/', [True, True, True], user2)
    job2_5.set_status(JobStatus.QUEUED)
    queued_job_ids_user2.append(job2_5.get_job_id())
    job2_5 = Job('storageC', 'storageD', '~/.data/', [True, True, True], user2)
    job2_5.set_status(JobStatus.QUEUED)
    queued_job_ids_user2.append(job2_5.get_job_id())

    active_jobs_user2 = active_job_ids_user2.copy()
    queued_jobs_user2 = queued_job_ids_user2.copy()

    jobs = Job.get_jobs(
        [user1.get_username(), user2.get_username()], [JobStatus.ACTIVE])
    assert len(jobs) == 6
    for job in jobs:
        if job.get_job_id() not in active_jobs_user1:
            if job.get_job_id() not in active_jobs_user2:
                assert 1 == 0
            else:
                assert 1 == 1
                active_jobs_user2.remove(job.get_job_id())
        else:
            assert 1 == 1
            active_jobs_user1.remove(job.get_job_id())
    assert active_jobs_user1 == []
    assert active_jobs_user2 == []
    active_jobs_user1 = active_job_ids_user1.copy()
    active_jobs_user2 = active_job_ids_user2.copy()

    job3 = Job('storageA', 'storageB', '~/.data/', [True, True, True], user3)
    job3.set_status(JobStatus.PAUSED)
    time = datetime.datetime.now() - datetime.timedelta(days=10)
    job3.set_end_time(time)
    jobs = Job.get_jobs(None, [JobStatus.PAUSED], 8)
    assert len(jobs) == 0
    jobs = Job.get_jobs(None, [JobStatus.PAUSED], 12)
    assert len(jobs) == 1