diff --git a/scripts/qesap/test/conftest.py b/scripts/qesap/test/conftest.py index a448f653..dde74dd8 100644 --- a/scripts/qesap/test/conftest.py +++ b/scripts/qesap/test/conftest.py @@ -117,6 +117,7 @@ def _callback(terraform_section, provider='pinocchio', apiver=3): return _callback + @pytest.fixture def provider_dir(tmpdir): def _callback(provider): @@ -414,4 +415,4 @@ def _callback(hana_media_file, account='ACCOUNT', container='CONTAINER', token=N return (True, '') - return _callback \ No newline at end of file + return _callback diff --git a/scripts/qesap/test/unit/test_qesap_ansible.py b/scripts/qesap/test/unit/test_qesap_ansible.py index 47e74904..63c78224 100644 --- a/scripts/qesap/test/unit/test_qesap_ansible.py +++ b/scripts/qesap/test/unit/test_qesap_ansible.py @@ -10,7 +10,8 @@ ANSIBLE_EXE = '/bin/ansible' ANSIBLEPB_EXE = '/paese/della/cuccagna/ansible-playbook' -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) + +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_create(run, _, base_args, tmpdir, create_inventory, create_playbooks, ansible_config, mock_call_ansibleplaybook): """ @@ -42,7 +43,7 @@ def test_ansible_create(run, _, base_args, tmpdir, create_inventory, create_play run.assert_has_calls(calls) -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_verbose(run, _, base_args, tmpdir, create_inventory, create_playbooks, ansible_config, mock_call_ansibleplaybook): """ @@ -169,7 +170,7 @@ def test_ansible_missing_playbook(run, tmpdir, base_args, create_inventory, crea run.assert_not_called() -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run", side_effect=[(0, []), (1, [])]) def test_ansible_stop(run, _, tmpdir, base_args, create_inventory, create_playbooks, ansible_config, mock_call_ansibleplaybook): """ @@ -199,7 +200,7 @@ def test_ansible_stop(run, _, tmpdir, base_args, create_inventory, create_playbo run.assert_has_calls(calls) -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_destroy(run, _, base_args, tmpdir, create_inventory, create_playbooks, ansible_config, mock_call_ansibleplaybook): """ @@ -233,7 +234,7 @@ def test_ansible_destroy(run, _, base_args, tmpdir, create_inventory, create_pla run.assert_has_calls(calls) -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_e_reg(run, _, base_args, tmpdir, create_inventory, create_playbooks, mock_call_ansibleplaybook): """ @@ -279,7 +280,7 @@ def test_ansible_e_reg(run, _, base_args, tmpdir, create_inventory, create_playb run.assert_has_calls(calls) -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_e_sapconf(run, _, base_args, tmpdir, create_inventory, create_playbooks, mock_call_ansibleplaybook): """ @@ -323,7 +324,7 @@ def test_ansible_e_sapconf(run, _, base_args, tmpdir, create_inventory, create_p run.assert_has_calls(calls) -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_ssh(run, _, base_args, tmpdir, create_inventory, create_playbooks, ansible_config, mock_call_ansibleplaybook): """ @@ -375,7 +376,7 @@ def test_ansible_ssh(run, _, base_args, tmpdir, create_inventory, create_playboo run.assert_has_calls(calls) -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_env_config(run, _, base_args, tmpdir, create_inventory, create_playbooks, ansible_config): """ @@ -414,7 +415,7 @@ def test_ansible_env_config(run, _, base_args, tmpdir, create_inventory, create_ run.assert_has_calls(calls) -@mock.patch('shutil.which', side_effect = [(ANSIBLEPB_EXE),(ANSIBLE_EXE)]) +@mock.patch('shutil.which', side_effect=[(ANSIBLEPB_EXE), (ANSIBLE_EXE)]) @mock.patch("lib.process_manager.subprocess_run") def test_ansible_profile(run, _, base_args, tmpdir, create_inventory, create_playbooks, ansible_config): """ diff --git a/scripts/qesap/test/unit/test_qesap_cli.py b/scripts/qesap/test/unit/test_qesap_cli.py index ab53317b..396df1fd 100644 --- a/scripts/qesap/test/unit/test_qesap_cli.py +++ b/scripts/qesap/test/unit/test_qesap_cli.py @@ -138,6 +138,17 @@ def test_cli_terraform_destroy(base_args): cli(args) +def test_cli_terraform_workspace(base_args): + ''' + Test terraform with -w to create a Terraform named workspace + ''' + args = base_args() + args.append('terraform') + args.append('-w') + args.append('PAPEROGA') + cli(args) + + def test_cli_ansible_noargs(check_manadatory_args): ''' ansible subcommand at least needs: diff --git a/scripts/qesap/test/unit/test_qesap_configure_ansible.py b/scripts/qesap/test/unit/test_qesap_configure_ansible.py index 00cee0c4..ce9056d0 100644 --- a/scripts/qesap/test/unit/test_qesap_configure_ansible.py +++ b/scripts/qesap/test/unit/test_qesap_configure_ansible.py @@ -14,7 +14,6 @@ def test_configure_create_ansible_hanamedia(configure_helper, config_yaml_sample args, _, hana_media, _ = configure_helper(provider, conf, []) assert main(args) == 0 - assert os.path.isfile(hana_media) @@ -68,26 +67,41 @@ def test_configure_ansible_hanamedia_content_apiver2_invalidurl(configure_helper """ provider = 'pinocchio' wrong_hana_urls = [ - ('MISSING ACCOUNT',""" + ( + "MISSING ACCOUNT", + """ - https://.blob.core.windows.net/SOMETHING/MY_IMDB_CLIENT - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_SERVER - - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_CLIENT"""), - ('MISSING CONTAINER',""" + - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_CLIENT""", + ), + ( + "MISSING CONTAINER", + """ - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_SAPCAR_EXE - https://SOMEONE.blob.core.windows.net//MY_IMDB_CLIENT - - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_CLIENT"""), - ('MISSING EXE',""" + - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_CLIENT""", + ), + ( + "MISSING EXE", + """ - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_SAPCAR_EXE - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_SERVER - - https://SOMEONE.blob.core.windows.net/SOMETHING/"""), - ('DIFFERENT ACCOUNT',""" + - https://SOMEONE.blob.core.windows.net/SOMETHING/""", + ), + ( + "DIFFERENT ACCOUNT", + """ - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_SAPCAR_EXE - https://SOMEONEELSE.blob.core.windows.net/SOMETHING/MY_IMDB_SERVER # This is the wrong one DIFFERENT ACCOUNT - - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_CLIENT"""), - ('DIFFERENT CONTAINER',""" + - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_CLIENT""", + ), + ( + "DIFFERENT CONTAINER", + """ - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_SAPCAR_EXE - https://SOMEONE.blob.core.windows.net/SOMETHING/MY_IMDB_SERVER # This is the wrong one DIFFERENT ACCOUNT - - https://SOMEONE.blob.core.windows.net/SOMETHINGELSE/MY_IMDB_CLIENT"""), + - https://SOMEONE.blob.core.windows.net/SOMETHINGELSE/MY_IMDB_CLIENT""", + ), ] for this_set in wrong_hana_urls: conf = f"""--- @@ -199,7 +213,6 @@ def test_configure_create_ansible_hanavars(configure_helper, config_yaml_sample) args, _, _, hana_vars = configure_helper(provider, conf, []) assert main(args) == 0 - assert os.path.isfile(hana_vars) @@ -225,7 +238,6 @@ def test_configure_not_create_ansible_hanavars_apiver1(configure_helper, config_ args, _, _, hana_vars = configure_helper(this_provider, conf, []) assert main(args) == 0 - assert not os.path.isfile(hana_vars) @@ -271,6 +283,7 @@ def test_configure_ansible_hanavar_content(configure_helper): assert data['moustique'] == 'komarac' assert len(data) == 10 + def test_configure_ansible_hanavar_values(configure_helper): """ Test about value of mandatory fields @@ -316,6 +329,7 @@ def test_configure_ansible_hanavar_values(configure_helper): args, _, _, hana_vars = configure_helper(provider, conf.format(provider, '/aaa/bbb/ccc', 'HDB', '000'), []) assert main(args) != 0, "Wrong 'sap_hana_install_instance_number'='000' not detected." + def test_configure_ansible_hana(configure_helper): """ Test that 'configure' fails if manadatory params are missing diff --git a/scripts/qesap/test/unit/test_qesap_configure_terraform.py b/scripts/qesap/test/unit/test_qesap_configure_terraform.py index 0b91ff53..b4dd1a23 100644 --- a/scripts/qesap/test/unit/test_qesap_configure_terraform.py +++ b/scripts/qesap/test/unit/test_qesap_configure_terraform.py @@ -181,7 +181,7 @@ def test_configure_tfvars_overwrite_variables(config_yaml_sample_for_terraform, """ provider = 'pinocchio' - terraform_section = f""" + terraform_section = """ terraform: variables: something : yamlrulez""" @@ -200,4 +200,3 @@ def test_configure_tfvars_overwrite_variables(config_yaml_sample_for_terraform, with open(tfvar_path, 'r', encoding='utf-8') as file: data = file.readlines() assert expected_tfvars == data - diff --git a/scripts/qesap/test/unit/test_qesap_deploy.py b/scripts/qesap/test/unit/test_qesap_deploy.py index 8bb8e362..b499a886 100644 --- a/scripts/qesap/test/unit/test_qesap_deploy.py +++ b/scripts/qesap/test/unit/test_qesap_deploy.py @@ -1,6 +1,7 @@ from qesap import main import pytest + @pytest.mark.skip(reason="Code not ready") def test_deploy(base_args, tmpdir): """ diff --git a/scripts/qesap/test/unit/test_qesap_destroy.py b/scripts/qesap/test/unit/test_qesap_destroy.py index 60465966..f2ec3ed0 100644 --- a/scripts/qesap/test/unit/test_qesap_destroy.py +++ b/scripts/qesap/test/unit/test_qesap_destroy.py @@ -1,6 +1,7 @@ from qesap import main import pytest + @pytest.mark.skip(reason="Code not ready") def test_destroy(base_args, tmpdir): """ diff --git a/scripts/qesap/test/unit/test_qesap_terraform.py b/scripts/qesap/test/unit/test_qesap_terraform.py index 73706aff..e45dd099 100644 --- a/scripts/qesap/test/unit/test_qesap_terraform.py +++ b/scripts/qesap/test/unit/test_qesap_terraform.py @@ -17,21 +17,25 @@ @mock.patch("lib.process_manager.subprocess_run") @pytest.mark.parametrize("terraform_cmd_args", terraform_cmds) -def test_terraform_call_terraform(run, terraform_cmd_args, args_helper, config_yaml_sample): +def test_terraform_call_terraform(subprocess_run, terraform_cmd_args, args_helper, config_yaml_sample): """ Command terraform calls all these 3: - 'terraform init' - 'terraform plan' - 'terraform apply' + + It is implemented as parametrized test, + so formally it is called 3 times, one for each expected terraform command. + Each test invocation verify that one specific command is called """ provider = 'mangiafuoco' conf = config_yaml_sample(provider) args, terraform_dir, *_ = args_helper(provider, conf, '') args.append('terraform') - run.return_value = (0, []) + subprocess_run.return_value = (0, []) assert main(args) == 0 - run.assert_called() + subprocess_run.assert_called() calls = [] terraform_cmd = [ @@ -45,11 +49,11 @@ def test_terraform_call_terraform(run, terraform_cmd_args, args_helper, config_y terraform_cmd.append('-no-color') calls.append(mock.call(terraform_cmd)) - run.assert_has_calls(calls) + subprocess_run.assert_has_calls(calls) @mock.patch("lib.process_manager.subprocess_run", side_effect=[(0, []), (1, []), (1, [])]) -def test_terraform_stop_at_failure(run, args_helper, config_yaml_sample): +def test_terraform_stop_at_failure(subprocess_run, args_helper, config_yaml_sample): """ Command stop at first subprocess(terraform) with not zero exit code. Simulate a failure at 'terraform plan' @@ -72,14 +76,14 @@ def test_terraform_stop_at_failure(run, args_helper, config_yaml_sample): assert main(args) == 1 - run.assert_called() - run.assert_has_calls(calls) - assert not any('apply' in name[0] for name, args in run.call_args_list), 'Unexpected terraform apply call' + subprocess_run.assert_called() + subprocess_run.assert_has_calls(calls) + assert not any('apply' in name[0] for name, args in subprocess_run.call_args_list), 'Unexpected terraform apply call' @mock.patch("lib.process_manager.subprocess_run") @pytest.mark.parametrize("terraform_cmd_args", terraform_cmds) -def test_terraform_logs(run, terraform_cmd_args, args_helper, config_yaml_sample, tmpdir): +def test_terraform_logs(subprocess_run, terraform_cmd_args, args_helper, config_yaml_sample, tmpdir): """ Command terraform create one log file for each command: - terraform.{cmd}.log.txt @@ -89,7 +93,7 @@ def test_terraform_logs(run, terraform_cmd_args, args_helper, config_yaml_sample args, *_ = args_helper(provider, conf, '') args.append('terraform') - run.return_value = (0, ['This is the terraform output', 'Two lines of that']) + subprocess_run.return_value = (0, ['This is the terraform output', 'Two lines of that']) assert main(args) == 0 @@ -102,7 +106,7 @@ def test_terraform_logs(run, terraform_cmd_args, args_helper, config_yaml_sample @mock.patch("lib.process_manager.subprocess_run") @pytest.mark.parametrize("terraform_cmd_args", terraform_cmds) -def test_terraform_logs_content(run, terraform_cmd_args, args_helper, config_yaml_sample, tmpdir): +def test_terraform_logs_content(subprocess_run, terraform_cmd_args, args_helper, config_yaml_sample, tmpdir): """ Each terraform log file contains terraform stdout """ @@ -112,7 +116,7 @@ def test_terraform_logs_content(run, terraform_cmd_args, args_helper, config_yam args, *_ = args_helper(provider, conf, '') args.append('terraform') terraform_output = ['This is the terraform output', 'Two lines of that'] - run.return_value = (0, terraform_output) + subprocess_run.return_value = (0, terraform_output) assert main(args) == 0 @@ -159,7 +163,7 @@ def test_integration_terraform(terraform_cmd_args, config_yaml_sample, tmpdir): @mock.patch("lib.process_manager.subprocess_run") -def test_terraform_dryrun(run, args_helper, config_yaml_sample): +def test_terraform_dryrun(subprocess_run, args_helper, config_yaml_sample): """ Command terraform does not call terraform executable in dryrun mode """ @@ -169,15 +173,15 @@ def test_terraform_dryrun(run, args_helper, config_yaml_sample): args, *_ = args_helper(provider, conf, '') args.append('terraform') args.insert(0, '--dryrun') - run.return_value = (0, []) + subprocess_run.return_value = (0, []) assert main(args) == 0 - run.assert_not_called() + subprocess_run.assert_not_called() @mock.patch("lib.process_manager.subprocess_run") -def test_terraform_call_terraform_destroy(run, args_helper, config_yaml_sample): +def test_terraform_call_terraform_destroy(subprocess_run, args_helper, config_yaml_sample): """ Command terraform with -d calls 'terraform destroy' """ @@ -188,7 +192,7 @@ def test_terraform_call_terraform_destroy(run, args_helper, config_yaml_sample): args.extend(['terraform', '-d']) - run.return_value = (0, []) + subprocess_run.return_value = (0, []) calls = [] calls.append(mock.call([ 'terraform', @@ -198,5 +202,34 @@ def test_terraform_call_terraform_destroy(run, args_helper, config_yaml_sample): '-no-color'])) assert main(args) == 0 - run.assert_called() - run.assert_has_calls(calls) + subprocess_run.assert_called() + subprocess_run.assert_has_calls(calls) + + +@mock.patch("lib.process_manager.subprocess_run") +def test_terraform_call_terraform_workspace(subprocess_run, args_helper, config_yaml_sample): + """ + Command terraform calls 'terraform workspace' if -w is used + """ + provider = 'mangiafuoco' + conf = config_yaml_sample(provider) + + args, terraform_dir, *_ = args_helper(provider, conf, '') + args.append('terraform') + args.append('-w') + args.append('lucignolo') + subprocess_run.return_value = (0, []) + assert main(args) == 0 + subprocess_run.assert_called() + + calls = [] + terraform_cmd = [ + 'terraform', + f"-chdir={terraform_dir}"] + terraform_cmd.append('workspace') + terraform_cmd.append('new') + terraform_cmd.append('lucignolo') + terraform_cmd.append('-no-color') + calls.append(mock.call(terraform_cmd)) + + subprocess_run.assert_has_calls(calls) diff --git a/scripts/qesap/test/unit/test_subprocess_run.py b/scripts/qesap/test/unit/test_subprocess_run.py index 99a39636..0a48a393 100644 --- a/scripts/qesap/test/unit/test_subprocess_run.py +++ b/scripts/qesap/test/unit/test_subprocess_run.py @@ -1,5 +1,6 @@ from lib.process_manager import subprocess_run + def test_no_command(): ''' Run subprocess_run providing no commands @@ -61,6 +62,6 @@ def test_env(): ''' _, stdout_list = subprocess_run(['printenv']) assert 'BANANA_VALUE' not in stdout_list - exit_code, stdout_list = subprocess_run(['printenv'], env={'BANANA_VALUE' : '1234'}) + exit_code, stdout_list = subprocess_run(['printenv'], env={'BANANA_VALUE': '1234'}) assert exit_code == 0 assert 'BANANA_VALUE=1234' in stdout_list diff --git a/scripts/qesap/tox.ini b/scripts/qesap/tox.ini index 361040b8..4aff5084 100644 --- a/scripts/qesap/tox.ini +++ b/scripts/qesap/tox.ini @@ -11,6 +11,10 @@ python = deps = -r{toxinidir}/requirements-dev.txt commands = flake8 qesap.py lib/ +[testenv:flake8_test] +deps = -r{toxinidir}/requirements-dev.txt +commands = flake8 test/ + [testenv:pylint] deps = -r{toxinidir}/requirements-dev.txt commands = pylint --rcfile=pylint.rc qesap.py lib/