diff --git a/tests/3_2_0/commands/test_bp_env_commands.py b/tests/3_2_0/commands/test_bp_env_commands.py index 0107896aa..7430e02ec 100644 --- a/tests/3_2_0/commands/test_bp_env_commands.py +++ b/tests/3_2_0/commands/test_bp_env_commands.py @@ -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")) @@ -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""" @@ -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""" @@ -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 diff --git a/tests/3_2_0/commands/test_marketplace_bp_env_commands.py b/tests/3_2_0/commands/test_marketplace_bp_env_commands.py index 45c6c3ec3..f14b6501f 100644 --- a/tests/3_2_0/commands/test_marketplace_bp_env_commands.py +++ b/tests/3_2_0/commands/test_marketplace_bp_env_commands.py @@ -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")) @@ -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""" @@ -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""" @@ -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 diff --git a/tests/brownfield/separate_file_example/test_brownfield_app.py b/tests/brownfield/separate_file_example/test_brownfield_app.py index d28378813..87138fb65 100644 --- a/tests/brownfield/separate_file_example/test_brownfield_app.py +++ b/tests/brownfield/separate_file_example/test_brownfield_app.py @@ -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] @@ -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""" @@ -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() @@ -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 @@ -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") @@ -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) @@ -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") @@ -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) diff --git a/tests/cli/runtime_helpers/ahv/test_ahv_substrate_editables.py b/tests/cli/runtime_helpers/ahv/test_ahv_substrate_editables.py index bd16afa5b..142e816b5 100644 --- a/tests/cli/runtime_helpers/ahv/test_ahv_substrate_editables.py +++ b/tests/cli/runtime_helpers/ahv/test_ahv_substrate_editables.py @@ -6,20 +6,15 @@ 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"] @@ -27,6 +22,8 @@ class TestEditables: + app_helper = ApplicationHelper() + def setup_method(self): """Method to instantiate to created_bp_list and created_app_list""" @@ -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" @@ -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) @@ -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( @@ -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( @@ -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 diff --git a/tests/cli/test_app_commands.py b/tests/cli/test_app_commands.py index bc32f84ff..b5bb70a54 100644 --- a/tests/cli/test_app_commands.py +++ b/tests/cli/test_app_commands.py @@ -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 = ( @@ -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() @@ -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 @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/tests/cli/test_marketplace_bp_commands.py b/tests/cli/test_marketplace_bp_commands.py index c2f5692b7..31486695e 100644 --- a/tests/cli/test_marketplace_bp_commands.py +++ b/tests/cli/test_marketplace_bp_commands.py @@ -16,18 +16,16 @@ ) from calm.dsl.cli.utils import get_states_filter from calm.dsl.builtins import read_local_file -from calm.dsl.cli.constants import APPLICATION, MARKETPLACE_ITEM +from calm.dsl.cli.constants import MARKETPLACE_ITEM from calm.dsl.log import get_logging_handle +from tests.utils import Application as ApplicationHelper LOG = get_logging_handle(__name__) + APP_ICON_IMAGE_PATH = "tests/cli/images/test_app_icon.jpg" DSL_BP_FILEPATH = "tests/existing_vm_example/test_existing_vm_bp.py" DSL_BP_EDITABLE_PARAMS = "tests/existing_vm_example/existing_vm_bp_editable_params.py" -NON_BUSY_APP_STATES = [ - APPLICATION.STATES.STOPPED, - APPLICATION.STATES.RUNNING, - APPLICATION.STATES.ERROR, -] + APP_STATES = [ MARKETPLACE_ITEM.STATES.PENDING, MARKETPLACE_ITEM.STATES.ACCEPTED, @@ -46,6 +44,8 @@ class TestMarketplaceBPCommands: + app_helper = ApplicationHelper() + def setup_method(self): """Method to instantiate to created_bp_list and created_app_list""" @@ -1928,21 +1928,10 @@ def test_all_projects_flag_on_publishing_marketplace_bp(self): pytest.fail("Deletion of marketplace blueprint failed") LOG.info("Success") - 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 _test_app_delete(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]) if result.exit_code: diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 000000000..b01dce911 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,41 @@ +import json +import time + +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 + +LOG = get_logging_handle(__name__) + + +class Application: + NON_BUSY_APP_STATES = [ + APPLICATION.STATES.STOPPED, + APPLICATION.STATES.RUNNING, + APPLICATION.STATES.ERROR, + ] + + def _wait_for_non_busy_state(self, name, timeout=300): + 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(self.NON_BUSY_APP_STATES)) + + is_terminal = True + poll_interval = 15 + while app_data["status"]["state"] not in self.NON_BUSY_APP_STATES: + time.sleep(poll_interval) + 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 timeout <= 0: + LOG.error("Failed to reach terminal state in 100 seconds") + LOG.debug("App: {}".format(app_data)) + is_terminal = False + break + timeout -= poll_interval + + return is_terminal