Skip to content

Commit

Permalink
adjusts tests and use refactored functions
Browse files Browse the repository at this point in the history
- file 'tools/job_metadata.py'
  - add constant for 'TEST' section

- file 'eessi_bot_job_manager.py'
  - replace read_job_metadata_from_file, read_job_test and read_job_result with get_section_from_file
  - some polishing: changing imports, removing unused functions

- file 'tests/test_tools_job_metadata.py'
  - replaced read_job_metadata_from_file with get_section_from_file
  - updated log file name accordingly
  • Loading branch information
truib committed Mar 15, 2024
1 parent 0753f29 commit ecc4a81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 50 deletions.
63 changes: 18 additions & 45 deletions eessi_bot_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@

# Local application imports (anything from EESSI/eessi-bot-software-layer)
from connections import github
from tools import config, run_cmd
from tools import config, job_metadata, run_cmd
from tools.args import job_manager_parse
from tools.job_metadata import read_job_metadata_from_file, read_metadata_file
from tools.pr_comments import get_submitted_job_comment, update_comment


Expand Down Expand Up @@ -254,42 +253,6 @@ def determine_finished_jobs(self, known_jobs, current_jobs):

return finished_jobs

def read_job_result(self, job_result_file_path):
"""
Read job result file and return the contents of the 'RESULT' section.
Args:
job_result_file_path (string): path to job result file
Returns:
(ConfigParser): instance of ConfigParser corresponding to the
'RESULT' section or None
"""
# reuse function from module tools.job_metadata to read metadata file
result = read_metadata_file(job_result_file_path, self.logfile)
if result and "RESULT" in result:
return result["RESULT"]
else:
return None

def read_job_test(self, job_test_file_path):
"""
Read job test file and return the contents of the 'TEST' section.
Args:
job_test_file_path (string): path to job test file
Returns:
(ConfigParser): instance of ConfigParser corresponding to the
'TEST' section or None
"""
# reuse function from module tools.job_metadata to read metadata file
test = read_metadata_file(job_test_file_path, self.logfile)
if test and "TEST" in test:
return test["TEST"]
else:
return None

def process_new_job(self, new_job):
"""
Process a new job by verifying that it is a bot job and if so
Expand Down Expand Up @@ -335,7 +298,9 @@ def process_new_job(self, new_job):

# assuming that a bot job's working directory contains a metadata
# file, its existence is used to check if the job belongs to the bot
metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile)
metadata_pr = job_metadata.get_section_from_file(job_metadata_path,
job_metadata.JOB_PR_SECTION,
self.logfile)

if metadata_pr is None:
log(f"No metadata file found at {job_metadata_path} for job {job_id}, so skipping it",
Expand Down Expand Up @@ -431,7 +396,9 @@ def process_running_jobs(self, running_job):
job_metadata_path = os.path.join(job_dir, metadata_file)

# check if metadata file exist
metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile)
metadata_pr = job_metadata.get_section_from_file(job_metadata_path,
job_metadata.JOB_PR_SECTION,
self.logfile)
if metadata_pr is None:
raise Exception("Unable to find metadata file")

Expand Down Expand Up @@ -525,11 +492,13 @@ def process_finished_job(self, finished_job):
# check if _bot_jobJOBID.result exits
job_result_file = f"_bot_job{job_id}.result"
job_result_file_path = os.path.join(new_symlink, job_result_file)
job_results = self.read_job_result(job_result_file_path)
job_results = job_metadata.get_section_from_file(job_result_file_path,
job_metadata.JOB_RESULT_SECTION,
self.logfile)

job_result_unknown_fmt = finished_job_comments_cfg[JOB_RESULT_UNKNOWN_FMT]
# set fallback comment_description in case no result file was found
# (self.read_job_result returned None)
# (job_metadata.get_section_from_file returned None)
comment_description = job_result_unknown_fmt.format(filename=job_result_file)
if job_results:
# get preformatted comment_description or use previously set default for unknown
Expand All @@ -552,11 +521,13 @@ def process_finished_job(self, finished_job):
# --> bot/test.sh and bot/check-test.sh scripts are run in job script used by bot for 'build' action
job_test_file = f"_bot_job{job_id}.test"
job_test_file_path = os.path.join(new_symlink, job_test_file)
job_tests = self.read_job_test(job_test_file_path)
job_tests = job_metadata.get_section_from_file(job_test_file_path,
job_metadata.JOB_TEST_SECTION,
self.logfile)

job_test_unknown_fmt = finished_job_comments_cfg[JOB_TEST_UNKNOWN_FMT]
# set fallback comment_description in case no test file was found
# (self.read_job_result returned None)
# (job_metadata.get_section_from_file returned None)
comment_description = job_test_unknown_fmt.format(filename=job_test_file)
if job_tests:
# get preformatted comment_description or use previously set default for unknown
Expand All @@ -576,7 +547,9 @@ def process_finished_job(self, finished_job):
# obtain id of PR comment to be updated (from file '_bot_jobID.metadata')
metadata_file = f"_bot_job{job_id}.metadata"
job_metadata_path = os.path.join(new_symlink, metadata_file)
metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile)
metadata_pr = job_metadata.get_section_from_file(job_metadata_path,
job_metadata.JOB_PR_SECTION,
self.logfile)
if metadata_pr is None:
raise Exception("Unable to find metadata file ... skip updating PR comment")

Expand Down
10 changes: 5 additions & 5 deletions tests/test_tools_job_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

import os

from tools.job_metadata import read_job_metadata_from_file
from tools.job_metadata import get_section_from_file, JOB_PR_SECTION


def test_read_job_metadata_from_file(tmpdir):
logfile = os.path.join(tmpdir, 'test_read_job_metadata_from_file.log')
def test_get_section_from_file(tmpdir):
logfile = os.path.join(tmpdir, 'test_get_section_from_file.log')
# if metadata file does not exist, we should get None as return value
path = os.path.join(tmpdir, 'test.metadata')
assert read_job_metadata_from_file(path, logfile) is None
assert get_section_from_file(path, JOB_PR_SECTION, logfile) is None

with open(path, 'w') as fp:
fp.write('''[PR]
repo=test
pr_number=12345''')

metadata_pr = read_job_metadata_from_file(path, logfile)
metadata_pr = get_section_from_file(path, JOB_PR_SECTION, logfile)
expected = {
"repo": "test",
"pr_number": "12345",
Expand Down
1 change: 1 addition & 0 deletions tools/job_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
JOB_RESULT_SECTION = "RESULT"
JOB_RESULT_STATUS = "status"
JOB_RESULT_SUCCESS = "SUCCESS"
JOB_TEST_SECTION = "TEST"


def create_metadata_file(job, job_id, pr_comment):
Expand Down

0 comments on commit ecc4a81

Please sign in to comment.