Skip to content

Commit

Permalink
Add tests for generate job info and get status
Browse files Browse the repository at this point in the history
  • Loading branch information
kks32 committed Sep 30, 2024
1 parent 0702902 commit ff7c163
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 213 deletions.
14 changes: 7 additions & 7 deletions dapi/jobs/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# )


def generate_job_description(
def generate_job_info(
t: Any, # Tapis client
app_name: str,
input_uri: str,
Expand All @@ -24,7 +24,7 @@ def generate_job_description(
allocation: Optional[str] = None,
) -> Dict[str, Any]:
"""
Generates a job description dictionary based on the provided application name, job name, input URI, input file, and optional allocation.
Generates a job info dictionary based on the provided application name, job name, input URI, input file, and optional allocation.
Args:
t (object): The Tapis API client object.
Expand All @@ -39,7 +39,7 @@ def generate_job_description(
allocation (str, optional): The allocation to use for the job. Defaults to None.
Returns:
dict: The job description dictionary.
dict: The job info dictionary.
"""

# Fetch the latest app information
Expand All @@ -49,8 +49,8 @@ def generate_job_description(
if not job_name:
job_name = f"{app_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}"

# Create the base job description
job_description = {
# Create the base job info
job_info = {
"name": job_name,
"appId": app_info.id,
"appVersion": app_info.version,
Expand All @@ -70,11 +70,11 @@ def generate_job_description(

# Add TACC allocation if provided
if allocation:
job_description["parameterSet"]["schedulerOptions"].append(
job_info["parameterSet"]["schedulerOptions"].append(
{"name": "TACC Allocation", "arg": f"-A {allocation}"}
)

return job_description
return job_info


def get_status(t, mjobUuid, tlapse=15):
Expand Down
120 changes: 80 additions & 40 deletions tests/jobs/test_job_gen_jobinfo.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,91 @@
import unittest
from unittest.mock import Mock, patch
from dapi.jobs import jobs
from datetime import datetime


class TestGenerateJobInfo(unittest.TestCase):
def setUp(self):
# Mock the ag object
self.ag_mock = Mock()
self.appid_valid = "valid-appid"
self.appid_invalid = "invalid-appid"

# Define the behavior of the mocked method
def mock_get_method(appId):
if appId == self.appid_valid:
return True
raise Exception("Invalid app ID")

# Set the side_effect for the mock to the defined behavior
self.ag_mock.apps.get.side_effect = mock_get_method

def test_valid_appid(self):
"""Test with a valid app ID."""
result = jobs.generate_job_info(self.ag_mock, self.appid_valid)
self.assertEqual(result["appId"], self.appid_valid)

def test_invalid_appid(self):
"""Test with an invalid app ID."""
try:
result = jobs.generate_job_info(self.ag_mock, self.appid_invalid)
print("Result:", result)
except Exception as e:
print("Exception raised:", e)

with self.assertRaises(ValueError):
jobs.generate_job_info(self.ag_mock, self.appid_invalid)

def test_default_values(self):
"""Test with default values."""
result = jobs.generate_job_info(self.ag_mock, self.appid_valid)
self.assertEqual(result["name"], "dsjob")
self.assertEqual(result["batchQueue"], "development")
self.t_mock = Mock()
self.app_name = "test-app"
self.input_uri = "tapis://test-system/input/data"
self.input_file = "input.txt"

# Mock the getAppLatestVersion method
self.app_info_mock = Mock()
self.app_info_mock.id = self.app_name
self.app_info_mock.version = "1.0"
self.app_info_mock.jobAttributes.execSystemId = "test-exec-system"
self.app_info_mock.jobAttributes.maxMinutes = 60
self.app_info_mock.jobAttributes.archiveOnAppError = True
self.app_info_mock.jobAttributes.execSystemLogicalQueue = "normal"
self.t_mock.apps.getAppLatestVersion.return_value = self.app_info_mock

@patch("dapi.jobs.jobs.datetime")
def test_generate_job_info_default(self, mock_datetime):
mock_datetime.now.return_value = datetime(2023, 5, 1, 12, 0, 0)

result = jobs.generate_job_info(
self.t_mock, self.app_name, self.input_uri, self.input_file
)

self.assertEqual(result["name"], f"{self.app_name}_20230501_120000")
self.assertEqual(result["appId"], self.app_name)
self.assertEqual(result["appVersion"], "1.0")
self.assertEqual(result["execSystemId"], "test-exec-system")
self.assertEqual(result["maxMinutes"], 60)
self.assertTrue(result["archiveOnAppError"])
self.assertEqual(
result["fileInputs"],
[{"name": "Input Directory", "sourceUrl": self.input_uri}],
)
self.assertEqual(result["execSystemLogicalQueue"], "normal")
self.assertEqual(result["nodeCount"], 1)
self.assertEqual(result["processorsPerNode"], 1)
self.assertEqual(result["maxRunTime"], "00:10:00")
self.assertTrue(result["archive"])
self.assertIsNone(result["inputs"])
self.assertIsNone(result["parameters"])
self.assertEqual(result["coresPerNode"], 1)
self.assertEqual(
result["parameterSet"]["appArgs"],
[{"name": "Input Script", "arg": self.input_file}],
)
self.assertEqual(result["parameterSet"]["schedulerOptions"], [])

def test_generate_job_info_custom(self):
custom_job_name = "custom-job"
custom_max_minutes = 120
custom_node_count = 2
custom_cores_per_node = 4
custom_queue = "high-priority"
custom_allocation = "project123"

result = jobs.generate_job_info(
self.t_mock,
self.app_name,
self.input_uri,
self.input_file,
job_name=custom_job_name,
max_minutes=custom_max_minutes,
node_count=custom_node_count,
cores_per_node=custom_cores_per_node,
queue=custom_queue,
allocation=custom_allocation,
)

self.assertEqual(result["name"], custom_job_name)
self.assertEqual(result["maxMinutes"], custom_max_minutes)
self.assertEqual(result["nodeCount"], custom_node_count)
self.assertEqual(result["coresPerNode"], custom_cores_per_node)
self.assertEqual(result["execSystemLogicalQueue"], custom_queue)
self.assertEqual(
result["parameterSet"]["schedulerOptions"],
[{"name": "TACC Allocation", "arg": f"-A {custom_allocation}"}],
)

def test_generate_job_info_invalid_app(self):
self.t_mock.apps.getAppLatestVersion.side_effect = Exception("Invalid app")

with self.assertRaises(Exception):
jobs.generate_job_info(
self.t_mock, "invalid-app", self.input_uri, self.input_file
)


if __name__ == "__main__":
Expand Down
34 changes: 0 additions & 34 deletions tests/jobs/test_job_get_archive_path.py

This file was deleted.

114 changes: 0 additions & 114 deletions tests/jobs/test_job_runtime.py

This file was deleted.

Loading

0 comments on commit ff7c163

Please sign in to comment.