From 44ea3ccb8aa28eb1ece3b0df9d9578ba025ddfde Mon Sep 17 00:00:00 2001 From: anantsharma13352 Date: Mon, 9 Oct 2023 10:42:14 +0530 Subject: [PATCH] Bug/m escript3 task fixes (#292) DSL Fixes for : - Tunnel support for escript(python3) tasks. - depth variable fix for filename param for escript all task types Bugs: https://jira.nutanix.com/browse/CALM-40389, https://jira.nutanix.com/browse/CALM-40387 (cherry picked from commit 41c26c4b41441a61c84409047462f5e3a62e815e) --- calm/dsl/builtins/models/task.py | 22 +- tests/unit/jsons/escript_all_tasks.json | 353 +++++++++++++++++++ tests/unit/scripts/escript_decision_false.py | 1 + tests/unit/scripts/escript_decision_true.py | 1 + tests/unit/scripts/escript_exec.py | 1 + tests/unit/scripts/escript_setvariable.py | 1 + tests/unit/test_escript_all_tasks.py | 89 +++++ 7 files changed, 457 insertions(+), 11 deletions(-) create mode 100644 tests/unit/jsons/escript_all_tasks.json create mode 100644 tests/unit/scripts/escript_decision_false.py create mode 100644 tests/unit/scripts/escript_decision_true.py create mode 100644 tests/unit/scripts/escript_exec.py create mode 100644 tests/unit/scripts/escript_setvariable.py create mode 100644 tests/unit/test_escript_all_tasks.py diff --git a/calm/dsl/builtins/models/task.py b/calm/dsl/builtins/models/task.py index effec63d..abb4e47f 100644 --- a/calm/dsl/builtins/models/task.py +++ b/calm/dsl/builtins/models/task.py @@ -214,8 +214,8 @@ def _exec_create( "Only one of script or filename should be given for exec task " + (name or "") ) - if script_type != "static" and tunnel is not None: - raise ValueError("Tunnel is supported only for Escript script type") + if script_type not in ["static", "static_py3"] and tunnel is not None: + raise ValueError("Tunnel is supported only for Escript script types") if filename is not None: file_path = os.path.join( @@ -276,8 +276,8 @@ def _decision_create( "One of script or filename is required for decision task " + (name or "") ) - if script_type != "static" and tunnel is not None: - raise ValueError("Tunnel is support only for Escript script type") + if script_type not in ["static", "static_py3"] and tunnel is not None: + raise ValueError("Tunnel is support only for Escript script types") params = { "name": name, @@ -453,7 +453,7 @@ def exec_task_escript( name=name, target=target, target_endpoint=None, - depth=depth + 1, + depth=depth, tunnel=tunnel, **kwargs, ) @@ -475,7 +475,7 @@ def exec_task_escript_py3( name=name, target=target, target_endpoint=None, - depth=depth + 1, + depth=depth, tunnel=tunnel, **kwargs, ) @@ -644,7 +644,7 @@ def decision_task_escript( filename=filename, name=name, target=target, - depth=depth + 1, + depth=depth, tunnel=tunnel, **kwargs, ) @@ -678,7 +678,7 @@ def decision_task_escript_py3( filename=filename, name=name, target=target, - depth=depth + 1, + depth=depth, tunnel=tunnel, **kwargs, ) @@ -860,7 +860,7 @@ def __new__( filename=filename, name=name, target=target, - depth=depth, + depth=depth + 1, tunnel=tunnel, **kwargs, ) @@ -884,7 +884,7 @@ def __new__( filename=filename, name=name, target=target, - depth=depth, + depth=depth + 1, tunnel=tunnel, **kwargs, ) @@ -910,7 +910,7 @@ def __new__( name=name, target=target, variables=variables, - depth=depth, + depth=depth + 1, tunnel=tunnel, **kwargs, ) diff --git a/tests/unit/jsons/escript_all_tasks.json b/tests/unit/jsons/escript_all_tasks.json new file mode 100644 index 00000000..49045107 --- /dev/null +++ b/tests/unit/jsons/escript_all_tasks.json @@ -0,0 +1,353 @@ +{ + "endpoint_definition_list": [], + "credential_definition_list": [], + "client_attrs": {}, + "runbook": { + "name": "DslAllEscriptTasks_runbook", + "description": "", + "main_task_local_reference": { + "kind": "app_task", + "name": "DslAllEscriptTasks_dag" + }, + "task_definition_list": [ + { + "name": "DslAllEscriptTasks_dag", + "description": "", + "type": "DAG", + "attrs": { + "edges": [ + { + "from_task_reference": { + "kind": "app_task", + "name": "escript_decision" + }, + "to_task_reference": { + "kind": "app_task", + "name": "escript2_decision" + } + }, + { + "from_task_reference": { + "kind": "app_task", + "name": "escript2_decision" + }, + "to_task_reference": { + "kind": "app_task", + "name": "escript3_decision" + } + } + ] + }, + "child_tasks_local_reference_list": [ + { + "kind": "app_task", + "name": "escript_decision" + }, + { + "kind": "app_task", + "name": "escript2_decision" + }, + { + "kind": "app_task", + "name": "escript3_decision" + } + ], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript_decision_success_meta_task", + "description": "", + "type": "META", + "attrs": {}, + "child_tasks_local_reference_list": [ + { + "kind": "app_task", + "name": "escript_exec" + }, + { + "kind": "app_task", + "name": "escript_setvar" + } + ], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript_exec", + "description": "", + "type": "EXEC", + "attrs": { + "script_type": "static", + "script": "print(\"just printing...\")\n" + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript_setvar", + "description": "", + "type": "SET_VARIABLE", + "attrs": { + "script_type": "static", + "script": "print(\"var1=abc\")\n", + "eval_variables": [ + "var1" + ] + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript_decision_failure_meta_task", + "description": "", + "type": "META", + "attrs": {}, + "child_tasks_local_reference_list": [ + { + "kind": "app_task", + "name": "escript_exec_print" + } + ], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript_exec_print", + "description": "", + "type": "EXEC", + "attrs": { + "script_type": "static", + "script": "print \"Decision else part\"" + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript_decision", + "description": "", + "type": "DECISION", + "attrs": { + "script_type": "static", + "script": "exit(0)\n", + "success_child_reference": { + "kind": "app_task", + "name": "escript_decision_success_meta_task" + }, + "failure_child_reference": { + "kind": "app_task", + "name": "escript_decision_failure_meta_task" + } + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript2_decision_success_meta_task", + "description": "", + "type": "META", + "attrs": {}, + "child_tasks_local_reference_list": [ + { + "kind": "app_task", + "name": "escript3_exec_print" + } + ], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript3_exec_print", + "description": "", + "type": "EXEC", + "attrs": { + "script_type": "static", + "script": "print \"Decision if part\"" + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript2_decision_failure_meta_task", + "description": "", + "type": "META", + "attrs": {}, + "child_tasks_local_reference_list": [ + { + "kind": "app_task", + "name": "escript2_exec" + }, + { + "kind": "app_task", + "name": "escript2_setvar" + } + ], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript2_exec", + "description": "", + "type": "EXEC", + "attrs": { + "script_type": "static", + "script": "print(\"just printing...\")\n" + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript2_setvar", + "description": "", + "type": "SET_VARIABLE", + "attrs": { + "script_type": "static", + "script": "print(\"var1=abc\")\n", + "eval_variables": [ + "var1" + ] + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript2_decision", + "description": "", + "type": "DECISION", + "attrs": { + "script_type": "static", + "script": "exit(1)\n", + "success_child_reference": { + "kind": "app_task", + "name": "escript2_decision_success_meta_task" + }, + "failure_child_reference": { + "kind": "app_task", + "name": "escript2_decision_failure_meta_task" + } + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript3_decision_success_meta_task", + "description": "", + "type": "META", + "attrs": {}, + "child_tasks_local_reference_list": [ + { + "kind": "app_task", + "name": "escript3_exec" + }, + { + "kind": "app_task", + "name": "escript3_setvar" + } + ], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript3_exec", + "description": "", + "type": "EXEC", + "attrs": { + "script_type": "static_py3", + "script": "print(\"just printing...\")\n" + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript3_setvar", + "description": "", + "type": "SET_VARIABLE", + "attrs": { + "script_type": "static_py3", + "script": "print(\"var1=abc\")\n", + "eval_variables": [ + "var1" + ] + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript3_decision_failure_meta_task", + "description": "", + "type": "META", + "attrs": {}, + "child_tasks_local_reference_list": [ + { + "kind": "app_task", + "name": "escript3_exec_print" + } + ], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript3_exec_print", + "description": "", + "type": "EXEC", + "attrs": { + "script_type": "static_py3", + "script": "print(\"Decision else part\")" + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + }, + { + "name": "escript3_decision", + "description": "", + "type": "DECISION", + "attrs": { + "script_type": "static_py3", + "script": "exit(0)\n", + "success_child_reference": { + "kind": "app_task", + "name": "escript3_decision_success_meta_task" + }, + "failure_child_reference": { + "kind": "app_task", + "name": "escript3_decision_failure_meta_task" + } + }, + "child_tasks_local_reference_list": [], + "variable_list": [], + "retries": "", + "timeout_secs": "" + } + ], + "variable_list": [] + } +} diff --git a/tests/unit/scripts/escript_decision_false.py b/tests/unit/scripts/escript_decision_false.py new file mode 100644 index 00000000..63a3a698 --- /dev/null +++ b/tests/unit/scripts/escript_decision_false.py @@ -0,0 +1 @@ +exit(1) diff --git a/tests/unit/scripts/escript_decision_true.py b/tests/unit/scripts/escript_decision_true.py new file mode 100644 index 00000000..ba71a0cc --- /dev/null +++ b/tests/unit/scripts/escript_decision_true.py @@ -0,0 +1 @@ +exit(0) diff --git a/tests/unit/scripts/escript_exec.py b/tests/unit/scripts/escript_exec.py new file mode 100644 index 00000000..20591c16 --- /dev/null +++ b/tests/unit/scripts/escript_exec.py @@ -0,0 +1 @@ +print("just printing...") diff --git a/tests/unit/scripts/escript_setvariable.py b/tests/unit/scripts/escript_setvariable.py new file mode 100644 index 00000000..0a0f9dd9 --- /dev/null +++ b/tests/unit/scripts/escript_setvariable.py @@ -0,0 +1 @@ +print("var1=abc") diff --git a/tests/unit/test_escript_all_tasks.py b/tests/unit/test_escript_all_tasks.py new file mode 100644 index 00000000..b6967b79 --- /dev/null +++ b/tests/unit/test_escript_all_tasks.py @@ -0,0 +1,89 @@ +import uuid +import os +import pytest + +from calm.dsl.runbooks import * +from calm.dsl.runbooks import ( + RunbookTask as CalmTask, +) +from calm.dsl.builtins import CalmTask as CalmVarTask, Metadata + + +@runbook +def DslAllEscriptTasks(endpoints=[], default=False): + "Runbook example with All Escript Type Tasks" + + with CalmTask.Decision.escript( + name="escript_decision", + filename=os.path.join("scripts", "escript_decision_true.py"), + ) as d: + if d.ok: + CalmTask.Exec.escript( + name="escript_exec", filename=os.path.join("scripts", "escript_exec.py") + ) + CalmTask.SetVariable.escript( + name="escript_setvar", + filename=os.path.join("scripts", "escript_setvariable.py"), + variables=["var1"], + ) + else: + CalmTask.Exec.escript( + name="escript_exec_print", script='''print "Decision else part"''' + ) + + with CalmTask.Decision.escript.py2( + name="escript2_decision", + filename=os.path.join("scripts", "escript_decision_false.py"), + ) as d: + if d.ok: + CalmTask.Exec.escript.py2( + name="escript3_exec_print", script='''print "Decision if part"''' + ) + else: + CalmTask.Exec.escript.py2( + name="escript2_exec", + filename=os.path.join("scripts", "escript_exec.py"), + ) + CalmTask.SetVariable.escript.py2( + name="escript2_setvar", + filename=os.path.join("scripts", "escript_setvariable.py"), + variables=["var1"], + ) + + with CalmTask.Decision.escript.py3( + name="escript3_decision", + filename=os.path.join("scripts", "escript_decision_true.py"), + ) as d: + if d.ok: + CalmTask.Exec.escript.py3( + name="escript3_exec", + filename=os.path.join("scripts", "escript_exec.py"), + ) + CalmTask.SetVariable.escript.py3( + name="escript3_setvar", + filename=os.path.join("scripts", "escript_setvariable.py"), + variables=["var1"], + ) + else: + CalmTask.Exec.escript.py3( + name="escript3_exec_print", script="""print("Decision else part")""" + ) + + +def _test_compare_compile_result(Runbook, json_file): + """compares the runbook compilation and known output""" + + print("JSON compilation test for {}".format(Runbook.action_name)) + dir_path = os.path.dirname(os.path.realpath(__file__)) + file_path = os.path.join(dir_path, json_file) + + generated_json = runbook_json(Runbook) + known_json = open(file_path).read() + assert generated_json == known_json + print("JSON compilation successful for {}".format(Runbook.action_name)) + + +@pytest.mark.runbook +@pytest.mark.escript +def test_all_escript_type_tasks(): + _test_compare_compile_result(DslAllEscriptTasks, "./jsons/escript_all_tasks.json")