Skip to content

Commit

Permalink
Add unit testings for terraform workspace (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpagot authored Feb 21, 2023
1 parent 7c34d0f commit e1fb8d4
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 45 deletions.
3 changes: 2 additions & 1 deletion scripts/qesap/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def _callback(terraform_section, provider='pinocchio', apiver=3):

return _callback


@pytest.fixture
def provider_dir(tmpdir):
def _callback(provider):
Expand Down Expand Up @@ -414,4 +415,4 @@ def _callback(hana_media_file, account='ACCOUNT', container='CONTAINER', token=N

return (True, '')

return _callback
return _callback
19 changes: 10 additions & 9 deletions scripts/qesap/test/unit/test_qesap_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down
11 changes: 11 additions & 0 deletions scripts/qesap/test/unit/test_qesap_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 27 additions & 13 deletions scripts/qesap/test/unit/test_qesap_configure_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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"""---
Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions scripts/qesap/test/unit/test_qesap_configure_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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

1 change: 1 addition & 0 deletions scripts/qesap/test/unit/test_qesap_deploy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from qesap import main
import pytest


@pytest.mark.skip(reason="Code not ready")
def test_deploy(base_args, tmpdir):
"""
Expand Down
1 change: 1 addition & 0 deletions scripts/qesap/test/unit/test_qesap_destroy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from qesap import main
import pytest


@pytest.mark.skip(reason="Code not ready")
def test_destroy(base_args, tmpdir):
"""
Expand Down
71 changes: 52 additions & 19 deletions scripts/qesap/test/unit/test_qesap_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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'
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
"""
Expand All @@ -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

Expand Down Expand Up @@ -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
"""
Expand All @@ -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'
"""
Expand All @@ -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',
Expand All @@ -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)
Loading

0 comments on commit e1fb8d4

Please sign in to comment.