Skip to content

Commit

Permalink
Handle runtime runbook,app name changes to DSL file (#124)
Browse files Browse the repository at this point in the history
* Handle runtime runbook,app name changes to DSL file

Handle runtime runbook,app name changes to DSL file

Fixes: c68c04854c6a80ffe088a0ea6f67963d4a674652

* Fix format issue reported by black

Fix format issue reported by black

(cherry picked from commit ae81e0bd73515af9820dcb75a1e2da16a0e37736)
  • Loading branch information
sathnaga authored and abhijeetkaurav1st committed Mar 13, 2022
1 parent e8e466f commit 70c9bc9
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 60 deletions.
108 changes: 74 additions & 34 deletions tests/cli/test_scheduler_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
import uuid
import pytest
import re
import os as os_lib

from distutils.version import LooseVersion as LV
Expand Down Expand Up @@ -44,6 +45,16 @@ def suffix(length=8):
return str(uuid.uuid4())[:length]


def file_replace(filename, match_str, replace_str):
"""Replaces given match_str with replace_str in file with filename"""
with open(filename, "r") as fd:
filecontent = fd.read()

newcontent = re.sub(match_str, replace_str, filecontent)
with open(filename, "w") as fd:
fd.write(newcontent)


@pytest.mark.skipif(
LV(CALM_VERSION) < LV("3.4.0"), reason="Scheduler FEAT is for v3.4.0"
)
Expand All @@ -67,12 +78,16 @@ def test_job_create_app_action(self, dsl_file):
bp_file = os_lib.path.dirname(current_path) + "/scheduler/" + DSL_BP_FILE
client = get_api_client()
bps.create_blueprint_from_dsl(client, bp_file, bp_name, force_create=True)
app_name = "{}_{}".format(bp_name, suffix())
# Launch Blueprint
bps.launch_blueprint_simple(
bp_name, app_name="{}_{}".format(bp_name, suffix()), patch_editables=False
)
bps.launch_blueprint_simple(bp_name, app_name=app_name, patch_editables=False)

jobname = "test_job_scheduler" + suffix()
file_replace(
dsl_file,
r'"{}.*\n'.format(dsl_file_name[: dsl_file_name.find(".")]),
r'"{}"\n'.format(app_name),
)
result = scheduler.create_job_command(dsl_file, jobname, None, False)
assert result.get("resources").get("state") == "ACTIVE"

Expand Down Expand Up @@ -108,17 +123,20 @@ def test_job_create_runbook(self, dsl_file):
# Create runbook
current_path = os_lib.path.dirname(os_lib.path.realpath(__file__))
dsl_file_name_for_runbook = dsl_file[dsl_file.rfind("/") :].replace("/", "")
runbook_name = "{}_{}".format(
dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")], suffix()
)
runbook_name = dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")]
runbook_name_suffixed = "{}_{}".format(runbook_name, suffix())
runbook_file = (
os_lib.path.dirname(current_path) + "/scheduler/" + DSL_RUNBOOK_FILE
)

runbooks.create_runbook_command(
runbook_file, runbook_name, description="", force=True
runbook_file, runbook_name_suffixed, description="", force=True
)
file_replace(
dsl_file,
r'"{}.*\n'.format(runbook_name),
r'"{}"\n'.format(runbook_name_suffixed),
)

result = scheduler.create_job_command(dsl_file, None, None, False)
LOG.info(result)
assert result.get("resources").get("state") == "ACTIVE"
Expand All @@ -135,17 +153,20 @@ def test_invalid_job_create_runbook(self, dsl_file):
# Create runbook
current_path = os_lib.path.dirname(os_lib.path.realpath(__file__))
dsl_file_name_for_runbook = dsl_file[dsl_file.rfind("/") :].replace("/", "")
runbook_name = "{}_{}".format(
dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")], suffix()
)
runbook_name = dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")]
runbook_name_suffixed = "{}_{}".format(runbook_name, suffix())
runbook_file = (
os_lib.path.dirname(current_path) + "/scheduler/" + DSL_RUNBOOK_FILE
)

runbooks.create_runbook_command(
runbook_file, runbook_name, description="", force=True
runbook_file, runbook_name_suffixed, description="", force=True
)
file_replace(
dsl_file,
r'"{}.*\n'.format(runbook_name),
r'"{}"\n'.format(runbook_name_suffixed),
)

result = scheduler.create_job_command(dsl_file, None, None, False)
LOG.info(result)
assert result.get("resources").get("state") == "INACTIVE"
Expand Down Expand Up @@ -193,18 +214,22 @@ def test_job_create_duplicate_name(self, dsl_file):
# Create runbook
current_path = os_lib.path.dirname(os_lib.path.realpath(__file__))
dsl_file_name_for_runbook = dsl_file[dsl_file.rfind("/") :].replace("/", "")
runbook_name = "{}_{}".format(
dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")], suffix()
)
runbook_name = dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")]
runbook_name_suffixed = "{}_{}".format(runbook_name, suffix())
runbook_file = (
os_lib.path.dirname(current_path) + "/scheduler/" + DSL_RUNBOOK_FILE
)

runbooks.create_runbook_command(
runbook_file, runbook_name, description="", force=True
runbook_file, runbook_name_suffixed, description="", force=True
)
jobname = "duplicate_name_check" + suffix()
# Create first job.
file_replace(
dsl_file,
r'"{}.*\n'.format(runbook_name),
r'"{}"\n'.format(runbook_name_suffixed),
)
result = scheduler.create_job_command(dsl_file, jobname, None, False)
LOG.info(result)
assert result.get("resources").get("state") == "ACTIVE"
Expand Down Expand Up @@ -234,17 +259,20 @@ def test_job_name_blank(self, dsl_file):

# Create runbook
dsl_file_name_for_runbook = dsl_file[dsl_file.rfind("/") :].replace("/", "")
runbook_name = "{}_{}".format(
dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")], suffix()
)
runbook_name = dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")]
runbook_name_suffixed = "{}_{}".format(runbook_name, suffix())
runbook_file = (
os_lib.path.dirname(current_path) + "/scheduler/" + DSL_RUNBOOK_FILE
)

runbooks.create_runbook_command(
runbook_file, runbook_name, description="", force=True
runbook_file, runbook_name_suffixed, description="", force=True
)
file_replace(
dsl_file,
r"{}.*\n".format(runbook_name),
'{}"\n'.format(runbook_name_suffixed),
)

result = _create_job_with_custom_name(dsl_file)
assert result.get("resources").get("state") == "INACTIVE"
msg_list = result.get("resources").get("message_list", [])
Expand All @@ -266,18 +294,22 @@ def test_job_list(self, dsl_file):
# Create runbook
current_path = os_lib.path.dirname(os_lib.path.realpath(__file__))
dsl_file_name_for_runbook = dsl_file[dsl_file.rfind("/") :].replace("/", "")
runbook_name = "{}_{}".format(
dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")], suffix()
)
runbook_name = dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")]
runbook_name_suffixed = "{}_{}".format(runbook_name, suffix())
runbook_file = (
os_lib.path.dirname(current_path) + "/scheduler/" + DSL_RUNBOOK_FILE
)

runbooks.create_runbook_command(
runbook_file, runbook_name, description="", force=True
runbook_file, runbook_name_suffixed, description="", force=True
)

job_name = "test_job_list_" + suffix()
file_replace(
dsl_file,
r'"{}.*\n'.format(runbook_name),
r'"{}"\n'.format(runbook_name_suffixed),
)
result = _create_job_with_custom_name(dsl_file, job_name)
LOG.info(result)
assert result.get("resources").get("state") == "ACTIVE"
Expand All @@ -303,17 +335,21 @@ def test_job_describe(self, dsl_file):
# Create runbook
current_path = os_lib.path.dirname(os_lib.path.realpath(__file__))
dsl_file_name_for_runbook = dsl_file[dsl_file.rfind("/") :].replace("/", "")
runbook_name = "{}_{}".format(
dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")], suffix()
)
runbook_name = dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")]
runbook_name_suffixed = "{}_{}".format(runbook_name, suffix())
runbook_file = (
os_lib.path.dirname(current_path) + "/scheduler/" + DSL_RUNBOOK_FILE
)

runbooks.create_runbook_command(
runbook_file, runbook_name, description="", force=True
runbook_file, runbook_name_suffixed, description="", force=True
)
job_name = "test_job_describe_" + suffix()
file_replace(
dsl_file,
r'"{}.*\n'.format(runbook_name),
r'"{}"\n'.format(runbook_name_suffixed),
)
result = _create_job_with_custom_name(dsl_file, job_name)
LOG.info(result)
assert result.get("resources").get("state") == "ACTIVE"
Expand Down Expand Up @@ -345,17 +381,21 @@ def test_job_scheduler(self, dsl_file, dsl_runbook_file):
# Create runbook
current_path = os_lib.path.dirname(os_lib.path.realpath(__file__))
dsl_file_name_for_runbook = dsl_file[dsl_file.rfind("/") :].replace("/", "")
runbook_name = "{}_{}".format(
dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")], suffix()
)
runbook_name = dsl_file_name_for_runbook[: dsl_file_name_for_runbook.find(".")]
runbook_name_suffixed = "{}_{}".format(runbook_name, suffix())
runbook_file = (
os_lib.path.dirname(current_path) + "/scheduler/" + dsl_runbook_file
)

runbooks.create_runbook_command(
runbook_file, runbook_name, description="", force=True
runbook_file, runbook_name_suffixed, description="", force=True
)
jobname = "test_job_scheduler" + suffix()
file_replace(
dsl_file,
r'"{}.*\n'.format(runbook_name),
r'"{}"\n'.format(runbook_name_suffixed),
)
result = scheduler.create_job_command(dsl_file, jobname, None, False)
assert result.get("resources").get("state") == "ACTIVE"

Expand Down
4 changes: 3 additions & 1 deletion tests/scheduler/execution_time_invalid_onetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
start_date_time = "2020-10-08 16:17:15"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "execution_time_invalid_onetime"


class JobOneTimeSpec(Job):
"""One Time Invalid Job for Executing a Runbook with execution date less than current date"""

name = "test_job_" + str(uuid.uuid4())[:8]
schedule_info = JobScheduler.ScheduleInfo.oneTime(start_date_time, time_zone)
executable = JobScheduler.Exec.runbook("execution_time_invalid_onetime", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
5 changes: 4 additions & 1 deletion tests/scheduler/expiry_less_currentdate_recurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
expiry_time = str(expiry_time.strftime("%Y-%m-%dT%H:%M:%SZ"))


RUNBOOK_NAME = "expiry_less_currentdate_recurring"


class JobInvalidRecurringSpec(Job):
"""Recurring Invalid Job for Executing a Runbook with expiry date less than current date"""

name = "test_job_invalid_recurring" + str(uuid.uuid4())[:8]
schedule_info = JobScheduler.ScheduleInfo.recurring(
cron, start_date_time, expiry_date_time, time_zone
)
executable = JobScheduler.Exec.runbook("expiry_less_currentdate_recurring", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
4 changes: 3 additions & 1 deletion tests/scheduler/invalid_cron_recurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
cron = "15 1 32 * *"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "invalid_cron_recurring"


class JobInvalidRecurringSpec(Job):
"""Recurring Job for Executing a Runbook with invalid cron"""
Expand All @@ -16,4 +18,4 @@ class JobInvalidRecurringSpec(Job):
schedule_info = JobScheduler.ScheduleInfo.recurring(
cron, start_date_time, expiry_date_time, time_zone
)
executable = JobScheduler.Exec.runbook("invalid_cron_recurring", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
4 changes: 3 additions & 1 deletion tests/scheduler/invalid_end_date_recurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
cron = "52 15 * * *"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "invalid_end_date_recurring"


class JobInvalidRecurringSpec(Job):
"""Recurring Invalid Job for Executing a Runbook with End date less than Current date"""
Expand All @@ -16,4 +18,4 @@ class JobInvalidRecurringSpec(Job):
schedule_info = JobScheduler.ScheduleInfo.recurring(
cron, start_date_time, expiry_date_time, time_zone
)
executable = JobScheduler.Exec.runbook("invalid_end_date_recurring", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
4 changes: 3 additions & 1 deletion tests/scheduler/invalid_schedule_recurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
cron = "52 15 * * *"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "invalid_schedule_recurring"


class JobInvalidRecurringSpec(Job):
"""Recurring Invalid Job for Executing a Runbook with start date less than current date"""
Expand All @@ -16,4 +18,4 @@ class JobInvalidRecurringSpec(Job):
schedule_info = JobScheduler.ScheduleInfo.recurring(
cron, start_date_time, expiry_date_time, time_zone
)
executable = JobScheduler.Exec.runbook("invalid_schedule_recurring", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
6 changes: 3 additions & 3 deletions tests/scheduler/job_app_action_onetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

time_zone = "Asia/Kolkata"

APP_NAME = "job_app_action_onetime"


class JobOneTimeSpec(Job):
"""One Time Job for Executing an App Action"""

schedule_info = JobScheduler.ScheduleInfo.oneTime(start_date, time_zone)
executable = JobScheduler.Exec.app_action(
"job_app_action_onetime", "sample_profile_action"
)
executable = JobScheduler.Exec.app_action(APP_NAME, "sample_profile_action")
8 changes: 4 additions & 4 deletions tests/scheduler/job_app_action_recc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

time_zone = "Asia/Kolkata"

APP_NAME = "job_app_action_recc"


class JobRecurring(Job):
"""Recurring Job for Executing an App Action"""

name = "job_app_action_recc"
name = "test_job_app_action_recc"
schedule_info = JobScheduler.ScheduleInfo.recurring(
"*/2 * * * *", start_date, expiry_date, time_zone
)
executable = JobScheduler.Exec.app_action(
"job_app_action_recc", "sample_profile_action"
)
executable = JobScheduler.Exec.app_action(APP_NAME, "sample_profile_action")
4 changes: 3 additions & 1 deletion tests/scheduler/job_create_blank_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
start_date_time = "2050-10-08 16:17:15"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "job_create_blank_name"


class JobOneTimeBlankNameSpec(Job):
"""One Time Job for Executing a Runbook"""

schedule_info = JobScheduler.ScheduleInfo.oneTime(start_date_time, time_zone)
executable = JobScheduler.Exec.runbook("job_create_blank_name", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
4 changes: 3 additions & 1 deletion tests/scheduler/job_create_duplicate_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
start_date_time = "2050-10-08 16:17:15"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "job_create_duplicate_name"


class JobOneTimeDuplicateNameSpec(Job):
"""One Time Job for Executing a Runbook"""

schedule_info = JobScheduler.ScheduleInfo.oneTime(start_date_time, time_zone)
executable = JobScheduler.Exec.runbook("job_create_duplicate_name", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
4 changes: 3 additions & 1 deletion tests/scheduler/job_create_one_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
start_date_time = "2050-10-08 16:17:15"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "job_create_one_time"


class JobOneTimeSpec(Job):
"""One Time Job for Executing a Runbook"""

name = "test_job" + str(uuid.uuid4())[:8]
schedule_info = JobScheduler.ScheduleInfo.oneTime(start_date_time, time_zone)
executable = JobScheduler.Exec.runbook("job_create_one_time", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
4 changes: 3 additions & 1 deletion tests/scheduler/job_create_recurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
cron = "52 15 * * *"
time_zone = "America/Jamaica"

RUNBOOK_NAME = "job_create_recurring"


class JobRecurringSpec(Job):
"""Recurring Job for Executing a Runbook"""
Expand All @@ -15,4 +17,4 @@ class JobRecurringSpec(Job):
schedule_info = JobScheduler.ScheduleInfo.recurring(
cron, start_date_time, expiry_date_time, time_zone
)
executable = JobScheduler.Exec.runbook("job_create_recurring", False)
executable = JobScheduler.Exec.runbook(RUNBOOK_NAME, False)
Loading

0 comments on commit 70c9bc9

Please sign in to comment.