fixed apply tests for 0.x
This commit is contained in:
parent
816b76070e
commit
2d57a67c63
1 changed files with 45 additions and 54 deletions
|
@ -17,24 +17,26 @@ root_logger = logging.getLogger()
|
||||||
|
|
||||||
current_path = os.path.dirname(os.path.realpath(__file__))
|
current_path = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
version = 1.0 if (os.environ.get("TFVER") and os.environ.get("TFVER").startswith("1")) else 0.13
|
version = 1.0 if (os.environ.get("TFVER") and os.environ.get(
|
||||||
|
"TFVER").startswith("1")) else 0.13
|
||||||
|
|
||||||
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!"
|
FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!"
|
||||||
STRING_CASES = [
|
STRING_CASES = [
|
||||||
[
|
[
|
||||||
lambda x: x.generate_cmd_string({}, "apply", "the_folder", no_color=IsFlagged),
|
lambda x: x.generate_cmd_string(
|
||||||
|
{}, "apply", "the_folder", no_color=IsFlagged),
|
||||||
"terraform apply -no-color the_folder",
|
"terraform apply -no-color the_folder",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
lambda x: x.generate_cmd_string({},
|
lambda x: x.generate_cmd_string({},
|
||||||
"push", "path", vcs=True, token="token", atlas_address="url"
|
"push", "path", vcs=True, token="token", atlas_address="url"
|
||||||
),
|
),
|
||||||
"terraform push -vcs=true -token=token -atlas-address=url path",
|
"terraform push -vcs=true -token=token -atlas-address=url path",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
lambda x: x.generate_cmd_string({},
|
lambda x: x.generate_cmd_string({},
|
||||||
"refresh", "path", token="token"
|
"refresh", "path", token="token"
|
||||||
),
|
),
|
||||||
"terraform refresh -token=token path",
|
"terraform refresh -token=token path",
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
@ -180,6 +182,22 @@ CMD_CASES_1_x = [
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
|
APPLY_CASES_0_x = [
|
||||||
|
["folder", "variables", "var_files", "expected_output", "options"],
|
||||||
|
[("var_to_output", {"test_var": "test"}, None, 'test_output="test"', {}),
|
||||||
|
("var_to_output", {"test_list_var": ["c", "d"]}, None, 'test_list_output=["c","d",]', {},),
|
||||||
|
("var_to_output", {"test_map_var": {"c": "c", "d": "d"}}, None, 'test_map_output={"c"="c""d"="d"}', {},),
|
||||||
|
("var_to_output", {"test_map_var": {"c": "c", "d": "d"}}, "test_map_var.json", 'test_map_output={"e"="e""f"="f"}', {},),
|
||||||
|
("var_to_output", {}, None, "\x1b[0m\x1b[1m\x1b[32mApplycomplete!", {"no_color": IsNotFlagged},), ]]
|
||||||
|
|
||||||
|
APPLY_CASES_1_x = [
|
||||||
|
["folder", "variables", "var_files", "expected_output", "options"],
|
||||||
|
[("var_to_output", {"test_var": "test"}, None, 'test_output="test"', {}),
|
||||||
|
("var_to_output", {"test_list_var": ["c", "d"]}, None, 'test_list_output=tolist(["c","d",])', {},),
|
||||||
|
("var_to_output", {"test_map_var": {"c": "c", "d": "d"}}, None, 'test_map_output=tomap({"c"="c""d"="d"})', {},),
|
||||||
|
("var_to_output", {"test_map_var": {"c": "c", "d": "d"}}, "test_map_var.json", 'test_map_output=tomap({"e"="e""f"="f"})', {},),
|
||||||
|
("var_to_output", {}, None, "\x1b[0m\x1b[1m\x1b[32mApplycomplete!", {"no_color": IsNotFlagged},), ]]
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
def fmt_test_file(request):
|
def fmt_test_file(request):
|
||||||
|
@ -233,7 +251,8 @@ def workspace_setup_teardown():
|
||||||
class TestTerraform:
|
class TestTerraform:
|
||||||
def teardown_method(self, _) -> None:
|
def teardown_method(self, _) -> None:
|
||||||
"""Teardown any state that was previously setup with a setup_method call."""
|
"""Teardown any state that was previously setup with a setup_method call."""
|
||||||
exclude = ["test_tfstate_file", "test_tfstate_file2", "test_tfstate_file3"]
|
exclude = ["test_tfstate_file",
|
||||||
|
"test_tfstate_file2", "test_tfstate_file3"]
|
||||||
|
|
||||||
def purge(dir: str, pattern: str) -> None:
|
def purge(dir: str, pattern: str) -> None:
|
||||||
for root, dirnames, filenames in os.walk(dir):
|
for root, dirnames, filenames in os.walk(dir):
|
||||||
|
@ -285,44 +304,8 @@ class TestTerraform:
|
||||||
assert expected_ret_code == ret
|
assert expected_ret_code == ret
|
||||||
assert expected_logs in caplog.text
|
assert expected_logs in caplog.text
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(*(APPLY_CASES_1_x if version >= 1.0 else APPLY_CASES_0_x))
|
||||||
@pytest.mark.parametrize(
|
|
||||||
("folder", "variables", "var_files", "expected_output", "options"),
|
|
||||||
[
|
|
||||||
("var_to_output", {"test_var": "test"}, None, 'test_output="test"', {}),
|
|
||||||
(
|
|
||||||
"var_to_output",
|
|
||||||
{"test_list_var": ["c", "d"]},
|
|
||||||
None,
|
|
||||||
'test_list_output=tolist(["c","d",])',
|
|
||||||
{},
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"var_to_output",
|
|
||||||
{"test_map_var": {"c": "c", "d": "d"}},
|
|
||||||
None,
|
|
||||||
'test_map_output=tomap({"c"="c""d"="d"})',
|
|
||||||
{},
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"var_to_output",
|
|
||||||
{"test_map_var": {"c": "c", "d": "d"}},
|
|
||||||
"test_map_var.json",
|
|
||||||
# Values are overriden
|
|
||||||
'test_map_output=tomap({"e"="e""f"="f"})',
|
|
||||||
{},
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"var_to_output",
|
|
||||||
{},
|
|
||||||
None,
|
|
||||||
"\x1b[0m\x1b[1m\x1b[32mApplycomplete!",
|
|
||||||
{"no_color": IsNotFlagged},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_apply(self, folder, variables, var_files, expected_output, options):
|
def test_apply(self, folder, variables, var_files, expected_output, options):
|
||||||
var_files_version = folder + var_files if version < 1.0 else var_files
|
|
||||||
tf = Terraform(
|
tf = Terraform(
|
||||||
working_dir=current_path, variables=variables, var_file=var_files, terraform_version=version
|
working_dir=current_path, variables=variables, var_file=var_files, terraform_version=version
|
||||||
)
|
)
|
||||||
|
@ -340,7 +323,8 @@ class TestTerraform:
|
||||||
tf.init(folder)
|
tf.init(folder)
|
||||||
tf.apply(
|
tf.apply(
|
||||||
folder,
|
folder,
|
||||||
var_file=os.path.join(current_path, "tfvar_files", "test.tfvars"),
|
var_file=os.path.join(
|
||||||
|
current_path, "tfvar_files", "test.tfvars"),
|
||||||
)
|
)
|
||||||
for log in caplog.messages:
|
for log in caplog.messages:
|
||||||
if log.startswith("Command: terraform apply"):
|
if log.startswith("Command: terraform apply"):
|
||||||
|
@ -361,7 +345,8 @@ class TestTerraform:
|
||||||
|
|
||||||
def test_state_data(self):
|
def test_state_data(self):
|
||||||
cwd = os.path.join(current_path, "test_tfstate_file")
|
cwd = os.path.join(current_path, "test_tfstate_file")
|
||||||
tf = Terraform(working_dir=cwd, state="tfstate.test", terraform_version=version)
|
tf = Terraform(working_dir=cwd, state="tfstate.test",
|
||||||
|
terraform_version=version)
|
||||||
tf.read_state_file()
|
tf.read_state_file()
|
||||||
assert tf.tfstate.modules[0]["path"] == ["root"]
|
assert tf.tfstate.modules[0]["path"] == ["root"]
|
||||||
|
|
||||||
|
@ -379,14 +364,16 @@ class TestTerraform:
|
||||||
|
|
||||||
def test_pre_load_state_data(self):
|
def test_pre_load_state_data(self):
|
||||||
cwd = os.path.join(current_path, "test_tfstate_file")
|
cwd = os.path.join(current_path, "test_tfstate_file")
|
||||||
tf = Terraform(working_dir=cwd, state="tfstate.test", terraform_version=version)
|
tf = Terraform(working_dir=cwd, state="tfstate.test",
|
||||||
|
terraform_version=version)
|
||||||
assert tf.tfstate.modules[0]["path"] == ["root"]
|
assert tf.tfstate.modules[0]["path"] == ["root"]
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("folder", "variables"), [("var_to_output", {"test_var": "test"})]
|
("folder", "variables"), [("var_to_output", {"test_var": "test"})]
|
||||||
)
|
)
|
||||||
def test_override_default(self, folder, variables):
|
def test_override_default(self, folder, variables):
|
||||||
tf = Terraform(working_dir=current_path, variables=variables, terraform_version=version)
|
tf = Terraform(working_dir=current_path,
|
||||||
|
variables=variables, terraform_version=version)
|
||||||
tf.init(folder)
|
tf.init(folder)
|
||||||
ret, out, err = tf.apply(
|
ret, out, err = tf.apply(
|
||||||
folder, var={"test_var": "test2"}, no_color=IsNotFlagged,
|
folder, var={"test_var": "test2"}, no_color=IsNotFlagged,
|
||||||
|
@ -415,7 +402,8 @@ class TestTerraform:
|
||||||
assert expected_value in caplog.messages[-1]
|
assert expected_value in caplog.messages[-1]
|
||||||
|
|
||||||
def test_destroy(self):
|
def test_destroy(self):
|
||||||
tf = Terraform(working_dir=current_path, variables={"test_var": "test"}, terraform_version=version)
|
tf = Terraform(working_dir=current_path, variables={
|
||||||
|
"test_var": "test"}, terraform_version=version)
|
||||||
tf.init("var_to_output")
|
tf.init("var_to_output")
|
||||||
ret, out, err = tf.destroy("var_to_output")
|
ret, out, err = tf.destroy("var_to_output")
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
@ -425,7 +413,8 @@ class TestTerraform:
|
||||||
("plan", "variables", "expected_ret"), [("vars_require_input", {}, 1)]
|
("plan", "variables", "expected_ret"), [("vars_require_input", {}, 1)]
|
||||||
)
|
)
|
||||||
def test_plan(self, plan, variables, expected_ret):
|
def test_plan(self, plan, variables, expected_ret):
|
||||||
tf = Terraform(working_dir=current_path, variables=variables, terraform_version=version)
|
tf = Terraform(working_dir=current_path,
|
||||||
|
variables=variables, terraform_version=version)
|
||||||
tf.init(plan)
|
tf.init(plan)
|
||||||
with pytest.raises(TerraformCommandError) as e:
|
with pytest.raises(TerraformCommandError) as e:
|
||||||
tf.plan(plan)
|
tf.plan(plan)
|
||||||
|
@ -434,7 +423,8 @@ class TestTerraform:
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_fmt(self, fmt_test_file):
|
def test_fmt(self, fmt_test_file):
|
||||||
tf = Terraform(working_dir=current_path, variables={"test_var": "test"}, terraform_version=version)
|
tf = Terraform(working_dir=current_path, variables={
|
||||||
|
"test_var": "test"}, terraform_version=version)
|
||||||
ret, out, err = tf.fmt(diff=True)
|
ret, out, err = tf.fmt(diff=True)
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
@ -493,6 +483,7 @@ class TestTerraform:
|
||||||
in caplog.messages
|
in caplog.messages
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def test_show_workspace(self, workspace_setup_teardown):
|
def test_show_workspace(self, workspace_setup_teardown):
|
||||||
workspace_name = "test"
|
workspace_name = "test"
|
||||||
with workspace_setup_teardown(workspace_name) as tf:
|
with workspace_setup_teardown(workspace_name) as tf:
|
||||||
|
|
Loading…
Reference in a new issue