Skip to content

Commit

Permalink
Events behavior - #29
Browse files Browse the repository at this point in the history
  • Loading branch information
rbeker-BMC committed Mar 4, 2024
1 parent 4c39802 commit 33a8983
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/aapi/addevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class AddEvents(AAPIObject):

def __attrs_post_init__(self):
if self.object_name == attrs.fields_dict(self.__class__)['object_name'].default:
self.object_name = f'{self.object_name}_{random.choices(string.ascii_letters + string.digits, k=8)}'
random_attr = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
self.object_name = f'{self.object_name}_{random_attr}'
3 changes: 2 additions & 1 deletion src/aapi/deleteevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class DeleteEvents(AAPIObject):

def __attrs_post_init__(self):
if self.object_name == attrs.fields_dict(self.__class__)['object_name'].default:
self.object_name = f'{self.object_name}_{random.choices(string.ascii_letters + string.digits, k=8)}'
random_attr = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
self.object_name = f'{self.object_name}_{random_attr}'
3 changes: 2 additions & 1 deletion src/aapi/waitforevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class WaitForEvents(AAPIObject):

def __attrs_post_init__(self):
if self.object_name == attrs.fields_dict(self.__class__)['object_name'].default:
self.object_name = f'{self.object_name}_{random.choices(string.ascii_letters + string.digits, k=8)}'
random_attr = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
self.object_name = f'{self.object_name}_{random_attr}'
6 changes: 3 additions & 3 deletions src/ctm_python_client/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,19 @@ def connect(self, srcpath: str, destpath: str, *, inpath: str = None):
# support backward compatibility
if not obj_src.add_events_list:
obj_src.add_events_list.append(
AddEvents('events_to_add', events=[]))
AddEvents(events=[]))
obj_src.add_events_list[-1].events.append(
ConditionOutAdd(event=event_name))

if not obj_dest.wait_for_events_list:
obj_dest.wait_for_events_list.append(
WaitForEvents('events_to_wait', events=[]))
WaitForEvents(events=[]))
obj_dest.wait_for_events_list[-1].events.append(
ConditionIn(event=event_name))

if not obj_dest.delete_events_list:
obj_dest.delete_events_list.append(
DeleteEvents('events_to_delete', events=[]))
DeleteEvents(events=[]))
obj_dest.delete_events_list[-1].events.append(
ConditionOutDelete(event=event_name))

Expand Down
53 changes: 49 additions & 4 deletions tests/test_aapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,57 @@ def test_json_generation():

def test_copy_workflow():
workflow = Workflow(Environment.create_workbench("refael"), WorkflowDefaults(run_as='workbench'), skip_initial_authentication=True)
workflow.add(aapi.JobCommand('MyFirstJob', command='ls -lt', run_as='workbench'), inpath='MyFirstFolder')
workflow.add(aapi.JobCommand('TestJob', command='ls -lt', run_as='workbench'), inpath='TestFolder')

newworkflow = BaseWorkflow()
newworkflow.copy_from(workflow=workflow)

assert newworkflow.get("MyFirstFolder").dumps_aapi(indent=2) == workflow.get("MyFirstFolder").dumps_aapi(indent=2)
assert newworkflow.get("TestFolder").dumps_aapi(indent=2) == workflow.get("TestFolder").dumps_aapi(indent=2)

o = json.loads('{\n "Type": "Folder",\n "MyFirstJob": {\n "Type": "Job:Command",\n "RunAs": "workbench",\n "Command": "ls -lt"\n },\n "RunAs": "workbench"\n}')
assert newworkflow.get("MyFirstFolder").as_aapi_dict() == o
o = json.loads('{\n "Type": "Folder",\n "TestJob": {\n "Type": "Job:Command",\n "RunAs": "workbench",\n "Command": "ls -lt"\n },\n "RunAs": "workbench"\n}')
assert newworkflow.get("TestFolder").as_aapi_dict() == o

def test_connecting_jobs():
workflow = Workflow(Environment.create_workbench("refael"), WorkflowDefaults(run_as='workbench'), skip_initial_authentication=True)
firstjob = workflow.add(
aapi.JobCommand('JobToConnect1', command="first"),
inpath='TestFolder'
)

secondjob = workflow.add(
aapi.JobCommand('JobToConnect2', command="second"),
inpath='TestFolder'
)
workflow.connect(firstjob, secondjob)

def test_chaining_jobs():
workflow = Workflow(Environment.create_workbench("refael"), WorkflowDefaults(run_as='workbench'), skip_initial_authentication=True)
workflow.chain(
[
aapi.JobCommand('FirstTestJob', "First"),
aapi.JobCommand('SecondTestJob', "Second"),
aapi.JobCommand('ThirdTestJob', "Third", run_as_dummy=True),
],
inpath='ChainedJobs'
)

def test_events():
job = aapi.JobCommand('Job1', command='echo Hello')

waitForEventList = aapi.WaitForEvents([aapi.Event(event="wait1"), aapi.Event(event="wait2", date=aapi.Event.Date.AnyDate)])
job.event_list.append(waitForEventList)

deleteEventListObject = aapi.DeleteEvents([aapi.EventOutDelete(event="delete")])
job.event_list.append(deleteEventListObject)

addEventListObject1 = aapi.AddEvents([aapi.EventOutAdd(event="add1")])
job.event_list.append(addEventListObject1)

addEventListObject2 = aapi.AddEvents([aapi.EventOutAdd(event="add2",date=aapi.Event.Date.NoDate)])
job.event_list.append(addEventListObject2)

assert any(isinstance(obj, aapi.WaitForEvents) and any(event.event == 'wait2' for event in obj.events) for obj in job.event_list)
assert any(isinstance(obj, aapi.DeleteEvents) and any(event.event == 'delete' for event in obj.events) for obj in job.event_list)
assert any(isinstance(obj, aapi.AddEvents) and any(event.event == 'add1' for event in obj.events) for obj in job.event_list)

assert json.loads(job.dumps_aapi())

0 comments on commit 33a8983

Please sign in to comment.