Skip to content

Commit

Permalink
[jobs] Handle graal jobs
Browse files Browse the repository at this point in the history
This code allows to handle graal jobs. The identification of a
graal job is done according to the category name. If it starts
with `code_`, arthur will look for graal backends.
  • Loading branch information
valeriocos committed Jun 6, 2018
1 parent 087d57e commit b8bb381
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
13 changes: 12 additions & 1 deletion arthur/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import pickle
import rq

import graal
import graal.graal
import graal.backends

import perceval
import perceval.backend
import perceval.backends
Expand Down Expand Up @@ -112,7 +116,11 @@ class PercevalJob:
"""
def __init__(self, job_id, task_id, backend, category, conn, qitems):
try:
self._bklass = perceval.backend.find_backends(perceval.backends)[0][backend]
if category.startswith("code_"):
self._bklass = graal.graal.find_backends(graal.backends)[0][backend]
else:
self._bklass = perceval.backend.find_backends(perceval.backends)[0][backend]

except KeyError:
raise NotFoundError(element=backend)

Expand Down Expand Up @@ -232,6 +240,9 @@ def _execute(self, backend_args, archive_args):
:raises AttributeError: raised when any of the required
parameters is not found
"""
if self.category.startswith("code_"):
return graal.graal.fetch(self._bklass, backend_args, self.category)

if not archive_args or not archive_args['fetch_from_archive']:
return perceval.backend.fetch(self._bklass, backend_args, self.category,
manager=self.archive_manager)
Expand Down
Binary file added tests/data/graaltest.zip
Binary file not shown.
84 changes: 83 additions & 1 deletion tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import os.path
import pickle
import shutil
import subprocess
import tempfile
import unittest

Expand Down Expand Up @@ -184,7 +185,7 @@ def request_callback(method, uri, headers):
elif uri.startswith(REDMINE_USER_25_URL):
body = user_25_body
else:
raise
raise Exception

http_requests.append(last_request)

Expand Down Expand Up @@ -293,6 +294,29 @@ def test_init(self):
self.assertEqual(result.offset, None)
self.assertEqual(result.nresumed, 0)

job = PercevalJob('arthur-job-1234567890', 'mytask', 'cocom', 'code_complexity',
self.conn, 'items')

self.assertEqual(job.job_id, 'arthur-job-1234567890')
self.assertEqual(job.task_id, 'mytask')
self.assertEqual(job.backend, 'cocom')
self.assertEqual(job.category, 'code_complexity')
self.assertEqual(job.conn, self.conn)
self.assertEqual(job.qitems, 'items')
self.assertEqual(job.archive_manager, None)

result = job.result
self.assertIsInstance(job.result, JobResult)
self.assertEqual(result.job_id, 'arthur-job-1234567890')
self.assertEqual(result.task_id, 'mytask')
self.assertEqual(result.backend, 'cocom')
self.assertEqual(job.category, 'code_complexity')
self.assertEqual(result.last_uuid, None)
self.assertEqual(result.max_date, None)
self.assertEqual(result.nitems, 0)
self.assertEqual(result.offset, None)
self.assertEqual(result.nresumed, 0)

def test_backend_not_found(self):
"""Test if it raises an exception when a backend is not found"""

Expand Down Expand Up @@ -346,6 +370,64 @@ def test_run(self):

self.assertEqual(commits, expected)

def test_run_graal(self):
"""Test run method using the Graal backend"""

tmp_path = tempfile.mkdtemp(prefix='graal_')
tmp_repo_path = os.path.join(tmp_path, 'repos')
os.mkdir(tmp_repo_path)

git_path = os.path.join(tmp_path, 'graaltest')

data_path = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(data_path, 'data')

repo_name = 'graaltest'

fdout, _ = tempfile.mkstemp(dir=tmp_path)

zip_path = os.path.join(data_path, repo_name + '.zip')
subprocess.check_call(['unzip', '-qq', zip_path, '-d', tmp_repo_path])

origin_path = os.path.join(tmp_repo_path, repo_name)
subprocess.check_call(['git', 'clone', '-q', '--bare', origin_path, git_path],
stderr=fdout)

job = PercevalJob('arthur-job-1234567890', 'mytask', 'coqua', 'code_quality',
self.conn, 'items')
args = {
'uri': 'http://example.com/',
'git_path': os.path.join(self.dir, git_path),
'entrypoint': "perceval"
}
archive_args = {
'archive_path': self.tmp_path,
'fetch_from_archive': False
}

job.run(args, archive_args)

self.assertIsInstance(job.archive_manager, ArchiveManager)
result = job.result
self.assertIsInstance(job.result, JobResult)
self.assertEqual(result.job_id, 'arthur-job-1234567890')
self.assertEqual(result.task_id, 'mytask')
self.assertEqual(result.backend, 'coqua')
self.assertEqual(result.category, 'code_quality')
self.assertEqual(result.last_uuid, '5873807157a089297ffddd74b50a46910eb822a0')
self.assertEqual(result.max_date, 1526909120.0)
self.assertEqual(result.nitems, 1)
self.assertEqual(result.offset, None)
self.assertEqual(result.nresumed, 0)

commits = self.conn.lrange('items', 0, -1)
commits = [pickle.loads(c) for c in commits]
commits = [commit['data']['commit'] for commit in commits]

expected = ['10f5a02b553a90fde36a8f151653d995faa3818e']

self.assertEqual(commits, expected)

def test_run_not_found_parameters(self):
"""Check if it fails when a required backend parameter is not found"""

Expand Down

0 comments on commit b8bb381

Please sign in to comment.