Skip to content

Commit

Permalink
Using common helper for waiting for non-busy state (#71)
Browse files Browse the repository at this point in the history
* Using common helper for waiting for non-busy state

* Minor fix
  • Loading branch information
abhijeetkaurav1st committed Oct 17, 2021
1 parent f68e3eb commit fef58ac
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 128 deletions.
22 changes: 4 additions & 18 deletions tests/3_2_0/commands/test_bp_env_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
from calm.dsl.config import get_context
from calm.dsl.log import get_logging_handle
from calm.dsl.store import Version
from calm.dsl.cli.constants import APPLICATION
from tests.utils import Application as ApplicationHelper

LOG = get_logging_handle(__name__)

DSL_BP_FILEPATH = "tests/3_2_0/blueprints/test_blueprint_having_ahv_helper/blueprint.py"
NON_BUSY_APP_STATES = [
APPLICATION.STATES.STOPPED,
APPLICATION.STATES.RUNNING,
APPLICATION.STATES.ERROR,
]

# project constants
DSL_CONFIG = json.loads(read_local_file(".tests/config.json"))
Expand All @@ -38,6 +33,8 @@
reason="Tests are for env changes introduced in 3.2.0",
)
class TestBpCommands:
app_helper = ApplicationHelper()

def setup_method(self):
"""Method to instantiate to created_bp_list and reset context"""

Expand All @@ -51,17 +48,6 @@ def setup_method(self):
self.created_bp_list = []
self.created_app_list = []

def _wait_for_non_busy_state(self, app_name):

runner = CliRunner()
non_busy_statuses = [
"Status: {}".format(state) for state in NON_BUSY_APP_STATES
]
result = runner.invoke(cli, ["describe", "app", app_name])
while not any([state_str in result.output for state_str in non_busy_statuses]):
time.sleep(5)
result = runner.invoke(cli, ["describe", "app", app_name])

def teardown_method(self):
"""Method to delete creates bps and apps during tests"""

Expand All @@ -80,7 +66,7 @@ def teardown_method(self):

for app_name in self.created_app_list:
LOG.info("Deleting App {}".format(app_name))
self._wait_for_non_busy_state(app_name=app_name)
self.app_helper._wait_for_non_busy_state(app_name)
runner = CliRunner()
result = runner.invoke(cli, ["delete", "app", app_name])
assert result.exit_code == 0
Expand Down
22 changes: 4 additions & 18 deletions tests/3_2_0/commands/test_marketplace_bp_env_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
from calm.dsl.config import get_context
from calm.dsl.store import Version
from calm.dsl.log import get_logging_handle
from calm.dsl.cli.constants import APPLICATION
from tests.utils import Application as ApplicationHelper

LOG = get_logging_handle(__name__)

DSL_BP_FILEPATH = "tests/3_2_0/blueprints/test_blueprint_having_ahv_helper/blueprint.py"
NON_BUSY_APP_STATES = [
APPLICATION.STATES.STOPPED,
APPLICATION.STATES.RUNNING,
APPLICATION.STATES.ERROR,
]

# project constants
DSL_CONFIG = json.loads(read_local_file(".tests/config.json"))
Expand All @@ -39,6 +34,8 @@
reason="Tests are for env changes introduced in 3.2.0",
)
class TestBpCommands:
app_helper = ApplicationHelper()

def setup_method(self):
"""Method to instantiate to created_bp_list and reset context"""

Expand All @@ -52,17 +49,6 @@ def setup_method(self):
self.created_bp_list = []
self.created_app_list = []

def _wait_for_non_busy_state(self, app_name):

runner = CliRunner()
non_busy_statuses = [
"Status: {}".format(state) for state in NON_BUSY_APP_STATES
]
result = runner.invoke(cli, ["describe", "app", app_name])
while not any([state_str in result.output for state_str in non_busy_statuses]):
time.sleep(5)
result = runner.invoke(cli, ["describe", "app", app_name])

def teardown_method(self):
"""Method to delete creates bps and apps during tests"""

Expand All @@ -81,7 +67,7 @@ def teardown_method(self):

for app_name in self.created_app_list:
LOG.info("Deleting App {}".format(app_name))
self._wait_for_non_busy_state(app_name=app_name)
self.app_helper._wait_for_non_busy_state(app_name)
runner = CliRunner()
result = runner.invoke(cli, ["delete", "app", app_name])
assert result.exit_code == 0
Expand Down
29 changes: 8 additions & 21 deletions tests/brownfield/separate_file_example/test_brownfield_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
from calm.dsl.cli.constants import APPLICATION
from calm.dsl.tools import make_file_dir
from calm.dsl.log import get_logging_handle
from tests.utils import Application as ApplicationHelper

LOG = get_logging_handle(__name__)

DSL_BP_FILEPATH = "tests/brownfield/separate_file_example/blueprint.py"
DSL_BP_BD_FILEPATH = "tests/brownfield/separate_file_example/brownfield.py"
LOCAL_VM_IP_PATH = "tests/brownfield/separate_file_example/.local/vm_ip"
NON_BUSY_APP_STATES = [
APPLICATION.STATES.STOPPED,
APPLICATION.STATES.RUNNING,
APPLICATION.STATES.ERROR,
]

NON_BUSY_APP_DELETE_STATES = [APPLICATION.STATES.ERROR, APPLICATION.STATES.DELETED]

Expand All @@ -32,6 +28,8 @@

@pytest.mark.slow
class TestBrownFieldCommands:
app_helper = ApplicationHelper()

def setup_method(self):
"""Method to instantiate to created_bp_list and created_app_list"""

Expand All @@ -55,17 +53,6 @@ def teardown_method(self):
self.created_app_list = []
self.created_bp_list = []

def _wait_for_non_busy_state(self, app_name):

runner = CliRunner()
non_busy_statuses = [
"Status: {}".format(state) for state in NON_BUSY_APP_STATES
]
result = runner.invoke(cli, ["describe", "app", app_name])
while not any([state_str in result.output for state_str in non_busy_statuses]):
time.sleep(5)
result = runner.invoke(cli, ["describe", "app", app_name])

def _wait_for_app_delete_busy_state(self, app_name):

runner = CliRunner()
Expand All @@ -80,7 +67,7 @@ def _wait_for_app_delete_busy_state(self, app_name):
def _delete_app(self, app_name):

runner = CliRunner()
self._wait_for_non_busy_state(app_name)
self.app_helper._wait_for_non_busy_state(app_name)
LOG.info("Deleting App {} ".format(app_name))
result = runner.invoke(cli, ["delete", "app", app_name])
assert result.exit_code == 0
Expand Down Expand Up @@ -141,7 +128,7 @@ def test_app_vm_in_brownfield_bp(self):
pytest.fail("Creation of app {} failed".format(app_name))

# Wait for app creation completion
self._wait_for_non_busy_state(app_name)
self.app_helper._wait_for_non_busy_state(app_name)
LOG.info("Application {} created successfully".format(app_name))

LOG.info("Extracting vm ip from the app")
Expand Down Expand Up @@ -205,7 +192,7 @@ def test_app_vm_in_brownfield_bp(self):
)
pytest.fail("Brownfield App creation failed")

self._wait_for_non_busy_state(app_name)
self.app_helper._wait_for_non_busy_state(app_name)
LOG.info("Brownfield App {} created successfully".format(app_name))
self.created_app_list.append(app_name)

Expand Down Expand Up @@ -283,7 +270,7 @@ def test_brownfield_app_from_existing_bp(self):
pytest.fail("Creation of app {} failed".format(app_name))

# Wait for app creation completion
self._wait_for_non_busy_state(app_name)
self.app_helper._wait_for_non_busy_state(app_name)
LOG.info("Application {} created successfully".format(app_name))

LOG.info("Extracting vm ip from the app")
Expand Down Expand Up @@ -350,7 +337,7 @@ def test_brownfield_app_from_existing_bp(self):
)
pytest.fail("Brownfield App creation failed")

self._wait_for_non_busy_state(app_name_2)
self.app_helper._wait_for_non_busy_state(app_name_2)
LOG.info("Brownfield App {} created successfully".format(app_name_2))
self.created_app_list.append(app_name_2)

Expand Down
35 changes: 8 additions & 27 deletions tests/cli/runtime_helpers/ahv/test_ahv_substrate_editables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@
import traceback

from calm.dsl.cli import main as cli
from calm.dsl.cli.constants import APPLICATION
from calm.dsl.builtins import read_local_file
from calm.dsl.log import get_logging_handle
from tests.utils import Application as ApplicationHelper

LOG = get_logging_handle(__name__)
BP_FILE_PATH = "tests/cli/runtime_helpers/ahv/blueprint.py"
LAUNCH_PARAMS = "tests/cli/runtime_helpers/ahv/editable_params.py"
ACTION_LAUNCH_PARAMS = "tests/cli/runtime_helpers/ahv/profile_action_editable_params.py"
PROFILE_ACTION_NAME = "test_profile_action"
NON_BUSY_APP_STATES = [
APPLICATION.STATES.STOPPED,
APPLICATION.STATES.RUNNING,
APPLICATION.STATES.ERROR,
]

DSL_CONFIG = json.loads(read_local_file(".tests/config.json"))
CENTOS_HM = DSL_CONFIG["AHV"]["IMAGES"]["DISK"]["CENTOS_HADOOP_MASTER"]
NETWORK1 = DSL_CONFIG["AHV"]["NETWORK"]["VLAN1211"] # TODO change network constants


class TestEditables:
app_helper = ApplicationHelper()

def setup_method(self):
"""Method to instantiate to created_bp_list and created_app_list"""

Expand All @@ -45,7 +42,8 @@ def teardown_method(self):
for app_name in self.created_app_list:
LOG.info("Deleting app {}".format(app_name))
runner = CliRunner()
self._wait_for_non_busy_state(app_name)

self.app_helper._wait_for_non_busy_state(app_name)
result = runner.invoke(cli, "delete app {}".format(app_name))
LOG.debug(result.output)
assert result.exit_code == 0, "Error occured in application deletion"
Expand Down Expand Up @@ -180,7 +178,7 @@ def _test_run_action_on_app(self, app_name):

runner = CliRunner()

self._wait_for_non_busy_state(app_name)
self.app_helper._wait_for_non_busy_state(app_name)

# Run profile action
command = "run action {} -a {} ".format(PROFILE_ACTION_NAME, app_name)
Expand Down Expand Up @@ -209,7 +207,7 @@ def _test_run_action_on_app(self, app_name):
)
LOG.info("Success")

self._wait_for_non_busy_state(app_name)
self.app_helper._wait_for_non_busy_state(app_name)

# Run using --ignore_runtime_variables flag
command = "run action {} -a {} --ignore_runtime_variables".format(
Expand Down Expand Up @@ -237,7 +235,7 @@ def _test_run_action_on_app(self, app_name):
)
LOG.info("Success")

self._wait_for_non_busy_state(app_name)
self.app_helper._wait_for_non_busy_state(app_name)

# Run using --launch_params flag
command = "run action {} -a {} --runtime_params {}".format(
Expand Down Expand Up @@ -265,20 +263,3 @@ def _test_run_action_on_app(self, app_name):
)

LOG.info("Success")

def _wait_for_non_busy_state(self, name):
runner = CliRunner()
result = runner.invoke(cli, ["describe", "app", name, "--out=json"])
app_data = json.loads(result.output)
LOG.info("App State: {}".format(app_data["status"]["state"]))
LOG.debug("App Terminal states: {}".format(NON_BUSY_APP_STATES))
cnt = 0
while app_data["status"]["state"] not in NON_BUSY_APP_STATES:
time.sleep(5)
result = runner.invoke(cli, ["describe", "app", name, "--out=json"])
app_data = json.loads(result.output)
LOG.info("App State: {}".format(app_data["status"]["state"]))
if cnt > 20:
LOG.error("Failed to reach terminal state in 100 seconds")
sys.exit(-1)
cnt += 1
33 changes: 7 additions & 26 deletions tests/cli/test_app_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
from click.testing import CliRunner

from calm.dsl.cli import main as cli
from calm.dsl.cli.constants import APPLICATION
from calm.dsl.log import get_logging_handle
from tests.utils import Application as ApplicationHelper

LOG = get_logging_handle(__name__)
NON_BUSY_APP_STATES = [
APPLICATION.STATES.STOPPED,
APPLICATION.STATES.RUNNING,
APPLICATION.STATES.ERROR,
]

DSL_BP_FILEPATH = "tests/blueprint_example/test_blueprint_example.py"
DSL_LAUNCH_PARAM_FILEPATH = (
Expand All @@ -26,7 +21,7 @@

@pytest.mark.slow
class TestAppCommands:
non_busy_statuses = ["Status: {}".format(state) for state in NON_BUSY_APP_STATES]
app_helper = ApplicationHelper()

def test_apps_list(self):
runner = CliRunner()
Expand Down Expand Up @@ -197,23 +192,9 @@ def _test_describe_app(self):
)
LOG.info("Success")

def _wait_for_non_busy_state(self):
runner = CliRunner()
result = runner.invoke(cli, ["describe", "app", self.created_app_name])
cnt = 0
while not any(
[state_str in result.output for state_str in self.non_busy_statuses]
):
time.sleep(5)
result = runner.invoke(cli, ["describe", "app", self.created_app_name])
if cnt > 20:
LOG.error("Failed to reach terminal state in 100 seconds")
sys.exit(-1)
cnt += 1

def _test_run_custom_action(self):
runner = CliRunner()
self._wait_for_non_busy_state()
self.app_helper._wait_for_non_busy_state(self.created_app_name)
LOG.info(
"Running {} action on app {}".format(
CUSTOM_ACTION_NAME, self.created_app_name
Expand Down Expand Up @@ -245,7 +226,7 @@ def _test_run_custom_action(self):
def _test_restart_app(self):

runner = CliRunner()
self._wait_for_non_busy_state()
self.app_helper._wait_for_non_busy_state(self.created_app_name)
LOG.info("Restarting app {}".format(self.created_app_name))
result = runner.invoke(cli, ["restart", "app", self.created_app_name])
if result.exit_code:
Expand All @@ -265,7 +246,7 @@ def _test_restart_app(self):
def _test_stop_app(self):

runner = CliRunner()
self._wait_for_non_busy_state()
self.app_helper._wait_for_non_busy_state(self.created_app_name)
LOG.info("Stopping app {}".format(self.created_app_name))
result = runner.invoke(cli, ["stop", "app", self.created_app_name])
if result.exit_code:
Expand All @@ -285,7 +266,7 @@ def _test_stop_app(self):
def _test_start_app(self):

runner = CliRunner()
self._wait_for_non_busy_state()
self.app_helper._wait_for_non_busy_state(self.created_app_name)
LOG.info("Starting app {}".format(self.created_app_name))
result = runner.invoke(cli, ["start", "app", self.created_app_name])
if result.exit_code:
Expand All @@ -304,7 +285,7 @@ def _test_start_app(self):

def _test_app_delete(self):
runner = CliRunner()
self._wait_for_non_busy_state()
self.app_helper._wait_for_non_busy_state(self.created_app_name)
LOG.info("Deleting App {} ".format(self.created_app_name))
result = runner.invoke(cli, ["delete", "app", self.created_app_name])
if result.exit_code:
Expand Down
Loading

0 comments on commit fef58ac

Please sign in to comment.